Add seperate class to facilitate unit tests related to the LifeCycleManagementTest

feature/appm-store/pbac
Gathika94 6 years ago
parent 64507c1011
commit c8b983fb93

@ -52,7 +52,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.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.VisibilityManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; 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.lifecycle.LifecycleStateManager;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -77,12 +77,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
private ApplicationDAO applicationDAO; private ApplicationDAO applicationDAO;
private ApplicationReleaseDAO applicationReleaseDAO; private ApplicationReleaseDAO applicationReleaseDAO;
private LifecycleStateDAO lifecycleStateDAO; private LifecycleStateDAO lifecycleStateDAO;
private LifecycleStateManger lifecycleStateManger; private LifecycleStateManager lifecycleStateManager;
public ApplicationManagerImpl() { public ApplicationManagerImpl() {
initDataAccessObjects(); initDataAccessObjects();
lifecycleStateManger = DataHolder.getInstance().getLifecycleStateManager(); lifecycleStateManager = DataHolder.getInstance().getLifecycleStateManager();
} }
private void initDataAccessObjects() { private void initDataAccessObjects() {
@ -576,7 +576,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid());
LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(), LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(),
appLifecycleState.getCurrentState()); appLifecycleState.getCurrentState());
if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(), if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(),
newAppLifecycleState.getCurrentState(),userName,tenantId)) { newAppLifecycleState.getCurrentState(),userName,tenantId)) {
this.lifecycleStateDAO this.lifecycleStateDAO
.addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(),
@ -623,7 +623,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(), LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(),
appLifecycleState.getCurrentState()); appLifecycleState.getCurrentState());
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(), if (lifecycleStateManager.isValidStateChange(newAppLifecycleState.getPreviousState(),
newAppLifecycleState.getCurrentState(),userName,tenantId)) { newAppLifecycleState.getCurrentState(),userName,tenantId)) {
this.lifecycleStateDAO this.lifecycleStateDAO
.addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(), .addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(),
@ -949,7 +949,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ releaseUuid); + releaseUuid);
} }
lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManger.getNextLifecycleStates(lifecycleState.getCurrentState()))); lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManager.getNextLifecycleStates(lifecycleState.getCurrentState())));
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
throw new ApplicationManagementException("Failed to get application and application management", e); throw new ApplicationManagementException("Failed to get application and application management", e);
@ -985,7 +985,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
state.setUpdatedBy(userName); state.setUpdatedBy(userName);
if (state.getCurrentState() != null && state.getPreviousState() != null) { if (state.getCurrentState() != null && state.getPreviousState() != null) {
if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState(),userName, if (lifecycleStateManager.isValidStateChange(state.getPreviousState(), state.getCurrentState(),userName,
tenantId)) { tenantId)) {
//todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there //todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there
//todo any other application release in PUBLISHED state for the application( i.e for the appid) //todo any other application release in PUBLISHED state for the application( i.e for the appid)

@ -31,7 +31,7 @@ import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import org.wso2.carbon.device.application.mgt.core.impl.ConfigManagerImpl; import org.wso2.carbon.device.application.mgt.core.impl.ConfigManagerImpl;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager;
import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; 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.application.mgt.core.util.ApplicationManagementUtil;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -79,10 +79,10 @@ public class ApplicationManagementServiceComponent {
List<LifecycleState> lifecycleStates = ConfigurationManager.getInstance(). List<LifecycleState> lifecycleStates = ConfigurationManager.getInstance().
getConfiguration().getLifecycleStates(); getConfiguration().getLifecycleStates();
LifecycleStateManger lifecycleStateManger = ApplicationManagementUtil.getLifecycleStateMangerInstance(); LifecycleStateManager lifecycleStateManager = ApplicationManagementUtil.getLifecycleStateMangerInstance();
lifecycleStateManger.init(lifecycleStates); lifecycleStateManager.init(lifecycleStates);
DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManger); DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManager);
bundleContext.registerService(LifecycleStateManger.class.getName(), lifecycleStateManger, null); bundleContext.registerService(LifecycleStateManager.class.getName(), lifecycleStateManager, null);
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
DataHolder.getInstance().setApplicationManager(applicationManager); DataHolder.getInstance().setApplicationManager(applicationManager);

@ -23,7 +23,7 @@ import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorage
import org.wso2.carbon.device.application.mgt.common.services.ConfigManager; import org.wso2.carbon.device.application.mgt.common.services.ConfigManager;
import org.wso2.carbon.device.application.mgt.common.services.ReviewManager; 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.SubscriptionManager;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
@ -44,7 +44,7 @@ public class DataHolder {
private ApplicationStorageManager applicationStorageManager; private ApplicationStorageManager applicationStorageManager;
private LifecycleStateManger lifecycleStateManger; private LifecycleStateManager lifecycleStateManager;
private ConfigManager configManager; private ConfigManager configManager;
@ -106,12 +106,12 @@ public class DataHolder {
return applicationStorageManager; return applicationStorageManager;
} }
public LifecycleStateManger getLifecycleStateManager() { public LifecycleStateManager getLifecycleStateManager() {
return lifecycleStateManger; return lifecycleStateManager;
} }
public void setLifecycleStateManger(LifecycleStateManger lifecycleStateManger) { public void setLifecycleStateManger(LifecycleStateManager lifecycleStateManager) {
this.lifecycleStateManger = lifecycleStateManger; this.lifecycleStateManager = lifecycleStateManager;
} }
public ConfigManager getConfigManager() { public ConfigManager getConfigManager() {

@ -55,10 +55,10 @@ import java.util.Set;
/** /**
* This class represents the activities related to lifecycle management * This class represents the activities related to lifecycle management
*/ */
public class LifecycleStateManger { public class LifecycleStateManager {
private Map<String, State> lifecycleStates; private Map<String, State> lifecycleStates;
private static Log log = LogFactory.getLog(LifecycleStateManger.class); private static Log log = LogFactory.getLog(LifecycleStateManager.class);
public void init(List<LifecycleState> states) throws LifecycleManagementException { public void init(List<LifecycleState> states) throws LifecycleManagementException {
lifecycleStates = new HashMap<>(); lifecycleStates = new HashMap<>();
@ -80,17 +80,6 @@ public class LifecycleStateManger {
} }
} }
public void initializeLifeCycleDetails(List<LifecycleState> states){
lifecycleStates = new HashMap<>();
for (LifecycleState s : states) {
if (s.getProceedingStates() != null) {
s.getProceedingStates().replaceAll(String::toUpperCase);
}
lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(),
s.getProceedingStates(), s.getPermission(), s.isAppUpdatable(), s.isAppInstallable(),
s.isInitialState(), s.isEndState()));
}
}
public Set<String> getNextLifecycleStates(String currentLifecycleState) { public Set<String> getNextLifecycleStates(String currentLifecycleState) {
return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates(); return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates();
@ -136,7 +125,6 @@ public class LifecycleStateManger {
if (pair.getKey().toString().equalsIgnoreCase(currentState)) { if (pair.getKey().toString().equalsIgnoreCase(currentState)) {
return lifecycleStates.get(pair.getKey().toString()); return lifecycleStates.get(pair.getKey().toString());
} }
//it.remove();
} }
return null; return null;
} }
@ -165,19 +153,23 @@ public class LifecycleStateManger {
return null; return null;
} }
public boolean isUpdatable(String state){ public boolean isUpdatable(String state) {
State currentState = getMatchingState(state); State currentState = getMatchingState(state);
if(currentState.getIsAppUpdatable()){ if (currentState.getIsAppUpdatable()) {
return true; return true;
} }
return false; return false;
} }
public boolean isInstallable(String state){ public boolean isInstallable(String state) {
State currentState = getMatchingState(state); State currentState = getMatchingState(state);
if(currentState.getIsAppInstallable()){ if (currentState.getIsAppInstallable()) {
return true; return true;
} }
return false; return false;
} }
public void setLifecycleStates(Map<String, State> lifecycleStates) {
this.lifecycleStates = lifecycleStates;
}
} }

@ -27,7 +27,7 @@ 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.SubscriptionManager;
import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.core.config.Extension; import org.wso2.carbon.device.application.mgt.core.config.Extension;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@ -64,10 +64,10 @@ public class ApplicationManagementUtil {
return getInstance(extension, ApplicationStorageManager.class); return getInstance(extension, ApplicationStorageManager.class);
} }
public static LifecycleStateManger getLifecycleStateMangerInstance() throws InvalidConfigurationException { public static LifecycleStateManager getLifecycleStateMangerInstance() throws InvalidConfigurationException {
ConfigurationManager configurationManager = ConfigurationManager.getInstance(); ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Extension extension = configurationManager.getExtension(Extension.Name.LifecycleStateManager); Extension extension = configurationManager.getExtension(Extension.Name.LifecycleStateManager);
return getInstance(extension, LifecycleStateManger.class); return getInstance(extension, LifecycleStateManager.class);
} }
private static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException { private static <T> T getInstance(Extension extension, Class<T> cls) throws InvalidConfigurationException {

@ -0,0 +1,25 @@
package org.wso2.carbon.device.application.mgt.core;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager;
import org.wso2.carbon.device.application.mgt.core.lifecycle.State;
import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState;
import java.util.HashMap;
import java.util.List;
public class LifeCycleStateManagerTest extends LifecycleStateManager {
public void initializeLifeCycleDetails(List<LifecycleState> states) {
HashMap<String, State> lifecycleStates = new HashMap<>();
for (LifecycleState s : states) {
if (s.getProceedingStates() != null) {
s.getProceedingStates().replaceAll(String::toUpperCase);
}
lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(),
s.getProceedingStates(), s.getPermission(), s.isAppUpdatable(), s.isAppInstallable(),
s.isInitialState(), s.isEndState()));
}
setLifecycleStates(lifecycleStates);
}
}

@ -6,7 +6,7 @@ import org.testng.annotations.Test;
import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException;
import org.wso2.carbon.device.application.mgt.core.config.Configuration; 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.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager;
import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState;
import java.util.List; import java.util.List;
@ -15,72 +15,70 @@ import java.util.Set;
public class LifecycleManagementTest { public class LifecycleManagementTest {
private List<LifecycleState> lifecycleStates; private List<LifecycleState> lifecycleStates;
private LifecycleStateManger lifecycleStateManger; private LifecycleStateManager lifecycleStateManager;
private final String CURRENT_STATE = "Approved"; private final String CURRENT_STATE = "Approved";
private final String NEXT_STATE = "Published"; private final String NEXT_STATE = "Published";
private final String BOGUS_STATE = "Removed"; private final String BOGUS_STATE = "Removed";
private final String UPDATABLE_STATE = "Created"; private final String UPDATABLE_STATE = "Created";
private final String NON_UPDATABLE_STATE= "Removed"; private final String NON_UPDATABLE_STATE = "Removed";
private final String INSTALLABLE_STATE = "Published"; private final String INSTALLABLE_STATE = "Published";
private final String UNINSTALlABLE_STATE = "Removed"; private final String UNINSTALlABLE_STATE = "Removed";
@BeforeClass @BeforeClass
public void init() throws LifecycleManagementException { public void init() throws LifecycleManagementException {
ConfigurationManager configurationManager = ConfigurationManager.getInstance(); ConfigurationManager configurationManager = ConfigurationManager.getInstance();
Configuration configuration = configurationManager.getConfiguration(); Configuration configuration = configurationManager.getConfiguration();
lifecycleStates = configuration.getLifecycleStates(); lifecycleStates = configuration.getLifecycleStates();
lifecycleStateManger = new LifecycleStateManger(); lifecycleStateManager = new LifeCycleStateManagerTest();
lifecycleStateManger.initializeLifeCycleDetails(lifecycleStates); ((LifeCycleStateManagerTest) lifecycleStateManager).initializeLifeCycleDetails(lifecycleStates);
} }
@Test @Test
public void checkValidNextLifecycleState() { public void checkValidNextLifecycleState() {
Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); Set<String> proceedingStates = lifecycleStateManager.getNextLifecycleStates(CURRENT_STATE);
Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE, Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(NEXT_STATE.toUpperCase())); proceedingStates.contains(NEXT_STATE.toUpperCase()));
} }
@Test @Test
public void checkInvalidNextLifecycleState() { public void checkInvalidNextLifecycleState() {
Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); Set<String> proceedingStates = lifecycleStateManager.getNextLifecycleStates(CURRENT_STATE);
Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(BOGUS_STATE.toUpperCase())); proceedingStates.contains(BOGUS_STATE.toUpperCase()));
} }
@Test @Test
public void CheckUpdatableState() { public void CheckUpdatableState() {
Boolean isUpdatable = lifecycleStateManger.isUpdatable(UPDATABLE_STATE); Boolean isUpdatable = lifecycleStateManager.isUpdatable(UPDATABLE_STATE);
System.out.println(isUpdatable); System.out.println(isUpdatable);
Assert.assertTrue("Updatable state: " + UPDATABLE_STATE, isUpdatable); Assert.assertTrue("Updatable state: " + UPDATABLE_STATE, isUpdatable);
} }
@Test @Test
public void CheckNonUpdatableState() { public void CheckNonUpdatableState() {
Boolean isUpdatable = lifecycleStateManger.isUpdatable(NON_UPDATABLE_STATE); Boolean isUpdatable = lifecycleStateManager.isUpdatable(NON_UPDATABLE_STATE);
Assert.assertFalse("Non Updatable state: " + CURRENT_STATE, isUpdatable); Assert.assertFalse("Non Updatable state: " + NON_UPDATABLE_STATE, isUpdatable);
} }
@Test @Test
public void CheckInstallableState() { public void CheckInstallableState() {
Boolean isInstallable = lifecycleStateManger.isInstallable(INSTALLABLE_STATE); Boolean isInstallable = lifecycleStateManager.isInstallable(INSTALLABLE_STATE);
Assert.assertTrue("Installable state: " + INSTALLABLE_STATE,isInstallable); Assert.assertTrue("Installable state: " + INSTALLABLE_STATE, isInstallable);
} }
@Test @Test
public void CheckUnInstallableState() { public void CheckUnInstallableState() {
Boolean isInstallable = lifecycleStateManger.isInstallable(UNINSTALlABLE_STATE); Boolean isInstallable = lifecycleStateManager.isInstallable(UNINSTALlABLE_STATE);
Assert.assertFalse("UnInstallable state: " + UNINSTALlABLE_STATE,isInstallable); Assert.assertFalse("UnInstallable state: " + UNINSTALlABLE_STATE, isInstallable);
} }
@Test @Test
public void check() { public void check() {
Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); Set<String> proceedingStates = lifecycleStateManager.getNextLifecycleStates(CURRENT_STATE);
Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(BOGUS_STATE.toUpperCase())); proceedingStates.contains(BOGUS_STATE.toUpperCase()));
} }
} }

@ -29,7 +29,7 @@
<ClassName>org.wso2.carbon.device.application.mgt.core.impl.ReviewManagerImpl</ClassName> <ClassName>org.wso2.carbon.device.application.mgt.core.impl.ReviewManagerImpl</ClassName>
</Extension> </Extension>
<Extension name="LifecycleStateManager"> <Extension name="LifecycleStateManager">
<ClassName>org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger</ClassName> <ClassName>org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager</ClassName>
</Extension> </Extension>
<Extension name="SubscriptionManager"> <Extension name="SubscriptionManager">
<ClassName>org.wso2.carbon.device.application.mgt.core.impl.SubscriptionManagerImpl</ClassName> <ClassName>org.wso2.carbon.device.application.mgt.core.impl.SubscriptionManagerImpl</ClassName>

@ -29,7 +29,7 @@
<ClassName>org.wso2.carbon.device.application.mgt.core.impl.ReviewManagerImpl</ClassName> <ClassName>org.wso2.carbon.device.application.mgt.core.impl.ReviewManagerImpl</ClassName>
</Extension> </Extension>
<Extension name="LifecycleStateManager"> <Extension name="LifecycleStateManager">
<ClassName>org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger</ClassName> <ClassName>org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager</ClassName>
</Extension> </Extension>
<Extension name="SubscriptionManager"> <Extension name="SubscriptionManager">
<ClassName>org.wso2.carbon.device.application.mgt.core.impl.SubscriptionManagerImpl</ClassName> <ClassName>org.wso2.carbon.device.application.mgt.core.impl.SubscriptionManagerImpl</ClassName>

Loading…
Cancel
Save