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