2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.test;
24 import static org.junit.Assert.assertEquals;
26 import jakarta.ws.rs.core.Response;
28 import java.util.UUID;
29 import java.util.concurrent.CompletableFuture;
30 import java.util.concurrent.Executor;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.common.utils.time.PseudoExecutor;
38 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
43 * Superclass for various Operation tests.
45 public class BasicOperation {
46 protected static final UUID REQ_ID = UUID.randomUUID();
47 protected static final String SUB_REQ_ID = "my-sub-request-id";
48 protected static final String DEFAULT_ACTOR = "default-Actor";
49 protected static final String DEFAULT_OPERATION = "default-Operation";
50 protected static final String TARGET_ENTITY = "my-target";
52 protected static final Executor blockingExecutor = command -> {
53 var thread = new Thread(command);
54 thread.setDaemon(true);
58 protected final String actorName;
59 protected final String operationName;
60 protected Coder coder = new StandardCoder();
63 protected ActorService service;
65 protected CompletableFuture<Response> future;
66 protected ControlLoopOperationParams params;
67 protected OperationOutcome outcome;
68 protected PseudoExecutor executor;
70 protected AutoCloseable closeable;
73 * Constructs the object using a default actor and operation name.
75 public BasicOperation() {
76 this.actorName = DEFAULT_ACTOR;
77 this.operationName = DEFAULT_OPERATION;
81 * Constructs the object.
83 * @param actor actor name
84 * @param operation operation name
86 public BasicOperation(String actor, String operation) {
87 this.actorName = actor;
88 this.operationName = operation;
92 * Initializes mocks and sets up.
94 public void setUpBasic() {
95 closeable = MockitoAnnotations.openMocks(this);
97 future = new CompletableFuture<>();
99 executor = new PseudoExecutor();
103 // get a fresh outcome
104 outcome = params.makeOutcome();
108 * Reinitializes {@link #params}.
110 * Note: {@link #params} is configured to use {@link #executor}.
112 protected void makeContext() {
113 params = ControlLoopOperationParams.builder().executor(executor).requestId(REQ_ID).actorService(service)
114 .actor(actorName).operation(operationName).payload(makePayload())
120 * Makes payload data.
122 * @return payload data
124 protected Map<String, Object> makePayload() {
129 * Pretty-prints a request and verifies that the result matches the expected JSON.
131 * @param <R> request type
132 * @param expectedJsonFile name of the file containing the expected JSON
133 * @param request request to verify
134 * @param ignore names of fields to be ignored, because they change with each request
135 * @throws CoderException if the request cannot be pretty-printed
137 protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
138 String json = coder.encode(request, true);
139 var expected = ResourceUtils.getResourceAsString(expectedJsonFile);
141 // strip various items, because they change for each request
142 for (String stripper : ignore) {
144 json = json.replaceAll(stripper, "");
145 expected = expected.replaceAll(stripper, "");
149 expected = expected.trim();
151 assertEquals(expected, json);