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