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