Remove Target and TargetType
[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.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.controlloop.actorserviceprovider.impl.OperationMaker;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
43 import org.onap.policy.sdnr.PciBody;
44 import org.onap.policy.sdnr.PciMessage;
45 import org.onap.policy.sdnr.PciResponse;
46 import org.onap.policy.sdnr.Status;
47 import org.onap.policy.sdnr.util.StatusCodeEnum;
48 import org.onap.policy.simulators.SdnrTopicServer;
49 import org.onap.policy.simulators.TopicServer;
50 import org.powermock.reflect.Whitebox;
51
52 public abstract class BasicSdnrOperation extends BasicBidirectionalTopicOperation<PciMessage> {
53
54     protected PciMessage response;
55
56     /**
57      * Constructs the object using a default actor and operation name.
58      */
59     public BasicSdnrOperation() {
60         super();
61     }
62
63     /**
64      * Constructs the object.
65      *
66      * @param actor actor name
67      * @param operation operation name
68      */
69     public BasicSdnrOperation(String actor, String operation) {
70         super(actor, operation);
71     }
72
73     /**
74      * Initializes mocks and sets up.
75      */
76     public void setUp() throws Exception {
77         super.setUpBasic();
78
79         response = new PciMessage();
80
81         PciBody body = new PciBody();
82         response.setBody(body);
83
84         PciResponse output = new PciResponse();
85         body.setOutput(output);
86
87         Status status = new Status();
88         output.setStatus(status);
89         status.setCode(100);
90         status.setValue(StatusCodeEnum.SUCCESS.toString());
91     }
92
93     public void tearDown() {
94         super.tearDownBasic();
95     }
96
97     @Override
98     protected TopicServer<PciMessage> makeServer(TopicSink sink, TopicSource source) {
99         return new SdnrTopicServer(sink, source);
100     }
101
102     /**
103      * Runs the operation and verifies that the response is successful.
104      *
105      * @param operation operation to run
106      */
107     protected void verifyOperation(SdnrOperation operation)
108                     throws InterruptedException, ExecutionException, TimeoutException {
109
110         CompletableFuture<OperationOutcome> future2 = operation.start();
111         executor.runAll(100);
112         assertFalse(future2.isDone());
113
114         verify(forwarder).register(any(), listenerCaptor.capture());
115         provideResponse(listenerCaptor.getValue(), StatusCodeEnum.SUCCESS.toString());
116
117         executor.runAll(100);
118         assertTrue(future2.isDone());
119
120         outcome = future2.get();
121         assertEquals(OperationResult.SUCCESS, outcome.getResult());
122     }
123
124     /**
125      * Verifies that an exception is thrown if a field is missing from the enrichment
126      * data.
127      *
128      * @param fieldName name of the field to be removed from the enrichment data
129      * @param expectedText text expected in the exception message
130      */
131     protected void verifyMissing(String fieldName, String expectedText,
132                     OperationMaker<BidirectionalTopicConfig, SdnrOperation> maker) {
133
134         makeContext();
135         enrichment.remove(fieldName);
136
137         SdnrOperation oper = maker.apply(params, config);
138
139         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
140                         .withMessageContaining("missing").withMessageContaining(expectedText);
141     }
142
143     /**
144      * Provides a response to the listener.
145      *
146      * @param listener listener to which to provide the response
147      * @param code response code
148      * @param description response description
149      */
150     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
151         PciResponse response = new PciResponse();
152
153         Status status = new Status();
154         response.setStatus(status);
155         status.setCode(code);
156
157         provideResponse(listener, Util.translate("", response, String.class));
158     }
159 }