3165d58141a2c16b93c2f9c864e8ac1d98dd2495
[policy/models.git] /
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.sdnr;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
38 import org.onap.policy.controlloop.policy.PolicyResult;
39 import org.onap.policy.sdnr.PciMessage;
40
41 public class ModifyConfigOperationTest extends BasicSdnrOperation {
42
43     private ModifyConfigOperation oper;
44
45     public ModifyConfigOperationTest() {
46         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
47     }
48
49     @Before
50     public void setUp() throws Exception {
51         super.setUp();
52         oper = new ModifyConfigOperation(params, config);
53     }
54
55
56     @Test
57     public void testModifyConfigOperation() {
58         assertEquals(DEFAULT_ACTOR, oper.getActorName());
59         assertEquals(ModifyConfigOperation.NAME, oper.getName());
60     }
61
62     @Test
63     public void testStartPreprocessorAsync() throws Exception {
64         final CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
65         context = mock(ControlLoopEventContext.class);
66         when(context.getEvent()).thenReturn(event);
67         params = params.toBuilder().context(context).build();
68
69         AtomicBoolean guardStarted = new AtomicBoolean();
70
71         oper = new ModifyConfigOperation(params, config) {
72             @Override
73             protected CompletableFuture<OperationOutcome> startGuardAsync() {
74                 guardStarted.set(true);
75                 return super.startGuardAsync();
76             }
77         };
78         CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
79
80         assertNotNull(future3);
81         assertFalse(future.isDone());
82         assertTrue(guardStarted.get());
83
84         future2.complete(params.makeOutcome());
85         assertTrue(executor.runAll(100));
86         assertTrue(future3.isDone());
87         assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
88     }
89
90     @Test
91     public void testMakeRequest() throws CoderException {
92         Pair<String, PciMessage> result = oper.makeRequest(1);
93         assertNotNull(result.getLeft());
94         assertNotNull(result.getRight());
95     }
96 }