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;
26 import java.util.TreeMap;
27 import java.util.UUID;
28 import java.util.function.BiConsumer;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Captor;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.coder.StandardCoderObject;
37 import org.onap.policy.common.utils.time.PseudoExecutor;
38 import org.onap.policy.controlloop.VirtualControlLoopEvent;
39 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
42 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
45 import org.onap.policy.controlloop.actorserviceprovider.topic.BidirectionalTopicHandler;
46 import org.onap.policy.controlloop.actorserviceprovider.topic.Forwarder;
49 * Superclass for various BidirectionalTopicOperation tests.
51 public class BasicBidirectionalTopicOperation {
52 protected static final UUID REQ_ID = UUID.randomUUID();
53 protected static final String DEFAULT_ACTOR = "default-actor";
54 protected static final String DEFAULT_OPERATION = "default-operation";
55 protected static final String MY_SINK = "my-sink";
56 protected static final String MY_SOURCE = "my-source";
57 protected static final String TARGET_ENTITY = "my-target";
58 protected static final Coder coder = new StandardCoder();
59 protected static final int TIMEOUT = 10;
61 protected final String actorName;
62 protected final String operationName;
65 protected ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor;
68 protected ActorService service;
70 protected BidirectionalTopicHandler topicHandler;
72 protected Forwarder forwarder;
74 protected BidirectionalTopicOperator operator;
76 protected BidirectionalTopicParams topicParams;
77 protected ControlLoopOperationParams params;
78 protected Map<String, String> enrichment;
79 protected VirtualControlLoopEvent event;
80 protected ControlLoopEventContext context;
81 protected OperationOutcome outcome;
82 protected PseudoExecutor executor;
85 * Constructs the object using a default actor and operation name.
87 public BasicBidirectionalTopicOperation() {
88 this.actorName = DEFAULT_ACTOR;
89 this.operationName = DEFAULT_OPERATION;
93 * Constructs the object.
95 * @param actor actor name
96 * @param operation operation name
98 public BasicBidirectionalTopicOperation(String actor, String operation) {
99 this.actorName = actor;
100 this.operationName = operation;
104 * Initializes mocks and sets up.
106 public void setUp() throws Exception {
107 MockitoAnnotations.initMocks(this);
109 executor = new PseudoExecutor();
113 outcome = params.makeOutcome();
114 topicParams = BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).timeoutSec(TIMEOUT)
121 * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
124 * Note: {@link #params} is configured to use {@link #executor}.
126 protected void makeContext() {
127 enrichment = new TreeMap<>(makeEnrichment());
129 event = new VirtualControlLoopEvent();
130 event.setRequestId(REQ_ID);
131 event.setAai(enrichment);
133 context = new ControlLoopEventContext(event);
135 params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
136 .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
140 protected Map<String, String> makePayload() {
145 * Initializes an operator so that it is "alive" and has the given names.
147 protected void initOperator() {
148 when(operator.isAlive()).thenReturn(true);
149 when(operator.getFullName()).thenReturn(actorName + "." + operationName);
150 when(operator.getActorName()).thenReturn(actorName);
151 when(operator.getName()).thenReturn(operationName);
152 when(operator.getTopicHandler()).thenReturn(topicHandler);
153 when(operator.getForwarder()).thenReturn(forwarder);
154 when(operator.getParams()).thenReturn(topicParams);
158 * Makes enrichment data.
160 * @return enrichment data
162 protected Map<String, String> makeEnrichment() {
163 return new TreeMap<>();
167 * Provides a response to the topic {@link #listenerCaptor}.
169 * @param listener listener to which to provide the response
170 * @param response response to be provided
172 protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, String response) {
174 StandardCoderObject sco = coder.decode(response, StandardCoderObject.class);
175 listener.accept(response, sco);
177 } catch (CoderException e) {
178 throw new IllegalArgumentException("response is not a Map", e);