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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.verify;
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.aai.domain.yang.GenericVnf;
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.parameters.ControlLoopOperationParams;
48 import org.onap.policy.simulators.AppcLegacyTopicServer;
49 import org.onap.policy.simulators.TopicServer;
52 * Superclass for various operator tests.
54 public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperation<Request> {
55 protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "TimeStamp"};
56 protected static final String MY_DESCRIPTION = "my-description";
57 protected static final String MY_VNF = "my-vnf";
58 protected static final String KEY1 = "my-key-A";
59 protected static final String KEY2 = "my-key-B";
60 protected static final String KEY3 = "my-key-C";
61 protected static final String VALUE1 = "{\"input\":\"hello\"}";
62 protected static final String VALUE2 = "{\"output\":\"world\"}";
63 protected static final String RESOURCE_ID = "my-resource";
65 protected Response response;
66 protected GenericVnf genvnf;
69 * Constructs the object using a default actor and operation name.
71 public BasicAppcOperation() {
72 this.coder = new StandardCoderInstantAsMillis();
76 * Constructs the object.
78 * @param actor actor name
79 * @param operation operation name
81 public BasicAppcOperation(String actor, String operation) {
82 super(actor, operation);
83 this.coder = new StandardCoderInstantAsMillis();
87 * Initializes mocks and sets up.
89 public void setUp() throws Exception {
92 response = new Response();
94 ResponseStatus status = new ResponseStatus();
95 response.setStatus(status);
96 status.setCode(ResponseCode.SUCCESS.getValue());
97 status.setDescription(MY_DESCRIPTION);
99 genvnf = new GenericVnf();
100 genvnf.setVnfId(MY_VNF);
103 public void tearDown() {
104 super.tearDownBasic();
108 protected TopicServer<Request> makeServer(TopicSink sink, TopicSource source) {
109 return new AppcLegacyTopicServer(sink, source);
113 * Runs the operation and verifies that the response is successful.
115 * @param operation operation to run
117 protected void verifyOperation(AppcOperation operation)
118 throws InterruptedException, ExecutionException, TimeoutException {
120 CompletableFuture<OperationOutcome> future2 = operation.start();
121 executor.runAll(100);
122 assertFalse(future2.isDone());
124 verify(forwarder).register(any(), listenerCaptor.capture());
125 provideResponse(listenerCaptor.getValue(), ResponseCode.SUCCESS.getValue(), MY_DESCRIPTION);
127 executor.runAll(100);
128 assertTrue(future2.isDone());
130 outcome = future2.get();
131 assertEquals(OperationResult.SUCCESS, outcome.getResult());
132 assertEquals(MY_DESCRIPTION, outcome.getMessage());
136 protected void makeContext() {
139 Map<String, String> entities = Map.of(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
141 params = params.toBuilder().targetEntityIds(entities).build();
145 * Provides a response to the listener.
147 * @param listener listener to which to provide the response
148 * @param code response code
149 * @param description response description
151 protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
152 Response response = new Response();
154 ResponseStatus status = new ResponseStatus();
155 response.setStatus(status);
156 status.setCode(code);
157 status.setDescription(description);
159 provideResponse(listener, Util.translate("", response, String.class));
163 protected Map<String, Object> makePayload() {
164 return Map.of(KEY1, VALUE1, KEY2, VALUE2);