Update new SDNR actor with v2.0 structures
[policy/models.git] / models-interactions / model-actors / actor.sdnr / src / test / java / org / onap / policy / controlloop / actor / sdnr / BasicSdnrOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.sdnr;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.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.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.Util;
38 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
40 import org.onap.policy.controlloop.policy.PolicyResult;
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.powermock.reflect.Whitebox;
47
48 public abstract class BasicSdnrOperation extends BasicBidirectionalTopicOperation {
49
50     protected PciMessage response;
51
52     /**
53      * Constructs the object using a default actor and operation name.
54      */
55     public BasicSdnrOperation() {
56         super();
57     }
58
59     /**
60      * Constructs the object.
61      *
62      * @param actor actor name
63      * @param operation operation name
64      */
65     public BasicSdnrOperation(String actor, String operation) {
66         super(actor, operation);
67     }
68
69     /**
70      * Initializes mocks and sets up.
71      */
72     public void setUp() throws Exception {
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     /**
90      * Runs the operation and verifies that the response is successful.
91      *
92      * @param operation operation to run
93      */
94     protected void verifyOperation(SdnrOperation operation)
95                     throws InterruptedException, ExecutionException, TimeoutException {
96
97         CompletableFuture<OperationOutcome> future2 = operation.start();
98         executor.runAll(100);
99         assertFalse(future2.isDone());
100
101         verify(forwarder).register(any(), listenerCaptor.capture());
102         provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString());
103
104         executor.runAll(100);
105         assertTrue(future2.isDone());
106
107         outcome = future2.get();
108         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
109     }
110
111     /**
112      * Verifies that an exception is thrown if a field is missing from the enrichment
113      * data.
114      *
115      * @param fieldName name of the field to be removed from the enrichment data
116      * @param expectedText text expected in the exception message
117      */
118     protected void verifyMissing(String fieldName, String expectedText,
119                     OperationMaker<BidirectionalTopicConfig, SdnrOperation> maker) {
120
121         makeContext();
122         enrichment.remove(fieldName);
123
124         SdnrOperation oper = maker.apply(params, config);
125
126         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
127                         .withMessageContaining("missing").withMessageContaining(expectedText);
128     }
129
130     /**
131      * Provides a response to the listener.
132      *
133      * @param listener listener to which to provide the response
134      * @param code response code
135      * @param description response description
136      */
137     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
138         PciResponse response = new PciResponse();
139
140         Status status = new Status();
141         response.setStatus(status);
142         status.setCode(code);
143
144         provideResponse(listener, Util.translate("", response, String.class));
145     }
146 }