summaryrefslogtreecommitdiffstats
path: root/feature-simulators/src/test/java/org
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-02-22 16:50:27 -0500
committerJim Hahn <jrh3@att.com>2021-02-22 16:51:33 -0500
commite4b3aa2e10fbb0506a0f3dd878cd0a2259d82bf4 (patch)
tree8bd7020412ec50b16a726207e7f3410d21e8876f /feature-simulators/src/test/java/org
parentde864c69085f155c2861990d1647295f5432ae8b (diff)
Deprecate feature-simulators from drools
feature-simulators was only used by server-pool. Now that server-pool has been deprecated, the simulators can be deprecated, too. Issue-ID: POLICY-3079 Change-Id: I4555432d90f99735de2f189ce626befb414027cb Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'feature-simulators/src/test/java/org')
-rw-r--r--feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java219
-rw-r--r--feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java200
2 files changed, 0 insertions, 419 deletions
diff --git a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java b/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java
deleted file mode 100644
index 6e1a8d68..00000000
--- a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorJaxRsTest.java
+++ /dev/null
@@ -1,219 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP
- * ================================================================================
- * Copyright (C) 2019, 2021 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.onap.policy.drools.simulators;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.doThrow;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.TimeUnit;
-import javax.servlet.http.HttpServletResponse;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-@RunWith(MockitoJUnitRunner.class)
-public class DMaaPSimulatorJaxRsTest {
- private static final String MESSAGE = "hello";
- private static final String MESSAGE2 = "world";
- private static final String EXPECTED_EXCEPTION = "expected exception";
- private static final String TOPIC = "my-topic";
- private static final int TIMEOUT_MS = 10;
- private static final int LONG_TIMEOUT_MS = 250;
-
- @Mock
- private HttpServletResponse resp;
-
- private DMaaPSimulatorJaxRs sim;
-
- /**
- * Initializes objects and creates the simulator.
- */
- @Before
- public void setUp() {
- sim = new DMaaPSimulatorJaxRs();
- }
-
- @After
- public void tearDown() {
- DMaaPSimulatorJaxRs.reset();
- }
-
- @Test
- public void testSubscribe() {
- sim.publish(TOPIC, MESSAGE);
-
- assertEquals(MESSAGE, sim.subscribe(0, TOPIC, resp));
- }
-
- @Test
- public void testSubscribe_FlushEx() throws IOException {
- doThrow(new IOException(EXPECTED_EXCEPTION)).when(resp).flushBuffer();
-
- assertEquals("Got an error", sim.subscribe(TIMEOUT_MS, TOPIC, resp));
- }
-
- @Test
- public void testSubscribe_BadStatus_testSetResponseCode() {
- sim.setStatus(199);
- assertEquals("You got response code: 199", sim.subscribe(TIMEOUT_MS, TOPIC, resp));
-
- sim.setStatus(300);
- assertEquals("You got response code: 300", sim.subscribe(TIMEOUT_MS, TOPIC, resp));
- }
-
- @Test
- public void testSubscribe_UnknownTopic_ZeroTimeout() {
- assertEquals(DMaaPSimulatorJaxRs.NO_TOPIC_MSG, sim.subscribe(0, TOPIC, resp));
- }
-
- @Test
- public void testSubscribe_UnknownTopic_NonZeroTimeout() {
- assertEquals(DMaaPSimulatorJaxRs.NO_TOPIC_MSG, sim.subscribe(TIMEOUT_MS, TOPIC, resp));
- }
-
- @Test
- public void testGetNextMessageFromQueue() {
- sim.publish(TOPIC, MESSAGE);
- sim.publish(TOPIC, MESSAGE2);
-
- assertEquals(MESSAGE, sim.subscribe(0, TOPIC, resp));
- assertEquals(MESSAGE2, sim.subscribe(0, TOPIC, resp));
-
- // repeat - no message
- assertEquals(DMaaPSimulatorJaxRs.NO_DATA_MSG, sim.subscribe(0, TOPIC, resp));
- }
-
- @Test
- public void testGetNextMessageFromQueue_Interrupted() throws InterruptedException {
- sim = new DMaaPSimulatorJaxRs() {
- @Override
- protected String poll(BlockingQueue<String> queue, int timeout) throws InterruptedException {
- throw new InterruptedException(EXPECTED_EXCEPTION);
- }
- };
-
- sim.publish(TOPIC, MESSAGE);
-
- // put it in the background so we don't interrupt the test thread
- BlockingQueue<String> queue = new LinkedBlockingQueue<>();
- backgroundSubscribe(queue);
-
- assertEquals(DMaaPSimulatorJaxRs.NO_DATA_MSG, queue.take());
- }
-
- @Test
- public void testWaitForNextMessageFromQueue() throws InterruptedException {
- CountDownLatch waitCalled = new CountDownLatch(1);
-
- sim = new DMaaPSimulatorJaxRs() {
- @Override
- protected String waitForNextMessageFromQueue(int timeout, String topicName) {
- waitCalled.countDown();
- return super.waitForNextMessageFromQueue(timeout, topicName);
- }
- };
-
- BlockingQueue<String> queue = new LinkedBlockingQueue<>();
-
- CountDownLatch latch1 = backgroundSubscribe(queue);
- CountDownLatch latch2 = backgroundSubscribe(queue);
-
- // wait for both threads to start
- latch1.await();
- latch2.await();
-
- /*
- * Must pause to prevent the topic from being created before subscribe() is
- * invoked.
- */
- assertTrue(waitCalled.await(1, TimeUnit.SECONDS));
-
- // only publish one message
- sim.publish(TOPIC, MESSAGE);
-
- // wait for both subscribers to add their messages to the queue
- List<String> messages = new ArrayList<>();
- messages.add(queue.take());
- messages.add(queue.take());
-
- // sort them so the order is consistent
- Collections.sort(messages);
-
- assertEquals("[No Data, hello]", messages.toString());
- }
-
- @Test
- public void testWaitForNextMessageFromQueue_Interrupted() throws InterruptedException {
- sim = new DMaaPSimulatorJaxRs() {
- @Override
- protected void sleep(int timeout) throws InterruptedException {
- throw new InterruptedException(EXPECTED_EXCEPTION);
- }
- };
-
- // put it in the background so we don't interrupt the test thread
- BlockingQueue<String> queue = new LinkedBlockingQueue<>();
- backgroundSubscribe(queue);
-
- assertEquals(DMaaPSimulatorJaxRs.NO_TOPIC_MSG, queue.take());
- }
-
- @Test
- public void testPublish() {
- assertEquals("", sim.publish(TOPIC, MESSAGE));
- assertEquals(MESSAGE, sim.subscribe(0, TOPIC, resp));
- }
-
- @Test
- public void testSetStatus() {
- assertEquals("Status code set", sim.setStatus(500));
- assertEquals("You got response code: 500", sim.subscribe(TIMEOUT_MS, TOPIC, resp));
- }
-
- /**
- * Invokes subscribe() in a background thread.
- *
- * @param queue where to place the returned result
- * @return a latch that will be counted down just before the background thread invokes
- * subscribe()
- */
- private CountDownLatch backgroundSubscribe(BlockingQueue<String> queue) {
- CountDownLatch latch = new CountDownLatch(1);
-
- new Thread(() -> {
- latch.countDown();
- queue.add(sim.subscribe(LONG_TIMEOUT_MS, TOPIC, resp));
- }).start();
-
- return latch;
- }
-}
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
deleted file mode 100644
index ad01870b..00000000
--- a/feature-simulators/src/test/java/org/onap/policy/drools/simulators/DMaaPSimulatorTest.java
+++ /dev/null
@@ -1,200 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * feature-simulators
- * ================================================================================
- * Copyright (C) 2017-2020 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.onap.policy.drools.simulators;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-
-import java.io.IOException;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.nio.charset.StandardCharsets;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.tuple.Pair;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.onap.policy.common.endpoints.http.server.HttpServletServer;
-import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
-import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.drools.utils.logging.LoggerUtil;
-
-public class DMaaPSimulatorTest {
-
- private static int DMAAPSIM_SERVER_PORT;
-
- /**
- * Setup the simulator.
- * @throws IOException if a server port cannot be allocated
- */
- @BeforeClass
- public static void setUpSimulator() throws IOException {
- LoggerUtil.setLevel("ROOT", "INFO");
- LoggerUtil.setLevel("org.eclipse.jetty", "WARN");
- DMAAPSIM_SERVER_PORT = NetworkUtil.allocPort();
- try {
- final HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().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());
- }
- }
-
- @AfterClass
- public static void tearDownSimulator() {
- HttpServletServerFactoryInstance.getServerFactory().destroy();
- }
-
- @Test
- public void testGetNoData() throws IOException {
- validateResponse(DMaaPSimulatorJaxRs.NO_TOPIC_MSG, dmaapGet("myTopicNoData", 1000));
- }
-
- @Test
- public void testSinglePost() throws IOException {
- String myTopic = "myTopicSinglePost";
- String testData = "This is some test data";
-
- validateResponse(dmaapPost(myTopic, testData));
-
- validateResponse(testData, dmaapGet(myTopic, 1000));
- }
-
- @Test
- public void testOneTopicMultiPost() throws IOException {
- String[] data = {"data point 1", "data point 2", "something random"};
- String myTopic = "myTopicMultiPost";
-
- validateResponse(dmaapPost(myTopic, data[0]));
- validateResponse(dmaapPost(myTopic, data[1]));
- validateResponse(dmaapPost(myTopic, data[2]));
-
- validateResponse(data[0], dmaapGet(myTopic, 1000));
- validateResponse(data[1], dmaapGet(myTopic, 1000));
- validateResponse(data[2], dmaapGet(myTopic, 1000));
- }
-
- @Test
- public void testMultiTopic() throws IOException {
- String[][] data = {{"Topic one message one", "Topic one message two"},
- {"Topic two message one", "Topic two message two"}};
- String[] topics = {"topic1", "topic2"};
-
- validateResponse(dmaapPost(topics[0], data[0][0]));
-
- validateResponse(data[0][0], dmaapGet(topics[0], 1000));
- validateResponse(DMaaPSimulatorJaxRs.NO_TOPIC_MSG, dmaapGet(topics[1], 1000));
-
- validateResponse(dmaapPost(topics[1], data[1][0]));
- validateResponse(dmaapPost(topics[1], data[1][1]));
- validateResponse(dmaapPost(topics[0], data[0][1]));
-
- validateResponse(data[1][0], dmaapGet(topics[1], 1000));
- validateResponse(data[0][1], dmaapGet(topics[0], 1000));
- validateResponse(data[1][1], dmaapGet(topics[1], 1000));
- validateResponse(DMaaPSimulatorJaxRs.NO_DATA_MSG, dmaapGet(topics[0], 1000));
- }
-
- @Test
- public void testResponseCode() throws IOException {
- validateResponse(dmaapPost("myTopic", "myTopicData"));
-
- validateResponse(setStatus(503));
- validateResponse(503, "You got response code: 503", dmaapGet("myTopic", 500));
-
- validateResponse(setStatus(202));
- validateResponse(202, "myTopicData", dmaapGet("myTopic", 500));
- }
-
- private void validateResponse(Pair<Integer, String> response) {
- assertNotNull(response);
- assertNotNull(response.getLeft());
- assertNotNull(response.getRight());
- }
-
- private void validateResponse(int expectedCode, String expectedResponse, Pair<Integer, String> response) {
- assertNotNull(response);
- assertEquals(expectedCode, response.getLeft().intValue());
- assertEquals(expectedResponse, response.getRight());
- }
-
- private void validateResponse(String expectedResponse, Pair<Integer, String> response) {
- assertNotNull(response);
- assertNotNull(response.getLeft());
- assertEquals(expectedResponse, response.getRight());
- }
-
- private static Pair<Integer, String> dmaapGet(String topic, int timeout) throws IOException {
- return dmaapGet(topic, "1", "1", timeout);
- }
-
- private static Pair<Integer, String> dmaapGet(String topic, String consumerGroup, String consumerId, int timeout)
- throws IOException {
- String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/" + topic + "/" + consumerGroup + "/"
- + consumerId + "?timeout=" + timeout;
- HttpURLConnection httpConn = (HttpURLConnection) new URL(url).openConnection();
- httpConn.setRequestMethod("GET");
- httpConn.connect();
- return getResponse(httpConn);
- }
-
- private static Pair<Integer, String> dmaapPost(String topic, String data) throws IOException {
- String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/" + topic;
- byte[] postData = data.getBytes(StandardCharsets.UTF_8);
- HttpURLConnection httpConn = (HttpURLConnection) new URL(url).openConnection();
- httpConn.setRequestMethod("POST");
- httpConn.setDoOutput(true);
- httpConn.setRequestProperty("Content-Type", "text/plain");
- httpConn.setRequestProperty("Content-Length", "" + postData.length);
- httpConn.connect();
- IOUtils.write(postData, httpConn.getOutputStream());
- return getResponse(httpConn);
- }
-
- private static Pair<Integer, String> setStatus(int status) throws IOException {
- String url = "http://localhost:" + DMAAPSIM_SERVER_PORT + "/events/setStatus?statusCode=" + status;
- HttpURLConnection httpConn = (HttpURLConnection) new URL(url).openConnection();
- httpConn.setRequestMethod("POST");
- httpConn.connect();
- return getResponse(httpConn);
- }
-
- private static Pair<Integer, String> getResponse(HttpURLConnection httpConn) throws IOException {
- try {
- String response = IOUtils.toString(httpConn.getInputStream(), StandardCharsets.UTF_8);
- return Pair.of(httpConn.getResponseCode(), response);
-
- } catch (IOException e) {
- if (e.getMessage().startsWith("Server returned HTTP response code")) {
- String response = IOUtils.toString(httpConn.getErrorStream(), StandardCharsets.UTF_8);
- return Pair.of(httpConn.getResponseCode(), response);
- }
-
- throw e;
- }
- }
-}