Set sub request ID before start callback
[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.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.aai.domain.yang.GenericVnf;
43 import org.onap.policy.aai.AaiCqResponse;
44 import org.onap.policy.appc.Request;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
47 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
48 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
49 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
50 import org.onap.policy.controlloop.policy.PolicyResult;
51
52 public class ModifyConfigOperationTest extends BasicAppcOperation {
53
54     private ModifyConfigOperation oper;
55
56     public ModifyConfigOperationTest() {
57         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
58     }
59
60     @BeforeClass
61     public static void setUpBeforeClass() throws Exception {
62         // use same topic name for both sides
63         initBeforeClass(MY_SINK, MY_SINK);
64     }
65
66     @AfterClass
67     public static void tearDownAfterClass() {
68         destroyAfterClass();
69     }
70
71     @Before
72     @Override
73     public void setUp() throws Exception {
74         super.setUp();
75         oper = new ModifyConfigOperation(params, config);
76     }
77
78     @After
79     @Override
80     public void tearDown() {
81         super.tearDown();
82     }
83
84     /**
85      * Tests "success" case with simulator.
86      */
87     @Test
88     public void testSuccess() throws Exception {
89         BidirectionalTopicParams opParams =
90                         BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SINK).build();
91         config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcOperation.SELECTOR_KEYS);
92
93         AaiCqResponse cq = mock(AaiCqResponse.class);
94         GenericVnf genvnf = mock(GenericVnf.class);
95         when(genvnf.getVnfId()).thenReturn(MY_VNF);
96         when(cq.getGenericVnfByModelInvariantId(any())).thenReturn(genvnf);
97
98         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
99
100         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
101
102         oper = new ModifyConfigOperation(params, config) {
103             @Override
104             protected CompletableFuture<OperationOutcome> startGuardAsync() {
105                 return null;
106             }
107         };
108
109         outcome = oper.start().get();
110         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
111     }
112
113     @Test
114     public void testConstructor() {
115         assertEquals(DEFAULT_ACTOR, oper.getActorName());
116         assertEquals(ModifyConfigOperation.NAME, oper.getName());
117     }
118
119     @Test
120     public void testStartPreprocessorAsync() throws Exception {
121         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
122         context = mock(ControlLoopEventContext.class);
123         when(context.obtain(eq(AaiCqResponse.CONTEXT_KEY), any())).thenReturn(future2);
124         when(context.getEvent()).thenReturn(event);
125         params = params.toBuilder().context(context).build();
126
127         AtomicBoolean guardStarted = new AtomicBoolean();
128
129         oper = new ModifyConfigOperation(params, config) {
130             @Override
131             protected CompletableFuture<OperationOutcome> startGuardAsync() {
132                 guardStarted.set(true);
133                 return super.startGuardAsync();
134             }
135         };
136
137         CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
138         assertNotNull(future3);
139         assertFalse(future.isDone());
140         assertTrue(guardStarted.get());
141         verify(context).obtain(eq(AaiCqResponse.CONTEXT_KEY), any());
142
143         future2.complete(params.makeOutcome());
144         assertTrue(executor.runAll(100));
145         assertTrue(future3.isDone());
146         assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
147     }
148
149     @Test
150     public void testMakeRequest() throws CoderException {
151         AaiCqResponse cq = new AaiCqResponse("{}");
152
153         // missing vnf-id
154         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, cq);
155         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(1));
156
157         // populate the CQ data with a vnf-id
158         GenericVnf genvnf = new GenericVnf();
159         genvnf.setVnfId(MY_VNF);
160         genvnf.setModelInvariantId(RESOURCE_ID);
161         cq.setInventoryResponseItems(Arrays.asList(genvnf));
162
163         oper.generateSubRequestId(2);
164         Request request = oper.makeRequest(2);
165         assertNotNull(request);
166         assertEquals(MY_VNF, request.getPayload().get(ModifyConfigOperation.VNF_ID_KEY));
167
168         verifyRequest("modifyConfig.json", request, IGNORE_FIELDS);
169     }
170 }