Include response in 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.endpoints.event.comm.bus.internal.BusTopicParams;
37 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
42 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
44 import org.onap.policy.controlloop.policy.PolicyResult;
45 import org.onap.policy.sdnc.SdncRequest;
46 import org.onap.policy.sdnc.SdncResponse;
47 import org.onap.policy.sdnc.SdncResponseOutput;
48 import org.onap.policy.simulators.Util;
49 import org.powermock.reflect.Whitebox;
50
51 /**
52  * Superclass for various operator tests.
53  */
54 public abstract class BasicSdncOperation extends BasicHttpOperation<SdncRequest> {
55     /**
56      * Fields to be ignored when comparing requests with JSON.
57      */
58     protected static final String[] IGNORE_FIELDS = {"svc-request-id"};
59
60     protected SdncResponse response;
61
62     /**
63      * Constructs the object using a default actor and operation name.
64      */
65     public BasicSdncOperation() {
66         super();
67     }
68
69     /**
70      * Constructs the object.
71      *
72      * @param actor actor name
73      * @param operation operation name
74      */
75     public BasicSdncOperation(String actor, String operation) {
76         super(actor, operation);
77     }
78
79     /**
80      * Starts the simulator.
81      */
82     protected static void initBeforeClass() throws Exception {
83         Util.buildSdncSim();
84
85         BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("restconf/operations/")
86                         .hostname("localhost").managed(true).port(Util.SDNCSIM_SERVER_PORT).build();
87         HttpClientFactoryInstance.getClientFactory().build(clientParams);
88     }
89
90     protected static void destroyAfterClass() {
91         HttpClientFactoryInstance.getClientFactory().destroy();
92         HttpServletServerFactoryInstance.getServerFactory().destroy();
93     }
94
95     /**
96      * Initializes mocks and sets up.
97      */
98     public void setUp() throws Exception {
99         super.setUpBasic();
100
101         response = new SdncResponse();
102
103         SdncResponseOutput output = new SdncResponseOutput();
104         response.setResponseOutput(output);
105         output.setResponseCode("200");
106
107         when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
108     }
109
110     /**
111      * Runs the operation and verifies that the response is successful.
112      *
113      * @param operation operation to run
114      * @return the request that was posted
115      */
116     protected SdncRequest verifyOperation(SdncOperation operation)
117                     throws InterruptedException, ExecutionException, TimeoutException {
118
119         CompletableFuture<OperationOutcome> future2 = operation.start();
120         executor.runAll(100);
121         assertFalse(future2.isDone());
122
123         verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
124         callbackCaptor.getValue().completed(rawResponse);
125
126         executor.runAll(100);
127         assertTrue(future2.isDone());
128
129         outcome = future2.get();
130         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
131         assertTrue(outcome.getResponse() instanceof SdncResponse);
132
133         assertNotNull(outcome.getSubRequestId());
134
135         return requestCaptor.getValue().getEntity();
136     }
137
138     /**
139      * Verifies that an exception is thrown if a field is missing from the enrichment
140      * data.
141      *
142      * @param fieldName name of the field to be removed from the enrichment data
143      * @param expectedText text expected in the exception message
144      */
145     protected void verifyMissing(String fieldName, String expectedText,
146                     OperationMaker<HttpConfig, SdncOperation> maker) {
147
148         makeContext();
149         enrichment.remove(fieldName);
150
151         SdncOperation oper = maker.apply(params, config);
152
153         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
154                         .withMessageContaining("missing").withMessageContaining(expectedText);
155     }
156
157     protected abstract Map<String, String> makeEnrichment();
158 }