2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
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;
30 import java.util.Arrays;
31 import java.util.List;
33 import java.util.concurrent.atomic.AtomicReference;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.controlloop.actorserviceprovider.Util;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
43 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
44 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager;
45 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
46 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
48 public class BidirectionalTopicOperatorTest {
49 private static final String ACTOR = "my-actor";
50 private static final String OPERATION = "my-operation";
51 private static final String MY_SOURCE = "my-source";
52 private static final String MY_SINK = "my-target";
53 private static final int TIMEOUT_SEC = 10;
56 private BidirectionalTopicManager mgr;
58 private BidirectionalTopicHandler handler;
60 private Forwarder forwarder;
62 private BidirectionalTopicOperation<String, Integer> operation;
64 private List<SelectorKey> keys;
65 private BidirectionalTopicParams params;
66 private MyOperator oper;
73 MockitoAnnotations.initMocks(this);
75 keys = List.of(new SelectorKey(""));
77 when(mgr.getTopicHandler(MY_SINK, MY_SOURCE)).thenReturn(handler);
78 when(handler.addForwarder(keys)).thenReturn(forwarder);
80 oper = new MyOperator(keys);
82 params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
84 oper.configure(Util.translateToMap(OPERATION, params));
89 public void testConstructor_testGetParams_testGetTopicHandler_testGetForwarder() {
90 assertEquals(ACTOR, oper.getActorName());
91 assertEquals(OPERATION, oper.getName());
92 assertNotNull(oper.getCurrentConfig());
96 public void testDoConfigure() {
100 params.setSourceTopic(null);
101 Map<String, Object> map = Util.translateToMap(OPERATION, params);
102 assertThatThrownBy(() -> oper.configure(map))
103 .isInstanceOf(ParameterValidationRuntimeException.class);
107 public void testBuildOperator() {
108 AtomicReference<ControlLoopOperationParams> paramsRef = new AtomicReference<>();
109 AtomicReference<BidirectionalTopicConfig> configRef = new AtomicReference<>();
112 OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation<?, ?>> maker =
113 (params, config) -> {
114 paramsRef.set(params);
115 configRef.set(config);
120 BidirectionalTopicOperator oper2 =
121 new BidirectionalTopicOperator(ACTOR, OPERATION, mgr, maker, new SelectorKey(""));
123 assertEquals(ACTOR, oper2.getActorName());
124 assertEquals(OPERATION, oper2.getName());
126 ControlLoopOperationParams params2 = ControlLoopOperationParams.builder().build();
128 // configure and start it
129 params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
131 oper2.configure(Util.translateToMap(OPERATION, params));
134 assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params2));
138 assertSame(operation, oper2.buildOperation(params2));
139 assertSame(params2, paramsRef.get());
140 assertSame(oper2.getCurrentConfig(), configRef.get());
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);
149 private class MyOperator extends BidirectionalTopicOperator {
150 public MyOperator(List<SelectorKey> selectorKeys) {
151 super(ACTOR, OPERATION, mgr, selectorKeys);
155 public BidirectionalTopicOperation<?, ?> buildOperation(ControlLoopOperationParams params) {