forked from community/device-mgt-core
Application mgt new (#7)
* Improve lifecycle state management * Add unit tests for lifecycle management * Add class level comments to lifecycle managementfeature/appm-store/pbac
parent
421dc5edf1
commit
7d59c342b2
@ -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<String, State> lifecycleStates;
|
||||
|
||||
public LifecycleStateManger(List<LifecycleState> states) {
|
||||
lifecycleStates = new HashMap<>();
|
||||
for (LifecycleState s : states) {
|
||||
lifecycleStates.put(s.getName(), new State(s.getName(), s.getProceedingStates()));
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> 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;
|
||||
}
|
||||
}
|
@ -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<String> proceedingStates;
|
||||
private String stateName;
|
||||
|
||||
public State(String stateName, List<String> states) {
|
||||
this.stateName = stateName;
|
||||
if (states != null && !states.isEmpty()) {
|
||||
proceedingStates = new HashSet<>(states);
|
||||
}
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return stateName;
|
||||
}
|
||||
|
||||
public Set<String> getProceedingStates() {
|
||||
return proceedingStates;
|
||||
}
|
||||
|
||||
}
|
@ -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<String> 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<String> getProceedingStates() {
|
||||
return proceedingStates;
|
||||
}
|
||||
|
||||
public void setProceedingStates(List<String> proceedingStates) {
|
||||
this.proceedingStates = proceedingStates;
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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<LifecycleState> 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<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE);
|
||||
Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE,
|
||||
proceedingStates.contains(NEXT_STATE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkInvalidNextLifecycleState() {
|
||||
Set<String> 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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue