aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actor.appclcm/src/test/java/org/onap/policy/controlloop/actor/appclcm/AppcLcmOperationTest.java
blob: e933621e9906af40f2fc3452906b0b4f0546cd95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.controlloop.actor.appclcm;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.policy.appclcm.AppcLcmBody;
import org.onap.policy.appclcm.AppcLcmCommonHeader;
import org.onap.policy.appclcm.AppcLcmMessageWrapper;
import org.onap.policy.appclcm.AppcLcmOutput;
import org.onap.policy.appclcm.AppcLcmResponseStatus;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.TopicSource;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.controlloop.ControlLoopOperation;
import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.simulators.AppcLcmTopicServer;
import org.onap.policy.simulators.TopicServer;

@RunWith(MockitoJUnitRunner.class)
public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmMessageWrapper> {

    private static final String EXPECTED_EXCEPTION = "expected exception";
    private static final String PAYLOAD_KEY1 = "key-A";
    private static final String PAYLOAD_VALUE1 = "value-A";
    private static final String MY_MESSAGE = "my-message";
    protected static final String MY_VNF = "my-vnf";
    protected static final String RESOURCE_ID = "my-resource";
    private static final int SUCCESS_CODE = 400;

    private AppcLcmMessageWrapper response;
    private AppcLcmOperation oper;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        initBeforeClass(MY_SINK, MY_SOURCE);
    }

    @AfterClass
    public static void tearDownAfterClass() {
        destroyAfterClass();
    }

    /**
     * Sets up.
     */
    @Before
    public void setUp() {
        super.setUpBasic();

        response = makeResponse();

        oper = new AppcLcmOperation(params, config);
        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
    }

    @After
    public void tearDown() {
        super.tearDownBasic();
    }

    @Override
    protected TopicServer<AppcLcmMessageWrapper> makeServer(TopicSink sink, TopicSource source) {
        return new AppcLcmTopicServer(sink, source);
    }

    /**
     * Tests "success" case with simulator.
     */
    @Test
    public void testSuccess() throws Exception {
        BidirectionalTopicParams opParams =
                        BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
        config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcLcmOperation.SELECTOR_KEYS);

        params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();

        oper = new AppcLcmOperation(params, config);
        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);

        outcome = oper.start().get();
        // assertEquals(OperationResult.SUCCESS, outcome.getResult());
        // assertTrue(outcome.getResponse() instanceof AppcLcmMessageWrapper);
        assertNotNull(outcome);
    }

    @Test
    public void testConstructor() {
        assertEquals(DEFAULT_ACTOR, oper.getActorName());
        assertEquals(DEFAULT_OPERATION, oper.getName());
    }

    @Test
    public void testGetPropertyNames() {
        assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
    }

    @Test
    public void testMakeRequest() {
        oper.generateSubRequestId(2);
        String subreq = oper.getSubRequestId();
        assertNotNull(subreq);

        AppcLcmMessageWrapper request = oper.makeRequest(2);
        assertEquals("DefaultOperation", request.getBody().getInput().getAction());

        AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
        assertNotNull(header);
        assertEquals(params.getRequestId(), header.getRequestId());

        assertEquals(subreq, header.getSubRequestId());

        assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());

        request = oper.makeRequest(2);
        assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
    }

    /**
     * Tests makeRequest() when a property is missing.
     */
    @Test
    public void testMakeRequestMissingProperty() throws Exception {
        oper = new AppcLcmOperation(params, config);
        oper.generateSubRequestId(1);

        assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
                        .withMessageContaining("missing target entity");
    }

    @Test
    public void testConvertPayload() {
        // only builds a payload for ConfigModify
        params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
        oper = new AppcLcmOperation(params, config);
        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);

        oper.generateSubRequestId(2);
        AppcLcmMessageWrapper req = oper.makeRequest(2);
        assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());

        // coder exception
        oper = new AppcLcmOperation(params, config) {
            @Override
            protected Coder getCoder() {
                return new StandardCoder() {
                    @Override
                    public String encode(Object object) throws CoderException {
                        throw new CoderException(EXPECTED_EXCEPTION);
                    }
                };
            }
        };

        oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
        oper.generateSubRequestId(2);

        assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
                        .withMessageContaining("Cannot convert payload");
    }

    @Test
    public void testGetExpectedKeyValues() {
        oper.generateSubRequestId(2);
        AppcLcmMessageWrapper request = oper.makeRequest(2);
        assertEquals(List.of(request.getBody().getInput().getCommonHeader().getSubRequestId()),
                        oper.getExpectedKeyValues(50, request));
    }

    @Test
    public void testDetmStatus() {
        assertEquals(Status.SUCCESS, oper.detmStatus(null, response));

        // failure
        response.getBody().getOutput().getStatus().setCode(405);
        assertEquals(Status.FAILURE, oper.detmStatus(null, response));

        // error
        response.getBody().getOutput().getStatus().setCode(200);
        assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));

        // reject
        response.getBody().getOutput().getStatus().setCode(305);
        assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));

        // accepted
        response.getBody().getOutput().getStatus().setCode(100);
        assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));

        // other
        response.getBody().getOutput().getStatus().setCode(-1);
        assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));

        // null status
        response.getBody().getOutput().setStatus(null);
        assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
    }

    @Test
    public void testSetOutcome() {
        oper.setOutcome(outcome, OperationResult.SUCCESS, response);
        assertEquals(OperationResult.SUCCESS, outcome.getResult());
        assertEquals(MY_MESSAGE, outcome.getMessage());
        assertSame(response, outcome.getResponse());

        // failure
        oper.setOutcome(outcome, OperationResult.FAILURE, response);
        assertEquals(OperationResult.FAILURE, outcome.getResult());
        assertEquals(MY_MESSAGE, outcome.getMessage());
        assertSame(response, outcome.getResponse());

        // null message
        response.getBody().getOutput().getStatus().setMessage(null);
        oper.setOutcome(outcome, OperationResult.SUCCESS, response);
        assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
        assertSame(response, outcome.getResponse());

        // null status
        response.getBody().getOutput().setStatus(null);
        oper.setOutcome(outcome, OperationResult.SUCCESS, response);
        assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
        assertSame(response, outcome.getResponse());
    }

    @Test
    public void testGetStatus() {
        assertNotNull(oper.getStatus(response));

        // null status
        response.getBody().getOutput().setStatus(null);
        assertNull(oper.getStatus(response));

        // null outcome
        response.getBody().setOutput(null);
        assertNull(oper.getStatus(response));

        // null body
        response.setBody(null);
        assertNull(oper.getStatus(response));

        // null response
        assertNull(oper.getStatus(null));
    }

    @Test
    public void testOperationSupportsPayload() {
        // these should support a payload
        Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);

        for (String name : supported) {
            params = params.toBuilder().operation(name).build();
            oper = new AppcLcmOperation(params, config);
            assertTrue(name, oper.operationSupportsPayload());
        }

        // these should NOT support a payload
        Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
                        .collect(Collectors.toSet());

        for (String name : unsupported) {
            params = params.toBuilder().operation(name).build();
            oper = new AppcLcmOperation(params, config);
            assertFalse(name, oper.operationSupportsPayload());
        }

        // pick an operation that would ordinarily support payloads
        String sup = supported.iterator().next();

        // verify that it still supports payload
        params = params.toBuilder().operation(sup).build();
        oper = new AppcLcmOperation(params, config);
        assertTrue(oper.operationSupportsPayload());

        // try with empty payload
        params = params.toBuilder().payload(Map.of()).build();
        oper = new AppcLcmOperation(params, config);
        assertFalse(oper.operationSupportsPayload());

        // try with null payload
        params = params.toBuilder().payload(null).build();
        oper = new AppcLcmOperation(params, config);
        assertFalse(oper.operationSupportsPayload());
    }

    @Override
    protected void makeContext() {
        super.makeContext();

        Map<String, String> targetEntities = new HashMap<>();
        targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);

        params = params.toBuilder().targetEntityIds(targetEntities).build();
    }

    @Override
    protected Map<String, Object> makePayload() {
        return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
    }

    private AppcLcmMessageWrapper makeResponse() {
        AppcLcmMessageWrapper response = new AppcLcmMessageWrapper();

        AppcLcmBody body = new AppcLcmBody();
        response.setBody(body);

        AppcLcmOutput output = new AppcLcmOutput();
        body.setOutput(output);

        AppcLcmResponseStatus status = new AppcLcmResponseStatus();
        output.setStatus(status);
        status.setMessage(MY_MESSAGE);
        status.setCode(SUCCESS_CODE);

        return response;
    }
}