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