Create A1P actor to talk to A1 PMS.
[policy/models.git] / models-interactions / model-actors / actor.a1p / src / test / java / org / onap / policy / controlloop / actor / a1p / BasicA1pOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.a1p;
22
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;
28
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;
46
47 public abstract class BasicA1pOperation extends BasicBidirectionalTopicOperation<PciMessage> {
48
49     protected PciMessage response;
50
51     /**
52      * Constructs the object using a default actor and operation name.
53      */
54     public BasicA1pOperation() {
55         super();
56     }
57
58     /**
59      * Constructs the object.
60      *
61      * @param actor actor name
62      * @param operation operation name
63      */
64     public BasicA1pOperation(String actor, String operation) {
65         super(actor, operation);
66     }
67
68     /**
69      * Initializes mocks and sets up.
70      */
71     public void setUp() throws Exception {
72         super.setUpBasic();
73
74         response = new PciMessage();
75
76         PciBody body = new PciBody();
77         response.setBody(body);
78
79         PciResponse output = new PciResponse();
80         body.setOutput(output);
81
82         Status status = new Status();
83         output.setStatus(status);
84         status.setCode(100);
85         status.setValue(StatusCodeEnum.SUCCESS.toString());
86     }
87
88     public void tearDown() {
89         super.tearDownBasic();
90     }
91
92     @Override
93     protected TopicServer<PciMessage> makeServer(TopicSink sink, TopicSource source) {
94         return new SdnrTopicServer(sink, source);
95     }
96
97     /**
98      * Runs the operation and verifies that the response is successful.
99      *
100      * @param operation operation to run
101      */
102     protected void verifyOperation(A1pOperation operation)
103                     throws InterruptedException, ExecutionException {
104
105         CompletableFuture<OperationOutcome> future2 = operation.start();
106         executor.runAll(100);
107         assertFalse(future2.isDone());
108
109         verify(forwarder).register(any(), listenerCaptor.capture());
110         provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString());
111
112         executor.runAll(100);
113         assertTrue(future2.isDone());
114
115         outcome = future2.get();
116         assertEquals(OperationResult.SUCCESS, outcome.getResult());
117     }
118
119     /**
120      * Provides a response to the listener.
121      *
122      * @param listener listener to which to provide the response
123      * @param code response code
124      * @param description response description
125      */
126     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
127         PciResponse response = new PciResponse();
128
129         Status status = new Status();
130         response.setStatus(status);
131         status.setCode(code);
132
133         provideResponse(listener, Util.translate("", response, String.class));
134     }
135 }