e5034017f738cc7b7a887c947e6f84c451efb77b
[policy/drools-applications.git] / controlloop / common / feature-controlloop-management / src / test / java / org / onap / policy / drools / apps / controlloop / feature / management / ControlLoopManagementFeatureTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2019 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.drools.apps.controlloop.feature.management;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Collections;
29 import org.junit.After;
30 import org.junit.Assert;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.drools.apps.controlloop.feature.management.ControlLoopManagementFeature.Factory;
34 import org.onap.policy.drools.controller.DroolsController;
35 import org.onap.policy.drools.system.PolicyController;
36 import org.powermock.reflect.Whitebox;
37
38 /**
39  * Control Loop Management Feature Test.
40  */
41 public class ControlLoopManagementFeatureTest {
42     private static final String FACTORY_FIELD = "factory";
43     private static final String SESSION_NAME = "my-session";
44     private static final String CONTROLLER_NAME = "my-controller";
45
46     private static Factory saveFactory;
47
48     @BeforeClass
49     public static void setUpBeforeClass() {
50         saveFactory = Whitebox.getInternalState(ControlLoopManagementFeature.class, FACTORY_FIELD);
51     }
52
53     @After
54     public void tearDown() {
55         Whitebox.setInternalState(ControlLoopManagementFeature.class, FACTORY_FIELD, saveFactory);
56     }
57
58     /**
59      * Sequence Number Test.
60      */
61     @Test
62     public void getSequenceNumber() {
63         Assert.assertEquals(1000, new ControlLoopManagementFeature().getSequenceNumber());
64     }
65
66     /**
67      * Name Test.
68      */
69     @Test
70     public void getName() {
71         Assert.assertEquals("controlloop-management", new ControlLoopManagementFeature().getName());
72     }
73
74     @Test
75     public void testControlLoops_InvalidArgs() {
76         Factory factory = mock(Factory.class);
77         Whitebox.setInternalState(ControlLoopManagementFeature.class, FACTORY_FIELD, factory);
78
79         // returns null controller
80         when(factory.getController(any())).thenReturn(null);
81         assertThatIllegalArgumentException()
82             .isThrownBy(() -> ControlLoopManagementFeature.controlLoops(CONTROLLER_NAME, SESSION_NAME))
83             .withMessage("Invalid Controller Name");
84
85         // non-matching session name
86         PolicyController ctlr = mock(PolicyController.class);
87         DroolsController drools = mock(DroolsController.class);
88         when(drools.getSessionNames()).thenReturn(Collections.emptyList());
89         when(ctlr.getDrools()).thenReturn(drools);
90         when(factory.getController(any())).thenReturn(ctlr);
91         assertThatIllegalArgumentException()
92             .isThrownBy(() -> ControlLoopManagementFeature.controlLoops(CONTROLLER_NAME, SESSION_NAME))
93             .withMessage("Invalid Session Name");
94     }
95
96     @Test
97     public void testFactoryGetController() {
98         // invoking controlLoops() will invoke the factory.getController() method
99         assertThatIllegalArgumentException().isThrownBy(
100             () -> ControlLoopManagementFeature.controlLoops("unknown-controller", SESSION_NAME));
101     }
102 }