Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

add rest example tests #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jdk:

install: ./gradlew --version

script: ./gradlew --continue
script: ./gradlew --continue build
5 changes: 5 additions & 0 deletions cnf/central.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>aQute.libg</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import junit.framework.TestCase;

/*
*
*
*
*
*
*
*/

public class ProxycacheImplTest extends TestCase {

/*
*
*
*
*
*
*
*/
public void testProxycache() {
}
}
8 changes: 8 additions & 0 deletions osgi.enroute.examples.rest.test/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin" path="src"/>
<classpathentry kind="src" output="bin_test" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="aQute.bnd.classpath.container"/>
<classpathentry kind="output" path="test_bin"/>
</classpath>
4 changes: 4 additions & 0 deletions osgi.enroute.examples.rest.test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/bin/
/generated/
/test_bin/
/bin_test/
23 changes: 23 additions & 0 deletions osgi.enroute.examples.rest.test/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>osgi.enroute.examples.rest.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>bndtools.core.bndbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>bndtools.core.bndnature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
47 changes: 47 additions & 0 deletions osgi.enroute.examples.rest.test/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Bundle-Version:1.0.0.${tstamp}
Bundle-Description: \
This bundle is an OSGi test bundle that tests the enRoute Rest application

Private-Package: \
osgi.enroute.examples.rest.test.*

Test-Cases: ${test-cases}

-includeresource: {readme.md}

-buildpath: \
osgi.enroute.base.api;version=1.0,\
osgi.enroute.junit.wrapper;version=4.12,\
biz.aQute.bndlib,\
aQute.libg;version=3.3

-runrequires: \
osgi.identity;filter:='(osgi.identity=osgi.enroute.examples.rest.application)',\
osgi.identity;filter:='(osgi.identity=osgi.enroute.examples.rest.test)'

-runbundles: \
org.apache.felix.log;version='[1.0.1,1.0.2)',\
osgi.enroute.hamcrest.wrapper;version='[1.3.0,1.3.1)',\
osgi.enroute.junit.wrapper;version='[4.12.0,4.12.1)',\
org.apache.felix.configadmin;version='[1.8.8,1.8.9)',\
org.apache.felix.http.jetty;version='[3.2.0,3.2.1)',\
org.apache.felix.http.servlet-api;version='[1.1.2,1.1.3)',\
org.apache.felix.scr;version='[2.0.2,2.0.3)',\
org.eclipse.equinox.coordinator;version='[1.3.100,1.3.101)',\
org.eclipse.equinox.event;version='[1.3.100,1.3.101)',\
org.eclipse.equinox.metatype;version='[1.4.100,1.4.101)',\
org.osgi.service.event;version='[1.3.1,1.3.2)',\
org.osgi.service.metatype;version='[1.3.0,1.3.1)',\
osgi.enroute.configurer.simple.provider;version='[2.0.0,2.0.1)',\
osgi.enroute.dtos.bndlib.provider;version='[2.0.0,2.0.1)',\
osgi.enroute.examples.rest.application;version=snapshot,\
osgi.enroute.examples.rest.test;version=snapshot,\
osgi.enroute.executor.simple.provider;version='[2.0.0,2.0.1)',\
osgi.enroute.google.angular.webresource;version='[1.5.7,1.5.8)',\
osgi.enroute.logger.simple.provider;version='[2.0.0,2.0.1)',\
osgi.enroute.rest.simple.provider;version='[2.0.0,2.0.1)',\
osgi.enroute.stackexchange.pagedown.webresource;version='[1.1.1,1.1.2)',\
osgi.enroute.twitter.bootstrap.webresource;version='[3.3.5,3.3.6)',\
osgi.enroute.web.simple.provider;version='[2.0.0,2.0.1)',\
biz.aQute.bndlib;version='[3.3.0,3.3.1)',\
aQute.libg;version='[3.3.0,3.3.1)'
8 changes: 8 additions & 0 deletions osgi.enroute.examples.rest.test/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# OSGI ENROUTE EXAMPLES TEST

${Bundle-Description}

## Example

## References

Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package osgi.enroute.examples.rest.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.osgi.dto.DTO;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

import aQute.bnd.http.HttpClient;
import aQute.bnd.http.HttpRequest;
import aQute.lib.json.JSONCodec;

public class RestExamplesTest {

private final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();

@Before
public void checkContext() {
Assert.assertNotNull(context);
}

@Test
public void testGetUpper() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper/");

String result = client.build().get(String.class).go(url);

Assert.assertEquals("\"CHUCKSTEAK IS GREAT\"", result);

client.close();
}

@Test
public void testGetUpperWithArg() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper/steak");

String result = client.build().get(String.class).go(url);

Assert.assertEquals("\"STEAK\"", result);

client.close();
}

@Test
public void testGetUpperWithTwoArgs() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper/steak1/steak2");

String result = client.build().get(String.class).go(url);

Assert.assertEquals("\"STEAK1 and STEAK2\"", result);

client.close();
}

@Test
public void testGetUpper2() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper2/?upper2=test");

String result = client.build().get(String.class).go(url);

Assert.assertEquals("\"TEST\"", result);

client.close();
}

public static class History extends DTO {
public String input;
public String output;
}

@Test
public void testGetUpper3() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper3/TesT");

HttpRequest<History> httpRequest = client.build().get(History.class);

History result = httpRequest.go(url);

Assert.assertNotNull(result);

Assert.assertEquals("TesT", result.input);

Assert.assertEquals("TEST", result.output);

url = new URI("http://localhost:8080/rest/upper3/TesT2");

result = httpRequest.go(url);

Assert.assertNotNull(result);

Assert.assertEquals("TesT2", result.input);

Assert.assertEquals("TEST2", result.output);

client.close();
}

@Test
public void testDeleteUpper3() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper3/TesT2");

Object result = client.build().delete().go(url);

Assert.assertNotNull(result);

client.close();
}

@Test
public void testPostUpper4() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper4/Sam");

HttpRequest<Object> request = client.build().post();

Object result = request.go(url);

String jsonResult = new JSONCodec().enc().to().put(result).toString();

Assert.assertEquals("{\"input\":\"Sam\",\"output\":\"SAM\"}", jsonResult);

url = new URI("http://localhost:8080/rest/upper4/Sam2");

result = request.go(url);

jsonResult = new JSONCodec().enc().to().put(result).toString();

Assert.assertEquals("{\"input\":\"Sam2\",\"output\":\"SAM2\"}", jsonResult);

client.close();
}

@Test
public void testPutUpper4() throws Exception {
HttpClient client = new HttpClient();

URI url = new URI("http://localhost:8080/rest/upper4/Sam");

HttpRequest<Object> request = client.build().put();

Object result = request.go(url);

String jsonResult = new JSONCodec().enc().to().put(result).toString();

Assert.assertEquals("{\"input\":\"Sam\",\"output\":\"SAM\"}", jsonResult);

url = new URI("http://localhost:8080/rest/upper4/Sam2");

result = request.go(url);

jsonResult = new JSONCodec().enc().to().put(result).toString();

Assert.assertEquals("{\"input\":\"Sam2\",\"output\":\"SAM2\"}", jsonResult);

client.close();
}

@Test
public void testPostUpper5() throws Exception {
URL url = new URL("http://localhost:8080/rest/upper5/");

String body = "{\"input\":\"Sam3\",\"output\":\"SAM3\"}";

String response = doVerbWithBody(url, "POST", body);

Assert.assertEquals("{\"input\":\"Sam3\",\"output\":\"SAM3\"}", response);
}

@Test
public void testPutUpper5() throws Exception {
URL url = new URL("http://localhost:8080/rest/upper5/");

String body = "{\"input\":\"Sam3\",\"output\":\"SAM3\"}";

String response = doVerbWithBody(url, "PUT", body);

Assert.assertEquals(body, response);
}

private String doVerbWithBody(URL url, String verb, String body) throws Exception {
HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setRequestMethod(verb);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type","application/json");

con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();

Assert.assertEquals(200, con.getResponseCode());

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

return response.toString();
}
}
Empty file.