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