2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 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.a1p;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.verify;
29 import java.util.concurrent.CompletableFuture;
30 import java.util.concurrent.ExecutionException;
31 import java.util.function.BiConsumer;
32 import org.onap.policy.common.endpoints.event.comm.TopicSink;
33 import org.onap.policy.common.endpoints.event.comm.TopicSource;
34 import org.onap.policy.common.utils.coder.StandardCoderObject;
35 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
38 import org.onap.policy.controlloop.actorserviceprovider.Util;
39 import org.onap.policy.sdnr.PciBody;
40 import org.onap.policy.sdnr.PciMessage;
41 import org.onap.policy.sdnr.PciResponse;
42 import org.onap.policy.sdnr.Status;
43 import org.onap.policy.sdnr.util.StatusCodeEnum;
44 import org.onap.policy.simulators.SdnrTopicServer;
45 import org.onap.policy.simulators.TopicServer;
47 public abstract class BasicA1pOperation extends BasicBidirectionalTopicOperation<PciMessage> {
49 protected PciMessage response;
52 * Constructs the object using a default actor and operation name.
54 public BasicA1pOperation() {
59 * Constructs the object.
61 * @param actor actor name
62 * @param operation operation name
64 public BasicA1pOperation(String actor, String operation) {
65 super(actor, operation);
69 * Initializes mocks and sets up.
71 public void setUp() throws Exception {
74 response = new PciMessage();
76 PciBody body = new PciBody();
77 response.setBody(body);
79 PciResponse output = new PciResponse();
80 body.setOutput(output);
82 Status status = new Status();
83 output.setStatus(status);
85 status.setValue(StatusCodeEnum.SUCCESS.toString());
88 public void tearDown() {
89 super.tearDownBasic();
93 protected TopicServer<PciMessage> makeServer(TopicSink sink, TopicSource source) {
94 return new SdnrTopicServer(sink, source);
98 * Runs the operation and verifies that the response is successful.
100 * @param operation operation to run
102 protected void verifyOperation(A1pOperation operation)
103 throws InterruptedException, ExecutionException {
105 CompletableFuture<OperationOutcome> future2 = operation.start();
106 executor.runAll(100);
107 assertFalse(future2.isDone());
109 verify(forwarder).register(any(), listenerCaptor.capture());
110 provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString());
112 executor.runAll(100);
113 assertTrue(future2.isDone());
115 outcome = future2.get();
116 assertEquals(OperationResult.SUCCESS, outcome.getResult());
120 * Provides a response to the listener.
122 * @param listener listener to which to provide the response
123 * @param code response code
124 * @param description response description
126 protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
127 PciResponse response = new PciResponse();
129 Status status = new Status();
130 response.setStatus(status);
131 status.setCode(code);
133 provideResponse(listener, Util.translate("", response, String.class));