Test new actors against simulators
[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 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 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.when;
26
27 import java.util.Map;
28 import java.util.TreeMap;
29 import java.util.UUID;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.Executor;
32 import javax.ws.rs.core.Response;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.aai.AaiConstants;
36 import org.onap.policy.aai.AaiCqResponse;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.common.utils.time.PseudoExecutor;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
44 import org.onap.policy.controlloop.actorserviceprovider.Operation;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
46 import org.onap.policy.controlloop.actorserviceprovider.Operator;
47 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
48 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
50 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
51 import org.onap.policy.controlloop.policy.PolicyResult;
52
53 /**
54  * Superclass for various Operation tests.
55  */
56 public class BasicOperation {
57     protected static final UUID REQ_ID = UUID.randomUUID();
58     protected static final String SUB_REQ_ID = "my-sub-request-id";
59     protected static final String DEFAULT_ACTOR = "default-actor";
60     protected static final String DEFAULT_OPERATION = "default-operation";
61     protected static final String TARGET_ENTITY = "my-target";
62
63     protected static final Executor blockingExecutor = command -> {
64         Thread thread = new Thread(command);
65         thread.setDaemon(true);
66         thread.start();
67     };
68
69     protected final String actorName;
70     protected final String operationName;
71     protected Coder coder = new StandardCoder();
72
73     @Mock
74     protected ActorService service;
75     @Mock
76     protected Actor guardActor;
77     @Mock
78     protected Operator guardOperator;
79     @Mock
80     protected Operation guardOperation;
81     @Mock
82     protected Actor cqActor;
83     @Mock
84     protected Operator cqOperator;
85     @Mock
86     protected Operation cqOperation;
87     @Mock
88     protected AaiCqResponse cqResponse;
89
90     protected CompletableFuture<OperationOutcome> cqFuture;
91     protected CompletableFuture<Response> future;
92     protected ControlLoopOperationParams params;
93     protected Map<String, String> enrichment;
94     protected VirtualControlLoopEvent event;
95     protected ControlLoopEventContext context;
96     protected OperationOutcome outcome;
97     protected PseudoExecutor executor;
98
99     /**
100      * Constructs the object using a default actor and operation name.
101      */
102     public BasicOperation() {
103         this.actorName = DEFAULT_ACTOR;
104         this.operationName = DEFAULT_OPERATION;
105     }
106
107     /**
108      * Constructs the object.
109      *
110      * @param actor actor name
111      * @param operation operation name
112      */
113     public BasicOperation(String actor, String operation) {
114         this.actorName = actor;
115         this.operationName = operation;
116     }
117
118     /**
119      * Initializes mocks and sets up.
120      */
121     public void setUpBasic() {
122         MockitoAnnotations.initMocks(this);
123
124         cqFuture = new CompletableFuture<>();
125         future = new CompletableFuture<>();
126
127         executor = new PseudoExecutor();
128
129         makeContext();
130
131         when(service.getActor(OperationPartial.GUARD_ACTOR_NAME)).thenReturn(guardActor);
132         when(guardActor.getOperator(OperationPartial.GUARD_OPERATION_NAME)).thenReturn(guardOperator);
133         when(guardOperator.buildOperation(any())).thenReturn(guardOperation);
134
135         outcome = params.makeOutcome();
136         outcome.setResult(PolicyResult.SUCCESS);
137         when(guardOperation.start()).thenReturn(CompletableFuture.completedFuture(outcome));
138
139         when(service.getActor(AaiConstants.ACTOR_NAME)).thenReturn(cqActor);
140         when(cqActor.getOperator("CustomQuery")).thenReturn(cqOperator);
141         when(cqOperator.buildOperation(any())).thenReturn(cqOperation);
142
143         when(cqOperation.start()).thenReturn(cqFuture);
144
145         // get a fresh outcome
146         outcome = params.makeOutcome();
147     }
148
149     /**
150      * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
151      * {@link #params}.
152      * <p/>
153      * Note: {@link #params} is configured to use {@link #executor}.
154      */
155     protected void makeContext() {
156         enrichment = new TreeMap<>(makeEnrichment());
157
158         event = new VirtualControlLoopEvent();
159         event.setRequestId(REQ_ID);
160         event.setAai(enrichment);
161
162         context = new ControlLoopEventContext(event);
163
164         params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
165                         .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).payload(makePayload())
166                         .build();
167     }
168
169     /**
170      * Makes enrichment data.
171      *
172      * @return enrichment data
173      */
174     protected Map<String, String> makeEnrichment() {
175         return new TreeMap<>();
176     }
177
178
179     /**
180      * Makes payload data.
181      *
182      * @return payload data
183      */
184     protected Map<String, Object> makePayload() {
185         return null;
186     }
187
188     /**
189      * Pretty-prints a request and verifies that the result matches the expected JSON.
190      *
191      * @param <R> request type
192      * @param expectedJsonFile name of the file containing the expected JSON
193      * @param request request to verify
194      * @param ignore names of fields to be ignored, because they change with each request
195      * @throws CoderException if the request cannot be pretty-printed
196      */
197     protected <R> void verifyRequest(String expectedJsonFile, R request, String... ignore) throws CoderException {
198         String json = coder.encode(request, true);
199         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
200
201         // strip various items, because they change for each request
202         for (String stripper : ignore) {
203             stripper += "[^,]*";
204             json = json.replaceAll(stripper, "");
205             expected = expected.replaceAll(stripper, "");
206         }
207
208         json = json.trim();
209         expected = expected.trim();
210
211         assertEquals(expected, json);
212     }
213
214     /**
215      * Provides a response to a custom query.
216      *
217      * @param cq response to provide
218      */
219     protected void provideCqResponse(AaiCqResponse cq) {
220         context.setProperty(AaiCqResponse.CONTEXT_KEY, cq);
221         OperationOutcome outcome2 = params.makeOutcome();
222         outcome2.setResult(PolicyResult.SUCCESS);
223         cqFuture.complete(outcome2);
224     }
225 }