2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.sdnc;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.verify;
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.TimeoutException;
34 import org.mockito.Mockito;
35 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
36 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
37 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
38 import org.onap.policy.common.utils.coder.CoderException;
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.OperationResult;
43 import org.onap.policy.sdnc.SdncRequest;
44 import org.onap.policy.sdnc.SdncResponse;
45 import org.onap.policy.sdnc.SdncResponseOutput;
46 import org.onap.policy.simulators.Util;
49 * Superclass for various operator tests.
51 abstract class BasicSdncOperation extends BasicHttpOperation {
53 * Fields to be ignored when comparing requests with JSON.
55 protected static final String[] IGNORE_FIELDS = {"svc-request-id"};
57 protected SdncResponse response;
60 * Constructs the object using a default actor and operation name.
62 BasicSdncOperation() {
67 * Constructs the object.
69 * @param actor actor name
70 * @param operation operation name
72 BasicSdncOperation(String actor, String operation) {
73 super(actor, operation);
77 * Starts the simulator.
79 protected static void initBeforeClass() throws Exception {
80 var testServer = Util.buildSdncSim();
81 assertNotNull(testServer);
83 BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("restconf/operations/")
84 .hostname("localhost").managed(true).port(Util.SDNCSIM_SERVER_PORT).build();
85 HttpClientFactoryInstance.getClientFactory().build(clientParams);
88 protected static void destroyAfterClass() {
89 HttpClientFactoryInstance.getClientFactory().destroy();
90 HttpServletServerFactoryInstance.getServerFactory().destroy();
94 * Initializes mocks and sets up.
96 void setUp() throws Exception {
99 response = new SdncResponse();
101 SdncResponseOutput output = new SdncResponseOutput();
102 response.setResponseOutput(output);
103 output.setResponseCode("200");
105 Mockito.lenient().when(rawResponse.readEntity(String.class)).thenReturn(new StandardCoder().encode(response));
109 * Runs the operation and verifies that the response is successful.
111 * @param operation operation to run
112 * @return the request that was posted
114 protected SdncRequest verifyOperation(SdncOperation operation)
115 throws InterruptedException, ExecutionException, TimeoutException, CoderException {
117 CompletableFuture<OperationOutcome> future2 = operation.start();
118 executor.runAll(100);
119 assertFalse(future2.isDone());
121 verify(client).post(callbackCaptor.capture(), any(), requestCaptor.capture(), any());
122 callbackCaptor.getValue().completed(rawResponse);
124 executor.runAll(100);
125 assertTrue(future2.isDone());
127 outcome = future2.get();
128 assertEquals(OperationResult.SUCCESS, outcome.getResult());
129 assertTrue(outcome.getResponse() instanceof SdncResponse);
131 assertNotNull(outcome.getSubRequestId());
133 String reqText = requestCaptor.getValue().getEntity();
135 return coder.decode(reqText, SdncRequest.class);