Use BidirectionalTopicClient from policy-common
[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.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Map;
32 import java.util.concurrent.CompletableFuture;
33 import java.util.concurrent.ExecutionException;
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         executor.runAll(100);
104         assertTrue(future2.isDone());
105
106         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
107
108         return requestCaptor.getValue().getEntity();
109     }
110
111     /**
112      * Pretty-prints a request and verifies that the result matches the expected JSON.
113      *
114      * @param <T> request type
115      * @param expectedJsonFile name of the file containing the expected JSON
116      * @param request request to verify
117      * @throws CoderException if the request cannot be pretty-printed
118      */
119     protected <T> void verifyRequest(String expectedJsonFile, T request) throws CoderException {
120         String json = new StandardCoder().encode(request, true);
121         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
122
123         // strip request id, because it changes each time
124         final String stripper = "svc-request-id[^,]*";
125         json = json.replaceFirst(stripper, "").trim();
126         expected = expected.replaceFirst(stripper, "").trim();
127
128         assertEquals(expected, json);
129     }
130
131     /**
132      * Verifies that an exception is thrown if a field is missing from the enrichment
133      * data.
134      *
135      * @param fieldName name of the field to be removed from the enrichment data
136      * @param expectedText text expected in the exception message
137      */
138     protected void verifyMissing(String fieldName, String expectedText,
139                     BiFunction<ControlLoopOperationParams,HttpOperator,SdncOperation> maker) {
140
141         makeContext();
142         enrichment.remove(fieldName);
143
144         SdncOperation oper = maker.apply(params, operator);
145
146         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
147                         .withMessageContaining("missing").withMessageContaining(expectedText);
148     }
149
150     protected abstract Map<String, String> makeEnrichment();
151 }