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