From 64f53ef14f5a9ea98208fd2b835bfb01fda9a5f9 Mon Sep 17 00:00:00 2001 From: mmis Date: Thu, 19 Jul 2018 13:21:08 +0100 Subject: Copy policy-endpoints from drools-pdp to common Removed policy-endpoints, and 3 classes from policy-core. Replaced refenences to the deleted classes with references to the corresponding classes in policy-common Issue-ID: POLICY-967 Change-Id: I547cde4894424b8f40b7ddd4e2342ebb729cb588 Signed-off-by: mmis --- feature-simulators/pom.xml | 7 +- .../drools/simulators/DMaaPSimulatorTest.java | 291 ++++++++++----------- 2 files changed, 148 insertions(+), 150 deletions(-) (limited to 'feature-simulators') diff --git a/feature-simulators/pom.xml b/feature-simulators/pom.xml index eecb8dde..dd0b934b 100644 --- a/feature-simulators/pom.xml +++ b/feature-simulators/pom.xml @@ -45,9 +45,14 @@ test - org.onap.policy.drools-pdp + org.onap.policy.common policy-endpoints ${project.version} + + org.onap.policy.drools-pdp + policy-utils + ${project.version} + diff --git a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java index b16804e6..1ba2fc44 100644 --- a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java +++ b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java @@ -36,43 +36,46 @@ import java.nio.charset.StandardCharsets; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import org.onap.policy.drools.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory; +import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.drools.utils.logging.LoggerUtil; -import org.onap.policy.drools.utils.NetworkUtil; public class DMaaPSimulatorTest { - private static final int DMAAPSIM_SERVER_PORT = 6670; + private static final int DMAAPSIM_SERVER_PORT = 6670; + @BeforeClass public static void setUpSimulator() { LoggerUtil.setLevel("ROOT", "INFO"); LoggerUtil.setLevel("org.eclipse.jetty", "WARN"); try { - final HttpServletServer testServer = HttpServletServer.factory.build("dmaapSim", - "localhost", DMAAPSIM_SERVER_PORT, "/", false, true); - testServer.addServletClass("/*", DMaaPSimulatorJaxRs.class.getName()); - testServer.waitedStart(5000); - if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L)) - throw new IllegalStateException("cannot connect to port " + testServer.getPort()); + final HttpServletServer testServer = IndexedHttpServletServerFactory.getInstance().build("dmaapSim", + "localhost", DMAAPSIM_SERVER_PORT, "/", false, true); + testServer.addServletClass("/*", DMaaPSimulatorJaxRs.class.getName()); + testServer.waitedStart(5000); + if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L)) { + throw new IllegalStateException("cannot connect to port " + testServer.getPort()); + } } catch (final Exception e) { - fail(e.getMessage()); + fail(e.getMessage()); } } @AfterClass public static void tearDownSimulator() { - HttpServletServer.factory.destroy(); + IndexedHttpServletServerFactory.getInstance().destroy(); } - + @Test public void testGetNoData() { int timeout = 1000; - Pair response = dmaapGet("myTopicNoData", timeout); + Pair response = dmaapGet("myTopicNoData", timeout); assertNotNull(response); assertNotNull(response.a); assertEquals("No topic", response.b); } - + @Test public void testSinglePost() { String myTopic = "myTopicSinglePost"; @@ -81,13 +84,13 @@ public class DMaaPSimulatorTest { assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapGet(myTopic, 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(testData, response.b); } - + @Test public void testOneTopicMultiPost() { String[] data = {"data point 1", "data point 2", "something random"}; @@ -96,269 +99,259 @@ public class DMaaPSimulatorTest { assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapPost(myTopic, data[1]); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapPost(myTopic, data[2]); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapGet(myTopic, 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(data[0], response.b); - + response = dmaapGet(myTopic, 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(data[1], response.b); - + response = dmaapGet(myTopic, 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(data[2], response.b); } - + @Test public void testMultiTopic() { - String[][] data = {{"Topic one message one", "Topic one message two"}, {"Topic two message one", "Topic two message two"}}; + String[][] data = {{"Topic one message one", "Topic one message two"}, + {"Topic two message one", "Topic two message two"}}; String[] topics = {"topic1", "topic2"}; - + Pair response = dmaapPost(topics[0], data[0][0]); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapGet(topics[0], 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(data[0][0], response.b); - + response = dmaapGet(topics[1], 1000); assertNotNull(response); assertNotNull(response.a); assertEquals("No topic", response.b); - + response = dmaapPost(topics[1], data[1][0]); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapPost(topics[1], data[1][1]); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapPost(topics[0], data[0][1]); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapGet(topics[1], 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(data[1][0], response.b); - + response = dmaapGet(topics[0], 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(data[0][1], response.b); - + response = dmaapGet(topics[1], 1000); assertNotNull(response); assertNotNull(response.a); assertEquals(data[1][1], response.b); - + response = dmaapGet(topics[0], 1000); assertNotNull(response); assertNotNull(response.a); assertEquals("No Data", response.b); } - + @Test public void testResponseCode() { Pair response = dmaapPost("myTopic", "myTopicData"); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = setStatus(503); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapGet("myTopic", 500); assertNotNull(response); assertEquals(503, response.a.intValue()); assertEquals("You got response code: 503", response.b); - + response = setStatus(202); assertNotNull(response); assertNotNull(response.a); assertNotNull(response.b); - + response = dmaapGet("myTopic", 500); assertNotNull(response); assertEquals(202, response.a.intValue()); assertEquals("myTopicData", response.b); } - - private static Pair dmaapGet (String topic, int timeout) { + + private static Pair dmaapGet(String topic, int timeout) { return dmaapGet(topic, "1", "1", timeout); } - - private static Pair dmaapGet (String topic, String consumerGroup, String consumerId, int timeout) { - String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/" + topic + "/" + consumerGroup + "/" + consumerId + "?timeout=" + timeout; + + private static Pair dmaapGet(String topic, String consumerGroup, String consumerId, int timeout) { + String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/" + topic + "/" + consumerGroup + "/" + + consumerId + "?timeout=" + timeout; try { - URLConnection conn = new URL(url).openConnection(); + URLConnection conn = new URL(url).openConnection(); HttpURLConnection httpConn = null; if (conn instanceof HttpURLConnection) { - httpConn = (HttpURLConnection) conn; - } - else { - fail("connection not set up right"); + httpConn = (HttpURLConnection) conn; + } else { + fail("connection not set up right"); } httpConn.setRequestMethod("GET"); httpConn.connect(); String response = ""; try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) { - String line; - while((line = connReader.readLine()) != null) { - response += line; - } - httpConn.disconnect(); - return new Pair(httpConn.getResponseCode(), response); - } - catch (IOException e) { - if (e.getMessage().startsWith("Server returned HTTP response code")) { - System.out.println("hi"); - BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); - String line; - while((line = connReader.readLine()) != null) { - response += line; - } - httpConn.disconnect(); - return new Pair(httpConn.getResponseCode(), response); - } - else { - fail("we got an exception: " + e); - } + String line; + while ((line = connReader.readLine()) != null) { + response += line; + } + httpConn.disconnect(); + return new Pair(httpConn.getResponseCode(), response); + } catch (IOException e) { + if (e.getMessage().startsWith("Server returned HTTP response code")) { + System.out.println("hi"); + BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); + String line; + while ((line = connReader.readLine()) != null) { + response += line; + } + httpConn.disconnect(); + return new Pair(httpConn.getResponseCode(), response); + } else { + fail("we got an exception: " + e); + } } + } catch (Exception e) { + fail("we got an exception" + e); } - catch (Exception e) { - fail("we got an exception" + e); - } - + return null; } - - private static Pair dmaapPost (String topic, String data) { + + private static Pair dmaapPost(String topic, String data) { String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/" + topic; byte[] postData = data.getBytes(StandardCharsets.UTF_8); try { - URLConnection conn = new URL(url).openConnection(); - HttpURLConnection httpConn = null; + URLConnection conn = new URL(url).openConnection(); + HttpURLConnection httpConn = null; if (conn instanceof HttpURLConnection) { - httpConn = (HttpURLConnection) conn; - } - else { - fail("connection not set up right"); + httpConn = (HttpURLConnection) conn; + } else { + fail("connection not set up right"); } httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); - httpConn.setRequestProperty( "Content-Type", "text/plain"); - httpConn.setRequestProperty("Content-Length", ""+postData.length); + httpConn.setRequestProperty("Content-Type", "text/plain"); + httpConn.setRequestProperty("Content-Length", "" + postData.length); httpConn.connect(); String response = ""; try (DataOutputStream connWriter = new DataOutputStream(httpConn.getOutputStream())) { - connWriter.write(postData); - connWriter.flush(); + connWriter.write(postData); + connWriter.flush(); } try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) { - String line; - while((line = connReader.readLine()) != null) { - response += line; - } - httpConn.disconnect(); - return new Pair(httpConn.getResponseCode(), response); - } - catch (IOException e) { - if (e.getMessage().startsWith("Server returned HTTP response code")) { - System.out.println("hi"); - BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); - String line; - while((line = connReader.readLine()) != null) { - response += line; - } - httpConn.disconnect(); - return new Pair(httpConn.getResponseCode(), response); - } - else { - fail("we got an exception: " + e); - } + String line; + while ((line = connReader.readLine()) != null) { + response += line; + } + httpConn.disconnect(); + return new Pair(httpConn.getResponseCode(), response); + } catch (IOException e) { + if (e.getMessage().startsWith("Server returned HTTP response code")) { + System.out.println("hi"); + BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); + String line; + while ((line = connReader.readLine()) != null) { + response += line; + } + httpConn.disconnect(); + return new Pair(httpConn.getResponseCode(), response); + } else { + fail("we got an exception: " + e); + } } - } - catch (Exception e) { - fail("we got an exception: " + e); + } catch (Exception e) { + fail("we got an exception: " + e); } return null; } - - private static Pair setStatus (int status) { + + private static Pair setStatus(int status) { String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/setStatus?statusCode=" + status; try { - URLConnection conn = new URL(url).openConnection(); + URLConnection conn = new URL(url).openConnection(); HttpURLConnection httpConn = null; if (conn instanceof HttpURLConnection) { - httpConn = (HttpURLConnection) conn; - } - else { - fail("connection not set up right"); + httpConn = (HttpURLConnection) conn; + } else { + fail("connection not set up right"); } httpConn.setRequestMethod("POST"); httpConn.connect(); String response = ""; try (BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()))) { - String line; - while((line = connReader.readLine()) != null) { - response += line; - } - httpConn.disconnect(); - return new Pair(httpConn.getResponseCode(), response); - } - catch (IOException e) { - if (e.getMessage().startsWith("Server returned HTTP response code")) { - System.out.println("hi"); - BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); - String line; - while((line = connReader.readLine()) != null) { - response += line; - } - httpConn.disconnect(); - return new Pair(httpConn.getResponseCode(), response); - } - else { - fail("we got an exception: " + e); - } + String line; + while ((line = connReader.readLine()) != null) { + response += line; + } + httpConn.disconnect(); + return new Pair(httpConn.getResponseCode(), response); + } catch (IOException e) { + if (e.getMessage().startsWith("Server returned HTTP response code")) { + System.out.println("hi"); + BufferedReader connReader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); + String line; + while ((line = connReader.readLine()) != null) { + response += line; + } + httpConn.disconnect(); + return new Pair(httpConn.getResponseCode(), response); + } else { + fail("we got an exception: " + e); + } } - } - catch (Exception e) { - fail("we got an exception" + e); + } catch (Exception e) { + fail("we got an exception" + e); } return null; } - + private static class Pair { - public final A a; - public final B b; - - public Pair(A a, B b) { - this.a = a; - this.b = b; - } - } + public final A a; + public final B b; + + public Pair(A a, B b) { + this.a = a; + this.b = b; + } + } } -- cgit 1.2.3-korg