Add subrequest ID to OperationOutcome
[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.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.Map;
33 import java.util.concurrent.CompletableFuture;
34 import java.util.concurrent.ExecutionException;
35 import java.util.concurrent.TimeoutException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
41 import org.onap.policy.controlloop.policy.PolicyResult;
42 import org.onap.policy.sdnc.SdncRequest;
43 import org.onap.policy.sdnc.SdncResponse;
44 import org.onap.policy.sdnc.SdncResponseOutput;
45 import org.powermock.reflect.Whitebox;
46
47 /**
48  * Superclass for various operator tests.
49  */
50 public abstract class BasicSdncOperation extends BasicHttpOperation<SdncRequest> {
51     /**
52      * Fields to be ignored when comparing requests with JSON.
53      */
54     protected static final String[] IGNORE_FIELDS = {"svc-request-id"};
55
56     protected SdncResponse response;
57
58     /**
59      * Constructs the object using a default actor and operation name.
60      */
61     public BasicSdncOperation() {
62         super();
63     }
64
65     /**
66      * Constructs the object.
67      *
68      * @param actor actor name
69      * @param operation operation name
70      */
71     public BasicSdncOperation(String actor, String operation) {
72         super(actor, operation);
73     }
74
75     /**
76      * Initializes mocks and sets up.
77      */
78     public void setUp() throws Exception {
79         super.setUpBasic();
80
81         response = new SdncResponse();
82
83         SdncResponseOutput output = new SdncResponseOutput();
84         response.setResponseOutput(output);
85         output.setResponseCode("200");
86
87         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
88     }
89
90     /**
91      * Runs the operation and verifies that the response is successful.
92      *
93      * @param operation operation to run
94      * @return the request that was posted
95      */
96     protected SdncRequest verifyOperation(SdncOperation operation)
97                     throws InterruptedException, ExecutionException, TimeoutException {
98
99         CompletableFuture<OperationOutcome> future2 = operation.start();
100         executor.runAll(100);
101         assertFalse(future2.isDone());
102
103         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
104         callbackCaptor.getValue().completed(rawResponse);
105
106         executor.runAll(100);
107         assertTrue(future2.isDone());
108
109         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
110
111         assertNotNull(future2.get().getSubRequestId());
112
113         return requestCaptor.getValue().getEntity();
114     }
115
116     /**
117      * Verifies that an exception is thrown if a field is missing from the enrichment
118      * data.
119      *
120      * @param fieldName name of the field to be removed from the enrichment data
121      * @param expectedText text expected in the exception message
122      */
123     protected void verifyMissing(String fieldName, String expectedText,
124                     OperationMaker<HttpConfig, SdncOperation> maker) {
125
126         makeContext();
127         enrichment.remove(fieldName);
128
129         SdncOperation oper = maker.apply(params, config);
130
131         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
132                         .withMessageContaining("missing").withMessageContaining(expectedText);
133     }
134
135     protected abstract Map<String, String> makeEnrichment();
136 }