Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / BidirectionalTopicOperationTest.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.actorserviceprovider.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
36
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.concurrent.CompletableFuture;
40 import java.util.function.BiConsumer;
41 import lombok.Getter;
42 import lombok.Setter;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Captor;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
50 import org.onap.policy.common.utils.coder.Coder;
51 import org.onap.policy.common.utils.coder.CoderException;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.common.utils.coder.StandardCoderObject;
54 import org.onap.policy.common.utils.time.PseudoExecutor;
55 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
57 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
58 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
59 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
60 import org.onap.policy.controlloop.policy.PolicyResult;
61
62 public class BidirectionalTopicOperationTest {
63     private static final CommInfrastructure SINK_INFRA = CommInfrastructure.NOOP;
64     private static final IllegalStateException EXPECTED_EXCEPTION = new IllegalStateException("expected exception");
65     private static final String ACTOR = "my-actor";
66     private static final String OPERATION = "my-operation";
67     private static final String REQ_ID = "my-request-id";
68     private static final String MY_SINK = "my-sink";
69     private static final String MY_SOURCE = "my-source";
70     private static final String TEXT = "some text";
71     private static final int TIMEOUT_SEC = 10;
72     private static final long TIMEOUT_MS = 1000 * TIMEOUT_SEC;
73     private static final int MAX_REQUESTS = 100;
74
75     private static final StandardCoder coder = new StandardCoder();
76
77     @Mock
78     private BidirectionalTopicOperator operator;
79     @Mock
80     private BidirectionalTopicHandler handler;
81     @Mock
82     private Forwarder forwarder;
83
84     @Captor
85     private ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor;
86
87     private ControlLoopOperationParams params;
88     private BidirectionalTopicParams topicParams;
89     private OperationOutcome outcome;
90     private StandardCoderObject stdResponse;
91     private String responseText;
92     private PseudoExecutor executor;
93     private int ntimes;
94     private BidirectionalTopicOperation<MyRequest, MyResponse> oper;
95
96     /**
97      * Sets up.
98      */
99     @Before
100     public void setUp() throws CoderException {
101         MockitoAnnotations.initMocks(this);
102
103         topicParams = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK)
104                         .timeoutSec(TIMEOUT_SEC).build();
105
106         when(operator.getActorName()).thenReturn(ACTOR);
107         when(operator.getName()).thenReturn(OPERATION);
108         when(operator.getTopicHandler()).thenReturn(handler);
109         when(operator.getForwarder()).thenReturn(forwarder);
110         when(operator.getParams()).thenReturn(topicParams);
111         when(operator.isAlive()).thenReturn(true);
112
113         when(handler.send(any())).thenReturn(true);
114         when(handler.getSinkTopicCommInfrastructure()).thenReturn(SINK_INFRA);
115
116         executor = new PseudoExecutor();
117
118         params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).executor(executor).build();
119         outcome = params.makeOutcome();
120
121         responseText = coder.encode(new MyResponse());
122         stdResponse = coder.decode(responseText, StandardCoderObject.class);
123
124         ntimes = 1;
125
126         oper = new MyOperation();
127     }
128
129     @Test
130     public void testConstructor_testGetTopicHandler_testGetForwarder_testGetTopicParams() {
131         assertEquals(ACTOR, oper.getActorName());
132         assertEquals(OPERATION, oper.getName());
133         assertSame(handler, oper.getTopicHandler());
134         assertSame(forwarder, oper.getForwarder());
135         assertSame(topicParams, oper.getTopicParams());
136         assertEquals(TIMEOUT_MS, oper.getTimeoutMs());
137         assertSame(MyResponse.class, oper.getResponseClass());
138     }
139
140     @Test
141     public void testStartOperationAsync() throws Exception {
142
143         // tell it to expect three responses
144         ntimes = 3;
145
146         CompletableFuture<OperationOutcome> future = oper.startOperationAsync(1, outcome);
147         assertFalse(future.isDone());
148
149         verify(forwarder).register(eq(Arrays.asList(REQ_ID)), listenerCaptor.capture());
150
151         verify(forwarder, never()).unregister(any(), any());
152
153         verify(handler).send(any());
154
155         // provide first response
156         listenerCaptor.getValue().accept(responseText, stdResponse);
157         assertTrue(executor.runAll(MAX_REQUESTS));
158         assertFalse(future.isDone());
159
160         // provide second response
161         listenerCaptor.getValue().accept(responseText, stdResponse);
162         assertTrue(executor.runAll(MAX_REQUESTS));
163         assertFalse(future.isDone());
164
165         // provide final response
166         listenerCaptor.getValue().accept(responseText, stdResponse);
167         assertTrue(executor.runAll(MAX_REQUESTS));
168         assertTrue(future.isDone());
169
170         assertSame(outcome, future.get());
171         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
172
173         verify(forwarder).unregister(eq(Arrays.asList(REQ_ID)), eq(listenerCaptor.getValue()));
174     }
175
176     /**
177      * Tests startOperationAsync() when the publisher throws an exception.
178      */
179     @Test
180     public void testStartOperationAsyncException() throws Exception {
181         // indicate that nothing was published
182         when(handler.send(any())).thenReturn(false);
183
184         assertThatIllegalStateException().isThrownBy(() -> oper.startOperationAsync(1, outcome));
185
186         verify(forwarder).register(eq(Arrays.asList(REQ_ID)), listenerCaptor.capture());
187
188         // must still unregister
189         verify(forwarder).unregister(eq(Arrays.asList(REQ_ID)), eq(listenerCaptor.getValue()));
190     }
191
192     @Test
193     public void testGetTimeoutMsInteger() {
194         // use default
195         assertEquals(TIMEOUT_MS, oper.getTimeoutMs(null));
196         assertEquals(TIMEOUT_MS, oper.getTimeoutMs(0));
197
198         // use provided value
199         assertEquals(5000, oper.getTimeoutMs(5));
200     }
201
202     @Test
203     public void testPublishRequest() {
204         assertThatCode(() -> oper.publishRequest(new MyRequest())).doesNotThrowAnyException();
205     }
206
207     /**
208      * Tests publishRequest() when nothing is published.
209      */
210     @Test
211     public void testPublishRequestUnpublished() {
212         when(handler.send(any())).thenReturn(false);
213         assertThatIllegalStateException().isThrownBy(() -> oper.publishRequest(new MyRequest()));
214     }
215
216     /**
217      * Tests publishRequest() when the request type is a String.
218      */
219     @Test
220     public void testPublishRequestString() {
221         MyStringOperation oper2 = new MyStringOperation();
222         assertThatCode(() -> oper2.publishRequest(TEXT)).doesNotThrowAnyException();
223     }
224
225     /**
226      * Tests publishRequest() when the coder throws an exception.
227      */
228     @Test
229     public void testPublishRequestException() {
230         setOperCoderException();
231         assertThatIllegalArgumentException().isThrownBy(() -> oper.publishRequest(new MyRequest()));
232     }
233
234     /**
235      * Tests processResponse() when it's a success and the response type is a String.
236      */
237     @Test
238     public void testProcessResponseSuccessString() {
239         MyStringOperation oper2 = new MyStringOperation();
240
241         assertSame(outcome, oper2.processResponse(outcome, TEXT, null));
242         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
243     }
244
245     /**
246      * Tests processResponse() when it's a success and the response type is a
247      * StandardCoderObject.
248      */
249     @Test
250     public void testProcessResponseSuccessSco() {
251         MyScoOperation oper2 = new MyScoOperation();
252
253         assertSame(outcome, oper2.processResponse(outcome, responseText, stdResponse));
254         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
255     }
256
257     /**
258      * Tests processResponse() when it's a failure.
259      */
260     @Test
261     public void testProcessResponseFailure() throws CoderException {
262         // indicate error in the response
263         MyResponse resp = new MyResponse();
264         resp.setOutput("error");
265
266         responseText = coder.encode(resp);
267         stdResponse = coder.decode(responseText, StandardCoderObject.class);
268
269         assertSame(outcome, oper.processResponse(outcome, responseText, stdResponse));
270         assertEquals(PolicyResult.FAILURE, outcome.getResult());
271     }
272
273     /**
274      * Tests processResponse() when the decoder succeeds.
275      */
276     @Test
277     public void testProcessResponseDecodeOk() throws CoderException {
278         assertSame(outcome, oper.processResponse(outcome, responseText, stdResponse));
279         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
280     }
281
282     /**
283      * Tests processResponse() when the decoder throws an exception.
284      */
285     @Test
286     public void testProcessResponseDecodeExcept() throws CoderException {
287         // @formatter:off
288         assertThatIllegalArgumentException().isThrownBy(
289             () -> oper.processResponse(outcome, "{invalid json", stdResponse));
290         // @formatter:on
291     }
292
293     @Test
294     public void testPostProcessResponse() {
295         assertThatCode(() -> oper.postProcessResponse(outcome, null, null)).doesNotThrowAnyException();
296     }
297
298     @Test
299     public void testMakeCoder() {
300         assertNotNull(oper.makeCoder());
301     }
302
303     /**
304      * Creates a new {@link #oper} whose coder will throw an exception.
305      */
306     private void setOperCoderException() {
307         oper = new MyOperation() {
308             @Override
309             protected Coder makeCoder() {
310                 return new StandardCoder() {
311                     @Override
312                     public String encode(Object object, boolean pretty) throws CoderException {
313                         throw new CoderException(EXPECTED_EXCEPTION);
314                     }
315                 };
316             }
317         };
318     }
319
320     @Getter
321     @Setter
322     public static class MyRequest {
323         private String theRequestId = REQ_ID;
324         private String input;
325     }
326
327     @Getter
328     @Setter
329     public static class MyResponse {
330         private String requestId = REQ_ID;
331         private String output;
332     }
333
334
335     private class MyStringOperation extends BidirectionalTopicOperation<String, String> {
336         public MyStringOperation() {
337             super(BidirectionalTopicOperationTest.this.params, operator, String.class);
338         }
339
340         @Override
341         protected String makeRequest(int attempt) {
342             return TEXT;
343         }
344
345         @Override
346         protected List<String> getExpectedKeyValues(int attempt, String request) {
347             return Arrays.asList(REQ_ID);
348         }
349
350         @Override
351         protected Status detmStatus(String rawResponse, String response) {
352             return (response != null ? Status.SUCCESS : Status.FAILURE);
353         }
354     }
355
356
357     private class MyScoOperation extends BidirectionalTopicOperation<MyRequest, StandardCoderObject> {
358         public MyScoOperation() {
359             super(BidirectionalTopicOperationTest.this.params, operator, StandardCoderObject.class);
360         }
361
362         @Override
363         protected MyRequest makeRequest(int attempt) {
364             return new MyRequest();
365         }
366
367         @Override
368         protected List<String> getExpectedKeyValues(int attempt, MyRequest request) {
369             return Arrays.asList(REQ_ID);
370         }
371
372         @Override
373         protected Status detmStatus(String rawResponse, StandardCoderObject response) {
374             return (response.getString("output") == null ? Status.SUCCESS : Status.FAILURE);
375         }
376     }
377
378
379     private class MyOperation extends BidirectionalTopicOperation<MyRequest, MyResponse> {
380         public MyOperation() {
381             super(BidirectionalTopicOperationTest.this.params, operator, MyResponse.class);
382         }
383
384         @Override
385         protected MyRequest makeRequest(int attempt) {
386             return new MyRequest();
387         }
388
389         @Override
390         protected List<String> getExpectedKeyValues(int attempt, MyRequest request) {
391             return Arrays.asList(REQ_ID);
392         }
393
394         @Override
395         protected Status detmStatus(String rawResponse, MyResponse response) {
396             if (--ntimes <= 0) {
397                 return (response.getOutput() == null ? Status.SUCCESS : Status.FAILURE);
398             }
399
400             return Status.STILL_WAITING;
401         }
402     }
403 }