215286ef9083f68b476746d502d67a2c25aee84b
[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-2021 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 lombok.AccessLevel;
29 import lombok.NoArgsConstructor;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Captor;
32 import org.mockito.Mock;
33 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
34 import org.onap.policy.common.endpoints.event.comm.TopicSink;
35 import org.onap.policy.common.endpoints.event.comm.TopicSource;
36 import org.onap.policy.common.endpoints.event.comm.client.BidirectionalTopicClientException;
37 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.endpoints.parameters.TopicParameters;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoderObject;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
43 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
44 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager;
45 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
46 import org.onap.policy.simulators.TopicServer;
47 import org.onap.policy.simulators.Util;
48
49 /**
50  * Superclass for various BidirectionalTopicOperation tests.
51  *
52  * @param <Q> request type
53  */
54 @NoArgsConstructor(access = AccessLevel.PROTECTED)
55 public abstract class BasicBidirectionalTopicOperation<Q> extends BasicOperation {
56     protected static final String MY_SINK = "my-sink";
57     protected static final String MY_SOURCE = "my-source";
58     protected static final int TIMEOUT_SEC = 10;
59     protected static final long TIMEOUT_MS = 1000L * TIMEOUT_SEC;
60
61     // sink and source used by the TopicServer
62     private static TopicSink serverSink;
63     private static TopicSource serverSource;
64     private static BidirectionalTopicHandler realTopicHandler;
65
66     protected static BidirectionalTopicManager topicMgr = (sink, source) -> {
67         // note: the sink and source names are swapped for the simulator
68         assertEquals(serverSource.getTopic(), sink);
69         assertEquals(serverSink.getTopic(), source);
70         return realTopicHandler;
71     };
72
73     @Captor
74     protected ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor;
75
76     @Mock
77     protected BidirectionalTopicHandler topicHandler;
78     @Mock
79     protected Forwarder forwarder;
80     @Mock
81     protected BidirectionalTopicConfig config;
82
83     private TopicServer<Q> topicServer;
84
85
86     /**
87      * Constructs the object.
88      *
89      * @param actor actor name
90      * @param operation operation name
91      */
92     protected BasicBidirectionalTopicOperation(String actor, String operation) {
93         super(actor, operation);
94     }
95
96     /**
97      * Starts the topic.
98      *
99      * @throws InterruptedException if interrupted
100      * @throws BidirectionalTopicClientException if the client cannot be built
101      */
102     protected static void initBeforeClass(String sinkTopic, String sourceTopic)
103                     throws InterruptedException, BidirectionalTopicClientException {
104
105         Util.buildDmaapSim();
106
107         // note: the sink and source names are swapped for the simulator
108         var ptopic = new TopicParameters();
109         ptopic.setTopic(sourceTopic);
110         ptopic.setManaged(true);
111         ptopic.setServers(List.of("localhost"));
112         ptopic.setTopicCommInfrastructure("dmaap");
113         ptopic.setFetchTimeout(500);
114         serverSink = TopicEndpointManager.getManager().addTopicSinks(List.of(ptopic)).get(0);
115
116         ptopic.setTopic(sinkTopic);
117         serverSource = TopicEndpointManager.getManager().addTopicSources(List.of(ptopic)).get(0);
118
119         serverSink.start();
120         serverSource.start();
121
122         if (!sinkTopic.equals(sourceTopic)) {
123             // sink and source are different - create other ends for the actor
124             initActorTopics(sinkTopic, sourceTopic, ptopic);
125         }
126
127         realTopicHandler = new BidirectionalTopicHandler(sinkTopic, sourceTopic);
128         realTopicHandler.start();
129     }
130
131     private static void initActorTopics(String sinkTopic, String sourceTopic, TopicParameters ptopic) {
132         // create sink and source for the actor, too
133         ptopic.setTopic(sinkTopic);
134         TopicEndpointManager.getManager().addTopicSinks(List.of(ptopic)).get(0).start();
135
136         ptopic.setTopic(sourceTopic);
137         TopicEndpointManager.getManager().addTopicSources(List.of(ptopic)).get(0).start();
138     }
139
140     protected static void destroyAfterClass() {
141         TopicEndpointManager.getManager().shutdown();
142         HttpServletServerFactoryInstance.getServerFactory().destroy();
143         HttpClientFactoryInstance.getClientFactory().destroy();
144     }
145
146     /**
147      * Initializes mocks and sets up.
148      */
149     @Override
150     public void setUpBasic() {
151         super.setUpBasic();
152         topicServer = makeServer(serverSink, serverSource);
153         initConfig();
154     }
155
156     public void tearDownBasic() {
157         topicServer.shutdown();
158     }
159
160     /**
161      * Makes a simulator for the given sink and source.
162      *
163      * @param sink topic to which the simulator should publish responses
164      * @param source topic from which the simulator should receive messages
165      * @return a new topic server/simulator
166      */
167     protected abstract TopicServer<Q> makeServer(TopicSink sink, TopicSource source);
168
169     /**
170      * Initializes a configuration.
171      */
172     protected void initConfig() {
173         when(config.getTopicHandler()).thenReturn(topicHandler);
174         when(config.getForwarder()).thenReturn(forwarder);
175         when(config.getTimeoutMs()).thenReturn(TIMEOUT_MS);
176     }
177
178     /**
179      * Provides a response to the topic {@link #listenerCaptor}.
180      *
181      * @param listener listener to which to provide the response
182      * @param response response to be provided
183      */
184     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, String response) {
185         try {
186             StandardCoderObject sco = coder.decode(response, StandardCoderObject.class);
187             listener.accept(response, sco);
188
189         } catch (CoderException e) {
190             throw new IllegalArgumentException("response is not a Map", e);
191         }
192     }
193 }