Fix sonars from depeendency upgrade
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / BidirectionalTopicOperatorTest.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.actorserviceprovider.impl;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertSame;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.concurrent.atomic.AtomicReference;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.policy.controlloop.actorserviceprovider.Util;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
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.controlloop.actorserviceprovider.topic.SelectorKey;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class BidirectionalTopicOperatorTest {
51     private static final String ACTOR = "my-actor";
52     private static final String OPERATION = "my-operation";
53     private static final String MY_SOURCE = "my-source";
54     private static final String MY_SINK = "my-target";
55     private static final int TIMEOUT_SEC = 10;
56
57     @Mock
58     private BidirectionalTopicManager mgr;
59     @Mock
60     private BidirectionalTopicHandler handler;
61     @Mock
62     private Forwarder forwarder;
63     @Mock
64     private BidirectionalTopicOperation<String, Integer> operation;
65
66     private List<SelectorKey> keys;
67     private BidirectionalTopicParams params;
68     private MyOperator oper;
69
70     /**
71      * Sets up.
72      */
73     @Before
74     public void setUp() {
75         keys = List.of(new SelectorKey(""));
76
77         when(mgr.getTopicHandler(MY_SINK, MY_SOURCE)).thenReturn(handler);
78         when(handler.addForwarder(keys)).thenReturn(forwarder);
79
80         oper = new MyOperator(keys);
81
82         params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
83                         .build();
84         oper.configure(Util.translateToMap(OPERATION, params));
85         oper.start();
86     }
87
88     @Test
89     public void testConstructor_testGetParams_testGetTopicHandler_testGetForwarder() {
90         assertEquals(ACTOR, oper.getActorName());
91         assertEquals(OPERATION, oper.getName());
92         assertNotNull(oper.getCurrentConfig());
93     }
94
95     @Test
96     public void testDoConfigure() {
97         oper.stop();
98
99         // invalid parameters
100         params.setSourceTopic(null);
101         Map<String, Object> map = Util.translateToMap(OPERATION, params);
102         assertThatThrownBy(() -> oper.configure(map))
103                         .isInstanceOf(ParameterValidationRuntimeException.class);
104     }
105
106     @Test
107     public void testBuildOperator() {
108         AtomicReference<ControlLoopOperationParams> paramsRef = new AtomicReference<>();
109         AtomicReference<BidirectionalTopicConfig> configRef = new AtomicReference<>();
110
111         // @formatter:off
112         OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation<?, ?>> maker =
113             (params, config) -> {
114                 paramsRef.set(params);
115                 configRef.set(config);
116                 return operation;
117             };
118         // @formatter:on
119
120         BidirectionalTopicOperator oper2 =
121                         new BidirectionalTopicOperator(ACTOR, OPERATION, mgr, maker, new SelectorKey(""));
122
123         assertEquals(ACTOR, oper2.getActorName());
124         assertEquals(OPERATION, oper2.getName());
125
126         ControlLoopOperationParams params2 = ControlLoopOperationParams.builder().build();
127
128         // configure and start it
129         params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
130                         .build();
131         oper2.configure(Util.translateToMap(OPERATION, params));
132
133         // not running yet
134         assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params2));
135
136         oper2.start();
137
138         assertSame(operation, oper2.buildOperation(params2));
139         assertSame(params2, paramsRef.get());
140         assertSame(oper2.getCurrentConfig(), configRef.get());
141
142         // with no operation-maker
143         BidirectionalTopicOperator oper3 =
144                         new BidirectionalTopicOperator(ACTOR, OPERATION, mgr, Arrays.asList(new SelectorKey("")));
145         assertThatThrownBy(() -> oper3.buildOperation(params2)).isInstanceOf(UnsupportedOperationException.class);
146     }
147
148
149     private class MyOperator extends BidirectionalTopicOperator {
150         public MyOperator(List<SelectorKey> selectorKeys) {
151             super(ACTOR, OPERATION, mgr, selectorKeys);
152         }
153
154         @Override
155         public BidirectionalTopicOperation<?, ?> buildOperation(ControlLoopOperationParams params) {
156             return null;
157         }
158     }
159 }