From 61a84fecc1eae8640fec2860f4b50102ed0baa64 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 27 Mar 2020 12:35:23 -0400 Subject: Test new actors against simulators Also modified HttpParams to allow "path" to be blank to support any cases that really have no "path" to append to the context URI base path. Issue-ID: POLICY-2405 Change-Id: I49eebde6759659d2804b5a11c1504c37674bd0c4 Signed-off-by: Jim Hahn --- .../model-actors/actor.test/pom.xml | 5 ++ .../test/BasicBidirectionalTopicOperation.java | 89 +++++++++++++++++++++- .../controlloop/actor/test/BasicOperation.java | 7 ++ .../test/BasicBidirectionalTopicOperationTest.java | 62 ++++++++++++++- 4 files changed, 160 insertions(+), 3 deletions(-) (limited to 'models-interactions/model-actors/actor.test') diff --git a/models-interactions/model-actors/actor.test/pom.xml b/models-interactions/model-actors/actor.test/pom.xml index c67e64857..16af025cb 100644 --- a/models-interactions/model-actors/actor.test/pom.xml +++ b/models-interactions/model-actors/actor.test/pom.xml @@ -58,6 +58,11 @@ junit provided + + org.onap.policy.models.policy-models-interactions + simulators + ${project.version} + org.onap.policy.common utils-test diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java index bba2bc13a..c8b69677f 100644 --- a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperation.java @@ -20,27 +20,50 @@ package org.onap.policy.controlloop.actor.test; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; +import java.util.List; import java.util.function.BiConsumer; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; +import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.TopicSource; +import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; +import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.endpoints.parameters.TopicParameters; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoderObject; import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig; import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler; +import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager; import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder; +import org.onap.policy.simulators.TopicServer; +import org.onap.policy.simulators.Util; /** * Superclass for various BidirectionalTopicOperation tests. */ -public class BasicBidirectionalTopicOperation extends BasicOperation { +public abstract class BasicBidirectionalTopicOperation extends BasicOperation { protected static final String MY_SINK = "my-sink"; protected static final String MY_SOURCE = "my-source"; protected static final int TIMEOUT_SEC = 10; protected static final long TIMEOUT_MS = 1000L * TIMEOUT_SEC; + // sink and source used by the TopicServer + private static TopicSink serverSink; + private static TopicSource serverSource; + private static BidirectionalTopicHandler realTopicHandler; + + protected static BidirectionalTopicManager topicMgr = (sink, source) -> { + // note: the sink and source names are swapped for the simulator + assertEquals(serverSource.getTopic(), sink); + assertEquals(serverSink.getTopic(), source); + return realTopicHandler; + }; + @Captor protected ArgumentCaptor> listenerCaptor; @@ -51,6 +74,9 @@ public class BasicBidirectionalTopicOperation extends BasicOperation { @Mock protected BidirectionalTopicConfig config; + @SuppressWarnings("rawtypes") + private TopicServer topicServer; + /** * Constructs the object using a default actor and operation name. */ @@ -68,15 +94,76 @@ public class BasicBidirectionalTopicOperation extends BasicOperation { super(actor, operation); } + /** + * Starts the topic. + */ + protected static void initBeforeClass(String sinkTopic, String sourceTopic) throws Exception { + + Util.buildDmaapSim(); + + // note: the sink and source names are swapped for the simulator + TopicParameters ptopic = new TopicParameters(); + ptopic.setTopic(sourceTopic); + ptopic.setManaged(true); + ptopic.setServers(List.of("localhost")); + ptopic.setTopicCommInfrastructure("dmaap"); + ptopic.setFetchTimeout(500); + serverSink = TopicEndpointManager.getManager().addTopicSinks(List.of(ptopic)).get(0); + + ptopic.setTopic(sinkTopic); + serverSource = TopicEndpointManager.getManager().addTopicSources(List.of(ptopic)).get(0); + + serverSink.start(); + serverSource.start(); + + if (!sinkTopic.equals(sourceTopic)) { + // sink and source are different - create other ends for the actor + initActorTopics(sinkTopic, sourceTopic, ptopic); + } + + realTopicHandler = new BidirectionalTopicHandler(sinkTopic, sourceTopic); + realTopicHandler.start(); + } + + private static void initActorTopics(String sinkTopic, String sourceTopic, TopicParameters ptopic) { + // create sink and source for the actor, too + ptopic.setTopic(sinkTopic); + TopicEndpointManager.getManager().addTopicSinks(List.of(ptopic)).get(0).start(); + + ptopic.setTopic(sourceTopic); + TopicEndpointManager.getManager().addTopicSources(List.of(ptopic)).get(0).start(); + } + + protected static void destroyAfterClass() { + TopicEndpointManager.getManager().shutdown(); + HttpServletServerFactoryInstance.getServerFactory().destroy(); + HttpClientFactoryInstance.getClientFactory().destroy(); + } + /** * Initializes mocks and sets up. */ @Override public void setUpBasic() { super.setUpBasic(); + topicServer = makeServer(serverSink, serverSource); initConfig(); } + public void tearDownBasic() { + topicServer.shutdown(); + } + + /** + * Makes a simulator for the given sink and source. + * + * @param sink topic to which the simulator should publish responses + * @param source topic from which the simulator should receive messages + * @return a new topic server/simulator + */ + @SuppressWarnings("rawtypes") + protected abstract TopicServer makeServer(TopicSink sink, TopicSource source); + /** * Initializes a configuration. */ diff --git a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java index d6940d299..3b1871f20 100644 --- a/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java +++ b/models-interactions/model-actors/actor.test/src/main/java/org/onap/policy/controlloop/actor/test/BasicOperation.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.TreeMap; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import javax.ws.rs.core.Response; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -59,6 +60,12 @@ public class BasicOperation { protected static final String DEFAULT_OPERATION = "default-operation"; protected static final String TARGET_ENTITY = "my-target"; + protected static final Executor blockingExecutor = command -> { + Thread thread = new Thread(command); + thread.setDaemon(true); + thread.start(); + }; + protected final String actorName; protected final String operationName; protected Coder coder = new StandardCoder(); diff --git a/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperationTest.java b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperationTest.java index 101f130a3..afb8f075e 100644 --- a/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperationTest.java +++ b/models-interactions/model-actors/actor.test/src/test/java/org/onap/policy/controlloop/actor/test/BasicBidirectionalTopicOperationTest.java @@ -28,12 +28,18 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import java.util.function.BiConsumer; +import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.TopicSource; import org.onap.policy.common.utils.coder.StandardCoderObject; +import org.onap.policy.simulators.TopicServer; public class BasicBidirectionalTopicOperationTest { private static final String ACTOR = "my-actor"; @@ -44,6 +50,16 @@ public class BasicBidirectionalTopicOperationTest { private BasicBidirectionalTopicOperation oper; + @BeforeClass + public static void setUpBeforeClass() throws Exception { + BasicBidirectionalTopicOperation.initBeforeClass(BasicBidirectionalTopicOperation.MY_SINK, + BasicBidirectionalTopicOperation.MY_SOURCE); + } + + @AfterClass + public static void tearDownAfterClass() { + BasicBidirectionalTopicOperation.destroyAfterClass(); + } /** * Sets up. @@ -52,13 +68,28 @@ public class BasicBidirectionalTopicOperationTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - oper = new BasicBidirectionalTopicOperation(ACTOR, OPERATION); + oper = new MyOperation(ACTOR, OPERATION); oper.setUpBasic(); } + @After + public void tearDown() { + oper.tearDownBasic(); + } + + @Test + public void testTopicMgr() { + assertNotNull(BasicBidirectionalTopicOperation.topicMgr.getTopicHandler( + BasicBidirectionalTopicOperation.MY_SINK, BasicBidirectionalTopicOperation.MY_SOURCE)); + } + @Test public void testBasicBidirectionalTopicOperation() { - oper = new BasicBidirectionalTopicOperation(); + oper.tearDownBasic(); + + oper = new MyOperation(); + oper.setUpBasic(); + assertEquals(BasicOperation.DEFAULT_ACTOR, oper.actorName); assertEquals(BasicOperation.DEFAULT_OPERATION, oper.operationName); } @@ -101,4 +132,31 @@ public class BasicBidirectionalTopicOperationTest { assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json")) .withMessage("response is not a Map"); } + + private static class MyOperation extends BasicBidirectionalTopicOperation { + public MyOperation() { + super(); + } + + /** + * Constructs the object. + * + * @param actor actor name + * @param operation operation name + */ + public MyOperation(String actor, String operation) { + super(actor, operation); + } + + @Override + @SuppressWarnings("rawtypes") + protected TopicServer makeServer(TopicSink sink, TopicSource source) { + return new TopicServer(sink, source, null, String.class) { + @Override + protected String process(String request) { + return null; + } + }; + } + } } -- cgit 1.2.3-korg