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