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