c8b69677f2dc0d57322a09738801eacb69f74b53
[policy/models.git] / models-interactions / model-actors / actor.test / src / main / java / org / onap / policy / controlloop / actor / test / BasicBidirectionalTopicOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
25
26 import java.util.List;
27 import java.util.function.BiConsumer;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Captor;
30 import org.mockito.Mock;
31 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
32 import org.onap.policy.common.endpoints.event.comm.TopicSink;
33 import org.onap.policy.common.endpoints.event.comm.TopicSource;
34 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
35 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
36 import org.onap.policy.common.endpoints.parameters.TopicParameters;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoderObject;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
40 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
41 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager;
42 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
43 import org.onap.policy.simulators.TopicServer;
44 import org.onap.policy.simulators.Util;
45
46 /**
47  * Superclass for various BidirectionalTopicOperation tests.
48  */
49 public abstract class BasicBidirectionalTopicOperation extends BasicOperation {
50     protected static final String MY_SINK = "my-sink";
51     protected static final String MY_SOURCE = "my-source";
52     protected static final int TIMEOUT_SEC = 10;
53     protected static final long TIMEOUT_MS = 1000L * TIMEOUT_SEC;
54
55     // sink and source used by the TopicServer
56     private static TopicSink serverSink;
57     private static TopicSource serverSource;
58     private static BidirectionalTopicHandler realTopicHandler;
59
60     protected static BidirectionalTopicManager topicMgr = (sink, source) -> {
61         // note: the sink and source names are swapped for the simulator
62         assertEquals(serverSource.getTopic(), sink);
63         assertEquals(serverSink.getTopic(), source);
64         return realTopicHandler;
65     };
66
67     @Captor
68     protected ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor;
69
70     @Mock
71     protected BidirectionalTopicHandler topicHandler;
72     @Mock
73     protected Forwarder forwarder;
74     @Mock
75     protected BidirectionalTopicConfig config;
76
77     @SuppressWarnings("rawtypes")
78     private TopicServer topicServer;
79
80     /**
81      * Constructs the object using a default actor and operation name.
82      */
83     public BasicBidirectionalTopicOperation() {
84         super();
85     }
86
87     /**
88      * Constructs the object.
89      *
90      * @param actor actor name
91      * @param operation operation name
92      */
93     public BasicBidirectionalTopicOperation(String actor, String operation) {
94         super(actor, operation);
95     }
96
97     /**
98      * Starts the topic.
99      */
100     protected static void initBeforeClass(String sinkTopic, String sourceTopic) throws Exception {
101
102         Util.buildDmaapSim();
103
104         // note: the sink and source names are swapped for the simulator
105         TopicParameters ptopic = new TopicParameters();
106         ptopic.setTopic(sourceTopic);
107         ptopic.setManaged(true);
108         ptopic.setServers(List.of("localhost"));
109         ptopic.setTopicCommInfrastructure("dmaap");
110         ptopic.setFetchTimeout(500);
111         serverSink = TopicEndpointManager.getManager().addTopicSinks(List.of(ptopic)).get(0);
112
113         ptopic.setTopic(sinkTopic);
114         serverSource = TopicEndpointManager.getManager().addTopicSources(List.of(ptopic)).get(0);
115
116         serverSink.start();
117         serverSource.start();
118
119         if (!sinkTopic.equals(sourceTopic)) {
120             // sink and source are different - create other ends for the actor
121             initActorTopics(sinkTopic, sourceTopic, ptopic);
122         }
123
124         realTopicHandler = new BidirectionalTopicHandler(sinkTopic, sourceTopic);
125         realTopicHandler.start();
126     }
127
128     private static void initActorTopics(String sinkTopic, String sourceTopic, TopicParameters ptopic) {
129         // create sink and source for the actor, too
130         ptopic.setTopic(sinkTopic);
131         TopicEndpointManager.getManager().addTopicSinks(List.of(ptopic)).get(0).start();
132
133         ptopic.setTopic(sourceTopic);
134         TopicEndpointManager.getManager().addTopicSources(List.of(ptopic)).get(0).start();
135     }
136
137     protected static void destroyAfterClass() {
138         TopicEndpointManager.getManager().shutdown();
139         HttpServletServerFactoryInstance.getServerFactory().destroy();
140         HttpClientFactoryInstance.getClientFactory().destroy();
141     }
142
143     /**
144      * Initializes mocks and sets up.
145      */
146     @Override
147     public void setUpBasic() {
148         super.setUpBasic();
149         topicServer = makeServer(serverSink, serverSource);
150         initConfig();
151     }
152
153     public void tearDownBasic() {
154         topicServer.shutdown();
155     }
156
157     /**
158      * Makes a simulator for the given sink and source.
159      *
160      * @param sink topic to which the simulator should publish responses
161      * @param source topic from which the simulator should receive messages
162      * @return a new topic server/simulator
163      */
164     @SuppressWarnings("rawtypes")
165     protected abstract TopicServer makeServer(TopicSink sink, TopicSource source);
166
167     /**
168      * Initializes a configuration.
169      */
170     protected void initConfig() {
171         when(config.getTopicHandler()).thenReturn(topicHandler);
172         when(config.getForwarder()).thenReturn(forwarder);
173         when(config.getTimeoutMs()).thenReturn(TIMEOUT_MS);
174     }
175
176     /**
177      * Provides a response to the topic {@link #listenerCaptor}.
178      *
179      * @param listener listener to which to provide the response
180      * @param response response to be provided
181      */
182     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, String response) {
183         try {
184             StandardCoderObject sco = coder.decode(response, StandardCoderObject.class);
185             listener.accept(response, sco);
186
187         } catch (CoderException e) {
188             throw new IllegalArgumentException("response is not a Map", e);
189         }
190     }
191 }