Change payload to Map<String,Object> so it's more versatile
[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.junit.Before;
38 import org.junit.Test;
39 import org.onap.aai.domain.yang.GenericVnf;
40 import org.onap.policy.aai.AaiCqResponse;
41 import org.onap.policy.appc.Request;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
44 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
45 import org.onap.policy.controlloop.policy.PolicyResult;
46
47 public class ModifyConfigOperationTest extends BasicAppcOperation {
48
49     private ModifyConfigOperation oper;
50
51     public ModifyConfigOperationTest() {
52         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
53     }
54
55     @Before
56     public void setUp() throws Exception {
57         super.setUp();
58         oper = new ModifyConfigOperation(params, config);
59     }
60
61     @Test
62     public void testModifyConfigOperation() {
63         assertEquals(DEFAULT_ACTOR, oper.getActorName());
64         assertEquals(ModifyConfigOperation.NAME, oper.getName());
65     }
66
67     @Test
68     public void testStartPreprocessorAsync() throws Exception {
69         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
70         context = mock(ControlLoopEventContext.class);
71         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
72         when(context.getEvent()).thenReturn(event);
73         params = params.toBuilder().context(context).build();
74
75         AtomicBoolean guardStarted = new AtomicBoolean();
76
77         oper = new ModifyConfigOperation(params, config) {
78             @Override
79             protected CompletableFuture<OperationOutcome> startGuardAsync() {
80                 guardStarted.set(true);
81                 return super.startGuardAsync();
82             }
83         };
84
85         CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
86         assertNotNull(future3);
87         assertFalse(future.isDone());
88         assertTrue(guardStarted.get());
89         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
90
91         future2.complete(params.makeOutcome());
92         assertTrue(executor.runAll(100));
93         assertTrue(future3.isDone());
94         assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
95     }
96
97     @Test
98     public void testMakeRequest() throws CoderException {
99         AaiCqResponse cq = new AaiCqResponse("{}");
100
101         // missing vnf-id
102         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
103         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
104
105         // populate the CQ data with a vnf-id
106         GenericVnf genvnf = new GenericVnf();
107         genvnf.setVnfId(MY_VNF);
108         genvnf.setModelInvariantId(RESOURCE_ID);
109         cq.setInventoryResponseItems(Arrays.asList(genvnf));
110
111         Request request = oper.makeRequest(2);
112         assertNotNull(request);
113         assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
114
115         verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);
116     }
117 }