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;
26 import java.util.TreeMap;
27 import java.util.UUID;
28 import java.util.concurrent.CompletableFuture;
29 import javax.ws.rs.core.Response;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.policy.common.utils.coder.Coder;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.common.utils.time.PseudoExecutor;
37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
44 * Superclass for various Operation tests.
46 public class BasicOperation {
47 protected static final Coder coder = new StandardCoder();
48 protected static final UUID REQ_ID = UUID.randomUUID();
49 protected static final String DEFAULT_ACTOR = "default-actor";
50 protected static final String DEFAULT_OPERATION = "default-operation";
51 protected static final String TARGET_ENTITY = "my-target";
53 protected final String actorName;
54 protected final String operationName;
57 protected ActorService service;
59 protected CompletableFuture<Response> future;
60 protected ControlLoopOperationParams params;
61 protected Map<String, String> enrichment;
62 protected VirtualControlLoopEvent event;
63 protected ControlLoopEventContext context;
64 protected OperationOutcome outcome;
65 protected PseudoExecutor executor;
68 * Constructs the object using a default actor and operation name.
70 public BasicOperation() {
71 this.actorName = DEFAULT_ACTOR;
72 this.operationName = DEFAULT_OPERATION;
76 * Constructs the object.
78 * @param actor actor name
79 * @param operation operation name
81 public BasicOperation(String actor, String operation) {
82 this.actorName = actor;
83 this.operationName = operation;
87 * Initializes mocks and sets up.
89 public void setUpBasic() {
90 MockitoAnnotations.initMocks(this);
92 future = new CompletableFuture<>();
94 executor = new PseudoExecutor();
98 outcome = params.makeOutcome();
102 * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
105 * Note: {@link #params} is configured to use {@link #executor}.
107 protected void makeContext() {
108 enrichment = new TreeMap<>(makeEnrichment());
110 event = new VirtualControlLoopEvent();
111 event.setRequestId(REQ_ID);
112 event.setAai(enrichment);
114 context = new ControlLoopEventContext(event);
116 params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
117 .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
122 * Makes enrichment data.
124 * @return enrichment data
126 protected Map<String, String> makeEnrichment() {
127 return new TreeMap<>();
132 * Makes payload data.
134 * @return payload data
136 protected Map<String, String> makePayload() {
141 * Pretty-prints a request and verifies that the result matches the expected JSON.
143 * @param <R> request type
144 * @param expectedJsonFile name of the file containing the expected JSON
145 * @param request request to verify
146 * @param ignore names of fields to be ignored, because they change with each request
147 * @throws CoderException if the request cannot be pretty-printed
149 protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
150 String json = coder.encode(request, true);
151 String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
153 // strip various items, because they change for each request
154 for (String stripper : ignore) {
156 json = json.replaceAll(stripper, "");
157 expected = expected.replaceAll(stripper, "");
161 expected = expected.trim();
163 assertEquals(expected, json);