diff options
Diffstat (limited to 'feature-session-persistence/src')
3 files changed, 35 insertions, 40 deletions
diff --git a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java index cc826905..a4965888 100644 --- a/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java +++ b/feature-session-persistence/src/main/java/org/onap/policy/drools/persistence/PersistenceFeature.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 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. @@ -38,6 +38,7 @@ import javax.transaction.UserTransaction; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSourceFactory; +import org.hibernate.cfg.AvailableSettings; import org.kie.api.KieServices; import org.kie.api.runtime.Environment; import org.kie.api.runtime.EnvironmentName; @@ -94,7 +95,7 @@ public class PersistenceFeature implements PolicySessionFeatureApi, PolicyEngine Object rval = policyContainer.getAdjunct(this); - if (rval == null || !(rval instanceof ContainerAdjunct)) { + if (!(rval instanceof ContainerAdjunct)) { // adjunct does not exist, or has the wrong type (should never // happen) rval = new ContainerAdjunct(policyContainer); @@ -199,11 +200,7 @@ public class PersistenceFeature implements PolicySessionFeatureApi, PolicyEngine **/ @Override public boolean beforeStart(PolicyEngine engine) { - synchronized (cleanupLock) { - sessInfoCleaned = false; - } - - return false; + return cleanup(); } /** @@ -211,6 +208,10 @@ public class PersistenceFeature implements PolicySessionFeatureApi, PolicyEngine **/ @Override public boolean beforeActivate(PolicyEngine engine) { + return cleanup(); + } + + private boolean cleanup() { synchronized (cleanupLock) { sessInfoCleaned = false; } @@ -641,7 +642,7 @@ public class PersistenceFeature implements PolicySessionFeatureApi, PolicyEngine try { minSleepTime = Math.max(1, Integer.valueOf(sleepTimeString)); } catch (Exception e) { - logger.error(sleepTimeString + ": Illegal value for 'minSleepTime'", e); + logger.error("{}: Illegal value for 'minSleepTime'", sleepTimeString, e); } } @@ -652,18 +653,14 @@ public class PersistenceFeature implements PolicySessionFeatureApi, PolicyEngine try { maxSleepTime = Math.max(1, Integer.valueOf(sleepTimeString)); } catch (Exception e) { - logger.error(sleepTimeString + ": Illegal value for 'maxSleepTime'", e); + logger.error("{}: Illegal value for 'maxSleepTime'", sleepTimeString, e); } } // swap values if needed if (minSleepTime > maxSleepTime) { - logger.error( - "minSleepTime(" - + minSleepTime - + ") is greater than maxSleepTime(" - + maxSleepTime - + ") -- swapping"); + logger.error("minSleepTime({}) is greater than maxSleepTime({}) -- swapping", minSleepTime, + maxSleepTime); long tmp = minSleepTime; minSleepTime = maxSleepTime; maxSleepTime = tmp; @@ -791,7 +788,7 @@ public class PersistenceFeature implements PolicySessionFeatureApi, PolicyEngine public DsEmf(BasicDataSource bds) { try { Map<String, Object> props = new HashMap<>(); - props.put(org.hibernate.cfg.Environment.JPA_JTA_DATASOURCE, bds); + props.put(AvailableSettings.JPA_JTA_DATASOURCE, bds); this.bds = bds; this.emf = makeEntMgrFact(props); diff --git a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java index 40918312..31fcfa84 100644 --- a/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java +++ b/feature-session-persistence/src/test/java/org/onap/policy/drools/persistence/GenSchema.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * feature-session-persistence * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 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. @@ -22,10 +22,7 @@ package org.onap.policy.drools.persistence; import java.util.HashMap; import java.util.Map; - -import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 97b6874a..20ae13d0 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 @@ -20,6 +20,7 @@ package org.onap.policy.drools.persistence; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -128,7 +129,7 @@ public class PersistenceFeatureTest { /** * Setup before class. - * + * * @throws Exception exception */ @BeforeClass @@ -148,7 +149,7 @@ public class PersistenceFeatureTest { /** * Setup. - * + * * @throws Exception exception */ @Before @@ -310,7 +311,7 @@ public class PersistenceFeatureTest { public void testActivatePolicySession() throws Exception { setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement ps = mockDbConn(5); - + feat.beforeActivate(null); KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); @@ -432,14 +433,14 @@ public class PersistenceFeatureTest { public void testConfigureKieEnv_RtEx() throws Exception { setUpKie(MY_SESS_NAME, 999L, false); mockDbConn(5); - + feat = new PersistenceFeatureMockDb() { @Override protected UserTransaction getUserTrans() { throw new IllegalArgumentException(EXPECTED); } }; - + feat.globalInit(null, SRC_TEST_RESOURCES); try { @@ -457,7 +458,7 @@ public class PersistenceFeatureTest { public void testLoadKieSession() throws Exception { setUpKie(MY_SESS_NAME, 999L, true); mockDbConn(5); - + KieSession session = feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); verify(kiestore).loadKieSession(999L, kiebase, kiecfg, kieenv); @@ -562,7 +563,7 @@ public class PersistenceFeatureTest { final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -579,7 +580,7 @@ public class PersistenceFeatureTest { public void testDisposeKieSession_NoAdjunct() throws Exception { feat.globalInit(null, SRC_TEST_RESOURCES); - feat.disposeKieSession(polsess); + assertThatCode(() -> feat.disposeKieSession(polsess)).doesNotThrowAnyException(); } @Test @@ -593,7 +594,7 @@ public class PersistenceFeatureTest { final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -617,7 +618,7 @@ public class PersistenceFeatureTest { final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -634,7 +635,7 @@ public class PersistenceFeatureTest { public void testDestroyKieSession_NoAdjunct() throws Exception { feat.globalInit(null, SRC_TEST_RESOURCES); - feat.destroyKieSession(polsess); + assertThatCode(() -> feat.destroyKieSession(polsess)).doesNotThrowAnyException(); } @Test @@ -648,7 +649,7 @@ public class PersistenceFeatureTest { final ArgumentCaptor<PersistenceFeature.ContainerAdjunct> adjcap = ArgumentCaptor.forClass(PersistenceFeature.ContainerAdjunct.class); - + verify(polcont).setAdjunct(any(), adjcap.capture()); when(polcont.getAdjunct(any())).thenReturn(adjcap.getValue()); @@ -767,7 +768,7 @@ public class PersistenceFeatureTest { @Test public void testGetPersistenceTimeout_Invalid() throws Exception { props.setProperty(DroolsPersistenceProperties.DB_SESSIONINFO_TIMEOUT, "abc"); - + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement s = mockDbConn(0); @@ -869,7 +870,7 @@ public class PersistenceFeatureTest { @Test public void testCleanUpSessionInfo_NoUrl() throws Exception { props.remove(DroolsPersistenceProperties.DB_URL); - + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(0); @@ -886,7 +887,7 @@ public class PersistenceFeatureTest { @Test public void testCleanUpSessionInfo_NoUser() throws Exception { props.remove(DroolsPersistenceProperties.DB_USER); - + setUpKie(MY_SESS_NAME, 999L, true); final PreparedStatement statement = mockDbConn(0); @@ -945,7 +946,7 @@ public class PersistenceFeatureTest { feat.activatePolicySession(polcont, MY_SESS_NAME, MY_KIE_BASE); final ArgumentCaptor<DroolsSession> sesscap = ArgumentCaptor.forClass(DroolsSession.class); - + verify(jpa).replace(sesscap.capture()); assertEquals(MY_SESS_NAME, sesscap.getValue().getSessionName()); @@ -1312,7 +1313,7 @@ public class PersistenceFeatureTest { return statement; } - + /** * Feature with a mock DB. */ @@ -1323,7 +1324,7 @@ public class PersistenceFeatureTest { return bds; } } - + /** * Feature supporting newKieSession. */ @@ -1341,7 +1342,7 @@ public class PersistenceFeatureTest { return jpa; } } - + /** * Feature with overrides. */ @@ -1364,7 +1365,7 @@ public class PersistenceFeatureTest { return null; } } - + /** * Feature with <i>some</i> overrides. */ |