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";
63 protected static final Executor blockingExecutor = command -> {
64 Thread thread = new Thread(command);
65 thread.setDaemon(true);
69 protected final String actorName;
70 protected final String operationName;
71 protected Coder coder = new StandardCoder();
74 protected ActorService service;
76 protected Actor guardActor;
78 protected Operator guardOperator;
80 protected Operation guardOperation;
82 protected Actor cqActor;
84 protected Operator cqOperator;
86 protected Operation cqOperation;
88 protected AaiCqResponse cqResponse;
90 protected CompletableFuture<OperationOutcome> cqFuture;
91 protected CompletableFuture<Response> future;
92 protected ControlLoopOperationParams params;
93 protected Map<String, String> enrichment;
94 protected VirtualControlLoopEvent event;
95 protected ControlLoopEventContext context;
96 protected OperationOutcome outcome;
97 protected PseudoExecutor executor;
100 * Constructs the object using a default actor and operation name.
102 public BasicOperation() {
103 this.actorName = DEFAULT_ACTOR;
104 this.operationName = DEFAULT_OPERATION;
108 * Constructs the object.
110 * @param actor actor name
111 * @param operation operation name
113 public BasicOperation(String actor, String operation) {
114 this.actorName = actor;
115 this.operationName = operation;
119 * Initializes mocks and sets up.
121 public void setUpBasic() {
122 MockitoAnnotations.initMocks(this);
124 cqFuture = new CompletableFuture<>();
125 future = new CompletableFuture<>();
127 executor = new PseudoExecutor();
131 when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor);
132 when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator);
133 when(guardOperator.buildOperation(any())).thenReturn(guardOperation);
135 outcome = params.makeOutcome();
136 outcome.setResult(PolicyResult.SUCCESS);
137 when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(outcome));
139 when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(cqActor);
140 when(cqActor.getOperator("CustomQuery")).thenReturn(cqOperator);
141 when(cqOperator.buildOperation(any())).thenReturn(cqOperation);
143 when(cqOperation.start()).thenReturn(cqFuture);
145 // get a fresh outcome
146 outcome = params.makeOutcome();
150 * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
153 * Note: {@link #params} is configured to use {@link #executor}.
155 protected void makeContext() {
156 enrichment = new TreeMap<>(makeEnrichment());
158 event = new VirtualControlLoopEvent();
159 event.setRequestId(REQ_ID);
160 event.setAai(enrichment);
162 context = new ControlLoopEventContext(event);
164 params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
165 .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
170 * Makes enrichment data.
172 * @return enrichment data
174 protected Map<String, String> makeEnrichment() {
175 return new TreeMap<>();
180 * Makes payload data.
182 * @return payload data
184 protected Map<String, Object> makePayload() {
189 * Pretty-prints a request and verifies that the result matches the expected JSON.
191 * @param <R> request type
192 * @param expectedJsonFile name of the file containing the expected JSON
193 * @param request request to verify
194 * @param ignore names of fields to be ignored, because they change with each request
195 * @throws CoderException if the request cannot be pretty-printed
197 protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
198 String json = coder.encode(request, true);
199 String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
201 // strip various items, because they change for each request
202 for (String stripper : ignore) {
204 json = json.replaceAll(stripper, "");
205 expected = expected.replaceAll(stripper, "");
209 expected = expected.trim();
211 assertEquals(expected, json);
215 * Provides a response to a custom query.
217 * @param cq response to provide
219 protected void provideCqResponse(AaiCqResponse cq) {
220 context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
221 OperationOutcome outcome2 = params.makeOutcome();
222 outcome2.setResult(PolicyResult.SUCCESS);
223 cqFuture.complete(outcome2);