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.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.when;
28 import java.util.TreeMap;
29 import java.util.UUID;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.Executor;
32 import javax.ws.rs.core.Response;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.aai.AaiConstants;
36 import org.onap.policy.aai.AaiCqResponse;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.common.utils.time.PseudoExecutor;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
44 import org.onap.policy.controlloop.actorserviceprovider.Operation;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
46 import org.onap.policy.controlloop.actorserviceprovider.Operator;
47 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
48 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
50 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
51 import org.onap.policy.controlloop.policy.PolicyResult;
54 * Superclass for various Operation tests.
56 public class BasicOperation {
57 protected static final UUID REQ_ID = UUID.randomUUID();
58 protected static final String SUB_REQ_ID = "my-sub-request-id";
59 protected static final String DEFAULT_ACTOR = "default-Actor";
60 protected static final String DEFAULT_OPERATION = "default-Operation";
61 protected static final String TARGET_ENTITY = "my-target";
62 protected static final String CL_NAME = "my-closed-loop";
63 protected static final String EVENT_POLICY_NAME = "my-event-policy-name";
64 protected static final String EVENT_POLICY_VERSION = "my-event-policy-version";
65 protected static final String EVENT_VERSION = "my-event-version";
67 protected static final Executor blockingExecutor = command -> {
68 Thread thread = new Thread(command);
69 thread.setDaemon(true);
73 protected final String actorName;
74 protected final String operationName;
75 protected Coder coder = new StandardCoder();
78 protected ActorService service;
80 protected Actor guardActor;
82 protected Operator guardOperator;
84 protected Operation guardOperation;
86 protected Actor cqActor;
88 protected Operator cqOperator;
90 protected Operation cqOperation;
92 protected AaiCqResponse cqResponse;
94 protected CompletableFuture<OperationOutcome> cqFuture;
95 protected CompletableFuture<Response> future;
96 protected ControlLoopOperationParams params;
97 protected Map<String, String> enrichment;
98 protected VirtualControlLoopEvent event;
99 protected ControlLoopEventContext context;
100 protected OperationOutcome outcome;
101 protected PseudoExecutor executor;
104 * Constructs the object using a default actor and operation name.
106 public BasicOperation() {
107 this.actorName = DEFAULT_ACTOR;
108 this.operationName = DEFAULT_OPERATION;
112 * Constructs the object.
114 * @param actor actor name
115 * @param operation operation name
117 public BasicOperation(String actor, String operation) {
118 this.actorName = actor;
119 this.operationName = operation;
123 * Initializes mocks and sets up.
125 public void setUpBasic() {
126 MockitoAnnotations.initMocks(this);
128 cqFuture = new CompletableFuture<>();
129 future = new CompletableFuture<>();
131 executor = new PseudoExecutor();
135 when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor);
136 when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator);
137 when(guardOperator.buildOperation(any())).thenReturn(guardOperation);
139 outcome = params.makeOutcome();
140 outcome.setResult(PolicyResult.SUCCESS);
141 when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(outcome));
143 when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(cqActor);
144 when(cqActor.getOperator("CustomQuery")).thenReturn(cqOperator);
145 when(cqOperator.buildOperation(any())).thenReturn(cqOperation);
147 when(cqOperation.start()).thenReturn(cqFuture);
149 // get a fresh outcome
150 outcome = params.makeOutcome();
154 * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
157 * Note: {@link #params} is configured to use {@link #executor}.
159 protected void makeContext() {
160 enrichment = new TreeMap<>(makeEnrichment());
162 event = new VirtualControlLoopEvent();
163 event.setRequestId(REQ_ID);
164 event.setAai(enrichment);
165 event.setClosedLoopControlName(CL_NAME);
166 event.setPolicyName(EVENT_POLICY_NAME);
167 event.setPolicyVersion(EVENT_POLICY_VERSION);
168 event.setVersion(EVENT_VERSION);
170 context = new ControlLoopEventContext(event);
172 params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
173 .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
178 * Makes enrichment data.
180 * @return enrichment data
182 protected Map<String, String> makeEnrichment() {
183 return new TreeMap<>();
188 * Makes payload data.
190 * @return payload data
192 protected Map<String, Object> makePayload() {
197 * Pretty-prints a request and verifies that the result matches the expected JSON.
199 * @param <R> request type
200 * @param expectedJsonFile name of the file containing the expected JSON
201 * @param request request to verify
202 * @param ignore names of fields to be ignored, because they change with each request
203 * @throws CoderException if the request cannot be pretty-printed
205 protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
206 String json = coder.encode(request, true);
207 String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
209 // strip various items, because they change for each request
210 for (String stripper : ignore) {
212 json = json.replaceAll(stripper, "");
213 expected = expected.replaceAll(stripper, "");
217 expected = expected.trim();
219 assertEquals(expected, json);
223 * Provides a response to a custom query.
225 * @param cq response to provide
227 protected void provideCqResponse(AaiCqResponse cq) {
228 context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
229 OperationOutcome outcome2 = params.makeOutcome();
230 outcome2.setResult(PolicyResult.SUCCESS);
231 cqFuture.complete(outcome2);