d8c707cc39782c819c6d2f4bfbb4f91a32915ca1
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / BasicSdncOperator.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.sdnc;
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.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Map;
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.TimeoutException;
35 import java.util.function.BiFunction;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
43 import org.onap.policy.controlloop.policy.PolicyResult;
44 import org.onap.policy.sdnc.SdncRequest;
45 import org.onap.policy.sdnc.SdncResponse;
46 import org.onap.policy.sdnc.SdncResponseOutput;
47 import org.powermock.reflect.Whitebox;
48
49 /**
50  * Superclass for various operator tests.
51  */
52 public abstract class BasicSdncOperator extends BasicHttpOperation<SdncRequest> {
53
54     protected SdncResponse response;
55
56     /**
57      * Constructs the object using a default actor and operation name.
58      */
59     public BasicSdncOperator() {
60         super();
61     }
62
63     /**
64      * Constructs the object.
65      *
66      * @param actor actor name
67      * @param operation operation name
68      */
69     public BasicSdncOperator(String actor, String operation) {
70         super(actor, operation);
71     }
72
73     /**
74      * Initializes mocks and sets up.
75      */
76     public void setUp() throws Exception {
77         super.setUp();
78
79         response = new SdncResponse();
80
81         SdncResponseOutput output = new SdncResponseOutput();
82         response.setResponseOutput(output);
83         output.setResponseCode("200");
84
85         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
86     }
87
88     /**
89      * Runs the operation and verifies that the response is successful.
90      *
91      * @param operation operation to run
92      * @return the request that was posted
93      */
94     protected SdncRequest verifyOperation(SdncOperation operation)
95                     throws InterruptedException, ExecutionException, TimeoutException {
96
97         CompletableFuture<OperationOutcome> future2 = operation.startOperationAsync(1, outcome);
98         assertFalse(future2.isDone());
99
100         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
101         callbackCaptor.getValue().completed(rawResponse);
102
103         assertEquals(PolicyResult.SUCCESS, future2.get(5, TimeUnit.SECONDS).getResult());
104
105         return requestCaptor.getValue().getEntity();
106     }
107
108     /**
109      * Pretty-prints a request and verifies that the result matches the expected JSON.
110      *
111      * @param <T> request type
112      * @param expectedJsonFile name of the file containing the expected JSON
113      * @param request request to verify
114      * @throws CoderException if the request cannot be pretty-printed
115      */
116     protected <T> void verifyRequest(String expectedJsonFile, T request) throws CoderException {
117         String json = new StandardCoder().encode(request, true);
118         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
119
120         // strip request id, because it changes each time
121         final String stripper = "svc-request-id[^,]*";
122         json = json.replaceFirst(stripper, "").trim();
123         expected = expected.replaceFirst(stripper, "").trim();
124
125         assertEquals(expected, json);
126     }
127
128     /**
129      * Verifies that an exception is thrown if a field is missing from the enrichment
130      * data.
131      *
132      * @param fieldName name of the field to be removed from the enrichment data
133      * @param expectedText text expected in the exception message
134      */
135     protected void verifyMissing(String fieldName, String expectedText,
136                     BiFunction<ControlLoopOperationParams,HttpOperator,SdncOperation> maker) {
137
138         makeContext();
139         enrichment.remove(fieldName);
140
141         SdncOperation oper = maker.apply(params, operator);
142
143         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
144                         .withMessageContaining("missing").withMessageContaining(expectedText);
145     }
146
147     protected abstract Map<String, String> makeEnrichment();
148 }