39adbf55d50a8b3e9ce1eb6d58469cbfed92ceb5
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.test;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.Map;
26 import java.util.UUID;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.concurrent.Executor;
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.actorserviceprovider.ActorService;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
40
41 /**
42  * Superclass for various Operation tests.
43  */
44 public class BasicOperation {
45     protected static final UUID REQ_ID = UUID.randomUUID();
46     protected static final String SUB_REQ_ID = "my-sub-request-id";
47     protected static final String DEFAULT_ACTOR = "default-Actor";
48     protected static final String DEFAULT_OPERATION = "default-Operation";
49     protected static final String TARGET_ENTITY = "my-target";
50     protected static final String CL_NAME = "my-closed-loop";
51     protected static final String EVENT_POLICY_NAME = "my-event-policy-name";
52     protected static final String EVENT_POLICY_VERSION = "my-event-policy-version";
53     protected static final String EVENT_VERSION = "my-event-version";
54
55     protected static final Executor blockingExecutor = command -> {
56         var thread = new Thread(command);
57         thread.setDaemon(true);
58         thread.start();
59     };
60
61     protected final String actorName;
62     protected final String operationName;
63     protected Coder coder = new StandardCoder();
64
65     @Mock
66     protected ActorService service;
67
68     protected CompletableFuture<Response> future;
69     protected ControlLoopOperationParams params;
70     protected OperationOutcome outcome;
71     protected PseudoExecutor executor;
72
73     /**
74      * Constructs the object using a default actor and operation name.
75      */
76     public BasicOperation() {
77         this.actorName = DEFAULT_ACTOR;
78         this.operationName = DEFAULT_OPERATION;
79     }
80
81     /**
82      * Constructs the object.
83      *
84      * @param actor actor name
85      * @param operation operation name
86      */
87     public BasicOperation(String actor, String operation) {
88         this.actorName = actor;
89         this.operationName = operation;
90     }
91
92     /**
93      * Initializes mocks and sets up.
94      */
95     public void setUpBasic() {
96         MockitoAnnotations.openMocks(this);
97
98         future = new CompletableFuture<>();
99
100         executor = new PseudoExecutor();
101
102         makeContext();
103
104         // get a fresh outcome
105         outcome = params.makeOutcome();
106     }
107
108     /**
109      * Reinitializes {@link #params}.
110      * <p/>
111      * Note: {@link #params} is configured to use {@link #executor}.
112      */
113     protected void makeContext() {
114         params = ControlLoopOperationParams.builder().executor(executor).requestId(REQ_ID).actorService(service)
115                         .actor(actorName).operation(operationName).payload(makePayload())
116                         .build();
117     }
118
119
120     /**
121      * Makes payload data.
122      *
123      * @return payload data
124      */
125     protected Map<String, Object> makePayload() {
126         return null;
127     }
128
129     /**
130      * Pretty-prints a request and verifies that the result matches the expected JSON.
131      *
132      * @param <R> request type
133      * @param expectedJsonFile name of the file containing the expected JSON
134      * @param request request to verify
135      * @param ignore names of fields to be ignored, because they change with each request
136      * @throws CoderException if the request cannot be pretty-printed
137      */
138     protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
139         String json = coder.encode(request, true);
140         var expected = ResourceUtils.getResourceAsString(expectedJsonFile);
141
142         // strip various items, because they change for each request
143         for (String stripper : ignore) {
144             stripper += "[^,]*";
145             json = json.replaceAll(stripper, "");
146             expected = expected.replaceAll(stripper, "");
147         }
148
149         json = json.trim();
150         expected = expected.trim();
151
152         assertEquals(expected, json);
153     }
154 }