6a254a4f17994424ec8cc551021289039240f948
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2022 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.a1p;
23
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;
29
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.ExecutionException;
32 import java.util.function.BiConsumer;
33 import org.onap.policy.common.endpoints.event.comm.TopicSink;
34 import org.onap.policy.common.endpoints.event.comm.TopicSource;
35 import org.onap.policy.common.utils.coder.StandardCoderObject;
36 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
39 import org.onap.policy.controlloop.actorserviceprovider.Util;
40 import org.onap.policy.sdnr.PciBody;
41 import org.onap.policy.sdnr.PciMessage;
42 import org.onap.policy.sdnr.PciResponse;
43 import org.onap.policy.sdnr.Status;
44 import org.onap.policy.sdnr.util.StatusCodeEnum;
45 import org.onap.policy.simulators.SdnrTopicServer;
46 import org.onap.policy.simulators.TopicServer;
47
48 public abstract class BasicA1pOperation extends BasicBidirectionalTopicOperation<PciMessage> {
49
50     protected PciMessage response;
51
52     /**
53      * Constructs the object using a default actor and operation name.
54      */
55     public BasicA1pOperation() {
56         super();
57     }
58
59     /**
60      * Constructs the object.
61      *
62      * @param actor actor name
63      * @param operation operation name
64      */
65     public BasicA1pOperation(String actor, String operation) {
66         super(actor, operation);
67     }
68
69     /**
70      * Initializes mocks and sets up.
71      */
72     void setUp() {
73         super.setUpBasic();
74
75         response = new PciMessage();
76
77         PciBody body = new PciBody();
78         response.setBody(body);
79
80         PciResponse output = new PciResponse();
81         body.setOutput(output);
82
83         Status status = new Status();
84         output.setStatus(status);
85         status.setCode(100);
86         status.setValue(StatusCodeEnum.SUCCESS.toString());
87     }
88
89     void tearDown() {
90         super.tearDownBasic();
91     }
92
93     @Override
94     protected TopicServer<PciMessage> makeServer(TopicSink sink, TopicSource source) {
95         return new SdnrTopicServer(sink, source);
96     }
97
98     /**
99      * Runs the operation and verifies that the response is successful.
100      *
101      * @param operation operation to run
102      */
103     protected void verifyOperation(A1pOperation operation)
104                     throws InterruptedException, ExecutionException {
105
106         CompletableFuture<OperationOutcome> future2 = operation.start();
107         executor.runAll(100);
108         assertFalse(future2.isDone());
109
110         verify(forwarder).register(any(), listenerCaptor.capture());
111         provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString());
112
113         executor.runAll(100);
114         assertTrue(future2.isDone());
115
116         outcome = future2.get();
117         assertEquals(OperationResult.SUCCESS, outcome.getResult());
118     }
119
120     /**
121      * Provides a response to the listener.
122      *
123      * @param listener listener to which to provide the response
124      * @param code response code
125      * @param description response description
126      */
127     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
128         PciResponse response = new PciResponse();
129
130         Status status = new Status();
131         response.setStatus(status);
132         status.setCode(code);
133
134         provideResponse(listener, Util.translate("", response, String.class));
135     }
136 }