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