diff options
Diffstat (limited to 'controlloop/common/feature-controlloop-management')
4 files changed, 86 insertions, 8 deletions
diff --git a/controlloop/common/feature-controlloop-management/pom.xml b/controlloop/common/feature-controlloop-management/pom.xml index acbbc737f..b3a3e3771 100644 --- a/controlloop/common/feature-controlloop-management/pom.xml +++ b/controlloop/common/feature-controlloop-management/pom.xml @@ -276,5 +276,15 @@ <version>3.0.0</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeature.java b/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeature.java index 800971a61..d78545a2d 100644 --- a/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeature.java +++ b/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeature.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -34,6 +34,12 @@ public class ControlLoopManagementFeature implements PolicyEngineFeatureAPI { private static final int SEQNO = 1000; /** + * Factory for various objects. May be overridden by junit tests. + */ + private static Factory factory = new Factory(); + + + /** * retrieves control loops. * * @param controllerName controller name. @@ -41,7 +47,7 @@ public class ControlLoopManagementFeature implements PolicyEngineFeatureAPI { * @return control loops. */ public static Stream<ControlLoopParams> controlLoops(String controllerName, String sessionName) { - PolicyController controller = PolicyController.factory.get(controllerName); + PolicyController controller = factory.getController(controllerName); if (controller == null) { throw new IllegalArgumentException("Invalid Controller Name"); } @@ -89,4 +95,12 @@ public class ControlLoopManagementFeature implements PolicyEngineFeatureAPI { return FEATURE_NAME; } + /** + * Factory that can be overridden by junit tests. + */ + public static class Factory { + public PolicyController getController(String controllerName) { + return PolicyController.factory.get(controllerName); + } + } } diff --git a/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeatureTest.java b/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeatureTest.java index ac9bff93b..e5034017f 100644 --- a/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeatureTest.java +++ b/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/apps/controlloop/feature/management/ControlLoopManagementFeatureTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,13 +20,40 @@ package org.onap.policy.drools.apps.controlloop.feature.management; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import org.junit.After; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; +import org.onap.policy.drools.apps.controlloop.feature.management.ControlLoopManagementFeature.Factory; +import org.onap.policy.drools.controller.DroolsController; +import org.onap.policy.drools.system.PolicyController; +import org.powermock.reflect.Whitebox; /** * Control Loop Management Feature Test. */ public class ControlLoopManagementFeatureTest { + private static final String FACTORY_FIELD = "factory"; + private static final String SESSION_NAME = "my-session"; + private static final String CONTROLLER_NAME = "my-controller"; + + private static Factory saveFactory; + + @BeforeClass + public static void setUpBeforeClass() { + saveFactory = Whitebox.getInternalState(ControlLoopManagementFeature.class, FACTORY_FIELD); + } + + @After + public void tearDown() { + Whitebox.setInternalState(ControlLoopManagementFeature.class, FACTORY_FIELD, saveFactory); + } /** * Sequence Number Test. @@ -43,4 +70,33 @@ public class ControlLoopManagementFeatureTest { public void getName() { Assert.assertEquals("controlloop-management", new ControlLoopManagementFeature().getName()); } -}
\ No newline at end of file + + @Test + public void testControlLoops_InvalidArgs() { + Factory factory = mock(Factory.class); + Whitebox.setInternalState(ControlLoopManagementFeature.class, FACTORY_FIELD, factory); + + // returns null controller + when(factory.getController(any())).thenReturn(null); + assertThatIllegalArgumentException() + .isThrownBy(() -> ControlLoopManagementFeature.controlLoops(CONTROLLER_NAME, SESSION_NAME)) + .withMessage("Invalid Controller Name"); + + // non-matching session name + PolicyController ctlr = mock(PolicyController.class); + DroolsController drools = mock(DroolsController.class); + when(drools.getSessionNames()).thenReturn(Collections.emptyList()); + when(ctlr.getDrools()).thenReturn(drools); + when(factory.getController(any())).thenReturn(ctlr); + assertThatIllegalArgumentException() + .isThrownBy(() -> ControlLoopManagementFeature.controlLoops(CONTROLLER_NAME, SESSION_NAME)) + .withMessage("Invalid Session Name"); + } + + @Test + public void testFactoryGetController() { + // invoking controlLoops() will invoke the factory.getController() method + assertThatIllegalArgumentException().isThrownBy( + () -> ControlLoopManagementFeature.controlLoops("unknown-controller", SESSION_NAME)); + } +} diff --git a/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/server/restful/RestControlLoopManagerTest.java b/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/server/restful/RestControlLoopManagerTest.java index 3ed1adfe3..3d89e5a1e 100644 --- a/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/server/restful/RestControlLoopManagerTest.java +++ b/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/server/restful/RestControlLoopManagerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,7 +126,6 @@ public class RestControlLoopManagerTest { Files.deleteIfExists(controllerPath); } catch (Exception ignored) { /* to satisfy checkstyle */ - ; } Path controllerBakPath = @@ -136,7 +135,6 @@ public class RestControlLoopManagerTest { Files.deleteIfExists(controllerBakPath); } catch (Exception ignored) { /* to satisfy checkstyle */ - ; } } |