Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / BasicAppcOperation.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.appc;
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.Map;
31 import java.util.concurrent.CompletableFuture;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.TimeoutException;
34 import java.util.function.BiConsumer;
35 import org.onap.policy.appc.Request;
36 import org.onap.policy.appc.Response;
37 import org.onap.policy.appc.ResponseCode;
38 import org.onap.policy.appc.ResponseStatus;
39 import org.onap.policy.common.endpoints.event.comm.TopicSink;
40 import org.onap.policy.common.endpoints.event.comm.TopicSource;
41 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
42 import org.onap.policy.common.utils.coder.StandardCoderObject;
43 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
46 import org.onap.policy.controlloop.actorserviceprovider.Util;
47 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
50 import org.onap.policy.simulators.AppcLegacyTopicServer;
51 import org.onap.policy.simulators.TopicServer;
52 import org.powermock.reflect.Whitebox;
53
54 /**
55  * Superclass for various operator tests.
56  */
57 public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperation<Request> {
58     protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "TimeStamp"};
59     protected static final String MY_DESCRIPTION = "my-description";
60     protected static final String MY_VNF = "my-vnf";
61     protected static final String KEY1 = "my-key-A";
62     protected static final String KEY2 = "my-key-B";
63     protected static final String KEY3 = "my-key-C";
64     protected static final String VALUE1 = "{\"input\":\"hello\"}";
65     protected static final String VALUE2 = "{\"output\":\"world\"}";
66     protected static final String RESOURCE_ID = "my-resource";
67
68     protected Response response;
69
70     /**
71      * Constructs the object using a default actor and operation name.
72      */
73     public BasicAppcOperation() {
74         this.coder = new StandardCoderInstantAsMillis();
75     }
76
77     /**
78      * Constructs the object.
79      *
80      * @param actor actor name
81      * @param operation operation name
82      */
83     public BasicAppcOperation(String actor, String operation) {
84         super(actor, operation);
85         this.coder = new StandardCoderInstantAsMillis();
86     }
87
88     /**
89      * Initializes mocks and sets up.
90      */
91     public void setUp() throws Exception {
92         super.setUpBasic();
93
94         response = new Response();
95
96         ResponseStatus status = new ResponseStatus();
97         response.setStatus(status);
98         status.setCode(ResponseCode.SUCCESS.getValue());
99         status.setDescription(MY_DESCRIPTION);
100     }
101
102     public void tearDown() {
103         super.tearDownBasic();
104     }
105
106     @Override
107     protected TopicServer<Request> makeServer(TopicSink sink, TopicSource source) {
108         return new AppcLegacyTopicServer(sink, source);
109     }
110
111     /**
112      * Runs the operation and verifies that the response is successful.
113      *
114      * @param operation operation to run
115      */
116     protected void verifyOperation(AppcOperation operation)
117                     throws InterruptedException, ExecutionException, TimeoutException {
118
119         CompletableFuture<OperationOutcome> future2 = operation.start();
120         executor.runAll(100);
121         assertFalse(future2.isDone());
122
123         verify(forwarder).register(any(), listenerCaptor.capture());
124         provideResponse(listenerCaptor.getValue(), ResponseCode.SUCCESS.getValue(), MY_DESCRIPTION);
125
126         executor.runAll(100);
127         assertTrue(future2.isDone());
128
129         outcome = future2.get();
130         assertEquals(OperationResult.SUCCESS, outcome.getResult());
131         assertEquals(MY_DESCRIPTION, outcome.getMessage());
132     }
133
134     /**
135      * Verifies that an exception is thrown if a field is missing from the enrichment
136      * data.
137      *
138      * @param fieldName name of the field to be removed from the enrichment data
139      * @param expectedText text expected in the exception message
140      */
141     protected void verifyMissing(String fieldName, String expectedText,
142                     OperationMaker<BidirectionalTopicConfig, AppcOperation> maker) {
143
144         makeContext();
145         enrichment.remove(fieldName);
146
147         AppcOperation oper = maker.apply(params, config);
148
149         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
150                         .withMessageContaining("missing").withMessageContaining(expectedText);
151     }
152
153     @Override
154     protected void makeContext() {
155         super.makeContext();
156
157         Map<String, String> entities = Map.of(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
158
159         params = params.toBuilder().targetEntityIds(entities).build();
160     }
161
162     /**
163      * Provides a response to the listener.
164      *
165      * @param listener listener to which to provide the response
166      * @param code response code
167      * @param description response description
168      */
169     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
170         Response response = new Response();
171
172         ResponseStatus status = new ResponseStatus();
173         response.setStatus(status);
174         status.setCode(code);
175         status.setDescription(description);
176
177         provideResponse(listener, Util.translate("", response, String.class));
178     }
179
180     @Override
181     protected Map<String, Object> makePayload() {
182         return Map.of(KEY1, VALUE1, KEY2, VALUE2);
183     }
184 }