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.actor.test;
23 import static org.mockito.Mockito.when;
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;
39 * Superclass for various BidirectionalTopicOperation tests.
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;
47 protected ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor;
50 protected BidirectionalTopicHandler topicHandler;
52 protected Forwarder forwarder;
54 protected BidirectionalTopicOperator operator;
56 protected BidirectionalTopicParams topicParams;
59 * Constructs the object using a default actor and operation name.
61 public BasicBidirectionalTopicOperation() {
66 * Constructs the object.
68 * @param actor actor name
69 * @param operation operation name
71 public BasicBidirectionalTopicOperation(String actor, String operation) {
72 super(actor, operation);
76 * Initializes mocks and sets up.
78 public void setUp() throws Exception {
79 MockitoAnnotations.initMocks(this);
81 executor = new PseudoExecutor();
85 outcome = params.makeOutcome();
86 topicParams = BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).timeoutSec(TIMEOUT)
93 * Initializes an operator so that it is "alive" and has the given names.
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);
106 * Provides a response to the topic {@link #listenerCaptor}.
108 * @param listener listener to which to provide the response
109 * @param response response to be provided
111 protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, String response) {
113 StandardCoderObject sco = coder.decode(response, StandardCoderObject.class);
114 listener.accept(response, sco);
116 } catch (CoderException e) {
117 throw new IllegalArgumentException("response is not a Map", e);