From 5332f5636076de055f37a1627082cb5728ac71ea Mon Sep 17 00:00:00 2001 From: Gregory Amerson Date: Fri, 30 Jun 2017 10:58:59 -0500 Subject: [PATCH] add new example rest application tests Signed-off-by: Gregory Amerson --- cnf/central.xml | 5 + osgi.enroute.examples.rest.test/.classpath | 8 + osgi.enroute.examples.rest.test/.gitignore | 4 + osgi.enroute.examples.rest.test/.project | 23 ++ .../.settings/org.eclipse.jdt.core.prefs | 12 + osgi.enroute.examples.rest.test/bnd.bnd | 47 ++++ osgi.enroute.examples.rest.test/readme.md | 8 + .../examples/rest/test/RestExamplesTest.java | 228 ++++++++++++++++++ .../test/.gitignore | 0 9 files changed, 335 insertions(+) create mode 100644 osgi.enroute.examples.rest.test/.classpath create mode 100644 osgi.enroute.examples.rest.test/.gitignore create mode 100644 osgi.enroute.examples.rest.test/.project create mode 100644 osgi.enroute.examples.rest.test/.settings/org.eclipse.jdt.core.prefs create mode 100644 osgi.enroute.examples.rest.test/bnd.bnd create mode 100644 osgi.enroute.examples.rest.test/readme.md create mode 100644 osgi.enroute.examples.rest.test/src/osgi/enroute/examples/rest/test/RestExamplesTest.java create mode 100644 osgi.enroute.examples.rest.test/test/.gitignore diff --git a/cnf/central.xml b/cnf/central.xml index 8f7ec9d..1cd9596 100644 --- a/cnf/central.xml +++ b/cnf/central.xml @@ -9,6 +9,11 @@ pom + + biz.aQute.bnd + aQute.libg + 3.3.0 + \ No newline at end of file diff --git a/osgi.enroute.examples.rest.test/.classpath b/osgi.enroute.examples.rest.test/.classpath new file mode 100644 index 0000000..3d01dd8 --- /dev/null +++ b/osgi.enroute.examples.rest.test/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/osgi.enroute.examples.rest.test/.gitignore b/osgi.enroute.examples.rest.test/.gitignore new file mode 100644 index 0000000..565b897 --- /dev/null +++ b/osgi.enroute.examples.rest.test/.gitignore @@ -0,0 +1,4 @@ +/bin/ +/generated/ +/test_bin/ +/bin_test/ diff --git a/osgi.enroute.examples.rest.test/.project b/osgi.enroute.examples.rest.test/.project new file mode 100644 index 0000000..96c55d2 --- /dev/null +++ b/osgi.enroute.examples.rest.test/.project @@ -0,0 +1,23 @@ + + + osgi.enroute.examples.rest.test + + + + + + org.eclipse.jdt.core.javabuilder + + + + + bndtools.core.bndbuilder + + + + + + org.eclipse.jdt.core.javanature + bndtools.core.bndnature + + diff --git a/osgi.enroute.examples.rest.test/.settings/org.eclipse.jdt.core.prefs b/osgi.enroute.examples.rest.test/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..a698e59 --- /dev/null +++ b/osgi.enroute.examples.rest.test/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/osgi.enroute.examples.rest.test/bnd.bnd b/osgi.enroute.examples.rest.test/bnd.bnd new file mode 100644 index 0000000..83ad8a3 --- /dev/null +++ b/osgi.enroute.examples.rest.test/bnd.bnd @@ -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)' diff --git a/osgi.enroute.examples.rest.test/readme.md b/osgi.enroute.examples.rest.test/readme.md new file mode 100644 index 0000000..9aa37fc --- /dev/null +++ b/osgi.enroute.examples.rest.test/readme.md @@ -0,0 +1,8 @@ +# OSGI ENROUTE EXAMPLES TEST + +${Bundle-Description} + +## Example + +## References + diff --git a/osgi.enroute.examples.rest.test/src/osgi/enroute/examples/rest/test/RestExamplesTest.java b/osgi.enroute.examples.rest.test/src/osgi/enroute/examples/rest/test/RestExamplesTest.java new file mode 100644 index 0000000..833521b --- /dev/null +++ b/osgi.enroute.examples.rest.test/src/osgi/enroute/examples/rest/test/RestExamplesTest.java @@ -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 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 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 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(); + } +} diff --git a/osgi.enroute.examples.rest.test/test/.gitignore b/osgi.enroute.examples.rest.test/test/.gitignore new file mode 100644 index 0000000..e69de29