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 javax.ws.rs.core.Response;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.policy.aai.AaiConstants;
35 import org.onap.policy.aai.AaiCqResponse;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.common.utils.time.PseudoExecutor;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
43 import org.onap.policy.controlloop.actorserviceprovider.Operation;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
45 import org.onap.policy.controlloop.actorserviceprovider.Operator;
46 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
47 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
49 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
50 import org.onap.policy.controlloop.policy.PolicyResult;
53 * Superclass for various Operation tests.
55 public class BasicOperation {
56 protected static final UUID REQ_ID = UUID.randomUUID();
57 protected static final String SUB_REQ_ID = "my-sub-request-id";
58 protected static final String DEFAULT_ACTOR = "default-actor";
59 protected static final String DEFAULT_OPERATION = "default-operation";
60 protected static final String TARGET_ENTITY = "my-target";
62 protected final String actorName;
63 protected final String operationName;
64 protected Coder coder = new StandardCoder();
67 protected ActorService service;
69 protected Actor guardActor;
71 protected Operator guardOperator;
73 protected Operation guardOperation;
75 protected Actor cqActor;
77 protected Operator cqOperator;
79 protected Operation cqOperation;
81 protected AaiCqResponse cqResponse;
83 protected CompletableFuture<OperationOutcome> cqFuture;
84 protected CompletableFuture<Response> future;
85 protected ControlLoopOperationParams params;
86 protected Map<String, String> enrichment;
87 protected VirtualControlLoopEvent event;
88 protected ControlLoopEventContext context;
89 protected OperationOutcome outcome;
90 protected PseudoExecutor executor;
93 * Constructs the object using a default actor and operation name.
95 public BasicOperation() {
96 this.actorName = DEFAULT_ACTOR;
97 this.operationName = DEFAULT_OPERATION;
101 * Constructs the object.
103 * @param actor actor name
104 * @param operation operation name
106 public BasicOperation(String actor, String operation) {
107 this.actorName = actor;
108 this.operationName = operation;
112 * Initializes mocks and sets up.
114 public void setUpBasic() {
115 MockitoAnnotations.initMocks(this);
117 cqFuture = new CompletableFuture<>();
118 future = new CompletableFuture<>();
120 executor = new PseudoExecutor();
124 when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor);
125 when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator);
126 when(guardOperator.buildOperation(any())).thenReturn(guardOperation);
128 outcome = params.makeOutcome();
129 outcome.setResult(PolicyResult.SUCCESS);
130 when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(outcome));
132 when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(cqActor);
133 when(cqActor.getOperator("CustomQuery")).thenReturn(cqOperator);
134 when(cqOperator.buildOperation(any())).thenReturn(cqOperation);
136 when(cqOperation.start()).thenReturn(cqFuture);
138 // get a fresh outcome
139 outcome = params.makeOutcome();
143 * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
146 * Note: {@link #params} is configured to use {@link #executor}.
148 protected void makeContext() {
149 enrichment = new TreeMap<>(makeEnrichment());
151 event = new VirtualControlLoopEvent();
152 event.setRequestId(REQ_ID);
153 event.setAai(enrichment);
155 context = new ControlLoopEventContext(event);
157 params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
158 .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
163 * Makes enrichment data.
165 * @return enrichment data
167 protected Map<String, String> makeEnrichment() {
168 return new TreeMap<>();
173 * Makes payload data.
175 * @return payload data
177 protected Map<String, Object> makePayload() {
182 * Pretty-prints a request and verifies that the result matches the expected JSON.
184 * @param <R> request type
185 * @param expectedJsonFile name of the file containing the expected JSON
186 * @param request request to verify
187 * @param ignore names of fields to be ignored, because they change with each request
188 * @throws CoderException if the request cannot be pretty-printed
190 protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
191 String json = coder.encode(request, true);
192 String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
194 // strip various items, because they change for each request
195 for (String stripper : ignore) {
197 json = json.replaceAll(stripper, "");
198 expected = expected.replaceAll(stripper, "");
202 expected = expected.trim();
204 assertEquals(expected, json);
208 * Provides a response to a custom query.
210 * @param cq response to provide
212 protected void provideCqResponse(AaiCqResponse cq) {
213 context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
214 OperationOutcome outcome2 = params.makeOutcome();
215 outcome2.setResult(PolicyResult.SUCCESS);
216 cqFuture.complete(outcome2);