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