* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018, 2020 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Nordix Foundation
* ================================================================================
* 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.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+
+import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
-import org.junit.AfterClass;
+import java.util.concurrent.CountDownLatch;
+import org.apache.commons.lang3.tuple.Pair;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.endpoints.event.comm.TopicSink;
+import org.onap.policy.common.endpoints.event.comm.TopicSource;
import org.onap.policy.drools.controller.DroolsController;
-import org.onap.policy.drools.event.comm.Topic.CommInfrastructure;
-import org.onap.policy.drools.pooling.PoolingFeature.Factory;
import org.onap.policy.drools.system.PolicyController;
import org.onap.policy.drools.system.PolicyEngine;
-import org.onap.policy.drools.utils.Pair;
public class PoolingFeatureTest {
private static final Object OBJECT1 = new Object();
private static final Object OBJECT2 = new Object();
- /**
- * Saved from PoolingFeature and restored on exit from this test class.
- */
- private static Factory saveFactory;
-
private Properties props;
private PolicyEngine engine;
private PolicyController controller1;
private List<Pair<PoolingManagerImpl, PoolingProperties>> managers;
private PoolingManagerImpl mgr1;
private PoolingManagerImpl mgr2;
- private Factory factory;
private PoolingFeature pool;
-
- @BeforeClass
- public static void setUpBeforeClass() {
- saveFactory = PoolingFeature.getFactory();
- }
-
- @AfterClass
- public static void tearDownAfterClass() {
- PoolingFeature.setFactory(saveFactory);
- }
-
+ /**
+ * Setup.
+ *
+ * @throws Exception exception
+ */
@Before
public void setUp() throws Exception {
props = initProperties();
engine = mock(PolicyEngine.class);
- factory = mock(Factory.class);
controller1 = mock(PolicyController.class);
controller2 = mock(PolicyController.class);
controllerDisabled = mock(PolicyController.class);
droolsDisabled = mock(DroolsController.class);
managers = new LinkedList<>();
- PoolingFeature.setFactory(factory);
-
when(controller1.getName()).thenReturn(CONTROLLER1);
when(controller2.getName()).thenReturn(CONTROLLER2);
when(controllerDisabled.getName()).thenReturn(CONTROLLER_DISABLED);
when(controllerException.getName()).thenReturn(CONTROLLER_EX);
when(controllerUnknown.getName()).thenReturn(CONTROLLER_UNKNOWN);
- when(factory.getProperties(PoolingProperties.FEATURE_NAME)).thenReturn(props);
- when(factory.getController(drools1)).thenReturn(controller1);
- when(factory.getController(drools2)).thenReturn(controller2);
- when(factory.getController(droolsDisabled)).thenReturn(controllerDisabled);
-
- when(factory.makeManager(any(), any())).thenAnswer(args -> {
- PoolingProperties props = args.getArgument(1);
-
- PoolingManagerImpl mgr = mock(PoolingManagerImpl.class);
-
- managers.add(new Pair<>(mgr, props));
-
- return mgr;
- });
-
- pool = new PoolingFeature();
+ pool = new PoolingFeatureImpl();
pool.beforeStart(engine);
pool.afterCreate(controller1);
pool.afterCreate(controller2);
- mgr1 = managers.get(0).first();
- mgr2 = managers.get(1).first();
+ mgr1 = managers.get(0).getLeft();
+ mgr2 = managers.get(1).getLeft();
}
@Test
assertEquals(2, managers.size());
}
+ @Test
+ public void testGetHost() {
+ String host = pool.getHost();
+ assertNotNull(host);
+
+ // create another and ensure it generates another host name
+ pool = new PoolingFeatureImpl();
+ String host2 = pool.getHost();
+ assertNotNull(host2);
+
+ assertNotEquals(host, host2);
+ }
+
@Test
public void testGetSequenceNumber() {
assertEquals(0, pool.getSequenceNumber());
@Test
public void testBeforeStartEngine() {
- pool = new PoolingFeature();
+ pool = new PoolingFeatureImpl();
assertFalse(pool.beforeStart(engine));
}
@Test
public void testAfterCreate() {
managers.clear();
- pool = new PoolingFeature();
+ pool = new PoolingFeatureImpl();
pool.beforeStart(engine);
assertFalse(pool.afterCreate(controller1));
@Test
public void testAfterCreate_NotEnabled() {
managers.clear();
- pool = new PoolingFeature();
+ pool = new PoolingFeatureImpl();
pool.beforeStart(engine);
assertFalse(pool.afterCreate(controllerDisabled));
@Test(expected = PoolingFeatureRtException.class)
public void testAfterCreate_PropertyEx() {
managers.clear();
- pool = new PoolingFeature();
+ pool = new PoolingFeatureImpl();
pool.beforeStart(engine);
pool.afterCreate(controllerException);
@Test(expected = PoolingFeatureRtException.class)
public void testAfterCreate_NoProps() {
- pool = new PoolingFeature();
+ pool = new PoolingFeatureImpl();
// did not perform globalInit, which is an error
@Test
public void testAfterCreate_NoFeatProps() {
managers.clear();
- pool = new PoolingFeature();
+ pool = new PoolingFeatureImpl();
pool.beforeStart(engine);
assertFalse(pool.afterCreate(controllerUnknown));
assertFalse(pool.afterStop(controller1));
verify(mgr1).afterStop();
- // ensure it has been removed from the map by re-invoking
- assertFalse(pool.afterStop(controller1));
+ assertFalse(pool.afterStop(controllerDisabled));
// count should be unchanged
verify(mgr1).afterStop();
+ }
+
+ @Test
+ public void testAfterHalt() {
+ assertFalse(pool.afterHalt(controller1));
+ assertFalse(pool.afterHalt(controller1));
+
+ verify(mgr1, never()).afterStop();
+
+ assertFalse(pool.afterStop(controllerDisabled));
+ }
+
+ @Test
+ public void testAfterShutdown() {
+ assertFalse(pool.afterShutdown(controller1));
+ assertFalse(pool.afterShutdown(controller1));
+
+ verify(mgr1, never()).afterStop();
assertFalse(pool.afterStop(controllerDisabled));
}
@Test
public void testBeforeOffer() {
assertFalse(pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC1, EVENT1));
- verify(mgr1).beforeOffer(CommInfrastructure.UEB, TOPIC1, EVENT1);
+ verify(mgr1).beforeOffer(TOPIC1, EVENT1);
// ensure that the args were captured
pool.beforeInsert(drools1, OBJECT1);
- verify(mgr1).beforeInsert(CommInfrastructure.UEB, TOPIC1, EVENT1, OBJECT1);
+ verify(mgr1).beforeInsert(TOPIC1, OBJECT1);
// ensure it's still in the map by re-invoking
assertFalse(pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC2, EVENT2));
- verify(mgr1).beforeOffer(CommInfrastructure.UEB, TOPIC2, EVENT2);
+ verify(mgr1).beforeOffer(TOPIC2, EVENT2);
// ensure that the new args were captured
pool.beforeInsert(drools1, OBJECT2);
- verify(mgr1).beforeInsert(CommInfrastructure.UEB, TOPIC2, EVENT2, OBJECT2);
+ verify(mgr1).beforeInsert(TOPIC2, OBJECT2);
assertFalse(pool.beforeOffer(controllerDisabled, CommInfrastructure.UEB, TOPIC1, EVENT1));
public void testBeforeOffer_MgrTrue() {
// manager will return true
- when(mgr1.beforeOffer(any(), any(), any())).thenReturn(true);
+ when(mgr1.beforeOffer(any(), any())).thenReturn(true);
assertTrue(pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC1, EVENT1));
- verify(mgr1).beforeOffer(CommInfrastructure.UEB, TOPIC1, EVENT1);
+ verify(mgr1).beforeOffer(TOPIC1, EVENT1);
// ensure it's still in the map by re-invoking
assertTrue(pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC2, EVENT2));
- verify(mgr1).beforeOffer(CommInfrastructure.UEB, TOPIC2, EVENT2);
+ verify(mgr1).beforeOffer(TOPIC2, EVENT2);
assertFalse(pool.beforeOffer(controllerDisabled, CommInfrastructure.UEB, TOPIC1, EVENT1));
}
public void testBeforeInsert() {
pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC1, EVENT1);
assertFalse(pool.beforeInsert(drools1, OBJECT1));
- verify(mgr1).beforeInsert(CommInfrastructure.UEB, TOPIC1, EVENT1, OBJECT1);
+ verify(mgr1).beforeInsert(TOPIC1, OBJECT1);
// ensure it's still in the map by re-invoking
pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC2, EVENT2);
assertFalse(pool.beforeInsert(drools1, OBJECT2));
- verify(mgr1).beforeInsert(CommInfrastructure.UEB, TOPIC2, EVENT2, OBJECT2);
+ verify(mgr1).beforeInsert(TOPIC2, OBJECT2);
pool.beforeOffer(controllerDisabled, CommInfrastructure.UEB, TOPIC2, EVENT2);
assertFalse(pool.beforeInsert(droolsDisabled, OBJECT1));
// call beforeInsert without beforeOffer
assertFalse(pool.beforeInsert(drools1, OBJECT1));
- verify(mgr1, never()).beforeInsert(any(), any(), any(), any());
+ verify(mgr1, never()).beforeInsert(any(), any());
assertFalse(pool.beforeInsert(droolsDisabled, OBJECT1));
- verify(mgr1, never()).beforeInsert(any(), any(), any(), any());
+ verify(mgr1, never()).beforeInsert(any(), any());
}
@Test
public void testBeforeInsert_ArgEx() {
-
// generate exception
- doThrow(new IllegalArgumentException()).when(factory).getController(any());
+ pool = new PoolingFeatureImpl() {
+ @Override
+ protected PolicyController getController(DroolsController droolsController) {
+ throw new IllegalArgumentException();
+ }
+ };
pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC1, EVENT1);
assertFalse(pool.beforeInsert(drools1, OBJECT1));
- verify(mgr1, never()).beforeInsert(any(), any(), any(), any());
+ verify(mgr1, never()).beforeInsert(any(), any());
}
@Test
public void testBeforeInsert_StateEx() {
-
// generate exception
- doThrow(new IllegalStateException()).when(factory).getController(any());
+ pool = new PoolingFeatureImpl() {
+ @Override
+ protected PolicyController getController(DroolsController droolsController) {
+ throw new IllegalStateException();
+ }
+ };
pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC1, EVENT1);
assertFalse(pool.beforeInsert(drools1, OBJECT1));
- verify(mgr1, never()).beforeInsert(any(), any(), any(), any());
+ verify(mgr1, never()).beforeInsert(any(), any());
}
@Test
public void testBeforeInsert_NullController() {
// return null controller
- when(factory.getController(any())).thenReturn(null);
+ pool = new PoolingFeatureImpl() {
+ @Override
+ protected PolicyController getController(DroolsController droolsController) {
+ return null;
+ }
+ };
pool.beforeOffer(controller1, CommInfrastructure.UEB, TOPIC1, EVENT1);
assertFalse(pool.beforeInsert(drools1, OBJECT1));
- verify(mgr1, never()).beforeInsert(any(), any(), any(), any());
+ verify(mgr1, never()).beforeInsert(any(), any());
}
@Test
assertFalse(pool.afterOffer(controller1, CommInfrastructure.UEB, TOPIC2, EVENT2, true));
assertFalse(pool.beforeInsert(drools1, OBJECT1));
- verify(mgr1, never()).beforeInsert(any(), any(), any(), any());
+ verify(mgr1, never()).beforeInsert(any(), any());
assertFalse(pool.beforeInsert(droolsDisabled, OBJECT1));
assertFalse(pool.beforeStart(controllerDisabled));
}
- @Test(expected = PoolingFeatureRtException.class)
+ @Test(expected = RuntimeException.class)
public void testDoManager_Ex() throws Exception {
// generate exception
- doThrow(new PoolingFeatureException()).when(mgr1).beforeStart();
+ doThrow(new RuntimeException()).when(mgr1).beforeStart();
pool.beforeStart(controller1);
}
- @Test
- public void testDoDeleteManager() {
- assertFalse(pool.afterStop(controller1));
- verify(mgr1).afterStop();
-
- // ensure it has been removed from the map by re-invoking
- assertFalse(pool.afterStop(controller1));
-
- // count should be unchanged
- verify(mgr1).afterStop();
-
-
- // different controller
- assertFalse(pool.afterStop(controller2));
- verify(mgr2).afterStop();
-
- // ensure it has been removed from the map by re-invoking
- assertFalse(pool.afterStop(controller2));
-
- // count should be unchanged
- verify(mgr2).afterStop();
-
-
- assertFalse(pool.afterStop(controllerDisabled));
- }
-
- @Test
- public void testDoDeleteManager_NotFound() {
- assertFalse(pool.afterStop(controllerDisabled));
- }
-
- @Test(expected = PoolingFeatureRtException.class)
- public void testDoDeleteManager_Ex() {
-
- // generate exception
- doThrow(new PoolingFeatureRtException()).when(mgr1).afterStop();
-
- pool.afterStop(controller1);
- }
-
private Properties initProperties() {
Properties props = new Properties();
String.valueOf(40 + offset));
props.setProperty("pooling.controller" + suffix + ".inter.heartbeat.milliseconds", String.valueOf(50 + offset));
}
+
+ /**
+ * Feature with overrides.
+ */
+ private class PoolingFeatureImpl extends PoolingFeature {
+
+ @Override
+ protected Properties getProperties(String featName) {
+ if (PoolingProperties.FEATURE_NAME.equals(featName)) {
+ return props;
+ } else {
+ throw new IllegalArgumentException("unknown feature name");
+ }
+ }
+
+ @Override
+ protected PoolingManagerImpl makeManager(String host, PolicyController controller, PoolingProperties props,
+ CountDownLatch activeLatch) {
+
+ PoolingManagerImpl mgr = mock(PoolingManagerImpl.class);
+
+ managers.add(Pair.of(mgr, props));
+
+ return mgr;
+ }
+
+ @Override
+ protected PolicyController getController(DroolsController droolsController) {
+ if (droolsController == drools1) {
+ return controller1;
+ } else if (droolsController == drools2) {
+ return controller2;
+ } else if (droolsController == droolsDisabled) {
+ return controllerDisabled;
+ } else {
+ throw new IllegalArgumentException("unknown drools controller");
+ }
+ }
+
+ @Override
+ protected List<TopicSource> initTopicSources(Properties props) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ protected List<TopicSink> initTopicSinks(Properties props) {
+ return Collections.emptyList();
+ }
+ }
}