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