0aea35f3798ab520ce196dd59984f85db2c72edb
[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.PciResponse;
42 import org.onap.policy.sdnr.PciResponseWrapper;
43 import org.onap.policy.sdnr.Status;
44 import org.onap.policy.sdnr.util.StatusCodeEnum;
45 import org.powermock.reflect.Whitebox;
46
47 public abstract class BasicSdnrOperation extends BasicBidirectionalTopicOperation {
48
49     protected PciResponseWrapper response;
50
51     /**
52      * Constructs the object using a default actor and operation name.
53      */
54     public BasicSdnrOperation() {
55         super();
56     }
57
58     /**
59      * Constructs the object.
60      *
61      * @param actor actor name
62      * @param operation operation name
63      */
64     public BasicSdnrOperation(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 PciResponseWrapper();
75
76         PciResponse body = new PciResponse();
77         Status status = new Status();
78         status.setCode(100);
79         status.setValue(StatusCodeEnum.SUCCESS.toString());
80         body.setStatus(status);
81         response.setBody(body);
82     }
83
84     /**
85      * Runs the operation and verifies that the response is successful.
86      *
87      * @param operation operation to run
88      */
89     protected void verifyOperation(SdnrOperation operation)
90                     throws InterruptedException, ExecutionException, TimeoutException {
91
92         CompletableFuture<OperationOutcome> future2 = operation.start();
93         executor.runAll(100);
94         assertFalse(future2.isDone());
95
96         verify(forwarder).register(any(), listenerCaptor.capture());
97         provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString());
98
99         executor.runAll(100);
100         assertTrue(future2.isDone());
101
102         outcome = future2.get();
103         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
104     }
105
106     /**
107      * Verifies that an exception is thrown if a field is missing from the enrichment
108      * data.
109      *
110      * @param fieldName name of the field to be removed from the enrichment data
111      * @param expectedText text expected in the exception message
112      */
113     protected void verifyMissing(String fieldName, String expectedText,
114                     OperationMaker<BidirectionalTopicConfig, SdnrOperation> maker) {
115
116         makeContext();
117         enrichment.remove(fieldName);
118
119         SdnrOperation oper = maker.apply(params, config);
120
121         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
122                         .withMessageContaining("missing").withMessageContaining(expectedText);
123     }
124
125     /**
126      * Provides a response to the listener.
127      *
128      * @param listener listener to which to provide the response
129      * @param code response code
130      * @param description response description
131      */
132     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
133         PciResponse response = new PciResponse();
134
135         Status status = new Status();
136         response.setStatus(status);
137         status.setCode(code);
138
139         provideResponse(listener, Util.translate("", response, String.class));
140     }
141 }