More changes to actor code
[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.Map;
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;
47
48 /**
49  * Superclass for various BidirectionalTopicOperation tests.
50  */
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;
60
61     protected final String actorName;
62     protected final String operationName;
63
64     @Captor
65     protected ArgumentCaptor<BiConsumer<String, StandardCoderObject>> listenerCaptor;
66
67     @Mock
68     protected ActorService service;
69     @Mock
70     protected BidirectionalTopicHandler topicHandler;
71     @Mock
72     protected Forwarder forwarder;
73     @Mock
74     protected BidirectionalTopicOperator operator;
75
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;
83
84     /**
85      * Constructs the object using a default actor and operation name.
86      */
87     public BasicBidirectionalTopicOperation() {
88         this.actorName = DEFAULT_ACTOR;
89         this.operationName = DEFAULT_OPERATION;
90     }
91
92     /**
93      * Constructs the object.
94      *
95      * @param actor actor name
96      * @param operation operation name
97      */
98     public BasicBidirectionalTopicOperation(String actor, String operation) {
99         this.actorName = actor;
100         this.operationName = operation;
101     }
102
103     /**
104      * Initializes mocks and sets up.
105      */
106     public void setUp() throws Exception {
107         MockitoAnnotations.initMocks(this);
108
109         executor = new PseudoExecutor();
110
111         makeContext();
112
113         outcome = params.makeOutcome();
114         topicParams = BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).timeoutSec(TIMEOUT)
115                         .build();
116
117         initOperator();
118     }
119
120     /**
121      * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
122      * {@link #params}.
123      * <p/>
124      * Note: {@link #params} is configured to use {@link #executor}.
125      */
126     protected void makeContext() {
127         enrichment = new TreeMap<>(makeEnrichment());
128
129         event = new VirtualControlLoopEvent();
130         event.setRequestId(REQ_ID);
131         event.setAai(enrichment);
132
133         context = new ControlLoopEventContext(event);
134
135         params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
136                         .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
137                         .build();
138     }
139
140     protected Map<String, String> makePayload() {
141         return null;
142     }
143
144     /**
145      * Initializes an operator so that it is "alive" and has the given names.
146      */
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);
155     }
156
157     /**
158      * Makes enrichment data.
159      *
160      * @return enrichment data
161      */
162     protected Map<String, String> makeEnrichment() {
163         return new TreeMap<>();
164     }
165
166     /**
167      * Provides a response to the topic {@link #listenerCaptor}.
168      *
169      * @param listener listener to which to provide the response
170      * @param response response to be provided
171      */
172     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, String response) {
173         try {
174             StandardCoderObject sco = coder.decode(response, StandardCoderObject.class);
175             listener.accept(response, sco);
176
177         } catch (CoderException e) {
178             throw new IllegalArgumentException("response is not a Map", e);
179         }
180     }
181 }