Upgrade Java 17 in policy-drools-apps
[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  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.apps.controlloop.feature.management;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.Collections;
31 import org.junit.jupiter.api.AfterEach;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.drools.apps.controlloop.feature.management.ControlLoopManagementFeature.Factory;
35 import org.onap.policy.drools.controller.DroolsController;
36 import org.onap.policy.drools.system.PolicyController;
37 import org.springframework.test.util.ReflectionTestUtils;
38
39 /**
40  * Control Loop Management Feature Test.
41  */
42 class ControlLoopManagementFeatureTest {
43     private static final String FACTORY_FIELD = "factory";
44     private static final String SESSION_NAME = "my-session";
45     private static final String CONTROLLER_NAME = "my-controller";
46
47     private static Factory saveFactory;
48
49     @BeforeAll
50     public static void setUpBeforeClass() {
51         saveFactory = (Factory) ReflectionTestUtils.getField(ControlLoopManagementFeature.class, FACTORY_FIELD);
52     }
53
54     @AfterEach
55     public void tearDown() {
56         ReflectionTestUtils.setField(ControlLoopManagementFeature.class, FACTORY_FIELD, saveFactory);
57     }
58
59     /**
60      * Sequence Number Test.
61      */
62     @Test
63     void getSequenceNumber() {
64         assertEquals(1000, new ControlLoopManagementFeature().getSequenceNumber());
65     }
66
67     /**
68      * Name Test.
69      */
70     @Test
71     void getName() {
72         assertEquals("controlloop-management", new ControlLoopManagementFeature().getName());
73     }
74
75     @Test
76     void testControlLoops_InvalidArgs() {
77         var factory = mock(Factory.class);
78         ReflectionTestUtils.setField(ControlLoopManagementFeature.class, FACTORY_FIELD, factory);
79
80         // returns null controller
81         when(factory.getController(any())).thenReturn(null);
82         assertThatIllegalArgumentException()
83             .isThrownBy(() -> ControlLoopManagementFeature.controlLoops(CONTROLLER_NAME, SESSION_NAME))
84             .withMessage("Invalid Controller Name");
85
86         // non-matching session name
87         var drools = mock(DroolsController.class);
88         when(drools.getSessionNames()).thenReturn(Collections.emptyList());
89         var ctlr = mock(PolicyController.class);
90         when(ctlr.getDrools()).thenReturn(drools);
91         when(factory.getController(any())).thenReturn(ctlr);
92         assertThatIllegalArgumentException()
93             .isThrownBy(() -> ControlLoopManagementFeature.controlLoops(CONTROLLER_NAME, SESSION_NAME))
94             .withMessage("Invalid Session Name");
95     }
96
97     @Test
98     void testFactoryGetController() {
99         // invoking controlLoops() will invoke the factory.getController() method
100         assertThatIllegalArgumentException().isThrownBy(
101             () -> ControlLoopManagementFeature.controlLoops("unknown-controller", SESSION_NAME));
102     }
103 }