Merge "add unit test for pdpstatistics provider"
[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.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertSame;
26 import static org.mockito.Mockito.when;
27
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicReference;
30 import java.util.function.BiFunction;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.controlloop.actorserviceprovider.Operation;
36 import org.onap.policy.controlloop.actorserviceprovider.Util;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
40 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
41 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicManager;
42 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
43 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
44
45 public class BidirectionalTopicOperatorTest {
46     private static final String ACTOR = "my-actor";
47     private static final String OPERATION = "my-operation";
48     private static final String MY_SOURCE = "my-source";
49     private static final String MY_SINK = "my-target";
50     private static final int TIMEOUT_SEC = 10;
51
52     @Mock
53     private BidirectionalTopicManager mgr;
54     @Mock
55     private BidirectionalTopicHandler handler;
56     @Mock
57     private Forwarder forwarder;
58     @Mock
59     private BidirectionalTopicOperation<String, Integer> operation;
60
61     private List<SelectorKey> keys;
62     private BidirectionalTopicParams params;
63     private MyOperator oper;
64
65     /**
66      * Sets up.
67      */
68     @Before
69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71
72         keys = List.of(new SelectorKey(""));
73
74         when(mgr.getTopicHandler(MY_SINK, MY_SOURCE)).thenReturn(handler);
75         when(handler.addForwarder(keys)).thenReturn(forwarder);
76
77         oper = new MyOperator(keys);
78
79         params = BidirectionalTopicParams.builder().sourceTopic(MY_SOURCE).sinkTopic(MY_SINK).timeoutSec(TIMEOUT_SEC)
80                         .build();
81         oper.configure(Util.translateToMap(OPERATION, params));
82         oper.start();
83     }
84
85     @Test
86     public void testConstructor_testGetParams_testGetTopicHandler_testGetForwarder() {
87         assertEquals(ACTOR, oper.getActorName());
88         assertEquals(OPERATION, oper.getName());
89         assertEquals(params, oper.getParams());
90         assertSame(handler, oper.getTopicHandler());
91         assertSame(forwarder, oper.getForwarder());
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 testMakeOperator() {
106         AtomicReference<ControlLoopOperationParams> paramsRef = new AtomicReference<>();
107         AtomicReference<BidirectionalTopicOperator> operRef = new AtomicReference<>();
108
109         // @formatter:off
110         BiFunction<ControlLoopOperationParams, BidirectionalTopicOperator,
111                     BidirectionalTopicOperation<String, Integer>> maker =
112                         (params, operator) -> {
113                             paramsRef.set(params);
114                             operRef.set(operator);
115                             return operation;
116                         };
117         // @formatter:on
118
119         BidirectionalTopicOperator oper2 =
120                         BidirectionalTopicOperator.makeOperator(ACTOR, OPERATION, mgr, maker, new SelectorKey(""));
121
122         assertEquals(ACTOR, oper2.getActorName());
123         assertEquals(OPERATION, oper2.getName());
124
125         ControlLoopOperationParams params2 = ControlLoopOperationParams.builder().build();
126
127         assertSame(operation, oper2.buildOperation(params2));
128         assertSame(params2, paramsRef.get());
129         assertSame(oper2, operRef.get());
130     }
131
132
133     private class MyOperator extends BidirectionalTopicOperator {
134         public MyOperator(List<SelectorKey> selectorKeys) {
135             super(ACTOR, OPERATION, mgr, selectorKeys);
136         }
137
138         @Override
139         public Operation buildOperation(ControlLoopOperationParams params) {
140             return null;
141         }
142     }
143 }