summaryrefslogtreecommitdiffstats
path: root/integrity-monitor/src
diff options
context:
space:
mode:
authoreikrwaq <waqas.ikram@ericsson.com>2018-03-15 12:59:14 +0000
committereikrwaq <waqas.ikram@ericsson.com>2018-03-16 13:04:01 +0000
commite97b6850387e1d3504b94ffbf99ba5680cf8ffbb (patch)
tree343db937a97cf12740569f1194c21e03720c4577 /integrity-monitor/src
parent02a86bddf5ccb93b6ef46b07b65079b90b97d034 (diff)
Fixing 12 reported bugs in Sonar for policy/common
Change-Id: I7bf453828f7a62070e7a73362798e6f2d4806b2f Issue-ID: POLICY-661 Signed-off-by: eikrwaq <waqas.ikram@ericsson.com>
Diffstat (limited to 'integrity-monitor/src')
-rw-r--r--integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java283
-rw-r--r--integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagementException.java29
-rw-r--r--integrity-monitor/src/main/java/org/onap/policy/common/im/exceptions/EntityRetrievalException.java31
-rw-r--r--integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java467
-rw-r--r--integrity-monitor/src/test/java/org/onap/policy/common/im/exceptions/EntityRetrievalExceptionTest.java34
5 files changed, 476 insertions, 368 deletions
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
index 1d899dee..5cafd876 100644
--- a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
+++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagement.java
@@ -23,13 +23,12 @@ package org.onap.policy.common.im;
import java.util.Date;
import java.util.List;
import java.util.Observable;
-
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
-import javax.persistence.Query;
-
+import javax.persistence.TypedQuery;
+import org.onap.policy.common.im.exceptions.EntityRetrievalException;
import org.onap.policy.common.im.jpa.StateManagementEntity;
import org.onap.policy.common.utils.jpa.EntityMgrCloser;
import org.onap.policy.common.utils.jpa.EntityTransCloser;
@@ -44,6 +43,9 @@ import org.slf4j.LoggerFactory;
*
*/
public class StateManagement extends Observable {
+ private static final String RESOURCE_NAME = "resource";
+ private static final String GET_STATE_MANAGEMENT_ENTITY_QUERY =
+ "Select p from StateManagementEntity p where p.resourceName=:" + RESOURCE_NAME;
private static final Logger logger = LoggerFactory.getLogger(StateManagement.class);
public static final String LOCKED = "locked";
public static final String UNLOCKED = "unlocked";
@@ -75,7 +77,7 @@ public class StateManagement extends Observable {
private String opState = null;
private String availStatus = null;
private String standbyStatus = null;
- private EntityManagerFactory emf;
+ private final EntityManagerFactory emf;
private StateTransition st = null;
/*
@@ -98,14 +100,14 @@ public class StateManagement extends Observable {
* @param resourceName
* @throws StateManagementException
*/
- public StateManagement(EntityManagerFactory entityManagerFactory, String resourceName) throws StateManagementException
+ public StateManagement(final EntityManagerFactory entityManagerFactory, final String resourceName) throws StateManagementException
{
emf = entityManagerFactory;
if(logger.isDebugEnabled()){
logger.debug("StateManagement: constructor, resourceName: {}", resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -120,15 +122,13 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
//persist the administrative state
- if (sm != null) {
- if(logger.isDebugEnabled()){
+ if(logger.isDebugEnabled()){
logger.debug("Persist adminstrative state, resourceName = {}", this.resourceName);
- }
- em.persist(sm);
}
+ em.persist(sm);
et.commit();
//Load the StateTransition hash table
@@ -137,7 +137,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: constructor end, resourceName: {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement: constructor caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement: Exception: " + ex.toString(), ex);
}
@@ -154,12 +154,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK initializeState() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK initializeState() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: initializeState() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -167,7 +165,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
// set state
sm.setAdminState(sm.getAdminState()); //preserve the Admin state
sm.setOpState(StateManagement.ENABLED);
@@ -182,7 +180,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: initializeState() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.initializeState() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.initializeState() Exception: " + ex);
}
@@ -197,12 +195,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK lock() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK lock() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: lock() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -210,9 +206,9 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), LOCK);
sm.setAdminState(stateElement.getEndingAdminState());
@@ -228,7 +224,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: lock() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.lock() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.lock() Exception: " + ex.toString());
}
@@ -243,12 +239,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK unlock() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK unlock() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: unlock() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -256,8 +250,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), UNLOCK);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -273,7 +267,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: unlock() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.unlock() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.unlock() Exception: " + ex);
}
@@ -289,12 +283,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK enabledNotFailed() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK enabledNotFailed() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: enableNotFailed() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -302,8 +294,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), ENABLE_NOT_FAILED);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -320,7 +312,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement enableNotFailed() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.enableNotFailed() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.enableNotFailed() Exception: " + ex);
}
@@ -335,12 +327,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK disabledFailed() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK disabledFailed() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: disableFailed() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -348,8 +338,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), DISABLE_FAILED);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -365,7 +355,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: disableFailed() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.disableFailed() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.disableFailed() Exception: " + ex);
}
@@ -376,7 +366,7 @@ public class StateManagement extends Observable {
* that remote resource has failed but its state is still showing that it is viable.
* @throws StateManagementException
*/
- public void disableFailed(String otherResourceName) throws StateManagementException
+ public void disableFailed(final String otherResourceName) throws StateManagementException
{
synchronized (SYNCLOCK){
if(otherResourceName == null){
@@ -384,14 +374,12 @@ public class StateManagement extends Observable {
return;
}
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK disabledFailed(otherResourceName) operation for resourceName = {}\n",
- otherResourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK disabledFailed(otherResourceName) operation for resourceName = {}\n",
+ otherResourceName);
logger.debug("StateManagement: disableFailed(otherResourceName) operation started, resourceName = {}",
otherResourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -399,8 +387,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for " + otherResourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, otherResourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, otherResourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), DISABLE_FAILED);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -417,7 +405,7 @@ public class StateManagement extends Observable {
logger.debug("StateManagement: disableFailed(otherResourceName) operation completed, resourceName = {}",
otherResourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.disableFailed(otherResourceName) caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.disableFailed(otherResourceName) Exception: " + ex);
}
@@ -432,12 +420,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK disableDependency() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK disableDependency() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: disableDependency() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -445,8 +431,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), DISABLE_DEPENDENCY);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -462,7 +448,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: disableDependency() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.disableDependency() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.disableDependency() Exception: " + ex);
}
@@ -478,12 +464,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK enableNoDependency() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK enableNoDependency() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: enableNoDependency() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -491,8 +475,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), ENABLE_NO_DEPENDENCY);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -508,7 +492,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: enableNoDependency() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.enableNoDependency() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.enableNoDependency() Exception: " + ex);
}
@@ -524,15 +508,13 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK promote() operation for resourceName = {}\n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK promote() operation for resourceName = {}\n", this.resourceName);
logger.debug("StateManagement: promote() operation started, resourceName = {}", this.resourceName);
}
StateManagementEntity sm;
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -541,7 +523,7 @@ public class StateManagement extends Observable {
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), PROMOTE);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -553,7 +535,7 @@ public class StateManagement extends Observable {
et.commit();
setChanged();
notifyObservers(STANDBY_STATUS);
- }catch(Exception ex){
+ }catch(final Exception ex){
logger.error("StateManagement.promote() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.promote() Exception: " + ex);
}
@@ -562,7 +544,7 @@ public class StateManagement extends Observable {
logger.debug("StateManagement: promote() operation completed, resourceName = ", this.resourceName);
}
if (sm.getStandbyStatus().equals(StateManagement.COLD_STANDBY)){
- String msg = "Failure to promote " + this.resourceName + " StandbyStatus = " + StateManagement.COLD_STANDBY;
+ final String msg = "Failure to promote " + this.resourceName + " StandbyStatus = " + StateManagement.COLD_STANDBY;
throw new StandbyStatusException(msg);
}
}
@@ -576,12 +558,10 @@ public class StateManagement extends Observable {
{
synchronized (SYNCLOCK){
if(logger.isDebugEnabled()){
- logger.debug("\nStateManagement: SYNCLOCK demote() operation for resourceName = \n", this.resourceName);
- }
- if(logger.isDebugEnabled()){
+ logger.debug("\nStateManagement: SYNCLOCK demote() operation for resourceName = \n", this.resourceName);
logger.debug("StateManagement: demote() operation started, resourceName = {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -589,8 +569,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("findStateManagementEntity for {}", this.resourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, this.resourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), DEMOTE);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -606,7 +586,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: demote() operation completed, resourceName = {}", this.resourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.demote() caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.demote() Exception: " + ex);
}
@@ -622,7 +602,7 @@ public class StateManagement extends Observable {
* @param otherResourceName
* @throws StateManagementException
*/
- public void demote(String otherResourceName) throws StateManagementException
+ public void demote(final String otherResourceName) throws StateManagementException
{
synchronized (SYNCLOCK){
if(otherResourceName==null){
@@ -632,7 +612,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("\nStateManagement: SYNCLOCK demote(otherResourceName) operation for resourceName = {}\n", otherResourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
@@ -640,8 +620,8 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: SYNCLOCK demote(otherResourceName) findStateManagementEntity for {}", otherResourceName);
}
- StateManagementEntity sm = findStateManagementEntity(em, otherResourceName);
- StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
+ final StateManagementEntity sm = findStateManagementEntity(em, otherResourceName);
+ final StateElement stateElement = st.getEndingState(sm.getAdminState(), sm.getOpState(),
sm.getAvailStatus(), sm.getStandbyStatus(), DEMOTE);
// set transition state
sm.setAdminState(stateElement.getEndingAdminState());
@@ -656,7 +636,7 @@ public class StateManagement extends Observable {
if(logger.isDebugEnabled()){
logger.debug("StateManagement: demote(otherResourceName) operation completed, resourceName = {}", otherResourceName);
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement.demote(otherResourceName) caught unexpected exception: ", ex);
throw new StateManagementException("StateManagement.demote(otherResourceName) Exception: " + ex);
}
@@ -672,26 +652,26 @@ public String getAdminState()
logger.debug("StateManagement(6/1/16): getAdminState for resourceName {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
- try(EntityMgrCloser emc = new EntityMgrCloser(em)) {
- Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource");
+ final EntityManager em = emf.createEntityManager();
+ try(final EntityMgrCloser emc = new EntityMgrCloser(em)) {
+ final TypedQuery<StateManagementEntity> query = em.createQuery(GET_STATE_MANAGEMENT_ENTITY_QUERY, StateManagementEntity.class);
- query.setParameter("resource", this.resourceName);
+ query.setParameter(RESOURCE_NAME, this.resourceName);
//Just test that we are retrieving the right object
- @SuppressWarnings("rawtypes")
- List resourceList = query.setLockMode(
+ final
+ List<StateManagementEntity> resourceList = query.setLockMode(
LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
if (!resourceList.isEmpty()) {
// exist
- StateManagementEntity stateManagementEntity = (StateManagementEntity) resourceList.get(0);
+ final StateManagementEntity stateManagementEntity = resourceList.get(0);
// refresh the object from DB in case cached data was returned
em.refresh(stateManagementEntity);
this.adminState = stateManagementEntity.getAdminState();
} else {
this.adminState = null;
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement: getAdminState exception: {}", ex.toString(), ex);
}
@@ -707,26 +687,26 @@ public String getOpState()
logger.debug("StateManagement(6/1/16): getOpState for resourceName {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em)) {
- Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource");
+ final TypedQuery<StateManagementEntity> query = em.createQuery(GET_STATE_MANAGEMENT_ENTITY_QUERY, StateManagementEntity.class);
- query.setParameter("resource", this.resourceName);
+ query.setParameter(RESOURCE_NAME, this.resourceName);
//Just test that we are retrieving the right object
- @SuppressWarnings("rawtypes")
- List resourceList = query.setLockMode(
+ final
+ List<StateManagementEntity> resourceList = query.setLockMode(
LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
if (!resourceList.isEmpty()) {
// exist
- StateManagementEntity stateManagementEntity = (StateManagementEntity) resourceList.get(0);
+ final StateManagementEntity stateManagementEntity = resourceList.get(0);
// refresh the object from DB in case cached data was returned
em.refresh(stateManagementEntity);
this.opState = stateManagementEntity.getOpState();
} else {
this.opState = null;
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement: getOpState exception: {}", ex.toString(), ex);
}
@@ -742,26 +722,26 @@ public String getOpState()
logger.debug("StateManagement(6/1/16): getAvailStatus for resourceName {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em)) {
- Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource");
+ final TypedQuery<StateManagementEntity> query = em.createQuery(GET_STATE_MANAGEMENT_ENTITY_QUERY, StateManagementEntity.class);
- query.setParameter("resource", this.resourceName);
+ query.setParameter(RESOURCE_NAME, this.resourceName);
//Just test that we are retrieving the right object
- @SuppressWarnings("rawtypes")
- List resourceList = query.setLockMode(
+ final
+ List<StateManagementEntity> resourceList = query.setLockMode(
LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
if (!resourceList.isEmpty()) {
// exist
- StateManagementEntity stateManagementEntity = (StateManagementEntity) resourceList.get(0);
+ final StateManagementEntity stateManagementEntity = resourceList.get(0);
// refresh the object from DB in case cached data was returned
em.refresh(stateManagementEntity);
this.availStatus = stateManagementEntity.getAvailStatus();
} else {
this.availStatus = null;
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement: getAvailStatus exception: {}", ex.toString(), ex);
}
@@ -777,26 +757,26 @@ public String getOpState()
logger.debug("StateManagement(6/1/16): getStandbyStatus for resourceName {}", this.resourceName);
}
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em)) {
- Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource");
+ final TypedQuery<StateManagementEntity> query = em.createQuery(GET_STATE_MANAGEMENT_ENTITY_QUERY, StateManagementEntity.class);
- query.setParameter("resource", this.resourceName);
+ query.setParameter(RESOURCE_NAME, this.resourceName);
//Just test that we are retrieving the right object
- @SuppressWarnings("rawtypes")
- List resourceList = query.setLockMode(
+ final
+ List<StateManagementEntity> resourceList = query.setLockMode(
LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
if (!resourceList.isEmpty()) {
// exist
- StateManagementEntity stateManagementEntity = (StateManagementEntity) resourceList.get(0);
+ final StateManagementEntity stateManagementEntity = resourceList.get(0);
// refresh the object from DB in case cached data was returned
em.refresh(stateManagementEntity);
this.standbyStatus = stateManagementEntity.getStandbyStatus();
} else {
this.standbyStatus = null;
}
- } catch(Exception ex) {
+ } catch(final Exception ex) {
logger.error("StateManagement: getStandbyStatus exception: {}", ex.toString(), ex);
}
@@ -809,40 +789,43 @@ public String getOpState()
* @param otherResourceName
* @return
*/
- private static StateManagementEntity findStateManagementEntity(EntityManager em, String otherResourceName)
+ private static StateManagementEntity findStateManagementEntity(final EntityManager em, final String otherResourceName)
{
if(logger.isDebugEnabled()){
logger.debug("StateManagementEntity: findStateManagementEntity: Entry");
}
- StateManagementEntity stateManagementEntity = null;
try {
- Query query = em.createQuery("Select p from StateManagementEntity p where p.resourceName=:resource");
-
- query.setParameter("resource", otherResourceName);
+ final TypedQuery<StateManagementEntity> query =
+ em.createQuery(GET_STATE_MANAGEMENT_ENTITY_QUERY, StateManagementEntity.class);
+
+ query.setParameter(RESOURCE_NAME, otherResourceName);
//Just test that we are retrieving the right object
- @SuppressWarnings("rawtypes")
- List resourceList = query.setLockMode(
+ final
+ List<StateManagementEntity> resourceList = query.setLockMode(
LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
if (!resourceList.isEmpty()) {
- // exist
- stateManagementEntity = (StateManagementEntity) resourceList.get(0);
- // refresh the object from DB in case cached data was returned
- em.refresh(stateManagementEntity);
- stateManagementEntity.setModifiedDate(new Date());
+ // exist
+ final StateManagementEntity stateManagementEntity = resourceList.get(0);
+ // refresh the object from DB in case cached data was returned
+ em.refresh(stateManagementEntity);
+ stateManagementEntity.setModifiedDate(new Date());
+ return stateManagementEntity;
} else {
// not exist - create one
- stateManagementEntity = new StateManagementEntity();
+ final StateManagementEntity stateManagementEntity = new StateManagementEntity();
stateManagementEntity.setResourceName(otherResourceName);
stateManagementEntity.setAdminState(UNLOCKED);
stateManagementEntity.setOpState(ENABLED);
stateManagementEntity.setAvailStatus(NULL_VALUE);
stateManagementEntity.setStandbyStatus(NULL_VALUE); // default
+ return stateManagementEntity;
}
- } catch(Exception ex) {
- logger.error("findStateManagementEntity exception: {}", ex.toString(), ex);
+ } catch(final Exception ex) {
+ final String message = "findStateManagementEntity exception";
+ logger.error("{}: {}", message, ex.toString(), ex);
+ throw new EntityRetrievalException(message, ex);
}
- return stateManagementEntity;
}
/**
@@ -850,7 +833,7 @@ public String getOpState()
* @param otherResourceName
* @return
*/
- public String getStandbyStatus(String otherResourceName) {
+ public String getStandbyStatus(final String otherResourceName) {
if (logger.isDebugEnabled()) {
logger.debug("StateManagement: getStandbyStatus: Entering, resourceName='{}'", otherResourceName);
@@ -859,20 +842,18 @@ public String getOpState()
String tempStandbyStatus = null;
// The transaction is required for the LockModeType
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
- Query stateManagementListQuery = em
- .createQuery("SELECT p FROM StateManagementEntity p WHERE p.resourceName=:resource");
- stateManagementListQuery.setParameter("resource", otherResourceName);
- List<?> stateManagementList = stateManagementListQuery.setLockMode(
+ final TypedQuery<StateManagementEntity> stateManagementListQuery = em
+ .createQuery(GET_STATE_MANAGEMENT_ENTITY_QUERY, StateManagementEntity.class);
+ stateManagementListQuery.setParameter(RESOURCE_NAME, otherResourceName);
+ final List<StateManagementEntity> stateManagementList = stateManagementListQuery.setLockMode(
LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- if (stateManagementList.size() == 1
- && stateManagementList.get(0) instanceof StateManagementEntity) {
- StateManagementEntity stateManagementEntity = (StateManagementEntity) stateManagementList
- .get(0);
+ if (!stateManagementList.isEmpty()) {
+ final StateManagementEntity stateManagementEntity = stateManagementList.get(0);
// refresh the object from DB in case cached data was returned
em.refresh(stateManagementEntity);
tempStandbyStatus = stateManagementEntity.getStandbyStatus();
@@ -884,7 +865,7 @@ public String getOpState()
}
et.commit();
- } catch (Exception e) {
+ } catch (final Exception e) {
logger.error("getStandbyStatus: Caught Exception attempting to get statemanagemententity record, message='{}'", e.getMessage(), e);
}
if (logger.isDebugEnabled()) {
@@ -906,19 +887,19 @@ public String getOpState()
/*
* Start transaction
*/
- EntityManager em = emf.createEntityManager();
+ final EntityManager em = emf.createEntityManager();
try(EntityMgrCloser emc = new EntityMgrCloser(em);
MyTransaction et = new MyTransaction(em)) {
- Query stateManagementEntityListQuery = em
- .createQuery("SELECT p FROM StateManagementEntity p");
- @SuppressWarnings("unchecked")
+ final TypedQuery<StateManagementEntity> stateManagementEntityListQuery = em
+ .createQuery("SELECT p FROM StateManagementEntity p", StateManagementEntity.class);
+ final
List<StateManagementEntity> stateManagementEntityList = stateManagementEntityListQuery.setLockMode(
LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
if(logger.isDebugEnabled()){
logger.debug("deleteAllStateManagementEntities: Deleting {} StateManagementEntity records", stateManagementEntityList.size());
}
- for (StateManagementEntity stateManagementEntity : stateManagementEntityList) {
+ for (final StateManagementEntity stateManagementEntity : stateManagementEntityList) {
if(logger.isDebugEnabled()){
logger.debug("deleteAllStateManagementEntities: Deleting statemanagemententity with resourceName={} and standbyStatus={}",
stateManagementEntity.getResourceName(),
@@ -928,7 +909,7 @@ public String getOpState()
}
et.commit();
- }catch(Exception ex){
+ }catch(final Exception ex){
logger.error("StateManagement.deleteAllStateManagementEntities() caught Exception: ", ex);
}
if(logger.isDebugEnabled()){
@@ -941,7 +922,7 @@ public String getOpState()
/**
* @param em
*/
- public MyTransaction(EntityManager em) {
+ public MyTransaction(final EntityManager em) {
super(em.getTransaction());
}
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagementException.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagementException.java
index 0a375b07..05597a2a 100644
--- a/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagementException.java
+++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/StateManagementException.java
@@ -21,19 +21,22 @@
package org.onap.policy.common.im;
public class StateManagementException extends IntegrityMonitorException {
- private static final long serialVersionUID = 1L;
- public StateManagementException() {
- super();
- }
- public StateManagementException(String message) {
- super(message);
- }
+ private static final long serialVersionUID = 1L;
- public StateManagementException(Throwable cause) {
- super(cause);
- }
- public StateManagementException(String message, Throwable cause) {
- super(message, cause);
- }
+ public StateManagementException() {
+ super();
+ }
+
+ public StateManagementException(final String message) {
+ super(message);
+ }
+
+ public StateManagementException(final Throwable cause) {
+ super(cause);
+ }
+
+ public StateManagementException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
}
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/exceptions/EntityRetrievalException.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/exceptions/EntityRetrievalException.java
new file mode 100644
index 00000000..9387805c
--- /dev/null
+++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/exceptions/EntityRetrievalException.java
@@ -0,0 +1,31 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Integrity Monitor
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.im.exceptions;
+
+public class EntityRetrievalException extends RuntimeException {
+
+ private static final long serialVersionUID = 8761143697306009072L;
+
+ public EntityRetrievalException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+}
diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java
index 29719215..377fed91 100644
--- a/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java
+++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/StateManagementTest.java
@@ -21,219 +21,278 @@
package org.onap.policy.common.im;
import static org.junit.Assert.assertEquals;
-
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.PersistenceException;
+import javax.persistence.QueryTimeoutException;
+import javax.persistence.TypedQuery;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.policy.common.im.jpa.StateManagementEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
- * All JUnits are designed to run in the local development environment
- * where they have write privileges and can execute time-sensitive
- * tasks.
+ * All JUnits are designed to run in the local development environment where they have write
+ * privileges and can execute time-sensitive tasks.
*/
public class StateManagementTest extends IntegrityMonitorTestBase {
- private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class);
- //
-
- @BeforeClass
- public static void setUpClass() throws Exception {
- IntegrityMonitorTestBase.setUpBeforeClass(DEFAULT_DB_URL_PREFIX + StateManagementTest.class.getSimpleName());
-
- }
-
- @AfterClass
- public static void tearDownClass() throws Exception {
- IntegrityMonitorTestBase.tearDownAfterClass();
- }
-
- @Before
- public void setUp() {
- super.setUpTest();
- }
-
- @After
- public void tearDown() {
- super.tearDownTest();
- }
-
- @Test
- public void test() throws Exception {
- logger.info("\n\nlogger.infor StateManagementTest: Entering\n\n");
- String resourceName = "test_resource1";
-
- // These parameters are in a properties file
- try {
- StateManagement sm = new StateManagement(emf, resourceName);
-
- logger.info("\n??? initial state");
- assertEquals("unlocked,enabled,null,null", makeString(sm));
-
- logger.info("\n??? test lock()");
- sm.lock();
- assertEquals("locked,enabled,null,null", makeString(sm));
-
- logger.info("\n??? test unlock()");
- sm.unlock();
- assertEquals("unlocked,enabled,null,null", makeString(sm));
-
- logger.info("\n??? test enableNotFailed()");
- sm.enableNotFailed();
- assertEquals("unlocked,enabled,null,null", makeString(sm));
-
- logger.info("\n??? test disableFailed()");
- sm.disableFailed();
- assertEquals("unlocked,disabled,failed,null", makeString(sm));
-
- // P4 If promote() is called while either the opState is disabled or
- // the adminState is locked,
- // the standbystatus shall transition to coldstandby and a
- // StandbyStatusException shall be thrown
- logger.info("\n??? promote() test case P4");
- assertException(sm, smx -> {
- sm.disableFailed();
- sm.lock();
-
- sm.promote();
- });
- assertEquals("locked,disabled,failed,coldstandby", makeString(sm));
-
- // P3 If promote() is called while standbyStatus is coldstandby, the
- // state shall not transition
- // and a StandbyStatusException shall be thrown
- logger.info("\n??? promote() test case P3");
- assertException(sm, smx -> {
- sm.promote();
- });
- assertEquals("locked,disabled,failed,coldstandby", makeString(sm));
-
- // P2 If promote() is called while the standbyStatus is null and the
- // opState is enabled and adminState is unlocked,
- // the state shall transition to providingservice
- logger.info("\n??? promote() test case P2");
- resourceName = "test_resource2";
- StateManagement sm2 = new StateManagement(emf, resourceName);
- sm2.enableNotFailed();
- sm2.unlock();
- assertEquals("unlocked,enabled,null,null", makeString(sm2));
- sm2.promote();
- assertEquals("unlocked,enabled,null,providingservice", makeString(sm2));
-
- // P5 If promote() is called while standbyStatus is
- // providingservice, no action is taken
- logger.info("\n??? promote() test case P5");
- sm2.promote();
- assertEquals("unlocked,enabled,null,providingservice", makeString(sm2));
-
- // D1 If demote() is called while standbyStatus is providingservice,
- // the state shall transition to hotstandby
- logger.info("\n??? demote() test case D1");
- sm2.demote();
- assertEquals("unlocked,enabled,null,hotstandby", makeString(sm2));
-
- // D4 If demote() is called while standbyStatus is hotstandby, no
- // action is taken
- logger.info("\n??? demote() test case D4");
- sm2.demote();
- assertEquals("unlocked,enabled,null,hotstandby", makeString(sm2));
-
- // D3 If demote() is called while standbyStatus is null and
- // adminState is locked or opState is disabled,
- // the state shall transition to coldstandby
- logger.info("\n??? demote() test case D3");
- resourceName = "test_resource3";
- StateManagement sm3 = new StateManagement(emf, resourceName);
- sm3.lock();
- sm3.disableFailed();
- sm3.demote();
- assertEquals("locked,disabled,failed,coldstandby", makeString(sm3));
-
- // D5 If demote() is called while standbyStatus is coldstandby, no
- // action is taken
- logger.info("\n??? demote() test case D5");
- sm3.demote();
- assertEquals("locked,disabled,failed,coldstandby", makeString(sm3));
-
- // D2 If demote() is called while standbyStatus is null and
- // adminState is unlocked and opState is enabled,
- // the state shall transition to hotstandby
- logger.info("\n??? demote() test case D2");
- resourceName = "test_resource4";
- StateManagement sm4 = new StateManagement(emf, resourceName);
- sm4.unlock();
- sm4.enableNotFailed();
- assertEquals("unlocked,enabled,null,null", makeString(sm4));
- sm4.demote();
- assertEquals("unlocked,enabled,null,hotstandby", makeString(sm4));
-
- // P1 If promote() is called while standbyStatus is hotstandby, the
- // state shall transition to providingservice.
- logger.info("\n??? promote() test case P1");
- sm4.promote();
- assertEquals("unlocked,enabled,null,providingservice", makeString(sm4));
-
- // State change notification
- logger.info("\n??? State change notification test case 1 - lock()");
- StateChangeNotifier stateChangeNotifier = new StateChangeNotifier();
- sm.addObserver(stateChangeNotifier);
- sm.lock();
- assertEquals("locked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
-
- logger.info("\n??? State change notification test case 2 - unlock()");
- sm.unlock();
- assertEquals("unlocked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
-
- logger.info("\n??? State change notification test case 3 - enabled()");
- sm.enableNotFailed();
- assertEquals("unlocked,enabled,null,hotstandby", makeString(stateChangeNotifier.getStateManagement()));
-
- logger.info("\n??? State change notification test case 4 - disableFailed()");
- sm.disableFailed();
- assertEquals("unlocked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
-
- logger.info("\n??? State change notification test case 5 - demote()");
- sm.demote();
- assertEquals("unlocked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
-
- logger.info("\n??? State change notification test case 6 - promote()");
- assertException(sm, smx -> {
- sm.promote();
- });
- assertEquals("unlocked,disabled,failed,coldstandby", makeString(sm));
-
- } catch (Exception ex) {
- logger.error("Exception: {}", ex.toString());
- throw ex;
- }
-
- logger.info("\n\nStateManagementTest: Exit\n\n");
- }
-
- /**
- * Converts a state element to a comma-separated string.
- *
- * @param se
- * element to be converted
- * @return a string representing the element
- */
- private String makeString(StateManagement sm) {
- if (sm == null) {
- return null;
- }
-
- StringBuilder b = new StringBuilder();
-
- b.append(sm.getAdminState());
- b.append(',');
- b.append(sm.getOpState());
- b.append(',');
- b.append(sm.getAvailStatus());
- b.append(',');
- b.append(sm.getStandbyStatus());
-
- return b.toString();
- }
+ private static final String TEST_RESOURCE_NAME = "test_resource1";
+ private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class);
+ //
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ IntegrityMonitorTestBase.setUpBeforeClass(DEFAULT_DB_URL_PREFIX + StateManagementTest.class.getSimpleName());
+
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ IntegrityMonitorTestBase.tearDownAfterClass();
+ }
+
+ @Before
+ public void setUp() {
+ super.setUpTest();
+ }
+
+ @After
+ public void tearDown() {
+ super.tearDownTest();
+ }
+
+ @Test
+ public void test() throws Exception {
+ logger.info("\n\nlogger.infor StateManagementTest: Entering\n\n");
+ String resourceName = TEST_RESOURCE_NAME;
+
+ // These parameters are in a properties file
+ try {
+ final StateManagement sm = new StateManagement(emf, resourceName);
+
+ logger.info("\n??? initial state");
+ assertEquals("unlocked,enabled,null,null", makeString(sm));
+
+ logger.info("\n??? test lock()");
+ sm.lock();
+ assertEquals("locked,enabled,null,null", makeString(sm));
+
+ logger.info("\n??? test unlock()");
+ sm.unlock();
+ assertEquals("unlocked,enabled,null,null", makeString(sm));
+
+ logger.info("\n??? test enableNotFailed()");
+ sm.enableNotFailed();
+ assertEquals("unlocked,enabled,null,null", makeString(sm));
+
+ logger.info("\n??? test disableFailed()");
+ sm.disableFailed();
+ assertEquals("unlocked,disabled,failed,null", makeString(sm));
+
+ // P4 If promote() is called while either the opState is disabled or
+ // the adminState is locked,
+ // the standbystatus shall transition to coldstandby and a
+ // StandbyStatusException shall be thrown
+ logger.info("\n??? promote() test case P4");
+ assertException(sm, smx -> {
+ sm.disableFailed();
+ sm.lock();
+
+ sm.promote();
+ });
+ assertEquals("locked,disabled,failed,coldstandby", makeString(sm));
+
+ // P3 If promote() is called while standbyStatus is coldstandby, the
+ // state shall not transition
+ // and a StandbyStatusException shall be thrown
+ logger.info("\n??? promote() test case P3");
+ assertException(sm, smx -> {
+ sm.promote();
+ });
+ assertEquals("locked,disabled,failed,coldstandby", makeString(sm));
+
+ // P2 If promote() is called while the standbyStatus is null and the
+ // opState is enabled and adminState is unlocked,
+ // the state shall transition to providingservice
+ logger.info("\n??? promote() test case P2");
+ resourceName = "test_resource2";
+ final StateManagement sm2 = new StateManagement(emf, resourceName);
+ sm2.enableNotFailed();
+ sm2.unlock();
+ assertEquals("unlocked,enabled,null,null", makeString(sm2));
+ sm2.promote();
+ assertEquals("unlocked,enabled,null,providingservice", makeString(sm2));
+
+ // P5 If promote() is called while standbyStatus is
+ // providingservice, no action is taken
+ logger.info("\n??? promote() test case P5");
+ sm2.promote();
+ assertEquals("unlocked,enabled,null,providingservice", makeString(sm2));
+
+ // D1 If demote() is called while standbyStatus is providingservice,
+ // the state shall transition to hotstandby
+ logger.info("\n??? demote() test case D1");
+ sm2.demote();
+ assertEquals("unlocked,enabled,null,hotstandby", makeString(sm2));
+
+ // D4 If demote() is called while standbyStatus is hotstandby, no
+ // action is taken
+ logger.info("\n??? demote() test case D4");
+ sm2.demote();
+ assertEquals("unlocked,enabled,null,hotstandby", makeString(sm2));
+
+ // D3 If demote() is called while standbyStatus is null and
+ // adminState is locked or opState is disabled,
+ // the state shall transition to coldstandby
+ logger.info("\n??? demote() test case D3");
+ resourceName = "test_resource3";
+ final StateManagement sm3 = new StateManagement(emf, resourceName);
+ sm3.lock();
+ sm3.disableFailed();
+ sm3.demote();
+ assertEquals("locked,disabled,failed,coldstandby", makeString(sm3));
+
+ // D5 If demote() is called while standbyStatus is coldstandby, no
+ // action is taken
+ logger.info("\n??? demote() test case D5");
+ sm3.demote();
+ assertEquals("locked,disabled,failed,coldstandby", makeString(sm3));
+
+ // D2 If demote() is called while standbyStatus is null and
+ // adminState is unlocked and opState is enabled,
+ // the state shall transition to hotstandby
+ logger.info("\n??? demote() test case D2");
+ resourceName = "test_resource4";
+ final StateManagement sm4 = new StateManagement(emf, resourceName);
+ sm4.unlock();
+ sm4.enableNotFailed();
+ assertEquals("unlocked,enabled,null,null", makeString(sm4));
+ sm4.demote();
+ assertEquals("unlocked,enabled,null,hotstandby", makeString(sm4));
+
+ // P1 If promote() is called while standbyStatus is hotstandby, the
+ // state shall transition to providingservice.
+ logger.info("\n??? promote() test case P1");
+ sm4.promote();
+ assertEquals("unlocked,enabled,null,providingservice", makeString(sm4));
+
+ // State change notification
+ logger.info("\n??? State change notification test case 1 - lock()");
+ final StateChangeNotifier stateChangeNotifier = new StateChangeNotifier();
+ sm.addObserver(stateChangeNotifier);
+ sm.lock();
+ assertEquals("locked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
+
+ logger.info("\n??? State change notification test case 2 - unlock()");
+ sm.unlock();
+ assertEquals("unlocked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
+
+ logger.info("\n??? State change notification test case 3 - enabled()");
+ sm.enableNotFailed();
+ assertEquals("unlocked,enabled,null,hotstandby", makeString(stateChangeNotifier.getStateManagement()));
+
+ logger.info("\n??? State change notification test case 4 - disableFailed()");
+ sm.disableFailed();
+ assertEquals("unlocked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
+
+ logger.info("\n??? State change notification test case 5 - demote()");
+ sm.demote();
+ assertEquals("unlocked,disabled,failed,coldstandby", makeString(stateChangeNotifier.getStateManagement()));
+
+ logger.info("\n??? State change notification test case 6 - promote()");
+ assertException(sm, smx -> {
+ sm.promote();
+ });
+ assertEquals("unlocked,disabled,failed,coldstandby", makeString(sm));
+
+ } catch (final Exception ex) {
+ logger.error("Exception: {}", ex.toString());
+ throw ex;
+ }
+
+ logger.info("\n\nStateManagementTest: Exit\n\n");
+ }
+
+ @Test(expected = StateManagementException.class)
+ @SuppressWarnings("unchecked")
+ public void test_StateManagementInitialization_ThrowStateManagementException_ifEntityManagerCreateQuerythrowsAnyException()
+ throws Exception {
+ final EntityManager mockedEm = getMockedEntityManager();
+ final EntityManagerFactory mockedEmf = getMockedEntityManagerFactory(mockedEm);
+
+ doThrow(PersistenceException.class).when(mockedEm).createQuery(anyString(),
+ any(StateManagementEntity.class.getClass()));
+
+ new StateManagement(mockedEmf, TEST_RESOURCE_NAME);
+
+ }
+
+ @Test(expected = StateManagementException.class)
+ @SuppressWarnings("unchecked")
+ public void test_StateManagementInitialization_ThrowStateManagementException_ifEntityManagerthrowsAnyException()
+ throws Exception {
+ final EntityManager mockedEm = getMockedEntityManager();
+ final EntityManagerFactory mockedEmf = getMockedEntityManagerFactory(mockedEm);
+ final TypedQuery<StateManagementEntity> mockedQuery = mock(TypedQuery.class);
+
+ when(mockedQuery.setFlushMode(Mockito.anyObject())).thenReturn(mockedQuery);
+ when(mockedQuery.setLockMode(Mockito.anyObject())).thenReturn(mockedQuery);
+ when(mockedEm.createQuery(anyString(), any(StateManagementEntity.class.getClass()))).thenReturn(mockedQuery);
+
+ doThrow(QueryTimeoutException.class).when(mockedQuery).getResultList();
+
+ new StateManagement(mockedEmf, TEST_RESOURCE_NAME);
+
+ }
+
+ private EntityManager getMockedEntityManager() {
+ final EntityManager mockedEm = mock(EntityManager.class);
+ final EntityTransaction mockedTransaction = mock(EntityTransaction.class);
+
+ when(mockedEm.getTransaction()).thenReturn(mockedTransaction);
+ return mockedEm;
+ }
+
+ private EntityManagerFactory getMockedEntityManagerFactory(final EntityManager entityManager) {
+ final EntityManagerFactory mockedEmf = mock(EntityManagerFactory.class);
+ when(mockedEmf.createEntityManager()).thenReturn(entityManager);
+
+ return mockedEmf;
+
+ }
+
+ /**
+ * Converts a state element to a comma-separated string.
+ *
+ * @param se element to be converted
+ * @return a string representing the element
+ */
+ private String makeString(final StateManagement sm) {
+ if (sm == null) {
+ return null;
+ }
+
+ final StringBuilder b = new StringBuilder();
+
+ b.append(sm.getAdminState());
+ b.append(',');
+ b.append(sm.getOpState());
+ b.append(',');
+ b.append(sm.getAvailStatus());
+ b.append(',');
+ b.append(sm.getStandbyStatus());
+
+ return b.toString();
+ }
}
diff --git a/integrity-monitor/src/test/java/org/onap/policy/common/im/exceptions/EntityRetrievalExceptionTest.java b/integrity-monitor/src/test/java/org/onap/policy/common/im/exceptions/EntityRetrievalExceptionTest.java
new file mode 100644
index 00000000..8d948241
--- /dev/null
+++ b/integrity-monitor/src/test/java/org/onap/policy/common/im/exceptions/EntityRetrievalExceptionTest.java
@@ -0,0 +1,34 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Integrity Monitor
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.im.exceptions;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+import org.onap.policy.common.utils.test.ExceptionsTester;
+
+public class EntityRetrievalExceptionTest extends ExceptionsTester {
+
+ @Test
+ public void testEntityRetrievalException() throws Exception {
+ assertEquals(1, test(EntityRetrievalException.class));
+ }
+
+}