2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.sdnc;
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;
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.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
44 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
46 import org.onap.policy.sdnc.SdncRequest;
47 import org.onap.policy.sdnc.SdncResponse;
48 import org.onap.policy.sdnc.SdncResponseOutput;
49 import org.onap.policy.simulators.Util;
50 import org.powermock.reflect.Whitebox;
53 * Superclass for various operator tests.
55 public abstract class BasicSdncOperation extends BasicHttpOperation {
57 * Fields to be ignored when comparing requests with JSON.
59 protected static final String[] IGNORE_FIELDS = {"svc-request-id"};
61 protected SdncResponse response;
64 * Constructs the object using a default actor and operation name.
66 public BasicSdncOperation() {
71 * Constructs the object.
73 * @param actor actor name
74 * @param operation operation name
76 public BasicSdncOperation(String actor, String operation) {
77 super(actor, operation);
81 * Starts the simulator.
83 protected static void initBeforeClass() throws Exception {
86 BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("restconf/operations/")
87 .hostname("localhost").managed(true).port(Util.SDNCSIM_SERVER_PORT).build();
88 HttpClientFactoryInstance.getClientFactory().build(clientParams);
91 protected static void destroyAfterClass() {
92 HttpClientFactoryInstance.getClientFactory().destroy();
93 HttpServletServerFactoryInstance.getServerFactory().destroy();
97 * Initializes mocks and sets up.
99 public void setUp() throws Exception {
102 response = new SdncResponse();
104 SdncResponseOutput output = new SdncResponseOutput();
105 response.setResponseOutput(output);
106 output.setResponseCode("200");
108 when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
112 * Runs the operation and verifies that the response is successful.
114 * @param operation operation to run
115 * @return the request that was posted
117 protected SdncRequest verifyOperation(SdncOperation operation)
118 throws InterruptedException, ExecutionException, TimeoutException, CoderException {
120 CompletableFuture<OperationOutcome> future2 = operation.start();
121 executor.runAll(100);
122 assertFalse(future2.isDone());
124 verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
125 callbackCaptor.getValue().completed(rawResponse);
127 executor.runAll(100);
128 assertTrue(future2.isDone());
130 outcome = future2.get();
131 assertEquals(OperationResult.SUCCESS, outcome.getResult());
132 assertTrue(outcome.getResponse() instanceof SdncResponse);
134 assertNotNull(outcome.getSubRequestId());
136 String reqText = requestCaptor.getValue().getEntity();
138 return coder.decode(reqText, SdncRequest.class);
142 * Verifies that an exception is thrown if a field is missing from the enrichment
145 * @param fieldName name of the field to be removed from the enrichment data
146 * @param expectedText text expected in the exception message
148 protected void verifyMissing(String fieldName, String expectedText,
149 OperationMaker<HttpConfig, SdncOperation> maker) {
152 enrichment.remove(fieldName);
154 SdncOperation oper = maker.apply(params, config);
156 assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
157 .withMessageContaining("missing").withMessageContaining(expectedText);
161 protected abstract Map<String, String> makeEnrichment();