Adding olingo-server side OSGi tests

This commit is contained in:
Aki Yoshida 2015-06-09 18:33:36 +02:00 committed by Michael Bolz
parent 0b8d46fc6a
commit 7ab54a2b85
5 changed files with 455 additions and 0 deletions

152
ext/karaf/itests/pom.xml Normal file
View File

@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>odata-karaf-itests</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<parent>
<groupId>org.apache.olingo</groupId>
<artifactId>odata-ext</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<dependencies>
<!-- Karaf features -->
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>odata-karaf-features</artifactId>
<version>${project.version}</version>
<type>xml</type>
<classifier>features</classifier>
</dependency>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>org.apache.karaf.features.core</artifactId>
<version>${karaf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
<version>${karaf.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>${osgi.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>${osgi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- pax-exam -->
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-karaf</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>${pax.exam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.karaf</groupId>
<artifactId>apache-karaf</artifactId>
<version>${karaf.version}</version>
<type>tar.gz</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.dev</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse</groupId>
<artifactId>osgi</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse</groupId>
<artifactId>org.eclipse.osgi</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.karaf</groupId>
<artifactId>org.apache.karaf.client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!-- generate dependencies versions that are needed -->
<plugin>
<groupId>org.apache.servicemix.tooling</groupId>
<artifactId>depends-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>generate-depends-file</id>
<goals>
<goal>generate-depends-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,56 @@
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
******************************************************************************/
package org.apache.olingo.osgi.itests;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class BundlesTest extends OlingoOSGiTestSupport {
@Test
public void test() throws Exception {
// check the server-api and server-core
assertBundleStarted("org.apache.olingo.odata-server-api");
assertBundleStarted("org.apache.olingo.odata-server-core");
// check the client-api and client-core
assertBundleStarted("org.apache.olingo.odata-client-api");
assertBundleStarted("org.apache.olingo.odata-client-core");
}
@Configuration
public Option[] config() {
return new Option[]{
olingoBaseConfig(),
features(olingoUrl, "olingo-server", "olingo-client"),
logLevel(LogLevel.INFO)};
}
}

View File

@ -0,0 +1,137 @@
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
******************************************************************************/
package org.apache.olingo.osgi.itests;
import java.io.File;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.inject.Inject;
import org.apache.karaf.features.FeaturesService;
import org.junit.Assert;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.ProbeBuilder;
import org.ops4j.pax.exam.TestProbeBuilder;
import org.ops4j.pax.exam.options.MavenUrlReference;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import static org.ops4j.pax.exam.CoreOptions.composite;
import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.CoreOptions.when;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
/**
*
*/
public class OlingoOSGiTestSupport {
private static final String MAVEN_DEPENDENCIES_PROPERTIES = "/META-INF/maven/dependencies.properties";
@Inject
protected BundleContext bundleContext;
@Inject
protected FeaturesService featureService;
protected ExecutorService executor = Executors.newCachedThreadPool();
protected MavenUrlReference olingoUrl;
protected MavenUrlReference karafUrl;
/**
* @param probe
* @return
*/
@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE, "*,org.apache.felix.service.*;status=provisional");
return probe;
}
private static String getKarafVersion() {
String karafVersion = getVersionFromPom("org.apache.karaf/apache-karaf/version");
if (karafVersion == null) {
karafVersion = System.getProperty("karaf.version");
}
if (karafVersion == null) {
// setup the default version of it
karafVersion = "3.0.3";
}
return karafVersion;
}
private static String getVersionFromPom(String key) {
try {
InputStream ins = OlingoOSGiTestSupport.class.getResourceAsStream(MAVEN_DEPENDENCIES_PROPERTIES);
Properties p = new Properties();
p.load(ins);
return p.getProperty(key);
} catch (Exception t) {
throw new IllegalStateException(MAVEN_DEPENDENCIES_PROPERTIES + " can not be found", t);
}
}
/**
* Create an {@link org.ops4j.pax.exam.Option} for using a .
*
* @return
*/
protected Option olingoBaseConfig() {
karafUrl = maven().groupId("org.apache.karaf").artifactId("apache-karaf").version(getKarafVersion())
.type("tar.gz");
olingoUrl = maven().groupId("org.apache.olingo").artifactId("odata-karaf-features").versionAsInProject()
.type("xml").classifier("features");
String localRepo = System.getProperty("localRepository");
return composite(karafDistributionConfiguration()
.frameworkUrl(karafUrl)
.karafVersion(getKarafVersion())
.name("Apache Karaf")
.useDeployFolder(false)
.unpackDirectory(new File("target/paxexam/")),
//DO NOT COMMIT WITH THIS LINE ENABLED!!!
//KarafDistributionOption.keepRuntimeFolder(),
systemProperty("java.awt.headless").value("true"),
when(localRepo != null)
.useOptions(editConfigurationFilePut("etc/org.ops4j.pax.url.mvn.cfg",
"org.ops4j.pax.url.mvn.localRepository",
localRepo)));
}
protected void assertBundleStarted(String name) {
Bundle bundle = findBundleByName(name);
Assert.assertNotNull("Bundle " + name + " should be installed", bundle);
Assert.assertEquals("Bundle " + name + " should be started", Bundle.ACTIVE, bundle.getState());
}
protected Bundle findBundleByName(String symbolicName) {
for (Bundle bundle : bundleContext.getBundles()) {
if (bundle.getSymbolicName().equals(symbolicName)) {
return bundle;
}
}
return null;
}
}

View File

@ -0,0 +1,105 @@
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
******************************************************************************/
package org.apache.olingo.osgi.itests.server;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.olingo.osgi.itests.OlingoOSGiTestSupport;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
/**
*
*/
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerMethod.class)
public class CarServiceTest extends OlingoOSGiTestSupport {
private static final String SERVICE_URL = "http://localhost:8181/olingo-cars/cars.svc/";
@Test
public void testServiceStarted() throws Exception {
// check if the bundle is started
assertBundleStarted("org.apache.olingo.odata-server-osgi-sample");
// use the jdk http client to verify the server side
verifyContent(getContent(SERVICE_URL));
verifyContent(getContent(SERVICE_URL + "$metadata"));
verifyContent(getContent(SERVICE_URL + "Cars"));
verifyContent(getContent(SERVICE_URL + "Cars(1)"));
verifyContent(getContent(SERVICE_URL + "Cars(1)/Price"));
}
private static void verifyContent(String content) {
// this is currently a simple test to check if there is no error
Assert.assertNotNull(content);
Assert.assertTrue(content.indexOf("error") < 0);
}
private static String getContent(String target) throws Exception {
InputStream in = null;
try {
URL url = new URL(target);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Accept", "*/*");
in = urlConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int n = 0;
while ((n = in.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, n);
}
return new String(baos.toByteArray(), "utf-8");
} finally {
if (in != null) {
in.close();
}
}
}
@Configuration
public Option[] config() {
return new Option[] {
olingoBaseConfig(),
features(olingoUrl, "olingo-server", "olingo-client"),
mavenBundle("org.apache.olingo", "odata-server-osgi-sample", "4.0.0-SNAPSHOT"),
logLevel(LogLevel.INFO)
};
}
}

View File

@ -97,6 +97,11 @@
<tomcat.servlet.port>9080</tomcat.servlet.port>
<tomcat.version>7.0.55</tomcat.version>
<!-- for osgi integration testing -->
<osgi.version>4.2.0</osgi.version>
<karaf.version>3.0.3</karaf.version>
<pax.exam.version>4.5.0</pax.exam.version>
<!-- Project build settings -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.source>1.6</project.build.source>