Make feature-pooling-dmaap work without filtering
[policy/drools-pdp.git] / feature-pooling-dmaap / src / test / java / org / onap / policy / drools / pooling / PoolingFeatureTest.java
index f8f3755..02a4db5 100644 (file)
@@ -2,14 +2,15 @@
  * ============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.
@@ -22,6 +23,7 @@ package org.onap.policy.drools.pooling;
 
 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;
@@ -31,19 +33,21 @@ import static org.mockito.Mockito.never;
 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 {
 
@@ -62,11 +66,6 @@ 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;
@@ -80,26 +79,18 @@ public class PoolingFeatureTest {
     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);
@@ -110,38 +101,21 @@ public class PoolingFeatureTest {
         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(), any(), any())).thenAnswer(args -> {
-            PoolingProperties props = args.getArgument(2);
-
-            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
@@ -155,11 +129,11 @@ public class PoolingFeatureTest {
         assertNotNull(host);
 
         // create another and ensure it generates another host name
-        pool = new PoolingFeature();
+        pool = new PoolingFeatureImpl();
         String host2 = pool.getHost();
         assertNotNull(host2);
 
-        assertTrue(!host.equals(host2));
+        assertNotEquals(host, host2);
     }
 
     @Test
@@ -169,7 +143,7 @@ public class PoolingFeatureTest {
 
     @Test
     public void testBeforeStartEngine() {
-        pool = new PoolingFeature();
+        pool = new PoolingFeatureImpl();
 
         assertFalse(pool.beforeStart(engine));
     }
@@ -177,7 +151,7 @@ public class PoolingFeatureTest {
     @Test
     public void testAfterCreate() {
         managers.clear();
-        pool = new PoolingFeature();
+        pool = new PoolingFeatureImpl();
         pool.beforeStart(engine);
 
         assertFalse(pool.afterCreate(controller1));
@@ -195,7 +169,7 @@ public class PoolingFeatureTest {
     @Test
     public void testAfterCreate_NotEnabled() {
         managers.clear();
-        pool = new PoolingFeature();
+        pool = new PoolingFeatureImpl();
         pool.beforeStart(engine);
 
         assertFalse(pool.afterCreate(controllerDisabled));
@@ -205,7 +179,7 @@ public class PoolingFeatureTest {
     @Test(expected = PoolingFeatureRtException.class)
     public void testAfterCreate_PropertyEx() {
         managers.clear();
-        pool = new PoolingFeature();
+        pool = new PoolingFeatureImpl();
         pool.beforeStart(engine);
 
         pool.afterCreate(controllerException);
@@ -213,7 +187,7 @@ public class PoolingFeatureTest {
 
     @Test(expected = PoolingFeatureRtException.class)
     public void testAfterCreate_NoProps() {
-        pool = new PoolingFeature();
+        pool = new PoolingFeatureImpl();
 
         // did not perform globalInit, which is an error
 
@@ -223,7 +197,7 @@ public class PoolingFeatureTest {
     @Test
     public void testAfterCreate_NoFeatProps() {
         managers.clear();
-        pool = new PoolingFeature();
+        pool = new PoolingFeatureImpl();
         pool.beforeStart(engine);
 
         assertFalse(pool.afterCreate(controllerUnknown));
@@ -271,11 +245,28 @@ public class PoolingFeatureTest {
         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));
     }
@@ -307,20 +298,20 @@ public class PoolingFeatureTest {
     @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));
@@ -335,14 +326,14 @@ public class PoolingFeatureTest {
     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));
     }
@@ -351,12 +342,12 @@ public class PoolingFeatureTest {
     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));
@@ -367,43 +358,56 @@ public class PoolingFeatureTest {
 
         // 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
@@ -422,7 +426,7 @@ public class PoolingFeatureTest {
         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));
@@ -455,55 +459,15 @@ public class PoolingFeatureTest {
         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();
 
@@ -531,4 +495,53 @@ public class PoolingFeatureTest {
                         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();
+        }
+    }
 }