Remove dmaap from models
[policy/models.git] / models-interactions / model-actors / actor.test / src / main / java / org / onap / policy / controlloop / actor / test / BasicOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.test;
23
24 import static org.junit.Assert.assertEquals;
25
26 import jakarta.ws.rs.core.Response;
27 import java.util.Map;
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;
41
42 /**
43  * Superclass for various Operation tests.
44  */
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";
51
52     protected static final Executor blockingExecutor = command -> {
53         var thread = new Thread(command);
54         thread.setDaemon(true);
55         thread.start();
56     };
57
58     protected final String actorName;
59     protected final String operationName;
60     protected Coder coder = new StandardCoder();
61
62     @Mock
63     protected ActorService service;
64
65     protected CompletableFuture<Response> future;
66     protected ControlLoopOperationParams params;
67     protected OperationOutcome outcome;
68     protected PseudoExecutor executor;
69
70     protected AutoCloseable closeable;
71
72     /**
73      * Constructs the object using a default actor and operation name.
74      */
75     public BasicOperation() {
76         this.actorName = DEFAULT_ACTOR;
77         this.operationName = DEFAULT_OPERATION;
78     }
79
80     /**
81      * Constructs the object.
82      *
83      * @param actor actor name
84      * @param operation operation name
85      */
86     public BasicOperation(String actor, String operation) {
87         this.actorName = actor;
88         this.operationName = operation;
89     }
90
91     /**
92      * Initializes mocks and sets up.
93      */
94     public void setUpBasic() {
95         closeable = MockitoAnnotations.openMocks(this);
96
97         future = new CompletableFuture<>();
98
99         executor = new PseudoExecutor();
100
101         makeContext();
102
103         // get a fresh outcome
104         outcome = params.makeOutcome();
105     }
106
107     /**
108      * Reinitializes {@link #params}.
109      * <p/>
110      * Note: {@link #params} is configured to use {@link #executor}.
111      */
112     protected void makeContext() {
113         params = ControlLoopOperationParams.builder().executor(executor).requestId(REQ_ID).actorService(service)
114                         .actor(actorName).operation(operationName).payload(makePayload())
115                         .build();
116     }
117
118
119     /**
120      * Makes payload data.
121      *
122      * @return payload data
123      */
124     protected Map<String, Object> makePayload() {
125         return null;
126     }
127
128     /**
129      * Pretty-prints a request and verifies that the result matches the expected JSON.
130      *
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
136      */
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);
140
141         // strip various items, because they change for each request
142         for (String stripper : ignore) {
143             stripper += "[^,]*";
144             json = json.replaceAll(stripper, "");
145             expected = expected.replaceAll(stripper, "");
146         }
147
148         json = json.trim();
149         expected = expected.trim();
150
151         assertEquals(expected, json);
152     }
153 }