From: Jim Hahn Date: Fri, 20 Mar 2020 19:53:47 +0000 (-0400) Subject: Bug fixes in models simulators X-Git-Tag: 2.2.1~4^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fmodels.git;a=commitdiff_plain;h=f3fe0b63bc3bf7efdfce815bf17034291f8ff265 Bug fixes in models simulators Fixed these issues: - topics weren't started - appc topics were reversed - prevent appc simulator from responding to a response (i.e., infinite loop) Issue-ID: POLICY-2434 Signed-off-by: Jim Hahn Change-Id: I1f43be72d35f874fec98f48f1a112c055c00eee5 --- diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/AppcLegacyTopicServer.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/AppcLegacyTopicServer.java index 5ebd3bd2a..0c259783d 100644 --- a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/AppcLegacyTopicServer.java +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/AppcLegacyTopicServer.java @@ -36,6 +36,15 @@ public class AppcLegacyTopicServer extends TopicServer { @Override protected String process(Request request) { + /* + * The request and response are on the same topic, thus this may be invoked with a + * request or with a response object. If the "action" is null, then we know it's a + * response. + */ + if (request.getAction() == null) { + return null; + } + String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/appc/appc.legacy.success.json"); return response.replace("${replaceMe}", request.getCommonHeader().getSubRequestId()); } diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/TopicServer.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/TopicServer.java index 0abe5f421..4c01511da 100644 --- a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/TopicServer.java +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/TopicServer.java @@ -65,8 +65,17 @@ public abstract class TopicServer implements TopicListener { throw new IllegalArgumentException("cannot decode request from " + source.getTopic()); } - sink.send(process(req)); + String resp = process(req); + if (resp != null) { + sink.send(resp); + } } + /** + * Processes a request. + * + * @param request request to be processed + * @return the response, or {@code null} if no response is to be sent + */ protected abstract String process(Q request); } diff --git a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/AppcLegacyTopicServerTest.java b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/AppcLegacyTopicServerTest.java index 57d574a0b..c3c3195df 100644 --- a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/AppcLegacyTopicServerTest.java +++ b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/AppcLegacyTopicServerTest.java @@ -22,6 +22,8 @@ package org.onap.policy.simulators; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import org.junit.Before; @@ -55,7 +57,7 @@ public class AppcLegacyTopicServerTest { } @Test - public void testProcessAppcLcmDmaapWrapper() { + public void testProcess() { String request = ResourceUtils.getResourceAsString("org/onap/policy/simulators/appc/appc.legacy.request.json"); assertNotNull(request); @@ -66,4 +68,18 @@ public class AppcLegacyTopicServerTest { assertThat(respCaptor.getValue()).contains("111be3d2").doesNotContain("replaceMe"); } + + /** + * Tests process() when the message is a response. + */ + @Test + public void testProcessNoResponse() { + // NOTE: this json file is a RESPONSE, not a request + String request = ResourceUtils.getResourceAsString("org/onap/policy/simulators/appc/appc.legacy.success.json"); + assertNotNull(request); + + server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, request); + + verify(sink, never()).send(any()); + } } diff --git a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/TopicServerTest.java b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/TopicServerTest.java index 6e176423e..11a5e3f81 100644 --- a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/TopicServerTest.java +++ b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/TopicServerTest.java @@ -85,6 +85,23 @@ public class TopicServerTest { verify(sink, never()).send(any()); } + /** + * Tests onTopicEvent() when there is no response. + */ + @Test + public void testOnTopicEventNoResponse() { + server = new MyServer() { + @Override + protected String process(MyRequest request) { + return null; + } + }; + + server.onTopicEvent(CommInfrastructure.NOOP, MY_TOPIC, "{\"text\": \"bye-bye\"}"); + + verify(sink, never()).send(any()); + } + private class MyRequest { private String text; diff --git a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/appc/appc.legacy.request.json b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/appc/appc.legacy.request.json index 8b3b73a88..61c2647cd 100644 --- a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/appc/appc.legacy.request.json +++ b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/appc/appc.legacy.request.json @@ -6,5 +6,6 @@ "SubRequestID": "111be3d2-6c12-4f4b-a3e7-c349acced200", "RequestTrack": [], "Flags": [] - } + }, + "Action": "ModifyConfig" } diff --git a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java index 8333800f3..a0b165564 100644 --- a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java +++ b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java @@ -22,7 +22,9 @@ package org.onap.policy.models.simulators; import java.io.FileNotFoundException; import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; import lombok.AccessLevel; @@ -85,9 +87,8 @@ public class Main extends ServiceManagerContainer { AtomicReference provRef = new AtomicReference<>(); addAction(provName, () -> provRef.set(buildDmaapProvider(dmaapProv)), () -> provRef.get().shutdown()); - // @formatter:off - // REST server simulators + // @formatter:off for (ClassRestServerParameters restsim : params.getRestServers()) { AtomicReference ref = new AtomicReference<>(); addAction(restsim.getName(), @@ -98,23 +99,30 @@ public class Main extends ServiceManagerContainer { // NOTE: topics must be started AFTER the (dmaap) rest servers // topic sinks - AtomicReference> sinkRef = new AtomicReference<>(); - addAction("topic sinks", () -> sinkRef.set(buildSinks(params.getTopicSinks())), - () -> shutdownSinks(sinkRef.get())); + Map sinks = new HashMap<>(); + for (TopicParameters topicParams : params.getTopicSinks()) { + String topic = topicParams.getTopic(); + addAction("Sink " + topic, + () -> sinks.put(topic, startSink(topicParams)), + () -> sinks.get(topic).shutdown()); + } // topic sources - AtomicReference> sourceRef = new AtomicReference<>(); - addAction("topic sources", () -> sourceRef.set(buildSources(params.getTopicSources())), - () -> shutdownSources(sourceRef.get())); + Map sources = new HashMap<>(); + for (TopicParameters topicParams : params.getTopicSources()) { + String topic = topicParams.getTopic(); + addAction("Source " + topic, + () -> sources.put(topic, startSource(topicParams)), + () -> sources.get(topic).shutdown()); + } // topic server simulators for (TopicServerParameters topicsim : params.getTopicServers()) { AtomicReference> ref = new AtomicReference<>(); addAction(topicsim.getName(), - () -> ref.set(buildTopicServer(topicsim, sinkRef.get(), sourceRef.get())), + () -> ref.set(buildTopicServer(topicsim, sinks, sources)), () -> ref.get().shutdown()); } - // @formatter:on } @@ -164,20 +172,16 @@ public class Main extends ServiceManagerContainer { return prov; } - protected List buildSinks(List params) { - return TopicEndpointManager.getManager().addTopicSinks(params); + private TopicSink startSink(TopicParameters params) { + TopicSink sink = TopicEndpointManager.getManager().addTopicSinks(List.of(params)).get(0); + sink.start(); + return sink; } - private void shutdownSinks(List sinks) { - sinks.forEach(TopicSink::shutdown); - } - - protected List buildSources(List params) { - return TopicEndpointManager.getManager().addTopicSources(params); - } - - private void shutdownSources(List sources) { - sources.forEach(TopicSource::shutdown); + private TopicSource startSource(TopicParameters params) { + TopicSource source = TopicEndpointManager.getManager().addTopicSources(List.of(params)).get(0); + source.start(); + return source; } private HttpServletServer buildRestServer(String dmaapName, ClassRestServerParameters params) { @@ -201,17 +205,20 @@ public class Main extends ServiceManagerContainer { } } - private TopicServer buildTopicServer(TopicServerParameters params, List sinks, - List sources) { + private TopicServer buildTopicServer(TopicServerParameters params, Map sinks, + Map sources) { try { // find the desired sink - TopicSink sink = sinks.stream().filter(sink2 -> sink2.getTopic().equals(params.getSink())).findAny() - .orElseThrow(() -> new IllegalArgumentException("invalid sink topic " + params.getSink())); + TopicSink sink = sinks.get(params.getSink()); + if (sink == null) { + throw new IllegalArgumentException("invalid sink topic " + params.getSink()); + } // find the desired source - TopicSource source = sources.stream().filter(source2 -> source2.getTopic().equals(params.getSource())) - .findAny().orElseThrow(() -> new IllegalArgumentException( - "invalid source topic " + params.getSource())); + TopicSource source = sources.get(params.getSource()); + if (source == null) { + throw new IllegalArgumentException("invalid source topic " + params.getSource()); + } // create the topic server return (TopicServer) Class.forName(params.getProviderClass()) diff --git a/models-sim/policy-models-simulators/src/test/resources/appc.lcm.request.json b/models-sim/policy-models-simulators/src/test/resources/appc.lcm.request.json new file mode 100644 index 000000000..cf2ebd521 --- /dev/null +++ b/models-sim/policy-models-simulators/src/test/resources/appc.lcm.request.json @@ -0,0 +1,17 @@ +{ + "body": { + "input": { + "common-header": { + "timestamp": "2017-08-25T21:06:23.037Z", + "api-ver": "5.00", + "originator-id": "664be3d2-6c12-4f4b-a3e7-c349acced200", + "request-id": "664be3d2-6c12-4f4b-a3e7-c349acced200", + "sub-request-id": "111be3d2-6c12-4f4b-a3e7-c349acced200", + "flags": {} + } + } + }, + "version": "2.0", + "rpc-name": "restart", + "type": "request" +} diff --git a/models-sim/policy-models-simulators/src/test/resources/simParameters.json b/models-sim/policy-models-simulators/src/test/resources/simParameters.json index c7abb2973..5f946f105 100644 --- a/models-sim/policy-models-simulators/src/test/resources/simParameters.json +++ b/models-sim/policy-models-simulators/src/test/resources/simParameters.json @@ -58,7 +58,7 @@ "useHttps": true }, { - "topic": "APPC-LCM-READ", + "topic": "APPC-LCM-WRITE", "servers": ["localhost"], "topicCommInfrastructure": "DMAAP", "useHttps": true @@ -72,7 +72,7 @@ "useHttps": true }, { - "topic": "APPC-LCM-WRITE", + "topic": "APPC-LCM-READ", "servers": ["localhost"], "topicCommInfrastructure": "DMAAP", "useHttps": true @@ -88,8 +88,8 @@ { "name": "APPC-LCM simulator", "providerClass": "org.onap.policy.simulators.AppcLcmTopicServer", - "sink": "APPC-LCM-READ", - "source": "APPC-LCM-WRITE" + "sink": "APPC-LCM-WRITE", + "source": "APPC-LCM-READ" } ] }