2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.appc;
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;
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.Response;
36 import org.onap.policy.appc.ResponseCode;
37 import org.onap.policy.appc.ResponseStatus;
38 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
39 import org.onap.policy.common.utils.coder.StandardCoderObject;
40 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
42 import org.onap.policy.controlloop.actorserviceprovider.Util;
43 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
45 import org.onap.policy.controlloop.policy.PolicyResult;
46 import org.onap.policy.controlloop.policy.Target;
47 import org.powermock.reflect.Whitebox;
50 * Superclass for various operator tests.
52 public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperation {
53 protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "TimeStamp"};
54 protected static final String MY_DESCRIPTION = "my-description";
55 protected static final String MY_VNF = "my-vnf";
56 protected static final String KEY1 = "my-key-A";
57 protected static final String KEY2 = "my-key-B";
58 protected static final String KEY3 = "my-key-C";
59 protected static final String VALUE1 = "{\"input\":\"hello\"}";
60 protected static final String VALUE2 = "{\"output\":\"world\"}";
61 protected static final String RESOURCE_ID = "my-resource";
63 protected Response response;
66 * Constructs the object using a default actor and operation name.
68 public BasicAppcOperation() {
69 this.coder = new StandardCoderInstantAsMillis();
73 * Constructs the object.
75 * @param actor actor name
76 * @param operation operation name
78 public BasicAppcOperation(String actor, String operation) {
79 super(actor, operation);
80 this.coder = new StandardCoderInstantAsMillis();
84 * Initializes mocks and sets up.
86 public void setUp() throws Exception {
89 response = new Response();
91 ResponseStatus status = new ResponseStatus();
92 response.setStatus(status);
93 status.setCode(ResponseCode.SUCCESS.getValue());
94 status.setDescription(MY_DESCRIPTION);
98 * Runs the operation and verifies that the response is successful.
100 * @param operation operation to run
102 protected void verifyOperation(AppcOperation operation)
103 throws InterruptedException, ExecutionException, TimeoutException {
105 CompletableFuture<OperationOutcome> future2 = operation.start();
106 executor.runAll(100);
107 assertFalse(future2.isDone());
109 verify(forwarder).register(any(), listenerCaptor.capture());
110 provideResponse(listenerCaptor.getValue(), ResponseCode.SUCCESS.getValue(), MY_DESCRIPTION);
112 executor.runAll(100);
113 assertTrue(future2.isDone());
115 outcome = future2.get();
116 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
117 assertEquals(MY_DESCRIPTION, outcome.getMessage());
121 * Verifies that an exception is thrown if a field is missing from the enrichment
124 * @param fieldName name of the field to be removed from the enrichment data
125 * @param expectedText text expected in the exception message
127 protected void verifyMissing(String fieldName, String expectedText,
128 OperationMaker<BidirectionalTopicConfig, AppcOperation> maker) {
131 enrichment.remove(fieldName);
133 AppcOperation oper = maker.apply(params, config);
135 assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
136 .withMessageContaining("missing").withMessageContaining(expectedText);
140 protected void makeContext() {
143 Target target = new Target();
144 target.setResourceID(RESOURCE_ID);
146 params = params.toBuilder().target(target).build();
150 * Provides a response to the listener.
152 * @param listener listener to which to provide the response
153 * @param code response code
154 * @param description response description
156 protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
157 Response response = new Response();
159 ResponseStatus status = new ResponseStatus();
160 response.setStatus(status);
161 status.setCode(code);
162 status.setDescription(description);
164 provideResponse(listener, Util.translate("", response, String.class));
168 protected Map<String, Object> makePayload() {
169 return Map.of(KEY1, VALUE1, KEY2, VALUE2);