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 DEFAULT_ACTOR = "default-actor";
58 protected static final String DEFAULT_OPERATION = "default-operation";
59 protected static final String TARGET_ENTITY = "my-target";
61 protected final String actorName;
62 protected final String operationName;
63 protected Coder coder = new StandardCoder();
66 protected ActorService service;
68 protected Actor guardActor;
70 protected Operator guardOperator;
72 protected Operation guardOperation;
74 protected Actor cqActor;
76 protected Operator cqOperator;
78 protected Operation cqOperation;
80 protected AaiCqResponse cqResponse;
82 protected CompletableFuture<OperationOutcome> cqFuture;
83 protected CompletableFuture<Response> future;
84 protected ControlLoopOperationParams params;
85 protected Map<String, String> enrichment;
86 protected VirtualControlLoopEvent event;
87 protected ControlLoopEventContext context;
88 protected OperationOutcome outcome;
89 protected PseudoExecutor executor;
92 * Constructs the object using a default actor and operation name.
94 public BasicOperation() {
95 this.actorName = DEFAULT_ACTOR;
96 this.operationName = DEFAULT_OPERATION;
100 * Constructs the object.
102 * @param actor actor name
103 * @param operation operation name
105 public BasicOperation(String actor, String operation) {
106 this.actorName = actor;
107 this.operationName = operation;
111 * Initializes mocks and sets up.
113 public void setUpBasic() {
114 MockitoAnnotations.initMocks(this);
116 cqFuture = new CompletableFuture<>();
117 future = new CompletableFuture<>();
119 executor = new PseudoExecutor();
123 when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor);
124 when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator);
125 when(guardOperator.buildOperation(any())).thenReturn(guardOperation);
127 outcome = params.makeOutcome();
128 outcome.setResult(PolicyResult.SUCCESS);
129 when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(outcome));
131 when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(cqActor);
132 when(cqActor.getOperator("CustomQuery")).thenReturn(cqOperator);
133 when(cqOperator.buildOperation(any())).thenReturn(cqOperation);
135 when(cqOperation.start()).thenReturn(cqFuture);
137 // get a fresh outcome
138 outcome = params.makeOutcome();
142 * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
145 * Note: {@link #params} is configured to use {@link #executor}.
147 protected void makeContext() {
148 enrichment = new TreeMap<>(makeEnrichment());
150 event = new VirtualControlLoopEvent();
151 event.setRequestId(REQ_ID);
152 event.setAai(enrichment);
154 context = new ControlLoopEventContext(event);
156 params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
157 .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
162 * Makes enrichment data.
164 * @return enrichment data
166 protected Map<String, String> makeEnrichment() {
167 return new TreeMap<>();
172 * Makes payload data.
174 * @return payload data
176 protected Map<String, Object> makePayload() {
181 * Pretty-prints a request and verifies that the result matches the expected JSON.
183 * @param <R> request type
184 * @param expectedJsonFile name of the file containing the expected JSON
185 * @param request request to verify
186 * @param ignore names of fields to be ignored, because they change with each request
187 * @throws CoderException if the request cannot be pretty-printed
189 protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
190 String json = coder.encode(request, true);
191 String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
193 // strip various items, because they change for each request
194 for (String stripper : ignore) {
196 json = json.replaceAll(stripper, "");
197 expected = expected.replaceAll(stripper, "");
201 expected = expected.trim();
203 assertEquals(expected, json);
207 * Provides a response to a custom query.
209 * @param cq response to provide
211 protected void provideCqResponse(AaiCqResponse cq) {
212 context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
213 OperationOutcome outcome2 = params.makeOutcome();
214 outcome2.setResult(PolicyResult.SUCCESS);
215 cqFuture.complete(outcome2);