2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.apps.controlloop.feature.management;
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;
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;
40 * Control Loop Management Feature Test.
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";
47 private static Factory saveFactory;
50 public static void setUpBeforeClass() {
51 saveFactory = (Factory) ReflectionTestUtils.getField(ControlLoopManagementFeature.class, FACTORY_FIELD);
55 public void tearDown() {
56 ReflectionTestUtils.setField(ControlLoopManagementFeature.class, FACTORY_FIELD, saveFactory);
60 * Sequence Number Test.
63 void getSequenceNumber() {
64 assertEquals(1000, new ControlLoopManagementFeature().getSequenceNumber());
72 assertEquals("controlloop-management", new ControlLoopManagementFeature().getName());
76 void testControlLoops_InvalidArgs() {
77 var factory = mock(Factory.class);
78 ReflectionTestUtils.setField(ControlLoopManagementFeature.class, FACTORY_FIELD, factory);
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");
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");
98 void testFactoryGetController() {
99 // invoking controlLoops() will invoke the factory.getController() method
100 assertThatIllegalArgumentException().isThrownBy(
101 () -> ControlLoopManagementFeature.controlLoops("unknown-controller", SESSION_NAME));