Make Actors event-agnostic
[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.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;
28
29 import java.util.Map;
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;
50
51 /**
52  * Superclass for various operator tests.
53  */
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";
64
65     protected Response response;
66     protected GenericVnf genvnf;
67
68     /**
69      * Constructs the object using a default actor and operation name.
70      */
71     public BasicAppcOperation() {
72         this.coder = new StandardCoderInstantAsMillis();
73     }
74
75     /**
76      * Constructs the object.
77      *
78      * @param actor actor name
79      * @param operation operation name
80      */
81     public BasicAppcOperation(String actor, String operation) {
82         super(actor, operation);
83         this.coder = new StandardCoderInstantAsMillis();
84     }
85
86     /**
87      * Initializes mocks and sets up.
88      */
89     public void setUp() throws Exception {
90         super.setUpBasic();
91
92         response = new Response();
93
94         ResponseStatus status = new ResponseStatus();
95         response.setStatus(status);
96         status.setCode(ResponseCode.SUCCESS.getValue());
97         status.setDescription(MY_DESCRIPTION);
98
99         genvnf = new GenericVnf();
100         genvnf.setVnfId(MY_VNF);
101     }
102
103     public void tearDown() {
104         super.tearDownBasic();
105     }
106
107     @Override
108     protected TopicServer<Request> makeServer(TopicSink sink, TopicSource source) {
109         return new AppcLegacyTopicServer(sink, source);
110     }
111
112     /**
113      * Runs the operation and verifies that the response is successful.
114      *
115      * @param operation operation to run
116      */
117     protected void verifyOperation(AppcOperation operation)
118                     throws InterruptedException, ExecutionException, TimeoutException {
119
120         CompletableFuture<OperationOutcome> future2 = operation.start();
121         executor.runAll(100);
122         assertFalse(future2.isDone());
123
124         verify(forwarder).register(any(), listenerCaptor.capture());
125         provideResponse(listenerCaptor.getValue(), ResponseCode.SUCCESS.getValue(), MY_DESCRIPTION);
126
127         executor.runAll(100);
128         assertTrue(future2.isDone());
129
130         outcome = future2.get();
131         assertEquals(OperationResult.SUCCESS, outcome.getResult());
132         assertEquals(MY_DESCRIPTION, outcome.getMessage());
133     }
134
135     @Override
136     protected void makeContext() {
137         super.makeContext();
138
139         Map<String, String> entities = Map.of(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
140
141         params = params.toBuilder().targetEntityIds(entities).build();
142     }
143
144     /**
145      * Provides a response to the listener.
146      *
147      * @param listener listener to which to provide the response
148      * @param code response code
149      * @param description response description
150      */
151     protected void provideResponse(BiConsumer<String, StandardCoderObject> listener, int code, String description) {
152         Response response = new Response();
153
154         ResponseStatus status = new ResponseStatus();
155         response.setStatus(status);
156         status.setCode(code);
157         status.setDescription(description);
158
159         provideResponse(listener, Util.translate("", response, String.class));
160     }
161
162     @Override
163     protected Map<String, Object> makePayload() {
164         return Map.of(KEY1, VALUE1, KEY2, VALUE2);
165     }
166 }