Merge "Fix cascaded get filtering and speed"
[policy/models.git] / models-interactions / model-actors / actor.test / src / main / java / org / onap / policy / controlloop / actor / test / BasicBidirectionalTopicOperation.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.actor.test;
22
23 import static org.mockito.Mockito.when;
24
25 import java.util.function.BiConsumer;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoderObject;
32 import org.onap.policy.common.utils.time.PseudoExecutor;
33 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
35 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
36 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
37
38 /**
39  * Superclass for various BidirectionalTopicOperation tests.
40  */
41 public class BasicBidirectionalTopicOperation extends BasicOperation {
42     protected static final String MY_SINK = "my-sink";
43     protected static final String MY_SOURCE = "my-source";
44     protected static final int TIMEOUT = 10;
45
46     @Captor
47     protected ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor;
48
49     @Mock
50     protected BidirectionalTopicHandler topicHandler;
51     @Mock
52     protected Forwarder forwarder;
53     @Mock
54     protected BidirectionalTopicOperator operator;
55
56     protected BidirectionalTopicParams topicParams;
57
58     /**
59      * Constructs the object using a default actor and operation name.
60      */
61     public BasicBidirectionalTopicOperation() {
62         super();
63     }
64
65     /**
66      * Constructs the object.
67      *
68      * @param actor actor name
69      * @param operation operation name
70      */
71     public BasicBidirectionalTopicOperation(String actor, String operation) {
72         super(actor, operation);
73     }
74
75     /**
76      * Initializes mocks and sets up.
77      */
78     public void setUp() throws Exception {
79         MockitoAnnotations.initMocks(this);
80
81         executor = new PseudoExecutor();
82
83         makeContext();
84
85         outcome = params.makeOutcome();
86         topicParams = BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).timeoutSec(TIMEOUT)
87                         .build();
88
89         initOperator();
90     }
91
92     /**
93      * Initializes an operator so that it is "alive" and has the given names.
94      */
95     protected void initOperator() {
96         when(operator.isAlive()).thenReturn(true);
97         when(operator.getFullName()).thenReturn(actorName + "." + operationName);
98         when(operator.getActorName()).thenReturn(actorName);
99         when(operator.getName()).thenReturn(operationName);
100         when(operator.getTopicHandler()).thenReturn(topicHandler);
101         when(operator.getForwarder()).thenReturn(forwarder);
102         when(operator.getParams()).thenReturn(topicParams);
103     }
104
105     /**
106      * Provides a response to the topic {@link #listenerCaptor}.
107      *
108      * @param listener listener to which to provide the response
109      * @param response response to be provided
110      */
111     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, String response) {
112         try {
113             StandardCoderObject sco = coder.decode(response, StandardCoderObject.class);
114             listener.accept(response, sco);
115
116         } catch (CoderException e) {
117             throw new IllegalArgumentException("response is not a Map", e);
118         }
119     }
120 }