Test new actors against simulators
[policy/models.git] / models-interactions / model-actors / actor.sdnr / src / test / java / org / onap / policy / controlloop / actor / sdnr / 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.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.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
44 import org.onap.policy.controlloop.policy.PolicyResult;
45 import org.onap.policy.sdnr.PciMessage;
46
47 public class ModifyConfigOperationTest extends BasicSdnrOperation {
48
49     private ModifyConfigOperation oper;
50
51     public ModifyConfigOperationTest() {
52         super(DEFAULT_ACTOR, ModifyConfigOperation.NAME);
53     }
54
55     @BeforeClass
56     public static void setUpBeforeClass() throws Exception {
57         BasicBidirectionalTopicOperation.initBeforeClass(MY_SINK, MY_SOURCE);
58     }
59
60     @AfterClass
61     public static void tearDownAfterClass() {
62         destroyAfterClass();
63     }
64
65     @Before
66     public void setUp() throws Exception {
67         super.setUp();
68         oper = new ModifyConfigOperation(params, config);
69     }
70
71     @After
72     @Override
73     public void tearDown() {
74         super.tearDown();
75     }
76
77
78     /**
79      * Tests "success" case with simulator.
80      */
81     @Test
82     public void testSuccess() throws Exception {
83         BidirectionalTopicParams opParams =
84                         BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
85         config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, SdnrOperation.SELECTOR_KEYS);
86
87         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
88
89         oper = new ModifyConfigOperation(params, config) {
90             @Override
91             protected CompletableFuture<OperationOutcome> startGuardAsync() {
92                 return null;
93             }
94         };
95
96         outcome = oper.start().get();
97         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
98     }
99
100     @Test
101     public void testConstructor() {
102         assertEquals(DEFAULT_ACTOR, oper.getActorName());
103         assertEquals(ModifyConfigOperation.NAME, oper.getName());
104     }
105
106     @Test
107     public void testStartPreprocessorAsync() throws Exception {
108         final CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
109         context = mock(ControlLoopEventContext.class);
110         when(context.getEvent()).thenReturn(event);
111         params = params.toBuilder().context(context).build();
112
113         AtomicBoolean guardStarted = new AtomicBoolean();
114
115         oper = new ModifyConfigOperation(params, config) {
116             @Override
117             protected CompletableFuture<OperationOutcome> startGuardAsync() {
118                 guardStarted.set(true);
119                 return super.startGuardAsync();
120             }
121         };
122         CompletableFuture<OperationOutcome> future3 = oper.startPreprocessorAsync();
123
124         assertNotNull(future3);
125         assertFalse(future.isDone());
126         assertTrue(guardStarted.get());
127
128         future2.complete(params.makeOutcome());
129         assertTrue(executor.runAll(100));
130         assertTrue(future3.isDone());
131         assertEquals(PolicyResult.SUCCESS, future3.get().getResult());
132     }
133
134     @Test
135     public void testMakeRequest() throws CoderException {
136         Pair<String, PciMessage> result = oper.makeRequest(1);
137         assertNotNull(result.getLeft());
138         assertNotNull(result.getRight());
139     }
140 }