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.sdnr;
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.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.verify;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeoutException;
33 import java.util.function.BiConsumer;
34 import org.onap.policy.common.endpoints.event.comm.TopicSink;
35 import org.onap.policy.common.endpoints.event.comm.TopicSource;
36 import org.onap.policy.common.utils.coder.StandardCoderObject;
37 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
40 import org.onap.policy.controlloop.actorserviceprovider.Util;
41 import org.onap.policy.sdnr.PciBody;
42 import org.onap.policy.sdnr.PciMessage;
43 import org.onap.policy.sdnr.PciResponse;
44 import org.onap.policy.sdnr.Status;
45 import org.onap.policy.sdnr.util.StatusCodeEnum;
46 import org.onap.policy.simulators.SdnrTopicServer;
47 import org.onap.policy.simulators.TopicServer;
49 abstract class BasicSdnrOperation extends BasicBidirectionalTopicOperation<PciMessage> {
51 protected PciMessage response;
54 * Constructs the object using a default actor and operation name.
56 BasicSdnrOperation() {
61 * Constructs the object.
63 * @param actor actor name
64 * @param operation operation name
66 BasicSdnrOperation(String actor, String operation) {
67 super(actor, operation);
71 * Initializes mocks and sets up.
73 void setUp() throws Exception {
76 response = new PciMessage();
78 PciBody body = new PciBody();
79 response.setBody(body);
81 PciResponse output = new PciResponse();
82 body.setOutput(output);
84 Status status = new Status();
85 output.setStatus(status);
87 status.setValue(StatusCodeEnum.SUCCESS.toString());
91 super.tearDownBasic();
95 protected TopicServer<PciMessage> makeServer(TopicSink sink, TopicSource source) {
96 return new SdnrTopicServer(sink, source);
100 * Runs the operation and verifies that the response is successful.
102 * @param operation operation to run
104 protected void verifyOperation(SdnrOperation operation)
105 throws InterruptedException, ExecutionException, TimeoutException {
107 CompletableFuture<OperationOutcome> future2 = operation.start();
108 executor.runAll(100);
109 assertFalse(future2.isDone());
111 verify(forwarder).register(any(), listenerCaptor.capture());
112 provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString());
114 executor.runAll(100);
115 assertTrue(future2.isDone());
117 outcome = future2.get();
118 assertEquals(OperationResult.SUCCESS, outcome.getResult());
122 * Provides a response to the listener.
124 * @param listener listener to which to provide the response
125 * @param code response code
126 * @param description response description
128 protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
129 PciResponse response = new PciResponse();
131 Status status = new Status();
132 response.setStatus(status);
133 status.setCode(code);
135 provideResponse(listener, Util.translate("", response, String.class));