Merge "Add safe entity delete, fix multiple entity get"
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / BasicSdncOperation.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 BasicSdncOperation extends BasicHttpOperation<SdncRequest> {
53
54     protected SdncResponse response;
55
56     /**
57      * Constructs the object using a default actor and operation name.
58      */
59     public BasicSdncOperation() {
60         super();
61     }
62
63     /**
64      * Constructs the object.
65      *
66      * @param actor actor name
67      * @param operation operation name
68      */
69     public BasicSdncOperation(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.start();
98         executor.runAll(100);
99         assertFalse(future2.isDone());
100
101         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
102         callbackCaptor.getValue().completed(rawResponse);
103
104         executor.runAll(100);
105         assertTrue(future2.isDone());
106
107         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
108
109         return requestCaptor.getValue().getEntity();
110     }
111
112     /**
113      * Pretty-prints a request and verifies that the result matches the expected JSON.
114      *
115      * @param <T> request type
116      * @param expectedJsonFile name of the file containing the expected JSON
117      * @param request request to verify
118      * @throws CoderException if the request cannot be pretty-printed
119      */
120     protected <T> void verifyRequest(String expectedJsonFile, T request) throws CoderException {
121         String json = new StandardCoder().encode(request, true);
122         String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
123
124         // strip request id, because it changes each time
125         final String stripper = "svc-request-id[^,]*";
126         json = json.replaceFirst(stripper, "").trim();
127         expected = expected.replaceFirst(stripper, "").trim();
128
129         assertEquals(expected, json);
130     }
131
132     /**
133      * Verifies that an exception is thrown if a field is missing from the enrichment
134      * data.
135      *
136      * @param fieldName name of the field to be removed from the enrichment data
137      * @param expectedText text expected in the exception message
138      */
139     protected void verifyMissing(String fieldName, String expectedText,
140                     BiFunction<ControlLoopOperationParams,HttpOperator,SdncOperation> maker) {
141
142         makeContext();
143         enrichment.remove(fieldName);
144
145         SdncOperation oper = maker.apply(params, operator);
146
147         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
148                         .withMessageContaining("missing").withMessageContaining(expectedText);
149     }
150
151     protected abstract Map<String, String> makeEnrichment();
152 }