Merge "More actor clean-up"
[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.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.mock;
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
46 public class ModifyConfigOperationTest extends BasicAppcOperation {
47
48     private ModifyConfigOperation oper;
49
50     public ModifyConfigOperationTest() {
51         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
52     }
53
54     @Before
55     public void setUp() throws Exception {
56         super.setUp();
57         oper = new ModifyConfigOperation(params, config);
58     }
59
60     @Test
61     public void testModifyConfigOperation() {
62         assertEquals(DEFAULT_ACTOR, oper.getActorName());
63         assertEquals(ModifyConfigOperation.NAME, oper.getName());
64     }
65
66     @Test
67     public void testStartPreprocessorAsync() {
68         CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
69         context = mock(ControlLoopEventContext.class);
70         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future);
71         params = params.toBuilder().context(context).build();
72
73         AtomicBoolean guardStarted = new AtomicBoolean();
74
75         oper = new ModifyConfigOperation(params, config) {
76             @Override
77             protected CompletableFuture<OperationOutcome> startGuardAsync() {
78                 guardStarted.set(true);
79                 return super.startGuardAsync();
80             }
81         };
82
83         assertSame(future, oper.startPreprocessorAsync());
84         assertFalse(future.isDone());
85         assertTrue(guardStarted.get());
86     }
87
88     @Test
89     public void testMakeRequest() throws CoderException {
90         AaiCqResponse cq = new AaiCqResponse("{}");
91
92         // missing vnf-id
93         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
94         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
95
96         // populate the CQ data with a vnf-id
97         GenericVnf genvnf = new GenericVnf();
98         genvnf.setVnfId(MY_VNF);
99         genvnf.setModelInvariantId(RESOURCE_ID);
100         cq.setInventoryResponseItems(Arrays.asList(genvnf));
101
102         Request request = oper.makeRequest(2);
103         assertNotNull(request);
104         assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
105
106         verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);
107     }
108 }