From f2c75918e425d62d0332da4685cae3518a50d4bb Mon Sep 17 00:00:00 2001 From: "Magnusen, Drew (dm741q)" Date: Wed, 29 Nov 2017 14:01:06 -0600 Subject: Refactored code to reduce technical debt. Major changes in this commit include the refactoring of the run method in TestTransaction.java to reduce the amount of technical debt identified by sonar. Other small changes were also made. Issue-ID: POLICY-467 Change-Id: I2522f690de58e3c6f4cc894e6dea47277404d745 Signed-off-by: Magnusen, Drew (dm741q) --- .../drools/testtransaction/TestTransaction.java | 65 ++++++++++++---------- .../testtransaction/TestTransactionFeature.java | 8 +-- 2 files changed, 40 insertions(+), 33 deletions(-) (limited to 'feature-test-transaction/src') diff --git a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java index a23f2a46..5e209faa 100644 --- a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java +++ b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransaction.java @@ -64,7 +64,7 @@ public interface TestTransaction { */ class TTImpl implements TestTransaction { - final protected Map controllers = new HashMap<>(); + protected final Map controllers = new HashMap<>(); @Override public synchronized void register(PolicyController controller) { @@ -158,34 +158,7 @@ class TTControllerTask implements Runnable { while (this.controller.isAlive() && !this.controller.isLocked() && drools.isBrained() && this.alive) { - for (final String session : sessions) { - final List facts = this.controller.getDrools().factQuery(session, - TestTransaction.TT_FPC, TestTransaction.TT_COUNTER, false); - if (facts == null || facts.size() != 1) { - /* - * unexpected something wrong here, can't expect to recover note this exception is - * caught right below at the exit of run() - */ - logger.error( - "Controller: {}, with rules artifact: (group) {}, (artifact) {}, (version) {} - FPC query failed after EventObject insertion! ", - this.controller.getName(), this.controller.getDrools().getGroupId(), - this.controller.getDrools().getArtifactId(), - this.controller.getDrools().getVersion()); - break; - } - logger.debug("Facts: {}", facts); - - final long fpc = (Long) facts.get(0); - if (fpc != fpcs.get(session)) - logger.info("Controller: {} , session {} - Forward progress successful: {} -> {}", - this.controller.getName(), session, fpcs.get(session), fpc); - else - logger.error("Controller: {}, session {} - Forward progress failure: {}", - this.controller.getName(), session, fpc); - - fpcs.put(session, fpc); - drools.getContainer().insert(session, new EventObject(TestTransaction.TT_UUID)); - } + injectTxIntoSessions(sessions, fpcs, drools); if (!this.alive) return; @@ -208,7 +181,41 @@ class TTControllerTask implements Runnable { this.alive = false; } } + + private void injectTxIntoSessions(List sessions, HashMap fpcs, DroolsController drools) { + for (final String session : sessions) { + final List facts = this.controller.getDrools().factQuery(session, + TestTransaction.TT_FPC, TestTransaction.TT_COUNTER, false); + if (facts == null || facts.size() != 1) { + /* + * unexpected something wrong here, can't expect to recover note this exception is + * caught right below at the exit of run() + */ + logger.error( + "Controller: {}, with rules artifact: (group) {}, (artifact) {}, (version) {} - FPC query failed after EventObject insertion! ", + this.controller.getName(), this.controller.getDrools().getGroupId(), + this.controller.getDrools().getArtifactId(), + this.controller.getDrools().getVersion()); + break; + } + logger.debug("Facts: {}", facts); + + final long fpc = (Long) facts.get(0); + if (fpc != fpcs.get(session)) + logger.info("Controller: {} , session {} - Forward progress successful: {} -> {}", + this.controller.getName(), session, fpcs.get(session), fpc); + else + logger.error("Controller: {}, session {} - Forward progress failure: {}", + this.controller.getName(), session, fpc); + + fpcs.put(session, fpc); + drools.getContainer().insert(session, new EventObject(TestTransaction.TT_UUID)); + } + + } + + @Override public String toString() { final StringBuilder builder = new StringBuilder(); diff --git a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java index 72c54969..e78668ab 100644 --- a/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java +++ b/feature-test-transaction/src/main/java/org/onap/policy/drools/testtransaction/TestTransactionFeature.java @@ -33,7 +33,7 @@ public class TestTransactionFeature implements PolicyControllerFeatureAPI { // get an instance of logger private static final Logger logger = LoggerFactory.getLogger(TestTransactionFeature.class); - + @Override public boolean afterStart(PolicyController controller){ @@ -49,7 +49,7 @@ public class TestTransactionFeature implements PolicyControllerFeatureAPI { @Override public boolean afterLock(PolicyController controller) { - logger.info("CONTROLLER " + controller.getName() + " LOCKED"); + logger.info("controller {} locked", controller.getName()); TestTransaction.manager.unregister(controller); return false; @@ -57,7 +57,7 @@ public class TestTransactionFeature implements PolicyControllerFeatureAPI { @Override public boolean afterUnlock(PolicyController controller) { - logger.info("CONTROLLER " + controller.getName() + " UNLOCKED"); + logger.info("controller {} unlocked", controller.getName()); if (controller.isAlive() && !controller.isLocked() && @@ -69,7 +69,7 @@ public class TestTransactionFeature implements PolicyControllerFeatureAPI { @Override public boolean beforeStop(PolicyController controller) { - logger.info("CONTROLLER " + controller.getName() + " ABOUT TO STOP"); + logger.info("controller {} stopping", controller.getName()); TestTransaction.manager.unregister(controller); -- cgit 1.2.3-korg