APPC Actor
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / BasicAppcOperation.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.appc;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.verify;
29
30 import java.util.Map;
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.TimeoutException;
34 import java.util.function.BiConsumer;
35 import java.util.function.BiFunction;
36 import org.onap.policy.appc.Response;
37 import org.onap.policy.appc.ResponseCode;
38 import org.onap.policy.appc.ResponseStatus;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.coder.StandardCoderObject;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
45 import org.onap.policy.controlloop.actorserviceprovider.Util;
46 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator;
47 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
48 import org.onap.policy.controlloop.policy.PolicyResult;
49 import org.onap.policy.controlloop.policy.Target;
50 import org.powermock.reflect.Whitebox;
51
52 /**
53  * Superclass for various operator tests.
54  */
55 public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperation {
56     protected static final String MY_DESCRIPTION = "my-description";
57     protected static final String MY_VNF = "my-vnf";
58     protected static final String KEY1 = "my-key-A";
59     protected static final String KEY2 = "my-key-B";
60     protected static final String VALUE1 = "{\"input\":\"hello\"}";
61     protected static final String VALUE2 = "{\"output\":\"world\"}";
62     protected static final String RESOURCE_ID = "my-resource";
63
64     protected Response response;
65
66     /**
67      * Constructs the object using a default actor and operation name.
68      */
69     public BasicAppcOperation() {
70         super();
71     }
72
73     /**
74      * Constructs the object.
75      *
76      * @param actor actor name
77      * @param operation operation name
78      */
79     public BasicAppcOperation(String actor, String operation) {
80         super(actor, operation);
81     }
82
83     /**
84      * Initializes mocks and sets up.
85      */
86     public void setUp() throws Exception {
87         super.setUp();
88
89         response = new Response();
90
91         ResponseStatus status = new ResponseStatus();
92         response.setStatus(status);
93         status.setCode(ResponseCode.SUCCESS.getValue());
94         status.setDescription(MY_DESCRIPTION);
95     }
96
97     /**
98      * Runs the operation and verifies that the response is successful.
99      *
100      * @param operation operation to run
101      */
102     protected void verifyOperation(AppcOperation operation)
103                     throws InterruptedException, ExecutionException, TimeoutException {
104
105         CompletableFuture<OperationOutcome> future2 = operation.start();
106         executor.runAll(100);
107         assertFalse(future2.isDone());
108
109         verify(forwarder).register(any(), listenerCaptor.capture());
110         provideResponse(listenerCaptor.getValue(), ResponseCode.SUCCESS.getValue(), MY_DESCRIPTION);
111
112         executor.runAll(100);
113         assertTrue(future2.isDone());
114
115         outcome = future2.get();
116         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
117         assertEquals(MY_DESCRIPTION, outcome.getMessage());
118     }
119
120     /**
121      * Pretty-prints a request and verifies that the result matches the expected JSON.
122      *
123      * @param <T> request type
124      * @param expectedJsonFile name of the file containing the expected JSON
125      * @param request request to verify
126      * @throws CoderException if the request cannot be pretty-printed
127      */
128     protected <T> void verifyRequest(String expectedJsonFile, T request) throws CoderException {
129         String json = new StandardCoder().encode(request, true);
130         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
131
132         // strip request id, because it changes each time
133         final String stripper = "svc-request-id[^,]*";
134         json = json.replaceFirst(stripper, "").trim();
135         expected = expected.replaceFirst(stripper, "").trim();
136
137         assertEquals(expected, json);
138     }
139
140     /**
141      * Verifies that an exception is thrown if a field is missing from the enrichment
142      * data.
143      *
144      * @param fieldName name of the field to be removed from the enrichment data
145      * @param expectedText text expected in the exception message
146      */
147     protected void verifyMissing(String fieldName, String expectedText,
148                     BiFunction<ControlLoopOperationParams, BidirectionalTopicOperator, AppcOperation> maker) {
149
150         makeContext();
151         enrichment.remove(fieldName);
152
153         AppcOperation oper = maker.apply(params, operator);
154
155         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
156                         .withMessageContaining("missing").withMessageContaining(expectedText);
157     }
158
159     @Override
160     protected void makeContext() {
161         super.makeContext();
162
163         Target target = new Target();
164         target.setResourceID(RESOURCE_ID);
165
166         params = params.toBuilder().target(target).build();
167     }
168
169     /**
170      * Provides a response to the listener.
171      *
172      * @param listener listener to which to provide the response
173      * @param code response code
174      * @param description response description
175      */
176     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
177         Response response = new Response();
178
179         ResponseStatus status = new ResponseStatus();
180         response.setStatus(status);
181         status.setCode(code);
182         status.setDescription(description);
183
184         provideResponse(listener, Util.translate("", response, String.class));
185     }
186
187     @Override
188     protected Map<String, String> makePayload() {
189         return Map.of(KEY1, VALUE1, KEY2, VALUE2);
190     }
191 }