diff options
Diffstat (limited to 'policy-endpoints/src/test')
5 files changed, 483 insertions, 0 deletions
diff --git a/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/HttpClientTest.java b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/HttpClientTest.java new file mode 100644 index 00000000..ced3dcf4 --- /dev/null +++ b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/HttpClientTest.java @@ -0,0 +1,230 @@ +/*- + * ============LICENSE_START======================================================= + * policy-endpoints + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.drools.http.server.test; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Properties; + +import javax.ws.rs.core.Response; + +import org.junit.Test; +import org.openecomp.policy.drools.http.client.HttpClient; +import org.openecomp.policy.drools.http.server.HttpServletServer; +import org.openecomp.policy.drools.properties.PolicyProperties; + +public class HttpClientTest { + + @Test + public void testHttpNoAuthClient() throws Exception { + System.out.println("-- testHttpNoAuthClient() --"); + + HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 6666, "/", true); + server.addServletPackage("/*", this.getClass().getPackage().getName()); + server.waitedStart(5000); + + HttpClient client = HttpClient.factory.build("testHttpNoAuthClient", false, false, + "localhost", 6666, "junit/echo", + null, null, true); + Response response = client.get("hello"); + String body = HttpClient.getBody(response, String.class); + + assertTrue(response.getStatus() == 200); + assertTrue(body.equals("hello")); + + HttpServletServer.factory.destroy(); + HttpClient.factory.destroy(); + } + + @Test + public void testHttpAuthClient() throws Exception { + System.out.println("-- testHttpAuthClient() --"); + + HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 6666, "/", true); + server.setBasicAuthentication("x", "y", null); + server.addServletPackage("/*", this.getClass().getPackage().getName()); + server.waitedStart(5000); + + HttpClient client = HttpClient.factory.build("testHttpAuthClient",false, false, + "localhost", 6666, "junit/echo", + "x", "y", true); + Response response = client.get("hello"); + String body = HttpClient.getBody(response, String.class); + + assertTrue(response.getStatus() == 200); + assertTrue(body.equals("hello")); + + HttpServletServer.factory.destroy(); + HttpClient.factory.destroy(); + } + + @Test + public void testHttpAuthClient401() throws Exception { + System.out.println("-- testHttpAuthClient401() --"); + + HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 6666, "/", true); + server.setBasicAuthentication("x", "y", null); + server.addServletPackage("/*", this.getClass().getPackage().getName()); + server.waitedStart(5000); + + HttpClient client = HttpClient.factory.build("testHttpAuthClient401",false, false, + "localhost", 6666, "junit/echo", + null, null, true); + Response response = client.get("hello"); + assertTrue(response.getStatus() == 401); + + HttpServletServer.factory.destroy(); + HttpClient.factory.destroy(); + } + + //@Test + public void testHttpAuthClientHttps() throws Exception { + System.out.println("-- testHttpAuthClientHttps() --"); + + HttpClient client = HttpClient.factory.build("testHttpAuthClientHttps", true, true, "somehost.somewhere.com", + 9091, "pap/test", "testpap", "alpha123", true); + Response response = client.get(); + assertTrue(response.getStatus() == 200); + + HttpClient client2 = HttpClient.factory.build("testHttpAuthClientHttps2", true, true, "somehost.somewhere.com", + 8081, "pdp", "testpdp", "alpha123", true); + Response response2 = client2.get("test"); + assertTrue(response2.getStatus() == 500); + + HttpServletServer.factory.destroy(); + HttpClient.factory.destroy(); + } + + @Test + public void testHttpAuthClientProps() throws Exception { + System.out.println("-- testHttpAuthClientProps() --"); + + Properties httpProperties = new Properties(); + + httpProperties.setProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES, "PAP,PDP"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, + "localhost"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + "9091"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + "testpap"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + "alpha123"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + "org.openecomp.policy.drools.http.server.test.RestMockHealthCheck"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, + "true"); + + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, + "localhost"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + "8081"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + "testpdp"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + "alpha123"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + "org.openecomp.policy.drools.http.server.test.RestMockHealthCheck"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, + "true"); + + httpProperties.setProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES, "PAP,PDP"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, + "localhost"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + "9091"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_URL_SUFFIX, + "pap/test"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_HTTPS_SUFFIX, + "false"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + "testpap"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + "alpha123"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PAP" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, + "true"); + + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, + "localhost"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, + "8081"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_URL_SUFFIX, + "pdp"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_HTTPS_SUFFIX, + "false"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + "testpdp"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + "alpha123"); + httpProperties.setProperty + (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "PDP" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, + "true"); + + ArrayList<HttpServletServer> servers = HttpServletServer.factory.build(httpProperties); + assertTrue(servers.size() == 2); + + ArrayList<HttpClient> clients = HttpClient.factory.build(httpProperties); + assertTrue(clients.size() == 2); + + for (HttpServletServer server: servers) { + server.waitedStart(5000); + } + + HttpClient clientPAP = HttpClient.factory.get("PAP"); + Response response = clientPAP.get(); + assertTrue(response.getStatus() == 200); + + HttpClient clientPDP = HttpClient.factory.get("PDP"); + Response response2 = clientPDP.get("test"); + assertTrue(response2.getStatus() == 500); + + HttpServletServer.factory.destroy(); + HttpClient.factory.destroy(); + } + + +} diff --git a/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/HttpServerTest.java b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/HttpServerTest.java new file mode 100644 index 00000000..94f29804 --- /dev/null +++ b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/HttpServerTest.java @@ -0,0 +1,181 @@ +/*- + * ============LICENSE_START======================================================= + * policy-endpoints + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ +package org.openecomp.policy.drools.http.server.test; + +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.UUID; + +import org.junit.Test; +import org.openecomp.policy.drools.http.server.HttpServletServer; + +/** + * + */ +public class HttpServerTest { + + @Test + public void testSingleServer() throws Exception { + System.out.println("-- testSingleServer() --"); + + HttpServletServer server = HttpServletServer.factory.build("echo", "localhost", 5678, "/", true); + server.addServletPackage("/*", this.getClass().getPackage().getName()); + server.waitedStart(5000); + + assertTrue(HttpServletServer.factory.get(5678).isAlive()); + + String echo = "hello"; + URL url = new URL("http://localhost:5678/junit/echo/" + echo); + String response = response(url); + System.out.println("Received .. " + response); + assertTrue(response.equals(echo)); + + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); + } + + @Test + public void testMultipleServers() throws Exception { + System.out.println("-- testMultipleServers() --"); + + HttpServletServer server1 = HttpServletServer.factory.build("echo-1", "localhost", 5678, "/", true); + server1.addServletPackage("/*", this.getClass().getPackage().getName()); + server1.waitedStart(5000); + + HttpServletServer server2 = HttpServletServer.factory.build("echo-2", "localhost", 5679, "/", true); + server2.addServletPackage("/*", this.getClass().getPackage().getName()); + server2.waitedStart(5000); + + assertTrue(HttpServletServer.factory.get(5678).isAlive()); + assertTrue(HttpServletServer.factory.get(5679).isAlive()); + + String echo = "hello"; + + URL url1 = new URL("http://localhost:5678/junit/echo/" + echo); + String response1 = response(url1); + System.out.println("Received .. " + response1); + assertTrue(response1.equals(echo)); + + URL url2 = new URL("http://localhost:5679/junit/echo/" + echo); + String response2 = response(url2); + System.out.println("Received .. " + response2); + assertTrue(response2.equals(echo)); + + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); + } + + @Test + public void testMultiServicePackage() throws Exception { + System.out.println("-- testMultiServicePackage() --"); + + String randomName = UUID.randomUUID().toString(); + + HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", true); + server.addServletPackage("/*", this.getClass().getPackage().getName()); + server.waitedStart(5000); + + assertTrue(HttpServletServer.factory.get(5678).isAlive()); + + String echo = "hello"; + URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo); + String responseService1 = response(urlService1); + System.out.println("Received .. " + responseService1); + assertTrue(responseService1.equals(echo)); + + URL urlService2 = new URL("http://localhost:5678/junit/endpoints/http/servers"); + String responseService2 = response(urlService2); + System.out.println("Received .. " + responseService2); + assertTrue(responseService2.contains(randomName)); + + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); + } + + @Test + public void testServiceClass() throws Exception { + System.out.println("-- testServiceClass() --"); + String randomName = UUID.randomUUID().toString(); + + HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", true); + server.addServletClass("/*", RestEchoService.class.getCanonicalName()); + server.waitedStart(5000); + + assertTrue(HttpServletServer.factory.get(5678).isAlive()); + + String echo = "hello"; + URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo); + String responseService1 = response(urlService1); + System.out.println("Received .. " + responseService1); + assertTrue(responseService1.equals(echo)); + + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); + } + + @Test + public void testMultiServiceClass() throws Exception { + System.out.println("-- testMultiServiceClass() --"); + + String randomName = UUID.randomUUID().toString(); + + HttpServletServer server = HttpServletServer.factory.build(randomName, "localhost", 5678, "/", true); + server.addServletClass("/*", RestEchoService.class.getCanonicalName()); + server.addServletClass("/*", RestEndpoints.class.getCanonicalName()); + server.waitedStart(5000); + + assertTrue(HttpServletServer.factory.get(5678).isAlive()); + + String echo = "hello"; + URL urlService1 = new URL("http://localhost:5678/junit/echo/" + echo); + String responseService1 = response(urlService1); + System.out.println("Received .. " + responseService1); + assertTrue(responseService1.equals(echo)); + + URL urlService2 = new URL("http://localhost:5678/junit/endpoints/http/servers"); + String responseService2 = response(urlService2); + System.out.println("Received .. " + responseService2); + assertTrue(responseService2.contains(randomName)); + + HttpServletServer.factory.destroy(); + assertTrue(HttpServletServer.factory.inventory().size() == 0); + } + + /** + * @param url + * @throws IOException + */ + protected String response(URL url) throws IOException { + BufferedReader ioReader = new BufferedReader(new InputStreamReader(url.openStream())); + String response = ""; + String line; + while ((line = ioReader.readLine()) != null) { + response += line; + } + return response; + } + + + +} diff --git a/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestEchoService.java b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestEchoService.java new file mode 100644 index 00000000..a0320a09 --- /dev/null +++ b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestEchoService.java @@ -0,0 +1,19 @@ +package org.openecomp.policy.drools.http.server.test; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/junit/echo") +public class RestEchoService { + + @GET + @Path("{word}") + @Produces(MediaType.TEXT_PLAIN) + public String echo(@PathParam("word") String word) { + return word; + } + +} diff --git a/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestEndpoints.java b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestEndpoints.java new file mode 100644 index 00000000..a00f2ffd --- /dev/null +++ b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestEndpoints.java @@ -0,0 +1,25 @@ +package org.openecomp.policy.drools.http.server.test; + +import java.util.List; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.openecomp.policy.drools.http.server.HttpServletServer; + +@Path("/junit/endpoints") +public class RestEndpoints { + + @GET + @Path("http/servers") + @Produces(MediaType.TEXT_PLAIN) + public String httpServers() { + List<HttpServletServer> servers = + HttpServletServer.factory.inventory(); + return servers.toString(); + } + + +} diff --git a/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestMockHealthCheck.java b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestMockHealthCheck.java new file mode 100644 index 00000000..ff1b4980 --- /dev/null +++ b/policy-endpoints/src/test/java/org/openecomp/policy/drools/http/server/test/RestMockHealthCheck.java @@ -0,0 +1,28 @@ +package org.openecomp.policy.drools.http.server.test; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +@Path("/") +public class RestMockHealthCheck { + + @GET + @Path("pap/test") + @Produces(MediaType.APPLICATION_JSON) + public Response papHealthCheck() { + return Response.status(Status.OK).entity("All Alive").build(); + } + + @GET + @Path("pdp/test") + @Produces(MediaType.APPLICATION_JSON) + public Response pdpHealthCheck() { + return Response.status(Status.INTERNAL_SERVER_ERROR).entity("At least some Dead").build(); + } + + +} |