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