Merge "Add subrequest ID to OperationOutcome"
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / ModifyConfigOperationTest.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.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.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Arrays;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.atomic.AtomicBoolean;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.aai.domain.yang.GenericVnf;
41 import org.onap.policy.aai.AaiCqResponse;
42 import org.onap.policy.appc.Request;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
45 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
46 import org.onap.policy.controlloop.policy.PolicyResult;
47
48 public class ModifyConfigOperationTest extends BasicAppcOperation {
49
50     private ModifyConfigOperation oper;
51
52     public ModifyConfigOperationTest() {
53         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
54     }
55
56     @Before
57     public void setUp() throws Exception {
58         super.setUp();
59         oper = new ModifyConfigOperation(params, config);
60     }
61
62     @Test
63     public void testModifyConfigOperation() {
64         assertEquals(DEFAULT_ACTOR, oper.getActorName());
65         assertEquals(ModifyConfigOperation.NAME, oper.getName());
66     }
67
68     @Test
69     public void testStartPreprocessorAsync() throws Exception {
70         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
71         context = mock(ControlLoopEventContext.class);
72         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
73         when(context.getEvent()).thenReturn(event);
74         params = params.toBuilder().context(context).build();
75
76         AtomicBoolean guardStarted = new AtomicBoolean();
77
78         oper = new ModifyConfigOperation(params, config) {
79             @Override
80             protected CompletableFuture<OperationOutcome> startGuardAsync() {
81                 guardStarted.set(true);
82                 return super.startGuardAsync();
83             }
84         };
85
86         CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
87         assertNotNull(future3);
88         assertFalse(future.isDone());
89         assertTrue(guardStarted.get());
90         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
91
92         future2.complete(params.makeOutcome());
93         assertTrue(executor.runAll(100));
94         assertTrue(future3.isDone());
95         assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
96     }
97
98     @Test
99     public void testMakeRequest() throws CoderException {
100         AaiCqResponse cq = new AaiCqResponse("{}");
101
102         // missing vnf-id
103         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
104         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
105
106         // populate the CQ data with a vnf-id
107         GenericVnf genvnf = new GenericVnf();
108         genvnf.setVnfId(MY_VNF);
109         genvnf.setModelInvariantId(RESOURCE_ID);
110         cq.setInventoryResponseItems(Arrays.asList(genvnf));
111
112         Pair<String, Request> result = oper.makeRequest(2);
113         assertNotNull(result.getLeft());
114
115         Request request = result.getRight();
116         assertNotNull(request);
117         assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
118
119         verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);
120     }
121 }