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