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;
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;
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;
55 private BidirectionalTopicManager mgr;
57 private BidirectionalTopicHandler handler;
59 private Forwarder forwarder;
61 private BidirectionalTopicOperation<String, Integer> operation;
63 private List<SelectorKey> keys;
64 private BidirectionalTopicParams params;
65 private MyOperator oper;
72 MockitoAnnotations.initMocks(this);
74 keys = List.of(new SelectorKey(""));
76 when(mgr.getTopicHandler(MY_SINK, MY_SOURCE)).thenReturn(handler);
77 when(handler.addForwarder(keys)).thenReturn(forwarder);
79 oper = new MyOperator(keys);
81 params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
83 oper.configure(Util.translateToMap(OPERATION, params));
88 public void testConstructor_testGetParams_testGetTopicHandler_testGetForwarder() {
89 assertEquals(ACTOR, oper.getActorName());
90 assertEquals(OPERATION, oper.getName());
91 assertNotNull(oper.getCurrentConfig());
95 public void testDoConfigure() {
99 params.setSourceTopic(null);
100 assertThatThrownBy(() -> oper.configure(Util.translateToMap(OPERATION, params)))
101 .isInstanceOf(ParameterValidationRuntimeException.class);
105 public void testBuildOperator() {
106 AtomicReference<ControlLoopOperationParams> paramsRef = new AtomicReference<>();
107 AtomicReference<BidirectionalTopicConfig> configRef = new AtomicReference<>();
110 OperationMaker<BidirectionalTopicConfig, BidirectionalTopicOperation<?,?>> maker =
111 (params, config) -> {
112 paramsRef.set(params);
113 configRef.set(config);
118 BidirectionalTopicOperator oper2 =
119 new BidirectionalTopicOperator(ACTOR, OPERATION, mgr, maker, new SelectorKey(""));
121 assertEquals(ACTOR, oper2.getActorName());
122 assertEquals(OPERATION, oper2.getName());
124 ControlLoopOperationParams params2 = ControlLoopOperationParams.builder().build();
126 // configure and start it
127 params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
129 oper2.configure(Util.translateToMap(OPERATION, params));
132 assertThatIllegalStateException().isThrownBy(() -> oper2.buildOperation(params2));
136 assertSame(operation, oper2.buildOperation(params2));
137 assertSame(params2, paramsRef.get());
138 assertSame(oper2.getCurrentConfig(), configRef.get());
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);
147 private class MyOperator extends BidirectionalTopicOperator {
148 public MyOperator(List<SelectorKey> selectorKeys) {
149 super(ACTOR, OPERATION, mgr, selectorKeys);
153 public BidirectionalTopicOperation<?,?> buildOperation(ControlLoopOperationParams params) {