summaryrefslogtreecommitdiffstats
path: root/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java')
-rw-r--r--feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java2093
1 files changed, 1045 insertions, 1048 deletions
diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java
index a7c33aba..27ac2cc1 100644
--- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java
+++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/PersistenceFeatureTest.java
@@ -2,14 +2,14 @@
* ============LICENSE_START=======================================================
* feature-session-persistence
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
* 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.
@@ -29,12 +29,12 @@ import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
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 static org.mockito.Mockito.doThrow;
import java.io.FileNotFoundException;
import java.io.FileReader;
@@ -83,1335 +83,1332 @@ import org.slf4j.LoggerFactory;
public class PersistenceFeatureTest {
- private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class);
-
- private static final String JDBC_DRIVER = "fake.driver";
- private static final String JDBC_URL = "fake.url";
- private static final String JDBC_USER = "fake.user";
- private static final String JDBC_PASSWD = "fake.password";
- private static final String JTA_OSDIR = "target";
- private static final String SRC_TEST_RESOURCES = "src/test/resources";
-
- private static Properties stdprops;
-
- private JpaDroolsSessionConnector jpa;
- private DroolsSession sess;
- private KieSession kiesess;
- private BasicDataSource bds;
- private EntityManagerFactory emf;
- private Connection conn;
- private Properties props;
- private KieServices kiesvc;
- private Environment kieenv;
- private KieSessionConfiguration kiecfg;
- private KieBase kiebase;
- private KieStoreServices kiestore;
- private KieContainer kiecont;
- private TransactionManager transmgr;
- private UserTransaction usertrans;
- private TransactionSynchronizationRegistry transreg;
- private PolicyController polctlr;
- private PolicyContainer polcont;
- private PolicySession polsess;
- private PersistenceFeature.Factory fact;
-
- private PersistenceFeature feat;
-
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- stdprops = new Properties();
-
- stdprops.put(DroolsPersistenceProperties.DB_DRIVER, JDBC_DRIVER);
- stdprops.put(DroolsPersistenceProperties.DB_URL, JDBC_URL);
- stdprops.put(DroolsPersistenceProperties.DB_USER, JDBC_USER);
- stdprops.put(DroolsPersistenceProperties.DB_PWD, JDBC_PASSWD);
- stdprops.put(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR, JTA_OSDIR);
- stdprops.put(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "50");
-
- System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
- System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
- }
-
- @Before
- public void setUp() throws Exception {
- jpa = mock(JpaDroolsSessionConnector.class);
- sess = mock(DroolsSession.class);
- bds = mock(BasicDataSource.class);
- emf = mock(EntityManagerFactory.class);
- kiesess = mock(KieSession.class);
- conn = null;
- props = new Properties();
- kiesvc = mock(KieServices.class);
- kieenv = mock(Environment.class);
- kiecfg = mock(KieSessionConfiguration.class);
- kiebase = mock(KieBase.class);
- kiestore = mock(KieStoreServices.class);
- kiecont = mock(KieContainer.class);
- transmgr = mock(TransactionManager.class);
- usertrans = mock(UserTransaction.class);
- transreg = mock(TransactionSynchronizationRegistry.class);
- polcont = mock(PolicyContainer.class);
- polctlr = mock(PolicyController.class);
- polsess = mock(PolicySession.class);
- fact = mock(PersistenceFeature.Factory.class);
-
- feat = new PersistenceFeature();
- feat.setFactory(fact);
-
- props.putAll(stdprops);
-
- System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
- System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
-
- when(fact.getKieServices()).thenReturn(kiesvc);
- when(fact.getTransMgr()).thenReturn(transmgr);
- when(fact.getUserTrans()).thenReturn(usertrans);
- when(fact.getTransSyncReg()).thenReturn(transreg);
- when(fact.loadProperties(anyString())).thenReturn(props);
-
- when(kiesvc.newEnvironment()).thenReturn(kieenv);
- when(kiesvc.getStoreServices()).thenReturn(kiestore);
- when(kiesvc.newKieSessionConfiguration()).thenReturn(kiecfg);
-
- when(polcont.getKieContainer()).thenReturn(kiecont);
-
- when(polsess.getPolicyContainer()).thenReturn(polcont);
-
- when(kiecont.getKieBase(anyString())).thenReturn(kiebase);
- }
-
- @After
- public void tearDown() {
- // this will cause the in-memory test DB to be dropped
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- logger.warn("failed to close connection", e);
- }
- }
-
- if (emf != null) {
- try {
- emf.close();
- } catch (IllegalArgumentException e) {
- logger.trace("ignored exception", e);
- }
- }
- }
-
- @Test
- public void testGetContainerAdjunct_New() throws Exception {
-
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ private static final Logger logger = LoggerFactory.getLogger(PersistenceFeatureTest.class);
+
+ private static final String JDBC_DRIVER = "fake.driver";
+ private static final String JDBC_URL = "fake.url";
+ private static final String JDBC_USER = "fake.user";
+ private static final String JDBC_PASSWD = "fake.password";
+ private static final String JTA_OSDIR = "target";
+ private static final String SRC_TEST_RESOURCES = "src/test/resources";
+
+ private static Properties stdprops;
+
+ private JpaDroolsSessionConnector jpa;
+ private DroolsSession sess;
+ private KieSession kiesess;
+ private BasicDataSource bds;
+ private EntityManagerFactory emf;
+ private Connection conn;
+ private Properties props;
+ private KieServices kiesvc;
+ private Environment kieenv;
+ private KieSessionConfiguration kiecfg;
+ private KieBase kiebase;
+ private KieStoreServices kiestore;
+ private KieContainer kiecont;
+ private TransactionManager transmgr;
+ private UserTransaction usertrans;
+ private TransactionSynchronizationRegistry transreg;
+ private PolicyController polctlr;
+ private PolicyContainer polcont;
+ private PolicySession polsess;
+ private PersistenceFeature.Factory fact;
+
+ private PersistenceFeature feat;
+
+ /**
+ * Setup before class.
+ *
+ * @throws Exception exception
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ stdprops = new Properties();
+
+ stdprops.put(DroolsPersistenceProperties.DB_DRIVER, JDBC_DRIVER);
+ stdprops.put(DroolsPersistenceProperties.DB_URL, JDBC_URL);
+ stdprops.put(DroolsPersistenceProperties.DB_USER, JDBC_USER);
+ stdprops.put(DroolsPersistenceProperties.DB_PWD, JDBC_PASSWD);
+ stdprops.put(DroolsPersistenceProperties.JTA_OBJECTSTORE_DIR, JTA_OSDIR);
+ stdprops.put(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "50");
+
+ System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
+ System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
+ }
+
+ /**
+ * Setup.
+ *
+ * @throws Exception exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ jpa = mock(JpaDroolsSessionConnector.class);
+ sess = mock(DroolsSession.class);
+ bds = mock(BasicDataSource.class);
+ emf = mock(EntityManagerFactory.class);
+ kiesess = mock(KieSession.class);
+ conn = null;
+ props = new Properties();
+ kiesvc = mock(KieServices.class);
+ kieenv = mock(Environment.class);
+ kiecfg = mock(KieSessionConfiguration.class);
+ kiebase = mock(KieBase.class);
+ kiestore = mock(KieStoreServices.class);
+ kiecont = mock(KieContainer.class);
+ transmgr = mock(TransactionManager.class);
+ usertrans = mock(UserTransaction.class);
+ transreg = mock(TransactionSynchronizationRegistry.class);
+ polcont = mock(PolicyContainer.class);
+ polctlr = mock(PolicyController.class);
+ polsess = mock(PolicySession.class);
+ fact = mock(PersistenceFeature.Factory.class);
+
+ feat = new PersistenceFeature();
+ feat.setFactory(fact);
+
+ props.putAll(stdprops);
+
+ System.setProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir", "target/tm");
+ System.setProperty("ObjectStoreEnvironmentBean.objectStoreDir", "target/tm");
+
+ when(fact.getKieServices()).thenReturn(kiesvc);
+ when(fact.getTransMgr()).thenReturn(transmgr);
+ when(fact.getUserTrans()).thenReturn(usertrans);
+ when(fact.getTransSyncReg()).thenReturn(transreg);
+ when(fact.loadProperties(anyString())).thenReturn(props);
+
+ when(kiesvc.newEnvironment()).thenReturn(kieenv);
+ when(kiesvc.getStoreServices()).thenReturn(kiestore);
+ when(kiesvc.newKieSessionConfiguration()).thenReturn(kiecfg);
+
+ when(polcont.getKieContainer()).thenReturn(kiecont);
+
+ when(polsess.getPolicyContainer()).thenReturn(polcont);
+
+ when(kiecont.getKieBase(anyString())).thenReturn(kiebase);
+ }
+
+ /**
+ * Tear down.
+ */
+ @After
+ public void tearDown() {
+ // this will cause the in-memory test DB to be dropped
+ if (conn != null) {
+ try {
+ conn.close();
+ } catch (SQLException e) {
+ logger.warn("failed to close connection", e);
+ }
+ }
+
+ if (emf != null) {
+ try {
+ emf.close();
+ } catch (IllegalArgumentException e) {
+ logger.trace("ignored exception", e);
+ }
+ }
+ }
+
+ @Test
+ public void testGetContainerAdjunct_New() throws Exception {
+
+ feat.globalInit(null, SRC_TEST_RESOURCES);
+
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
+
+ // force getContainerAdjunct() to be invoked
+ feat.activatePolicySession(polcont, "myname", "mybase");
+
+ ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
+
+ verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
+
+ assertNotNull(adjcap.getValue());
+ }
+
+ @Test
+ public void testGetContainerAdjunct_Existing() throws Exception {
+
+ feat.globalInit(null, SRC_TEST_RESOURCES);
+
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
+
+ // force getContainerAdjunct() to be invoked
+ feat.activatePolicySession(polcont, "myname", "mybase");
+
+ ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
+
+ verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
+
+ // return adjunct on next call
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+
+ // force getContainerAdjunct() to be invoked again
+ setUpKie("myname2", 999L, true);
+ feat.activatePolicySession(polcont, "myname2", "mybase");
+
+ // ensure it isn't invoked again
+ verify(polcont, times(1)).setAdjunct(any(), any());
+ }
+
+ @Test
+ public void testGetContainerAdjunct_WrongType() throws Exception {
+
+ feat.globalInit(null, SRC_TEST_RESOURCES);
+
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
+
+ // return false adjunct on next call
+ when(polcont.getAdjunct(any())).thenReturn("not-a-real-adjunct");
+
+ // force getContainerAdjunct() to be invoked
+ setUpKie("myname2", 999L, true);
+ feat.activatePolicySession(polcont, "myname2", "mybase");
+
+ ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
+
+ verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
+
+ assertNotNull(adjcap.getValue());
+ }
+
+ @Test
+ public void testGetSequenceNumber() {
+ assertEquals(1, feat.getSequenceNumber());
+ }
+
+ @Test
+ public void testGlobalInit() throws Exception {
+
+ feat.globalInit(null, SRC_TEST_RESOURCES);
+
+ // verify that various factory methods were invoked
+ verify(fact).getKieServices();
+ verify(fact).loadProperties("src/test/resources/feature-session-persistence.properties");
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testGlobalInitIoEx() throws Exception {
+
+ when(fact.loadProperties(anyString())).thenThrow(new IOException("expected exception"));
+
+ feat.globalInit(null, SRC_TEST_RESOURCES);
+ }
+
+ @Test
+ public void testActivatePolicySession() throws Exception {
+ final PreparedStatement ps = mockDbConn(5);
+ setUpKie("myname", 999L, true);
+
+ feat.globalInit(null, SRC_TEST_RESOURCES);
+ feat.beforeActivate(null);
+
+ KieSession session = feat.activatePolicySession(polcont, "myname", "mybase");
+
+ verify(kiestore).loadKieSession(anyLong(), any(), any(), any());
+ verify(kiestore, never()).newKieSession(any(), any(), any());
+
+ assertEquals(session, kiesess);
+
+ verify(ps).executeUpdate();
+
+ verify(kieenv, times(4)).set(anyString(), any());
+
+ verify(jpa).get("myname");
+ verify(jpa).replace(any());
+ }
+
+ @Test
+ public void testActivatePolicySession_NoPersistence() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ final PreparedStatement ps = mockDbConn(5);
+ setUpKie("myname", 999L, true);
- // force getContainerAdjunct() to be invoked
- feat.activatePolicySession(polcont, "myname", "mybase");
+ props.remove("persistence.type");
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ feat.beforeStart(null);
- verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
+ assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- assertNotNull(adjcap.getValue());
- }
+ verify(ps, never()).executeUpdate();
+ verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any());
+ verify(kiestore, never()).newKieSession(any(), any(), any());
+ }
- @Test
- public void testGetContainerAdjunct_Existing() throws Exception {
+ /** Verifies that a new KIE session is created when there is no existing session entity. */
+ @Test
+ public void testActivatePolicySession_New() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("noName", 999L, true);
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ KieSession session = feat.activatePolicySession(polcont, "myname", "mybase");
- // force getContainerAdjunct() to be invoked
- feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any());
+ verify(kiestore).newKieSession(any(), any(), any());
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ assertEquals(session, kiesess);
- verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
+ verify(kieenv, times(4)).set(anyString(), any());
- // return adjunct on next call
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+ verify(jpa).get("myname");
+ verify(jpa).replace(any());
+ }
- // force getContainerAdjunct() to be invoked again
- setUpKie("myname2", 999L, true);
- feat.activatePolicySession(polcont, "myname2", "mybase");
+ /**
+ * Verifies that a new KIE session is created when there KIE fails to load an existing session.
+ */
+ @Test
+ public void testActivatePolicySession_LoadFailed() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- // ensure it isn't invoked again
- verify(polcont, times(1)).setAdjunct(any(), any());
- }
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- @Test
- public void testGetContainerAdjunct_WrongType() throws Exception {
+ KieSession session = feat.activatePolicySession(polcont, "myname", "mybase");
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ verify(kiestore).loadKieSession(anyLong(), any(), any(), any());
+ verify(kiestore).newKieSession(any(), any(), any());
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ assertEquals(session, kiesess);
- // return false adjunct on next call
- when(polcont.getAdjunct(any())).thenReturn("not-a-real-adjunct");
+ verify(kieenv, times(4)).set(anyString(), any());
- // force getContainerAdjunct() to be invoked
- setUpKie("myname2", 999L, true);
- feat.activatePolicySession(polcont, "myname2", "mybase");
+ verify(jpa).get("myname");
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ ArgumentCaptor<DroolsSession> drools = ArgumentCaptor.forClass(DroolsSession.class);
+ verify(jpa).replace(drools.capture());
- verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
+ assertEquals("myname", drools.getValue().getSessionName());
+ assertEquals(100L, drools.getValue().getSessionId());
+ }
- assertNotNull(adjcap.getValue());
- }
+ @Test
+ public void testLoadDataSource() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testGetSequenceNumber() {
- assertEquals(1, feat.getSequenceNumber());
- }
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- @Test
- public void testGlobalInit() throws Exception {
+ feat.activatePolicySession(polcont, "myname", "mybase");
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ verify(fact).makeEntMgrFact(any());
+ }
- // verify that various factory methods were invoked
- verify(fact).getKieServices();
- verify(fact).loadProperties("src/test/resources/feature-session-persistence.properties");
- }
+ @Test
+ public void testConfigureSysProps() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test(expected = NullPointerException.class)
- public void testGlobalInit_IOEx() throws Exception {
-
- when(fact.loadProperties(anyString())).thenThrow(new IOException("expected exception"));
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- feat.globalInit(null, SRC_TEST_RESOURCES);
- }
+ feat.activatePolicySession(polcont, "myname", "mybase");
- @Test
- public void testActivatePolicySession() throws Exception {
- PreparedStatement ps = mockDbConn(5);
- setUpKie("myname", 999L, true);
+ assertEquals("60", System.getProperty("com.arjuna.ats.arjuna.coordinator.defaultTimeout"));
+ assertEquals(JTA_OSDIR, System.getProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir"));
+ assertEquals(JTA_OSDIR, System.getProperty("ObjectStoreEnvironmentBean.objectStoreDir"));
+ }
- feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.beforeActivate(null);
+ @Test
+ public void testConfigureKieEnv() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- KieSession s = feat.activatePolicySession(polcont, "myname", "mybase");
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- verify(kiestore).loadKieSession(anyLong(), any(), any(), any());
- verify(kiestore, never()).newKieSession(any(), any(), any());
+ feat.activatePolicySession(polcont, "myname", "mybase");
- assertEquals(s, kiesess);
+ verify(kieenv, times(4)).set(any(), any());
- verify(ps).executeUpdate();
+ verify(kieenv).set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
+ verify(kieenv).set(EnvironmentName.TRANSACTION, usertrans);
+ verify(kieenv).set(EnvironmentName.TRANSACTION_MANAGER, transmgr);
+ verify(kieenv).set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, transreg);
- verify(kieenv, times(4)).set(anyString(), any());
+ verify(bds, times(1)).close();
+ }
- verify(jpa).get("myname");
- verify(jpa).replace(any());
- }
+ @Test
+ public void testConfigureKieEnv_RtEx() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testActivatePolicySession_NoPersistence() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- PreparedStatement ps = mockDbConn(5);
- setUpKie("myname", 999L, true);
+ when(fact.getUserTrans()).thenThrow(new IllegalArgumentException("expected exception"));
- props.remove("persistence.type");
+ try {
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ fail("missing exception");
- feat.beforeStart(null);
+ } catch (IllegalArgumentException ex) {
+ logger.trace("expected exception", ex);
+ }
- assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ verify(bds, times(2)).close();
+ }
- verify(ps, never()).executeUpdate();
- verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any());
- verify(kiestore, never()).newKieSession(any(), any(), any());
- }
+ @Test
+ public void testLoadKieSession() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- /**
- * Verifies that a new KIE session is created when there is no existing
- * session entity.
- */
- @Test
- public void testActivatePolicySession_New() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- mockDbConn(5);
- setUpKie("noName", 999L, true);
+ KieSession session = feat.activatePolicySession(polcont, "myname", "mybase");
- KieSession s = feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(kiestore).loadKieSession(999L, kiebase, kiecfg, kieenv);
+ verify(kiestore, never()).newKieSession(any(), any(), any());
- verify(kiestore, never()).loadKieSession(anyLong(), any(), any(), any());
- verify(kiestore).newKieSession(any(), any(), any());
+ assertEquals(session, kiesess);
+ }
- assertEquals(s, kiesess);
+ /*
+ * Verifies that loadKieSession() returns null (thus causing newKieSession()
+ * to be called) when an Exception occurs.
+ */
+ @Test
+ public void testLoadKieSession_Ex() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- verify(kieenv, times(4)).set(anyString(), any());
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- verify(jpa).get("myname");
- verify(jpa).replace(any());
- }
+ when(kiestore.loadKieSession(anyLong(), any(), any(), any()))
+ .thenThrow(new IllegalArgumentException("expected exception"));
- /**
- * Verifies that a new KIE session is created when there KIE fails to load
- * an existing session.
- */
- @Test
- public void testActivatePolicySession_LoadFailed() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ KieSession session = feat.activatePolicySession(polcont, "myname", "mybase");
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ verify(kiestore).loadKieSession(anyLong(), any(), any(), any());
+ verify(kiestore).newKieSession(any(), any(), any());
- KieSession s = feat.activatePolicySession(polcont, "myname", "mybase");
+ assertEquals(session, kiesess);
+ }
- verify(kiestore).loadKieSession(anyLong(), any(), any(), any());
- verify(kiestore).newKieSession(any(), any(), any());
+ @Test
+ public void testNewKieSession() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- assertEquals(s, kiesess);
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- verify(kieenv, times(4)).set(anyString(), any());
+ KieSession session = feat.activatePolicySession(polcont, "myname", "mybase");
- verify(jpa).get("myname");
+ verify(kiestore).newKieSession(kiebase, null, kieenv);
- ArgumentCaptor<DroolsSession> d = ArgumentCaptor.forClass(DroolsSession.class);
- verify(jpa).replace(d.capture());
+ assertEquals(session, kiesess);
+ }
- assertEquals("myname", d.getValue().getSessionName());
- assertEquals(100L, d.getValue().getSessionId());
- }
+ @Test
+ public void testLoadDataSource_DiffSession() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testLoadDataSource() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
- feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(polcont).setAdjunct(any(), adjcap.capture());
- verify(fact).makeEntMgrFact(any());
- }
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
- @Test
- public void testConfigureSysProps() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ setUpKie("myname2", 999L, false);
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ // invoke it again
+ feat.activatePolicySession(polcont, "myname2", "mybase");
- feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(fact, times(2)).makeEntMgrFact(any());
+ }
- assertEquals("60", System.getProperty("com.arjuna.ats.arjuna.coordinator.defaultTimeout"));
- assertEquals(JTA_OSDIR, System.getProperty("com.arjuna.ats.arjuna.objectstore.objectStoreDir"));
- assertEquals(JTA_OSDIR, System.getProperty("ObjectStoreEnvironmentBean.objectStoreDir"));
- }
+ @Test
+ public void testSelectThreadModel_Persistent() throws Exception {
+ setUpKie("myname", 999L, true);
- @Test
- public void testConfigureKieEnv() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ ThreadModel model = feat.selectThreadModel(polsess);
+ assertNotNull(model);
+ assertTrue(model instanceof PersistentThreadModel);
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ @Test
+ public void testSelectThreadModel_NotPersistent() throws Exception {
+ when(fact.getPolicyController(any())).thenReturn(polctlr);
+ assertNull(feat.selectThreadModel(polsess));
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testSelectThreadModel_Start__Run_Update_Stop() throws Exception {
+ setUpKie("myname", 999L, true);
- verify(kieenv, times(4)).set(any(), any());
+ ThreadModel model = feat.selectThreadModel(polsess);
+ assertNotNull(model);
+ assertTrue(model instanceof PersistentThreadModel);
- verify(kieenv).set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
- verify(kieenv).set(EnvironmentName.TRANSACTION, usertrans);
- verify(kieenv).set(EnvironmentName.TRANSACTION_MANAGER, transmgr);
- verify(kieenv).set(EnvironmentName.TRANSACTION_SYNCHRONIZATION_REGISTRY, transreg);
-
- verify(bds, times(1)).close();
- }
+ when(polsess.getKieSession()).thenReturn(kiesess);
- @Test
- public void testConfigureKieEnv_RtEx() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ model.start();
+ new CountDownLatch(1).await(10, TimeUnit.MILLISECONDS);
+ model.updated();
+ model.stop();
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
-
- when(fact.getUserTrans()).thenThrow(new IllegalArgumentException("expected exception"));
+ @Test
+ public void testDisposeKieSession() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- try {
- feat.activatePolicySession(polcont, "myname", "mybase");
- fail("missing exception");
-
- } catch(IllegalArgumentException ex) {
- logger.trace("expected exception", ex);
- }
+ final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
- verify(bds, times(2)).close();
- }
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- @Test
- public void testLoadKieSession() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ verify(emf, never()).close();
+ verify(polcont).setAdjunct(any(), adjcap.capture());
- KieSession s = feat.activatePolicySession(polcont, "myname", "mybase");
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
- verify(kiestore).loadKieSession(999L, kiebase, kiecfg, kieenv);
- verify(kiestore, never()).newKieSession(any(), any(), any());
+ feat.disposeKieSession(polsess);
- assertEquals(s, kiesess);
- }
+ // call twice to ensure it isn't re-closed
+ feat.disposeKieSession(polsess);
- /*
- * Verifies that loadKieSession() returns null (thus causing newKieSession()
- * to be called) when an Exception occurs.
- */
- @Test
- public void testLoadKieSession_Ex() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ verify(emf, times(1)).close();
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ @Test
+ public void testDisposeKieSession_NoAdjunct() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- when(kiestore.loadKieSession(anyLong(), any(), any(), any()))
- .thenThrow(new IllegalArgumentException("expected exception"));
+ feat.disposeKieSession(polsess);
+ }
- KieSession s = feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testDisposeKieSession_NoPersistence() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- verify(kiestore).loadKieSession(anyLong(), any(), any(), any());
- verify(kiestore).newKieSession(any(), any(), any());
+ final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
- assertEquals(s, kiesess);
- }
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- @Test
- public void testNewKieSession() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ verify(emf, never()).close();
+ verify(polcont).setAdjunct(any(), adjcap.capture());
- KieSession s = feat.activatePolicySession(polcont, "myname", "mybase");
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
- verify(kiestore).newKieSession(kiebase, null, kieenv);
+ // specify a session that was never loaded
+ when(polsess.getName()).thenReturn("anotherName");
- assertEquals(s, kiesess);
- }
+ feat.disposeKieSession(polsess);
- @Test
- public void testLoadDataSource_DiffSession() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ verify(emf, never()).close();
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testDestroyKieSession() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
- verify(polcont).setAdjunct(any(), adjcap.capture());
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+ feat.activatePolicySession(polcont, "myname", "mybase");
- setUpKie("myname2", 999L, false);
+ verify(emf, never()).close();
+ verify(polcont).setAdjunct(any(), adjcap.capture());
- // invoke it again
- feat.activatePolicySession(polcont, "myname2", "mybase");
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
- verify(fact, times(2)).makeEntMgrFact(any());
- }
-
- @Test
- public void testSelectThreadModel_Persistent() throws Exception {
- setUpKie("myname", 999L, true);
-
- ThreadModel m = feat.selectThreadModel(polsess);
- assertNotNull(m);
- assertTrue(m instanceof PersistentThreadModel);
-
- }
-
- @Test
- public void testSelectThreadModel_NotPersistent() throws Exception {
- when(fact.getPolicyController(any())).thenReturn(polctlr);
- assertNull(feat.selectThreadModel(polsess));
-
- }
-
- @Test
- public void testSelectThreadModel_Start__Run_Update_Stop() throws Exception {
- setUpKie("myname", 999L, true);
-
- ThreadModel m = feat.selectThreadModel(polsess);
- assertNotNull(m);
- assertTrue(m instanceof PersistentThreadModel);
-
- when(polsess.getKieSession()).thenReturn(kiesess);
-
- m.start();
- new CountDownLatch(1).await(10, TimeUnit.MILLISECONDS);
- m.updated();
- m.stop();
- }
+ feat.destroyKieSession(polsess);
- @Test
- public void testDisposeKieSession() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ // call twice to ensure it isn't re-closed
+ feat.destroyKieSession(polsess);
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ verify(emf, times(1)).close();
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ @Test
+ public void testDestroyKieSession_NoAdjunct() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.activatePolicySession(polcont, "myname", "mybase");
+ feat.destroyKieSession(polsess);
+ }
- verify(emf, never()).close();
- verify(polcont).setAdjunct(any(), adjcap.capture());
+ @Test
+ public void testDestroyKieSession_NoPersistence() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+ final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
- feat.disposeKieSession(polsess);
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- // call twice to ensure it isn't re-closed
- feat.disposeKieSession(polsess);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- verify(emf, times(1)).close();
- }
+ verify(emf, never()).close();
+ verify(polcont).setAdjunct(any(), adjcap.capture());
- @Test
- public void testDisposeKieSession_NoAdjunct() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
- feat.disposeKieSession(polsess);
- }
+ // specify a session that was never loaded
+ when(polsess.getName()).thenReturn("anotherName");
- @Test
- public void testDisposeKieSession_NoPersistence() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ feat.destroyKieSession(polsess);
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ verify(emf, never()).close();
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ @Test
+ public void testAfterStart() {
+ assertFalse(feat.afterStart(null));
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testBeforeStart() {
+ assertFalse(feat.beforeStart(null));
+ }
- verify(emf, never()).close();
- verify(polcont).setAdjunct(any(), adjcap.capture());
+ @Test
+ public void testBeforeShutdown() {
+ assertFalse(feat.beforeShutdown(null));
+ }
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+ @Test
+ public void testAfterShutdown() {
+ assertFalse(feat.afterShutdown(null));
+ }
- // specify a session that was never loaded
- when(polsess.getName()).thenReturn("anotherName");
+ @Test
+ public void testBeforeConfigure() {
+ assertFalse(feat.beforeConfigure(null, null));
+ }
- feat.disposeKieSession(polsess);
+ @Test
+ public void testAfterConfigure() {
+ assertFalse(feat.afterConfigure(null));
+ }
- verify(emf, never()).close();
- }
+ @Test
+ public void testBeforeActivate() {
+ assertFalse(feat.beforeActivate(null));
+ }
- @Test
- public void testDestroyKieSession() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ @Test
+ public void testAfterActivate() {
+ assertFalse(feat.afterActivate(null));
+ }
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ @Test
+ public void testBeforeDeactivate() {
+ assertFalse(feat.beforeDeactivate(null));
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ @Test
+ public void testAfterDeactivate() {
+ assertFalse(feat.afterDeactivate(null));
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testBeforeStop() {
+ assertFalse(feat.beforeStop(null));
+ }
- verify(emf, never()).close();
- verify(polcont).setAdjunct(any(), adjcap.capture());
+ @Test
+ public void testAfterStop() {
+ assertFalse(feat.afterStop(null));
+ }
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+ @Test
+ public void testBeforeLock() {
+ assertFalse(feat.beforeLock(null));
+ }
- feat.destroyKieSession(polsess);
+ @Test
+ public void testAfterLock() {
+ assertFalse(feat.afterLock(null));
+ }
- // call twice to ensure it isn't re-closed
- feat.destroyKieSession(polsess);
+ @Test
+ public void testBeforeUnlock() {
+ assertFalse(feat.beforeUnlock(null));
+ }
- verify(emf, times(1)).close();
- }
+ @Test
+ public void testAfterUnlock() {
+ assertFalse(feat.afterUnlock(null));
+ }
- @Test
- public void testDestroyKieSession_NoAdjunct() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ @Test
+ public void testGetPersistenceTimeout_Valid() throws Exception {
+ final PreparedStatement statement = mockDbConn(5);
- feat.destroyKieSession(polsess);
- }
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testDestroyKieSession_NoPersistence() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ setUpKie("myname", 999L, true);
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- mockDbConn(5);
- setUpKie("myname", 999L, false);
+ verify(statement).executeUpdate();
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testGetPersistenceTimeout_Missing() throws Exception {
- verify(emf, never()).close();
- verify(polcont).setAdjunct(any(), adjcap.capture());
+ props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT);
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+ final PreparedStatement statement = mockDbConn(0);
- // specify a session that was never loaded
- when(polsess.getName()).thenReturn("anotherName");
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.destroyKieSession(polsess);
+ setUpKie("myname", 999L, true);
- verify(emf, never()).close();
- }
+ feat.activatePolicySession(polcont, "myname", "mybase");
- @Test
- public void testAfterStart() {
- assertFalse(feat.afterStart(null));
- }
+ verify(statement, never()).executeUpdate();
+ }
- @Test
- public void testBeforeStart() {
- assertFalse(feat.beforeStart(null));
- }
+ @Test
+ public void testGetPersistenceTimeout_Invalid() throws Exception {
+ props.setProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "abc");
+ final PreparedStatement s = mockDbConn(0);
- @Test
- public void testBeforeShutdown() {
- assertFalse(feat.beforeShutdown(null));
- }
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testAfterShutdown() {
- assertFalse(feat.afterShutdown(null));
- }
+ setUpKie("myname", 999L, true);
- @Test
- public void testBeforeConfigure() {
- assertFalse(feat.beforeConfigure(null, null));
- }
+ feat.activatePolicySession(polcont, "myname", "mybase");
- @Test
- public void testAfterConfigure() {
- assertFalse(feat.afterConfigure(null));
- }
+ verify(s, never()).executeUpdate();
+ }
- @Test
- public void testBeforeActivate() {
- assertFalse(feat.beforeActivate(null));
- }
+ @Test
+ public void testCleanUpSessionInfo() throws Exception {
+ setUpKie("myname", 999L, true);
- @Test
- public void testAfterActivate() {
- assertFalse(feat.afterActivate(null));
- }
+ // use a real DB so we can verify that the "delete" works correctly
+ fact = new PartialFactory();
+ feat.setFactory(fact);
- @Test
- public void testBeforeDeactivate() {
- assertFalse(feat.beforeDeactivate(null));
- }
+ makeSessionInfoTbl(20000);
- @Test
- public void testAfterDeactivate() {
- assertFalse(feat.afterDeactivate(null));
- }
+ // create mock entity manager for use by JPA connector
+ EntityManager em = mock(EntityManager.class);
+ when(emf.createEntityManager()).thenReturn(em);
- @Test
- public void testBeforeStop() {
- assertFalse(feat.beforeStop(null));
- }
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testAfterStop() {
- assertFalse(feat.afterStop(null));
- }
+ feat.beforeStart(null);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- @Test
- public void testBeforeLock() {
- assertFalse(feat.beforeLock(null));
- }
+ assertEquals("[1, 4, 5]", getSessions().toString());
+ }
- @Test
- public void testAfterLock() {
- assertFalse(feat.afterLock(null));
- }
+ @Test
+ public void testCleanUpSessionInfo_WithBeforeStart() throws Exception {
+ final PreparedStatement statement = mockDbConn(0);
- @Test
- public void testBeforeUnlock() {
- assertFalse(feat.beforeUnlock(null));
- }
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testAfterUnlock() {
- assertFalse(feat.afterUnlock(null));
- }
+ setUpKie("myname", 999L, true);
- @Test
- public void testGetPersistenceTimeout_Valid() throws Exception {
- PreparedStatement s = mockDbConn(5);
+ // reset
+ feat.beforeStart(null);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(1)).executeUpdate();
- setUpKie("myname", 999L, true);
+ // should not clean-up again
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(1)).executeUpdate();
- feat.activatePolicySession(polcont, "myname", "mybase");
+ // reset
+ feat.beforeStart(null);
- verify(s).executeUpdate();
- }
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(2)).executeUpdate();
- @Test
- public void testGetPersistenceTimeout_Missing() throws Exception {
+ // should not clean-up again
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(2)).executeUpdate();
+ }
- props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT);
+ @Test
+ public void testCleanUpSessionInfo_WithBeforeActivate() throws Exception {
+ final PreparedStatement statement = mockDbConn(0);
- PreparedStatement s = mockDbConn(0);
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ setUpKie("myname", 999L, true);
- setUpKie("myname", 999L, true);
+ // reset
+ feat.beforeActivate(null);
- feat.activatePolicySession(polcont, "myname", "mybase");
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(1)).executeUpdate();
- verify(s, never()).executeUpdate();
- }
+ // should not clean-up again
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(1)).executeUpdate();
- @Test
- public void testGetPersistenceTimeout_Invalid() throws Exception {
- props.setProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "abc");
- PreparedStatement s = mockDbConn(0);
+ // reset
+ feat.beforeActivate(null);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(2)).executeUpdate();
- setUpKie("myname", 999L, true);
+ // should not clean-up again
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ verify(statement, times(2)).executeUpdate();
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testCleanUpSessionInfo_NoTimeout() throws Exception {
- verify(s, never()).executeUpdate();
- }
+ props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT);
- @Test
- public void testCleanUpSessionInfo() throws Exception {
- setUpKie("myname", 999L, true);
+ final PreparedStatement statement = mockDbConn(0);
- // use a real DB so we can verify that the "delete" works correctly
- fact = new PartialFactory();
- feat.setFactory(fact);
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- makeSessionInfoTbl(20000);
+ setUpKie("myname", 999L, true);
- // create mock entity manager for use by JPA connector
- EntityManager em = mock(EntityManager.class);
- when(emf.createEntityManager()).thenReturn(em);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ verify(statement, never()).executeUpdate();
+ }
- feat.beforeStart(null);
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testCleanUpSessionInfo_NoUrl() throws Exception {
+ final PreparedStatement statement = mockDbConn(0);
- assertEquals("[1, 4, 5]", getSessions().toString());
- }
+ props.remove(DroolsPersistenceProperties.DB_URL);
- @Test
- public void testCleanUpSessionInfo_WithBeforeStart() throws Exception {
- PreparedStatement s = mockDbConn(0);
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ setUpKie("myname", 999L, true);
- setUpKie("myname", 999L, true);
+ try {
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ fail("missing exception");
+ } catch (RuntimeException e) {
+ logger.trace("expected exception", e);
+ }
- // reset
- feat.beforeStart(null);
+ verify(statement, never()).executeUpdate();
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(1)).executeUpdate();
+ @Test
+ public void testCleanUpSessionInfo_NoUser() throws Exception {
+ final PreparedStatement statement = mockDbConn(0);
- // should not clean-up again
- feat.activatePolicySession(polcont, "myname", "mybase");
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(1)).executeUpdate();
+ props.remove(DroolsPersistenceProperties.DB_USER);
- // reset
- feat.beforeStart(null);
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(2)).executeUpdate();
+ setUpKie("myname", 999L, true);
- // should not clean-up again
- feat.activatePolicySession(polcont, "myname", "mybase");
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(2)).executeUpdate();
- }
+ try {
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ fail("missing exception");
+ } catch (RuntimeException e) {
+ logger.trace("expected exception", e);
+ }
- @Test
- public void testCleanUpSessionInfo_WithBeforeActivate() throws Exception {
- PreparedStatement s = mockDbConn(0);
+ verify(statement, never()).executeUpdate();
+ }
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ @Test
+ public void testCleanUpSessionInfo_NoPassword() throws Exception {
+ final PreparedStatement statement = mockDbConn(0);
- setUpKie("myname", 999L, true);
+ props.remove(DroolsPersistenceProperties.DB_PWD);
- // reset
- feat.beforeActivate(null);
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(1)).executeUpdate();
+ setUpKie("myname", 999L, true);
- // should not clean-up again
- feat.activatePolicySession(polcont, "myname", "mybase");
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(1)).executeUpdate();
+ try {
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ fail("missing exception");
+ } catch (RuntimeException e) {
+ logger.trace("expected exception", e);
+ }
- // reset
- feat.beforeActivate(null);
+ verify(statement, never()).executeUpdate();
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(2)).executeUpdate();
+ @Test
+ public void testCleanUpSessionInfo_SqlEx() throws Exception {
+ final PreparedStatement statement = mockDbConn(-1);
- // should not clean-up again
- feat.activatePolicySession(polcont, "myname", "mybase");
- feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, times(2)).executeUpdate();
- }
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testCleanUpSessionInfo_NoTimeout() throws Exception {
+ setUpKie("myname", 999L, true);
- props.remove(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- PreparedStatement s = mockDbConn(0);
+ verify(statement).executeUpdate();
+ }
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ @Test
+ public void testGetDroolsSessionConnector() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- setUpKie("myname", 999L, true);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- feat.activatePolicySession(polcont, "myname", "mybase");
+ feat.activatePolicySession(polcont, "myname", "mybase");
- verify(s, never()).executeUpdate();
- }
+ verify(fact).makeJpaConnector(emf);
+ }
- @Test
- public void testCleanUpSessionInfo_NoUrl() throws Exception {
- PreparedStatement s = mockDbConn(0);
+ @Test
+ public void testReplaceSession() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- props.remove(DroolsPersistenceProperties.DB_URL);
+ final ArgumentCaptor<DroolsSession> sesscap = ArgumentCaptor.forClass(DroolsSession.class);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- setUpKie("myname", 999L, true);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- try {
- feat.activatePolicySession(polcont, "myname", "mybase");
- fail("missing exception");
- } catch (RuntimeException e) {
- logger.trace("expected exception", e);
- }
+ verify(jpa).replace(sesscap.capture());
- verify(s, never()).executeUpdate();
- }
+ assertEquals("myname", sesscap.getValue().getSessionName());
+ assertEquals(999L, sesscap.getValue().getSessionId());
+ }
- @Test
- public void testCleanUpSessionInfo_NoUser() throws Exception {
- PreparedStatement s = mockDbConn(0);
+ @Test
+ public void testIsPersistenceEnabled_Auto() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- props.remove(DroolsPersistenceProperties.DB_USER);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ props.setProperty("persistence.type", "auto");
- setUpKie("myname", 999L, true);
+ assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- try {
- feat.activatePolicySession(polcont, "myname", "mybase");
- fail("missing exception");
- } catch (RuntimeException e) {
- logger.trace("expected exception", e);
- }
+ @Test
+ public void testIsPersistenceEnabled_Native() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- verify(s, never()).executeUpdate();
- }
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- @Test
- public void testCleanUpSessionInfo_NoPassword() throws Exception {
- PreparedStatement s = mockDbConn(0);
+ props.setProperty("persistence.type", "native");
- props.remove(DroolsPersistenceProperties.DB_PWD);
+ assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ @Test
+ public void testIsPersistenceEnabled_None() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- setUpKie("myname", 999L, true);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- try {
- feat.activatePolicySession(polcont, "myname", "mybase");
- fail("missing exception");
- } catch (RuntimeException e) {
- logger.trace("expected exception", e);
- }
+ props.remove("persistence.type");
- verify(s, never()).executeUpdate();
- }
+ assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- @Test
- public void testCleanUpSessionInfo_SqlEx() throws Exception {
- PreparedStatement s = mockDbConn(-1);
+ @Test
+ public void testGetProperties_Ex() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- setUpKie("myname", 999L, true);
+ when(fact.getPolicyController(polcont))
+ .thenThrow(new IllegalArgumentException("expected exception"));
- feat.activatePolicySession(polcont, "myname", "mybase");
+ assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- verify(s).executeUpdate();
- }
+ @Test
+ public void testGetProperty_Specific() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testGetDroolsSessionConnector() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ props.remove("persistence.type");
+ props.setProperty("persistence.myname.type", "auto");
- feat.activatePolicySession(polcont, "myname", "mybase");
+ assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- verify(fact).makeJpaConnector(emf);
- }
+ @Test
+ public void testGetProperty_Specific_None() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testReplaceSession() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- ArgumentCaptor<DroolsSession> sesscap = ArgumentCaptor.forClass(DroolsSession.class);
+ props.remove("persistence.type");
+ props.setProperty("persistence.xxx.type", "auto");
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- feat.activatePolicySession(polcont, "myname", "mybase");
+ @Test
+ public void testGetProperty_Both_SpecificOn() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- verify(jpa).replace(sesscap.capture());
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- assertEquals("myname", sesscap.getValue().getSessionName());
- assertEquals(999L, sesscap.getValue().getSessionId());
- }
+ props.setProperty("persistence.type", "other");
+ props.setProperty("persistence.myname.type", "auto");
- @Test
- public void testIsPersistenceEnabled_Auto() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ @Test
+ public void testGetProperty_Both_SpecificDisabledOff() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- props.setProperty("persistence.type", "auto");
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ props.setProperty("persistence.type", "auto");
+ props.setProperty("persistence.myname.type", "other");
- @Test
- public void testIsPersistenceEnabled_Native() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ @Test
+ public void testGetProperty_None() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- props.setProperty("persistence.type", "native");
+ mockDbConn(5);
+ setUpKie("myname", 999L, true);
- assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ props.remove("persistence.type");
- @Test
- public void testIsPersistenceEnabled_None() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ @Test
+ public void testPersistenceFeatureException() {
+ SecurityException secex = new SecurityException("expected exception");
+ PersistenceFeatureException ex = new PersistenceFeatureException(secex);
- props.remove("persistence.type");
+ assertEquals(secex, ex.getCause());
+ }
- assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ @Test
+ public void testDsEmf_RtEx() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testGetProperties_Ex() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ when(fact.makeEntMgrFact(any())).thenThrow(new IllegalArgumentException("expected exception"));
- when(fact.getPolicyController(polcont)).thenThrow(new IllegalArgumentException("expected exception"));
+ try {
+ feat.activatePolicySession(polcont, "myname", "mybase");
+ fail("missing exception");
- assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ } catch (IllegalArgumentException ex) {
+ logger.trace("expected exception", ex);
+ }
- @Test
- public void testGetProperty_Specific() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ verify(bds, times(2)).close();
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ @Test
+ public void testDsEmf_Close_RtEx() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- props.remove("persistence.type");
- props.setProperty("persistence.myname.type", "auto");
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ feat.activatePolicySession(polcont, "myname", "mybase");
- @Test
- public void testGetProperty_Specific_None() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
- props.remove("persistence.type");
- props.setProperty("persistence.xxx.type", "auto");
+ // return adjunct on next call
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
- assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ try {
+ doThrow(new IllegalArgumentException("expected exception")).when(emf).close();
- @Test
- public void testGetProperty_Both_SpecificOn() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ feat.destroyKieSession(polsess);
+ fail("missing exception");
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ } catch (IllegalArgumentException ex) {
+ logger.trace("expected exception", ex);
+ }
- props.setProperty("persistence.type", "other");
- props.setProperty("persistence.myname.type", "auto");
+ verify(bds, times(2)).close();
+ }
- assertNotNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ @Test
+ public void testDsEmf_CloseDataSource_RtEx() throws Exception {
+ feat.globalInit(null, SRC_TEST_RESOURCES);
- @Test
- public void testGetProperty_Both_SpecificDisabledOff() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ mockDbConn(5);
+ setUpKie("myname", 999L, false);
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ feat.activatePolicySession(polcont, "myname", "mybase");
- props.setProperty("persistence.type", "auto");
- props.setProperty("persistence.myname.type", "other");
+ ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap =
+ ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class);
- assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
- @Test
- public void testGetProperty_None() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ // return adjunct on next call
+ when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
- mockDbConn(5);
- setUpKie("myname", 999L, true);
+ try {
+ doThrow(new SQLException("expected exception")).when(bds).close();
- props.remove("persistence.type");
+ feat.destroyKieSession(polsess);
+ fail("missing exception");
- assertNull(feat.activatePolicySession(polcont, "myname", "mybase"));
- }
+ } catch (PersistenceFeatureException ex) {
+ logger.trace("expected exception", ex);
+ }
+ }
- @Test
- public void testPersistenceFeatureException() {
- SecurityException secex = new SecurityException("expected exception");
- PersistenceFeatureException ex = new PersistenceFeatureException(secex);
+ /**
+ * Gets an ordered list of ids of the current SessionInfo records.
+ *
+ * @return ordered list of SessInfo IDs
+ * @throws SQLException sql exception
+ * @throws IOException io exception
+ */
+ private List<Integer> getSessions() throws SQLException, IOException {
+ attachDb();
- assertEquals(secex, ex.getCause());
+ ArrayList<Integer> lst = new ArrayList<>(5);
- }
+ try (PreparedStatement stmt = conn.prepareStatement("SELECT id from sessioninfo order by id");
+ ResultSet rs = stmt.executeQuery()) {
- @Test
- public void testDsEmf_RtEx() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ while (rs.next()) {
+ lst.add(rs.getInt(1));
+ }
+ }
- mockDbConn(5);
- setUpKie("myname", 999L, false);
-
- when(fact.makeEntMgrFact(any())).thenThrow(new IllegalArgumentException("expected exception"));
+ return lst;
+ }
- try {
- feat.activatePolicySession(polcont, "myname", "mybase");
- fail("missing exception");
-
- } catch(IllegalArgumentException ex) {
- logger.trace("expected exception", ex);
- }
+ /**
+ * Sets up for doing invoking the newKieSession() method.
+ *
+ * @param sessnm name to which JPA should respond with a session
+ * @param sessid session id to be returned by the session
+ * @param loadOk {@code true} if loadKieSession() should return a value, {@code false} to return
+ * null
+ * @throws Exception exception
+ */
+ private void setUpKie(String sessnm, long sessid, boolean loadOk) throws Exception {
- verify(bds, times(2)).close();
- }
+ when(fact.makeJpaConnector(emf)).thenReturn(jpa);
+ when(fact.makeEntMgrFact(any())).thenReturn(emf);
+ when(fact.getPolicyController(polcont)).thenReturn(polctlr);
- @Test
- public void testDsEmf_Close_RtEx() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
+ props.setProperty("persistence.type", "auto");
- mockDbConn(5);
- setUpKie("myname", 999L, false);
-
- feat.activatePolicySession(polcont, "myname", "mybase");
+ when(polctlr.getProperties()).thenReturn(props);
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
+ when(jpa.get(sessnm)).thenReturn(sess);
- verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
+ when(sess.getSessionId()).thenReturn(sessid);
- // return adjunct on next call
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
+ when(polsess.getPolicyContainer()).thenReturn(polcont);
+ when(polsess.getName()).thenReturn(sessnm);
- try {
- doThrow(new IllegalArgumentException("expected exception")).when(emf).close();
-
- feat.destroyKieSession(polsess);
- fail("missing exception");
-
- } catch(IllegalArgumentException ex) {
- logger.trace("expected exception", ex);
- }
+ if (loadOk) {
+ when(kiesess.getIdentifier()).thenReturn(sessid);
+ when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(kiesess);
- verify(bds, times(2)).close();
- }
-
- @Test
- public void testDsEmf_CloseDataSource_RtEx() throws Exception {
- feat.globalInit(null, SRC_TEST_RESOURCES);
-
- mockDbConn(5);
- setUpKie("myname", 999L, false);
-
- feat.activatePolicySession(polcont, "myname", "mybase");
-
- ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor
- .forClass(PersistenceFeature.ContainerAdjunct.class);
-
- verify(polcont, times(1)).setAdjunct(any(), adjcap.capture());
-
- // return adjunct on next call
- when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue());
-
- try {
- doThrow(new SQLException("expected exception")).when(bds).close();
-
- feat.destroyKieSession(polsess);
- fail("missing exception");
-
- } catch(PersistenceFeatureException ex) {
- logger.trace("expected exception", ex);
- }
- }
-
- /**
- * Gets an ordered list of ids of the current SessionInfo records.
- *
- * @return ordered list of SessInfo IDs
- * @throws SQLException
- * @throws IOException
- */
- private List<Integer> getSessions() throws SQLException, IOException {
- attachDb();
-
- ArrayList<Integer> lst = new ArrayList<>(5);
-
- try (PreparedStatement stmt = conn.prepareStatement("SELECT id from sessioninfo order by id");
- ResultSet rs = stmt.executeQuery()) {
-
- while (rs.next()) {
- lst.add(rs.getInt(1));
- }
- }
-
- return lst;
- }
-
- /**
- * Sets up for doing invoking the newKieSession() method.
- *
- * @param sessnm
- * name to which JPA should respond with a session
- * @param sessid
- * session id to be returned by the session
- * @param loadOk
- * {@code true} if loadKieSession() should return a value,
- * {@code false} to return null
- * @throws Exception
- */
- private void setUpKie(String sessnm, long sessid, boolean loadOk) throws Exception {
-
- when(fact.makeJpaConnector(emf)).thenReturn(jpa);
- when(fact.makeEntMgrFact(any())).thenReturn(emf);
- when(fact.getPolicyController(polcont)).thenReturn(polctlr);
-
- props.setProperty("persistence.type", "auto");
-
- when(polctlr.getProperties()).thenReturn(props);
-
- when(jpa.get(sessnm)).thenReturn(sess);
-
- when(sess.getSessionId()).thenReturn(sessid);
-
- when(polsess.getPolicyContainer()).thenReturn(polcont);
- when(polsess.getName()).thenReturn(sessnm);
-
- if (loadOk) {
- when(kiesess.getIdentifier()).thenReturn(sessid);
- when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(kiesess);
-
- } else {
- // use an alternate id for the new session
- when(kiesess.getIdentifier()).thenReturn(100L);
- when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(null);
- }
-
- when(kiestore.newKieSession(any(), any(), any())).thenReturn(kiesess);
- }
-
- /**
- * Creates the SessionInfo DB table and populates it with some data.
- *
- * @param expMs
- * number of milli-seconds for expired sessioninfo records
- * @throws SQLException
- * @throws IOException
- */
- private void makeSessionInfoTbl(int expMs) throws SQLException, IOException {
-
- attachDb();
-
- try (PreparedStatement stmt = conn
- .prepareStatement("CREATE TABLE sessioninfo(id int, lastmodificationdate timestamp)")) {
-
- stmt.executeUpdate();
- }
-
- try (PreparedStatement stmt = conn
- .prepareStatement("INSERT into sessioninfo(id, lastmodificationdate) values(?, ?)")) {
-
- Timestamp ts;
-
- // current data
- ts = new Timestamp(System.currentTimeMillis());
- stmt.setTimestamp(2, ts);
-
- stmt.setInt(1, 1);
- stmt.executeUpdate();
-
- stmt.setInt(1, 4);
- stmt.executeUpdate();
-
- stmt.setInt(1, 5);
- stmt.executeUpdate();
-
- // expired data
- ts = new Timestamp(System.currentTimeMillis() - expMs);
- stmt.setTimestamp(2, ts);
-
- stmt.setInt(1, 2);
- stmt.executeUpdate();
-
- stmt.setInt(1, 3);
- stmt.executeUpdate();
- }
- }
-
- /**
- * Attaches {@link #conn} to the DB, if it isn't already attached.
- *
- * @throws SQLException
- * @throws IOException
- * if the property file cannot be read
- */
- private void attachDb() throws SQLException, IOException {
- if (conn == null) {
- Properties p = loadDbProps();
-
- conn = DriverManager.getConnection(p.getProperty(DroolsPersistenceProperties.DB_URL),
- p.getProperty(DroolsPersistenceProperties.DB_USER),
- p.getProperty(DroolsPersistenceProperties.DB_PWD));
- conn.setAutoCommit(true);
- }
- }
-
- /**
- * Loads the DB properties from the file,
- * <i>feature-session-persistence.properties</i>.
- *
- * @return the properties that were loaded
- * @throws IOException
- * if the property file cannot be read
- * @throws FileNotFoundException
- * if the property file does not exist
- */
- private Properties loadDbProps() throws IOException, FileNotFoundException {
-
- Properties p = new Properties();
-
- try (FileReader rdr = new FileReader("src/test/resources/feature-session-persistence.properties")) {
- p.load(rdr);
- }
-
- return p;
- }
-
- /**
- * Create a mock DB connection and statement.
- *
- * @param retval
- * value to be returned when the statement is executed, or
- * negative to throw an exception
- * @return the statement that will be returned by the connection
- * @throws SQLException
- */
- private PreparedStatement mockDbConn(int retval) throws SQLException {
- Connection c = mock(Connection.class);
- PreparedStatement s = mock(PreparedStatement.class);
-
- when(bds.getConnection()).thenReturn(c);
- when(fact.makeDataSource(any())).thenReturn(bds);
- when(c.prepareStatement(anyString())).thenReturn(s);
-
- if (retval < 0) {
- // should throw an exception
- when(s.executeUpdate()).thenThrow(new SQLException("expected exception"));
-
- } else {
- // should return the value
- when(s.executeUpdate()).thenReturn(retval);
- }
-
- return s;
- }
-
- /**
- * A partial factory, which exports a few of the real methods, but overrides
- * the rest.
- */
- private class PartialFactory extends PersistenceFeature.Factory {
-
- @Override
- public TransactionManager getTransMgr() {
- return transmgr;
- }
-
- @Override
- public UserTransaction getUserTrans() {
- return usertrans;
- }
-
- @Override
- public TransactionSynchronizationRegistry getTransSyncReg() {
- return transreg;
- }
-
- @Override
- public KieServices getKieServices() {
- return kiesvc;
- }
-
- @Override
- public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) {
- return emf;
- }
-
- @Override
- public PolicyController getPolicyController(PolicyContainer container) {
- return polctlr;
- }
-
- }
+ } else {
+ // use an alternate id for the new session
+ when(kiesess.getIdentifier()).thenReturn(100L);
+ when(kiestore.loadKieSession(anyLong(), any(), any(), any())).thenReturn(null);
+ }
+
+ when(kiestore.newKieSession(any(), any(), any())).thenReturn(kiesess);
+ }
+
+ /**
+ * Creates the SessionInfo DB table and populates it with some data.
+ *
+ * @param expMs number of milli-seconds for expired sessioninfo records
+ * @throws SQLException exception
+ * @throws IOException exception
+ */
+ private void makeSessionInfoTbl(int expMs) throws SQLException, IOException {
+
+ attachDb();
+
+ try (PreparedStatement stmt =
+ conn.prepareStatement("CREATE TABLE sessioninfo(id int, lastmodificationdate timestamp)")) {
+
+ stmt.executeUpdate();
+ }
+
+ try (PreparedStatement stmt =
+ conn.prepareStatement("INSERT into sessioninfo(id, lastmodificationdate) values(?, ?)")) {
+
+ Timestamp ts;
+
+ // current data
+ ts = new Timestamp(System.currentTimeMillis());
+ stmt.setTimestamp(2, ts);
+
+ stmt.setInt(1, 1);
+ stmt.executeUpdate();
+
+ stmt.setInt(1, 4);
+ stmt.executeUpdate();
+
+ stmt.setInt(1, 5);
+ stmt.executeUpdate();
+
+ // expired data
+ ts = new Timestamp(System.currentTimeMillis() - expMs);
+ stmt.setTimestamp(2, ts);
+
+ stmt.setInt(1, 2);
+ stmt.executeUpdate();
+
+ stmt.setInt(1, 3);
+ stmt.executeUpdate();
+ }
+ }
+
+ /**
+ * Attaches {@link #conn} to the DB, if it isn't already attached.
+ *
+ * @throws SQLException sql exception
+ * @throws IOException if the property file cannot be read
+ */
+ private void attachDb() throws SQLException, IOException {
+ if (conn == null) {
+ Properties props = loadDbProps();
+
+ conn =
+ DriverManager.getConnection(
+ props.getProperty(DroolsPersistenceProperties.DB_URL),
+ props.getProperty(DroolsPersistenceProperties.DB_USER),
+ props.getProperty(DroolsPersistenceProperties.DB_PWD));
+ conn.setAutoCommit(true);
+ }
+ }
+
+ /**
+ * Loads the DB properties from the file, <i>feature-session-persistence.properties</i>.
+ *
+ * @return the properties that were loaded
+ * @throws IOException if the property file cannot be read
+ * @throws FileNotFoundException if the property file does not exist
+ */
+ private Properties loadDbProps() throws IOException, FileNotFoundException {
+
+ Properties props = new Properties();
+
+ try (FileReader rdr =
+ new FileReader("src/test/resources/feature-session-persistence.properties")) {
+ props.load(rdr);
+ }
+
+ return props;
+ }
+
+ /**
+ * Create a mock DB connection and statement.
+ *
+ * @param retval value to be returned when the statement is executed, or negative to throw an
+ * exception
+ * @return the statement that will be returned by the connection
+ * @throws SQLException sql exception
+ */
+ private PreparedStatement mockDbConn(int retval) throws SQLException {
+ Connection connection = mock(Connection.class);
+ PreparedStatement statement = mock(PreparedStatement.class);
+
+ when(bds.getConnection()).thenReturn(connection);
+ when(fact.makeDataSource(any())).thenReturn(bds);
+ when(connection.prepareStatement(anyString())).thenReturn(statement);
+
+ if (retval < 0) {
+ // should throw an exception
+ when(statement.executeUpdate()).thenThrow(new SQLException("expected exception"));
+
+ } else {
+ // should return the value
+ when(statement.executeUpdate()).thenReturn(retval);
+ }
+
+ return statement;
+ }
+
+ /** A partial factory, which exports a few of the real methods, but overrides the rest. */
+ private class PartialFactory extends PersistenceFeature.Factory {
+
+ @Override
+ public TransactionManager getTransMgr() {
+ return transmgr;
+ }
+
+ @Override
+ public UserTransaction getUserTrans() {
+ return usertrans;
+ }
+
+ @Override
+ public TransactionSynchronizationRegistry getTransSyncReg() {
+ return transreg;
+ }
+
+ @Override
+ public KieServices getKieServices() {
+ return kiesvc;
+ }
+
+ @Override
+ public EntityManagerFactory makeEntMgrFact(Map<String, Object> props) {
+ return emf;
+ }
+
+ @Override
+ public PolicyController getPolicyController(PolicyContainer container) {
+ return polctlr;
+ }
+ }
}