import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
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.List;
import java.util.Properties;
+import java.util.concurrent.ScheduledExecutorService;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.junit.Before;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
import org.onap.policy.common.utils.gson.GsonTestUtils;
import org.onap.policy.drools.controller.DroolsController;
-import org.onap.policy.drools.features.PolicyControllerFeatureAPI;
-import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
+import org.onap.policy.drools.core.lock.Lock;
+import org.onap.policy.drools.core.lock.LockCallback;
+import org.onap.policy.drools.core.lock.PolicyResourceLockManager;
+import org.onap.policy.drools.features.PolicyControllerFeatureApi;
+import org.onap.policy.drools.features.PolicyEngineFeatureApi;
import org.onap.policy.drools.persistence.SystemPersistence;
-import org.onap.policy.drools.properties.DroolsProperties;
+import org.onap.policy.drools.properties.DroolsPropertyConstants;
import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
import org.onap.policy.drools.protocol.configuration.ControllerConfiguration;
import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
import org.onap.policy.drools.protocol.configuration.PdpdConfiguration;
+import org.onap.policy.drools.system.internal.SimpleLockManager;
+import org.onap.policy.drools.system.internal.SimpleLockProperties;
public class PolicyEngineManagerTest {
-
private static final String EXPECTED = "expected exception";
private static final String NOOP_STR = CommInfrastructure.NOOP.name();
private static final String FEATURE2 = "feature-b";
private static final String MY_TOPIC = "my-topic";
private static final String MESSAGE = "my-message";
+ private static final String MY_OWNER = "my-owner";
+ private static final String MY_RESOURCE = "my-resource";
private static final Object MY_EVENT = new Object();
.addHttpServletServerMock().build();
private Properties properties;
- private PolicyEngineFeatureAPI prov1;
- private PolicyEngineFeatureAPI prov2;
- private List<PolicyEngineFeatureAPI> providers;
- private PolicyControllerFeatureAPI contProv1;
- private PolicyControllerFeatureAPI contProv2;
- private List<PolicyControllerFeatureAPI> contProviders;
+ private PolicyEngineFeatureApi prov1;
+ private PolicyEngineFeatureApi prov2;
+ private List<PolicyEngineFeatureApi> providers;
+ private PolicyControllerFeatureApi contProv1;
+ private PolicyControllerFeatureApi contProv2;
+ private List<PolicyControllerFeatureApi> contProviders;
private String[] globalInitArgs;
private TopicSource source1;
private TopicSource source2;
private PdpdConfiguration pdpConfig;
private String pdpConfigJson;
private PolicyEngineManager mgr;
+ private ScheduledExecutorService exsvc;
+ private PolicyResourceLockManager lockmgr;
/**
* Initializes the object to be tested.
public void setUp() throws Exception {
properties = new Properties();
- prov1 = mock(PolicyEngineFeatureAPI.class);
- prov2 = mock(PolicyEngineFeatureAPI.class);
+ prov1 = mock(PolicyEngineFeatureApi.class);
+ prov2 = mock(PolicyEngineFeatureApi.class);
providers = Arrays.asList(prov1, prov2);
- contProv1 = mock(PolicyControllerFeatureAPI.class);
- contProv2 = mock(PolicyControllerFeatureAPI.class);
+ contProv1 = mock(PolicyControllerFeatureApi.class);
+ contProv2 = mock(PolicyControllerFeatureApi.class);
contProviders = Arrays.asList(contProv1, contProv2);
globalInitArgs = null;
source1 = mock(TopicSource.class);
config3 = new ControllerConfiguration();
config4 = new ControllerConfiguration();
pdpConfig = new PdpdConfiguration();
+ exsvc = mock(ScheduledExecutorService.class);
+ lockmgr = mock(PolicyResourceLockManager.class);
+
+ when(lockmgr.start()).thenReturn(true);
+ when(lockmgr.stop()).thenReturn(true);
+ when(lockmgr.lock()).thenReturn(true);
+ when(lockmgr.unlock()).thenReturn(true);
+
+ when(prov2.beforeCreateLockManager(any(), any())).thenReturn(lockmgr);
when(prov1.getName()).thenReturn(FEATURE1);
when(prov2.getName()).thenReturn(FEATURE2);
when(controller4.lock()).thenReturn(true);
when(controller4.unlock()).thenReturn(true);
- when(endpoint.addTopicSources(any())).thenReturn(sources);
- when(endpoint.addTopicSinks(any())).thenReturn(sinks);
+ when(endpoint.addTopicSources(any(Properties.class))).thenReturn(sources);
+ when(endpoint.addTopicSinks(any(Properties.class))).thenReturn(sinks);
when(endpoint.start()).thenReturn(true);
when(endpoint.stop()).thenReturn(true);
when(endpoint.lock()).thenReturn(true);
assertNotNull(mgr.getEnvironmentProperty("PATH"));
assertNull(mgr.getEnvironmentProperty("unknown-env-property"));
+
+ System.setProperty("propS-a", "valueS-a");
+ assertEquals("valueS-a", mgr.getEnvironmentProperty("propS-a"));
+
+ Properties props3 = new Properties();
+ props3.put("prop3-a", "${env:HOME}");
+ mgr.setEnvironment(props3);
+ assertEquals(System.getenv("HOME"), mgr.getEnvironmentProperty("prop3-a"));
+ assertEquals("valueS-a", mgr.getEnvironmentProperty("propS-a"));
+ assertEquals(newValue, mgr.getEnvironmentProperty(propKey));
}
@Test
assertFalse(config.isEmpty());
}
+ /**
+ * Tests that makeExecutorService() uses the value from the thread
+ * property.
+ */
+ @Test
+ public void testMakeExecutorServicePropertyProvided() {
+ PolicyEngineManager mgrspy = spy(mgr);
+
+ properties.setProperty(PolicyEngineManager.EXECUTOR_THREAD_PROP, "3");
+ mgrspy.configure(properties);
+ assertSame(exsvc, mgrspy.getExecutorService());
+ verify(mgrspy).makeScheduledExecutor(3);
+ }
+
+ /**
+ * Tests that makeExecutorService() uses the default thread count when no thread
+ * property is provided.
+ */
+ @Test
+ public void testMakeExecutorServiceNoProperty() {
+ PolicyEngineManager mgrspy = spy(mgr);
+
+ mgrspy.configure(properties);
+ assertSame(exsvc, mgrspy.getExecutorService());
+ verify(mgrspy).makeScheduledExecutor(PolicyEngineManager.DEFAULT_EXECUTOR_THREADS);
+ }
+
+ /**
+ * Tests that makeExecutorService() uses the default thread count when the thread
+ * property is invalid.
+ */
+ @Test
+ public void testMakeExecutorServiceInvalidProperty() {
+ PolicyEngineManager mgrspy = spy(mgr);
+
+ properties.setProperty(PolicyEngineManager.EXECUTOR_THREAD_PROP, "abc");
+ mgrspy.configure(properties);
+ assertSame(exsvc, mgrspy.getExecutorService());
+ verify(mgrspy).makeScheduledExecutor(PolicyEngineManager.DEFAULT_EXECUTOR_THREADS);
+ }
+
+ /**
+ * Tests createLockManager() when beforeCreateLock throws an exception and returns a
+ * manager.
+ */
+ @Test
+ public void testCreateLockManagerHaveProvider() {
+ // first provider throws an exception
+ when(prov1.beforeCreateLockManager(any(), any())).thenThrow(new RuntimeException(EXPECTED));
+
+ mgr.configure(properties);
+ assertSame(lockmgr, mgr.getLockManager());
+ }
+
+ /**
+ * Tests createLockManager() when SimpleLockManager throws an exception.
+ */
+ @Test
+ public void testCreateLockManagerSimpleEx() {
+ when(prov2.beforeCreateLockManager(any(), any())).thenReturn(null);
+
+ // invalid property for SimpleLockManager
+ properties.setProperty(SimpleLockProperties.EXPIRE_CHECK_SEC, "abc");
+ mgr.configure(properties);
+
+ // should create a manager using default properties
+ assertTrue(mgr.getLockManager() instanceof SimpleLockManager);
+ }
+
+ /**
+ * Tests createLockManager() when SimpleLockManager is returned.
+ */
+ @Test
+ public void testCreateLockManagerSimple() {
+ when(prov2.beforeCreateLockManager(any(), any())).thenReturn(null);
+
+ mgr.configure(properties);
+ assertTrue(mgr.getLockManager() instanceof SimpleLockManager);
+ }
+
@Test
public void testConfigureProperties() throws Exception {
// arrange for first provider to throw exceptions
// source list of size 1
setUp();
- when(endpoint.addTopicSources(any())).thenReturn(Arrays.asList(source1));
+ when(endpoint.addTopicSources(any(Properties.class))).thenReturn(Arrays.asList(source1));
mgr.configure(properties);
assertTrue(mgr.configure(pdpConfig));
// empty name in properties - same result
setUp();
- properties.setProperty(DroolsProperties.PROPERTY_CONTROLLER_NAME, "");
+ properties.setProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, "");
assertEquals(controller, mgr.createPolicyController(MY_NAME, properties));
verify(contProv1).beforeCreate(MY_NAME, properties);
// matching name in properties - same result
setUp();
- properties.setProperty(DroolsProperties.PROPERTY_CONTROLLER_NAME, MY_NAME);
+ properties.setProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, MY_NAME);
assertEquals(controller, mgr.createPolicyController(MY_NAME, properties));
verify(contProv1).beforeCreate(MY_NAME, properties);
// mismatching name in properties - nothing should happen besides exception
setUp();
- properties.setProperty(DroolsProperties.PROPERTY_CONTROLLER_NAME, "mistmatched-name");
+ properties.setProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, "mistmatched-name");
assertThatIllegalStateException().isThrownBy(() -> mgr.createPolicyController(MY_NAME, properties));
verify(contProv1, never()).beforeCreate(MY_NAME, properties);
when(sink1.start()).thenThrow(new RuntimeException(EXPECTED));
});
+ // lock manager fails to start - still does everything
+ testStart(false, () -> when(lockmgr.start()).thenReturn(false));
+
+ // lock manager throws an exception - still does everything
+ testStart(false, () -> when(lockmgr.start()).thenThrow(new RuntimeException(EXPECTED)));
+
// servlet wait fails - still does everything
testStart(false, () -> when(server1.waitedStart(anyLong())).thenReturn(false));
- // topic source fails to start - still does everything
- testStart(false, () -> when(source1.start()).thenReturn(false));
+ // topic source is not started with start
+ testStart(true, () -> when(source1.start()).thenReturn(false));
- // topic sink fails to start - still does everything
- testStart(false, () -> when(sink1.start()).thenReturn(false));
+ // topic sink is not started with start
+ testStart(true, () -> when(sink1.start()).thenReturn(false));
// controller fails to start - still does everything
testStart(false, () -> when(controller.start()).thenReturn(false));
verify(server1).waitedStart(anyLong());
verify(server2).waitedStart(anyLong());
- verify(source1).start();
- verify(source2).start();
+ verify(source1, never()).start();
+ verify(source2, never()).start();
- verify(sink1).start();
- verify(sink2).start();
+ verify(sink1, never()).start();
+ verify(sink2, never()).start();
verify(controller).start();
verify(controller2).start();
// servlet fails to stop - still does everything
testStop(false, () -> when(server1.stop()).thenReturn(false));
+ // lock manager fails to stop - still does everything
+ testStop(false, () -> when(lockmgr.stop()).thenReturn(false));
+
+ // lock manager throws an exception - still does everything
+ testStop(false, () -> when(lockmgr.stop()).thenThrow(new RuntimeException(EXPECTED)));
+
// other tests
checkBeforeAfter(
(prov, flag) -> when(prov.beforeStop(mgr)).thenReturn(flag),
assertTrue(threadStarted);
assertTrue(threadInterrupted);
+
+ // lock manager throws an exception - still does everything
+ testShutdown(() -> doThrow(new RuntimeException(EXPECTED)).when(lockmgr).shutdown());
+
// other tests
checkBeforeAfter(
(prov, flag) -> when(prov.beforeShutdown(mgr)).thenReturn(flag),
verify(prov1).afterShutdown(mgr);
verify(prov2).afterShutdown(mgr);
+
+ verify(exsvc).shutdownNow();
}
@Test
// endpoint manager fails to lock - still does everything
testLock(false, () -> when(endpoint.lock()).thenReturn(false));
+ // lock manager fails to lock - still does everything
+ testLock(false, () -> when(lockmgr.lock()).thenReturn(false));
+
+ // lock manager throws an exception - still does everything
+ testLock(false, () -> when(lockmgr.lock()).thenThrow(new RuntimeException(EXPECTED)));
+
// other tests
checkBeforeAfter(
(prov, flag) -> when(prov.beforeLock(mgr)).thenReturn(flag),
// endpoint manager fails to unlock - still does everything
testUnlock(false, () -> when(endpoint.unlock()).thenReturn(false));
+ // lock manager fails to lock - still does everything
+ testUnlock(false, () -> when(lockmgr.unlock()).thenReturn(false));
+
+ // lock manager throws an exception - still does everything
+ testUnlock(false, () -> when(lockmgr.unlock()).thenThrow(new RuntimeException(EXPECTED)));
+
// other tests
checkBeforeAfter(
(prov, flag) -> when(prov.beforeUnlock(mgr)).thenReturn(flag),
verify(prov2).afterDeactivate(mgr);
}
+ @Test
+ public void testCreateLock() {
+ Lock lock = mock(Lock.class);
+ LockCallback callback = mock(LockCallback.class);
+ when(lockmgr.createLock(MY_RESOURCE, MY_OWNER, 10, callback, false)).thenReturn(lock);
+
+ // not configured yet, thus no lock manager
+ assertThatIllegalStateException()
+ .isThrownBy(() -> mgr.createLock(MY_RESOURCE, MY_OWNER, 10, callback, false));
+
+ // now configure it and try again
+ mgr.configure(properties);
+ assertSame(lock, mgr.createLock(MY_RESOURCE, MY_OWNER, 10, callback, false));
+
+ // test illegal args
+ assertThatThrownBy(() -> mgr.createLock(null, MY_OWNER, 10, callback, false))
+ .hasMessageContaining("resourceId");
+ assertThatThrownBy(() -> mgr.createLock(MY_RESOURCE, null, 10, callback, false))
+ .hasMessageContaining("ownerKey");
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> mgr.createLock(MY_RESOURCE, MY_OWNER, -1, callback, false))
+ .withMessageContaining("holdSec");
+ assertThatThrownBy(() -> mgr.createLock(MY_RESOURCE, MY_OWNER, 10, null, false))
+ .hasMessageContaining("callback");
+ }
+
+ @Test
+ public void testOpen() throws Throwable {
+ when(prov1.beforeOpen(mgr)).thenThrow(new RuntimeException(EXPECTED));
+ when(prov1.afterOpen(mgr)).thenThrow(new RuntimeException(EXPECTED));
+
+ assertTrue(mgr.lock());
+ assertThatIllegalStateException().isThrownBy(() -> mgr.open());
+ unsuccessfulOpen();
+
+ assertTrue(mgr.unlock());
+ unsuccessfulOpen();
+
+ setUp();
+ mgr.configure(properties);
+ assertTrue(mgr.start());
+
+ verify(source1, never()).start();
+ verify(source2, never()).start();
+
+ assertTrue(mgr.open());
+
+ verify(prov1).beforeOpen(mgr);
+ verify(prov2).beforeOpen(mgr);
+
+ verify(source1).start();
+ verify(source2).start();
+
+ verify(prov1).afterOpen(mgr);
+ verify(prov2).afterOpen(mgr);
+
+ when(source1.start()).thenReturn(false);
+ assertFalse(mgr.open());
+ when(source1.start()).thenReturn(true);
+
+ when(sink1.start()).thenReturn(false);
+ assertFalse(mgr.open());
+ when(sink1.start()).thenReturn(true);
+
+ assertTrue(mgr.open());
+ }
+
+ private void unsuccessfulOpen() {
+ verify(prov1).beforeOpen(mgr);
+ verify(prov2).beforeOpen(mgr);
+
+ verify(prov1, never()).afterOpen(mgr);
+ verify(prov2, never()).afterOpen(mgr);
+
+ verify(source1, never()).start();
+ verify(source2, never()).start();
+
+ verify(sink1, never()).start();
+ verify(sink2, never()).start();
+ }
+
@Test
public void testControllerConfig() throws Exception {
mgr.configure(properties);
* @param verifyAfter verifies that a provider's afterXxx method was invoked
* @throws Exception if an error occurs while calling {@link #setUp()}
*/
- private void checkBeforeAfter(BiConsumer<PolicyEngineFeatureAPI, Boolean> setBefore,
- BiConsumer<PolicyEngineFeatureAPI, Boolean> setAfter, Runnable action,
- Consumer<PolicyEngineFeatureAPI> verifyBefore, Runnable verifyMiddle,
- Consumer<PolicyEngineFeatureAPI> verifyAfter) throws Exception {
+ private void checkBeforeAfter(BiConsumer<PolicyEngineFeatureApi, Boolean> setBefore,
+ BiConsumer<PolicyEngineFeatureApi, Boolean> setAfter, Runnable action,
+ Consumer<PolicyEngineFeatureApi> verifyBefore, Runnable verifyMiddle,
+ Consumer<PolicyEngineFeatureApi> verifyAfter) throws Exception {
checkBeforeAfter_FalseFalse(setBefore, setAfter, action, verifyBefore, verifyMiddle, verifyAfter);
checkBeforeAfter_FalseTrue(setBefore, setAfter, action, verifyBefore, verifyMiddle, verifyAfter);
* @param verifyAfter verifies that a provider's afterXxx method was invoked
* @throws Exception if an error occurs while calling {@link #setUp()}
*/
- private void checkBeforeAfter_FalseFalse(BiConsumer<PolicyEngineFeatureAPI, Boolean> setBefore,
- BiConsumer<PolicyEngineFeatureAPI, Boolean> setAfter, Runnable action,
- Consumer<PolicyEngineFeatureAPI> verifyBefore, Runnable verifyMiddle,
- Consumer<PolicyEngineFeatureAPI> verifyAfter) throws Exception {
+ private void checkBeforeAfter_FalseFalse(BiConsumer<PolicyEngineFeatureApi, Boolean> setBefore,
+ BiConsumer<PolicyEngineFeatureApi, Boolean> setAfter, Runnable action,
+ Consumer<PolicyEngineFeatureApi> verifyBefore, Runnable verifyMiddle,
+ Consumer<PolicyEngineFeatureApi> verifyAfter) throws Exception {
setUp();
* @param verifyAfter verifies that a provider's afterXxx method was invoked
* @throws Exception if an error occurs while calling {@link #setUp()}
*/
- private void checkBeforeAfter_FalseTrue(BiConsumer<PolicyEngineFeatureAPI, Boolean> setBefore,
- BiConsumer<PolicyEngineFeatureAPI, Boolean> setAfter, Runnable action,
- Consumer<PolicyEngineFeatureAPI> verifyBefore, Runnable verifyMiddle,
- Consumer<PolicyEngineFeatureAPI> verifyAfter) throws Exception {
+ private void checkBeforeAfter_FalseTrue(BiConsumer<PolicyEngineFeatureApi, Boolean> setBefore,
+ BiConsumer<PolicyEngineFeatureApi, Boolean> setAfter, Runnable action,
+ Consumer<PolicyEngineFeatureApi> verifyBefore, Runnable verifyMiddle,
+ Consumer<PolicyEngineFeatureApi> verifyAfter) throws Exception {
setUp();
* @param verifyAfter verifies that a provider's afterXxx method was invoked
* @throws Exception if an error occurs while calling {@link #setUp()}
*/
- private void checkBeforeAfter_TrueFalse(BiConsumer<PolicyEngineFeatureAPI, Boolean> setBefore,
- BiConsumer<PolicyEngineFeatureAPI, Boolean> setAfter, Runnable action,
- Consumer<PolicyEngineFeatureAPI> verifyBefore, Runnable verifyMiddle,
- Consumer<PolicyEngineFeatureAPI> verifyAfter) throws Exception {
+ private void checkBeforeAfter_TrueFalse(BiConsumer<PolicyEngineFeatureApi, Boolean> setBefore,
+ BiConsumer<PolicyEngineFeatureApi, Boolean> setAfter, Runnable action,
+ Consumer<PolicyEngineFeatureApi> verifyBefore, Runnable verifyMiddle,
+ Consumer<PolicyEngineFeatureApi> verifyAfter) throws Exception {
setUp();
private class PolicyEngineManagerImpl extends PolicyEngineManager {
@Override
- protected List<PolicyEngineFeatureAPI> getEngineProviders() {
+ protected List<PolicyEngineFeatureApi> getEngineProviders() {
return providers;
}
@Override
- protected List<PolicyControllerFeatureAPI> getControllerProviders() {
+ protected List<PolicyControllerFeatureApi> getControllerProviders() {
return contProviders;
}
return engine;
}
+ @Override
+ protected ScheduledExecutorService makeScheduledExecutor(int nthreads) {
+ return exsvc;
+ }
+
/**
* Shutdown thread with overrides.
*/