From 7d59c342b277e7ee69502c18a70ba3e2e81473a3 Mon Sep 17 00:00:00 2001 From: Milan Perera Date: Wed, 19 Sep 2018 11:21:40 +0530 Subject: [PATCH] Application mgt new (#7) * Improve lifecycle state management * Add unit tests for lifecycle management * Add class level comments to lifecycle management --- .../common/services/ApplicationManager.java | 2 +- .../mgt/core/config/Configuration.java | 15 +++ .../mgt/core/impl/ApplicationManagerImpl.java | 121 +++--------------- .../mgt/core/internal/DataHolder.java | 11 ++ .../mgt/core/internal/ServiceComponent.java | 9 ++ .../core/lifecycle/LifecycleStateManger.java | 34 +++++ .../application/mgt/core/lifecycle/State.java | 30 +++++ .../core/lifecycle/config/LifecycleState.java | 35 +++++ .../ConfigurationTest.java | 23 ++-- .../InitTest.java | 20 +++ .../LifecycleManagementTest.java | 57 +++++++++ .../src/test/resources/application-mgt.xml | 43 +++++++ .../src/test/resources/testng.xml | 3 + .../impl/ApplicationManagementAPIImpl.java | 2 +- .../main/resources/conf/application-mgt.xml | 57 +++++++++ 15 files changed, 348 insertions(+), 114 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/InitTest.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 1335c6d9dc..4df7b50d33 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -150,7 +150,7 @@ public interface ApplicationManager { * @param applicationUuid UUID of the Application Release. * @throws ApplicationManagementException Application Management Exception. */ - void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws ApplicationManagementException; + void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws ApplicationManagementException; /** * Get the application if application is an accessible one. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java index 3ef617ff0e..99f1aa1c56 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java @@ -18,6 +18,8 @@ */ package org.wso2.carbon.device.application.mgt.core.config; +import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; + import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; @@ -37,6 +39,8 @@ public class Configuration { private PaginationConfiguration paginationConfiguration; + private List lifecycleStates; + @XmlElement(name = "DatasourceName", required = true) public String getDatasourceName() { return datasourceName; @@ -60,6 +64,17 @@ public class Configuration { public PaginationConfiguration getPaginationConfiguration() { return paginationConfiguration; } + + @XmlElementWrapper(name = "LifecycleStates") + @XmlElement(name = "LifecycleState") + public List getLifecycleStates() { + return lifecycleStates; + } + + public void setLifecycleStates( + List lifecycleStates) { + this.lifecycleStates = lifecycleStates; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index b4540d23bd..dc6d240b19 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -37,7 +37,6 @@ import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.User; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; -import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; @@ -49,6 +48,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagement import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.ValidationException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; @@ -73,10 +73,12 @@ public class ApplicationManagerImpl implements ApplicationManager { private ApplicationDAO applicationDAO; private ApplicationReleaseDAO applicationReleaseDAO; private LifecycleStateDAO lifecycleStateDAO; + private LifecycleStateManger lifecycleStateManger; public ApplicationManagerImpl() { initDataAccessObjects(); + lifecycleStateManger = DataHolder.getInstance().getLifecycleStateManager(); } private void initDataAccessObjects() { @@ -139,7 +141,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); + changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -216,7 +218,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); + changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; @@ -406,7 +408,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); storedLocations.add(applicationRelease.getAppHashValue()); } ConnectionManagerUtil.openDBConnection(); @@ -437,7 +439,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); }else{ throw new ApplicationManagementException("Can't delete the application release, You have to move the " + "lifecycle state from "+ currentState + " to acceptable " + @@ -689,7 +691,8 @@ public class ApplicationManagerImpl implements ApplicationManager { + applicationUuid); } - lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState())); + lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManger. + getNextLifecycleStates(lifecycleState.getCurrentState()))); } catch (ApplicationManagementDAOException e) { throw new ApplicationManagementException("Failed to get lifecycle state", e); } catch (ApplicationManagementException e) { @@ -701,7 +704,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) + public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws ApplicationManagementException { try { ConnectionManagerUtil.openDBConnection(); @@ -712,9 +715,14 @@ public class ApplicationManagerImpl implements ApplicationManager { state.setUpdatedBy(userName); if (state.getCurrentState() != null && state.getPreviousState() != null) { - validateLifecycleState(state); - this.lifecycleStateDAO - .addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId); + if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState())) { + this.lifecycleStateDAO + .addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId); + } else { + log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + + " to '" + state.getCurrentState() + "'"); + throw new ApplicationManagementException("Lifecycle State Validation failed"); + } } } catch (LifeCycleManagementDAOException | DBConnectionException e) { throw new ApplicationManagementException("Failed to add lifecycle state", e); @@ -725,99 +733,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - private List getNextLifecycleStates(String currentLifecycleState) { - List nextLifecycleStates = new ArrayList<>(); - if (AppLifecycleState.CREATED.toString().equals(currentLifecycleState)) { - nextLifecycleStates.add(AppLifecycleState.IN_REVIEW.toString()); - } - if (AppLifecycleState.IN_REVIEW.toString().equals(currentLifecycleState)) { - nextLifecycleStates.add(AppLifecycleState.APPROVED.toString()); - nextLifecycleStates.add(AppLifecycleState.REJECTED.toString()); - } - if (AppLifecycleState.REJECTED.toString().equals(currentLifecycleState)) { - nextLifecycleStates.add(AppLifecycleState.IN_REVIEW.toString()); - nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); - } - if (AppLifecycleState.APPROVED.toString().equals(currentLifecycleState)) { - nextLifecycleStates.add(AppLifecycleState.PUBLISHED.toString()); - } - if (AppLifecycleState.PUBLISHED.toString().equals(currentLifecycleState)) { - nextLifecycleStates.add(AppLifecycleState.UNPUBLISHED.toString()); - nextLifecycleStates.add(AppLifecycleState.DEPRECATED.toString()); - } - if (AppLifecycleState.UNPUBLISHED.toString().equals(currentLifecycleState)) { - nextLifecycleStates.add(AppLifecycleState.PUBLISHED.toString()); - nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); - } - if (AppLifecycleState.DEPRECATED.toString().equals(currentLifecycleState)) { - nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); - } - return nextLifecycleStates; - } - - private void validateLifecycleState(LifecycleState state) throws LifecycleManagementException { - - if (AppLifecycleState.CREATED.toString().equals(state.getCurrentState())) { - throw new LifecycleManagementException("Current State Couldn't be " + state.getCurrentState()); - } - if (AppLifecycleState.IN_REVIEW.toString().equals(state.getCurrentState()) && !AppLifecycleState.CREATED - .toString().equals(state.getPreviousState()) && !AppLifecycleState.REJECTED.toString() - .equals(state.getPreviousState())) { - throw new LifecycleManagementException( - "If Current State is " + state.getCurrentState() + "Previous State should be either " - + AppLifecycleState.CREATED.toString() + " or " + AppLifecycleState.REJECTED.toString()); - - } - if (AppLifecycleState.APPROVED.toString().equals(state.getCurrentState()) && !AppLifecycleState.IN_REVIEW - .toString().equals(state.getPreviousState())) { - throw new LifecycleManagementException( - "If Current State is " + state.getCurrentState() + "Previous State should be " - + AppLifecycleState.IN_REVIEW.toString()); - - } - if (AppLifecycleState.PUBLISHED.toString().equals(state.getCurrentState()) && !AppLifecycleState.APPROVED - .toString().equals(state.getPreviousState()) && !AppLifecycleState.UNPUBLISHED.toString() - .equals(state.getPreviousState())) { - throw new LifecycleManagementException( - "If Current State is " + state.getCurrentState() + "Previous State should be either " - + AppLifecycleState.APPROVED.toString() + " or " + AppLifecycleState.UNPUBLISHED - .toString()); - - } - if (AppLifecycleState.UNPUBLISHED.toString().equals(state.getCurrentState()) && !AppLifecycleState.PUBLISHED - .toString().equals(state.getPreviousState())) { - throw new LifecycleManagementException( - "If Current State is " + state.getCurrentState() + "Previous State should be " - + AppLifecycleState.PUBLISHED.toString()); - - } - if (AppLifecycleState.REJECTED.toString().equals(state.getCurrentState()) && !AppLifecycleState.IN_REVIEW - .toString().equals(state.getPreviousState())) { - throw new LifecycleManagementException( - "If Current State is " + state.getCurrentState() + "Previous State should be " - + AppLifecycleState.IN_REVIEW.toString()); - - } - if (AppLifecycleState.DEPRECATED.toString().equals(state.getCurrentState()) && !AppLifecycleState.PUBLISHED - .toString().equals(state.getPreviousState())) { - - throw new LifecycleManagementException( - "If Current State is " + state.getCurrentState() + "Previous State should be " - + AppLifecycleState.PUBLISHED.toString()); - - } - if (AppLifecycleState.REMOVED.toString().equals(state.getCurrentState()) && !AppLifecycleState.DEPRECATED - .toString().equals(state.getPreviousState()) && !AppLifecycleState.REJECTED.toString() - .equals(state.getPreviousState()) && !AppLifecycleState.UNPUBLISHED.toString() - .equals(state.getPreviousState())) { - - throw new LifecycleManagementException( - "If Current State is " + state.getCurrentState() + "Previous State should be either " - + AppLifecycleState.DEPRECATED.toString() + " or " + AppLifecycleState.REJECTED.toString() - + " or " + AppLifecycleState.UNPUBLISHED.toString()); - } - } - @Override public Application updateApplication(Application application) throws ApplicationManagementException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java index 4f74582336..68cfce54cd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorage import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.common.services.UnrestrictedRoleManager; +import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.user.core.service.RealmService; @@ -45,6 +46,8 @@ public class DataHolder { private ApplicationStorageManager applicationStorageManager; + private LifecycleStateManger lifecycleStateManger; + private static final DataHolder applicationMgtDataHolder = new DataHolder(); private DataHolder() { @@ -110,4 +113,12 @@ public class DataHolder { public ApplicationStorageManager getApplicationStorageManager() { return applicationStorageManager; } + + public LifecycleStateManger getLifecycleStateManager() { + return lifecycleStateManger; + } + + public void setLifecycleStateManger(LifecycleStateManger lifecycleStateManger) { + this.lifecycleStateManger = lifecycleStateManger; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java index 7a2fd489ed..e2224b2c30 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java @@ -31,12 +31,15 @@ import org.wso2.carbon.device.application.mgt.common.services.UnrestrictedRoleMa import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; +import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.user.core.service.RealmService; import javax.naming.NamingException; +import java.util.List; /** * @scr.component name="org.wso2.carbon.application.mgt.service" immediate="true" @@ -93,6 +96,12 @@ public class ServiceComponent { ApplicationManagementDAOFactory.init(datasourceName); ApplicationManagementDAOFactory.initDatabases(); + + List lifecycleStates = ConfigurationManager.getInstance(). + getConfiguration().getLifecycleStates(); + LifecycleStateManger lifecycleStateManger = new LifecycleStateManger(lifecycleStates); + DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManger); + log.info("ApplicationManagement core bundle has been successfully initialized"); } catch (InvalidConfigurationException e) { log.error("Error while activating Application Management core component. ", e); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java new file mode 100644 index 0000000000..1a0218b20b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java @@ -0,0 +1,34 @@ +package org.wso2.carbon.device.application.mgt.core.lifecycle; + +import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * This class represents the activities related to lifecycle management + */ +public class LifecycleStateManger { + + private Map lifecycleStates; + + public LifecycleStateManger(List states) { + lifecycleStates = new HashMap<>(); + for (LifecycleState s : states) { + lifecycleStates.put(s.getName(), new State(s.getName(), s.getProceedingStates())); + } + } + + public Set getNextLifecycleStates(String currentLifecycleState) { + return lifecycleStates.get(currentLifecycleState).getProceedingStates(); + } + + public boolean isValidStateChange(String currentState, String nextState) { + if (lifecycleStates.get(currentState).getProceedingStates().contains(nextState)) { + return true; + } + return false; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java new file mode 100644 index 0000000000..e09b070c19 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/State.java @@ -0,0 +1,30 @@ +package org.wso2.carbon.device.application.mgt.core.lifecycle; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * This class represents the state of the lifecycle + */ +public class State { + + private Set proceedingStates; + private String stateName; + + public State(String stateName, List states) { + this.stateName = stateName; + if (states != null && !states.isEmpty()) { + proceedingStates = new HashSet<>(states); + } + } + + public String getState() { + return stateName; + } + + public Set getProceedingStates() { + return proceedingStates; + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java new file mode 100644 index 0000000000..c6682e93e9 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/config/LifecycleState.java @@ -0,0 +1,35 @@ +package org.wso2.carbon.device.application.mgt.core.lifecycle.config; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import java.util.List; + +/** + * This class represents the lifecycle state config + */ +public class LifecycleState { + + private String name; + + private List proceedingStates; + + @XmlAttribute(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlElementWrapper(name = "ProceedingStates") + @XmlElement(name = "State") + public List getProceedingStates() { + return proceedingStates; + } + + public void setProceedingStates(List proceedingStates) { + this.proceedingStates = proceedingStates; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java index 4040237b07..cd6803a1cb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/ConfigurationTest.java @@ -17,25 +17,30 @@ */ package org.wso2.carbon.device.application.mgt.core; -import org.junit.BeforeClass; -import org.junit.Test; -import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; +import org.junit.Assert; +import org.testng.annotations.Test; import org.wso2.carbon.device.application.mgt.core.config.Configuration; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; +import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; -import java.io.File; +import java.util.List; public class ConfigurationTest { - @BeforeClass - public static void init() throws InvalidConfigurationException { - File configPath = new File("src/test/resources/application-mgt.xml"); - ConfigurationManager.setConfigLocation(configPath.getAbsolutePath()); + @Test + public void validateConfiguration() { + ConfigurationManager configurationManager = ConfigurationManager.getInstance(); + Configuration configuration = configurationManager.getConfiguration(); + Assert.assertNotNull("Invalid app manager configuration", configuration); } @Test - public void validateConfiguration() { + public void validateLifecycleStateConfiguration() { ConfigurationManager configurationManager = ConfigurationManager.getInstance(); Configuration configuration = configurationManager.getConfiguration(); + List lifecycleStates = configuration.getLifecycleStates(); + Assert.assertNotNull("Invalid lifecycle states configuration", lifecycleStates); + Assert.assertTrue("Invalid lifecycle states configuration. Lifecycle states cannot be empty", + !lifecycleStates.isEmpty()); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/InitTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/InitTest.java new file mode 100644 index 0000000000..fae842e8b0 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/InitTest.java @@ -0,0 +1,20 @@ +package org.wso2.carbon.device.application.mgt.core; + +import org.testng.annotations.BeforeSuite; +import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; +import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; + +import java.io.File; + +/** + * This class initializes the required configurations prior running the tests + */ +public class InitTest { + + @BeforeSuite + public void init() throws InvalidConfigurationException { + File configPath = new File("src/test/resources/application-mgt.xml"); + ConfigurationManager.setConfigLocation(configPath.getAbsolutePath()); + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java new file mode 100644 index 0000000000..4b644debac --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java @@ -0,0 +1,57 @@ +package org.wso2.carbon.device.application.mgt.core; + +import org.junit.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.device.application.mgt.core.config.Configuration; +import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; +import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; +import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; + +import java.util.List; +import java.util.Set; + +public class LifecycleManagementTest { + + private List lifecycleStates; + private LifecycleStateManger lifecycleStateManger; + + private final String CURRENT_STATE = "Approved"; + private final String NEXT_STATE = "Published"; + private final String BOGUS_STATE = "Removed"; + + @BeforeClass + public void init() { + ConfigurationManager configurationManager = ConfigurationManager.getInstance(); + Configuration configuration = configurationManager.getConfiguration(); + lifecycleStates = configuration.getLifecycleStates(); + lifecycleStateManger = new LifecycleStateManger(lifecycleStates); + } + + @Test + public void checkValidNextLifecycleState() { + Set proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); + Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE, + proceedingStates.contains(NEXT_STATE)); + } + + @Test + public void checkInvalidNextLifecycleState() { + Set proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); + Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, + proceedingStates.contains(BOGUS_STATE)); + } + + @Test + public void checkValidStateChange() { + Assert.assertTrue("Invalid state transition from: " + CURRENT_STATE + " to: " + NEXT_STATE, + lifecycleStateManger.isValidStateChange(CURRENT_STATE, NEXT_STATE)); + } + + @Test + public void checkInvalidStateChange() { + Assert.assertFalse("Invalid state transition from: " + CURRENT_STATE + " to: " + BOGUS_STATE, + lifecycleStateManger.isValidStateChange(CURRENT_STATE, BOGUS_STATE)); + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml index 71ebffdd6b..b764b9e53e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/application-mgt.xml @@ -58,4 +58,47 @@ + + + + In-Review + + + + + Rejected + Approved + + + + + Published + + + + + In-Review + Removed + + + + + Unpublished + Deprecated + + + + + Removed + + + + + Removed + + + + + + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml index 9a69c5a87f..8733d4f336 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml @@ -22,8 +22,11 @@ + + + \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java index f212747cef..f57b2185df 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java @@ -448,7 +448,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { LifecycleState state) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.addLifecycleState(applicationId, applicationUuid, state); + applicationManager.changeLifecycleState(applicationId, applicationUuid, state); } catch (ApplicationManagementException e) { String msg = "Error occurred while adding lifecycle state."; log.error(msg, e); diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml index 26a011de63..cfe2145e56 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml @@ -60,4 +60,61 @@ + + + + + + + In-Review + + + + + Rejected + Approved + + + + + Published + + + + + In-Review + Removed + + + + + Unpublished + Deprecated + + + + + Removed + + + + + Removed + + + + + +