From 822dc090f9cee9b8ee370f1c11e11e40511e5caa Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 20 Jun 2019 20:09:23 +0530 Subject: [PATCH] Refactore code --- .../common/services/ApplicationManager.java | 18 +- .../mgt/core/dao/ApplicationDAO.java | 56 +-- .../GenericApplicationDAOImpl.java | 225 +---------- .../mgt/core/impl/ApplicationManagerImpl.java | 152 +------ .../management/ApplicationManagementTest.java | 372 +++++++++--------- 5 files changed, 199 insertions(+), 624 deletions(-) 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 ed6d77a61c..cf6e9904b7 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 @@ -45,12 +45,13 @@ import java.util.List; */ public interface ApplicationManager { - /** - * Creates an application. + /*** + * The method is responsible to add new application into entgra App Manager. * * @param applicationWrapper Application that need to be created. - * @return Created application - * @throws ApplicationManagementException ApplicationDTO Management Exception + * @param applicationArtifact contains artifact data. i.e image name and stream, icon name and stream etc. + * @return {@link Application} + * @throws ApplicationManagementException Catch all other throwing exceptions and throw {@link ApplicationManagementException} */ Application createApplication(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact) throws ApplicationManagementException; @@ -134,15 +135,6 @@ public interface ApplicationManager { */ Application getApplicationByUuid(String uuid, String state) throws ApplicationManagementException; - /** - * To get an application associated with the release. - * - * @param appReleaseUUID UUID of the app release - * @return {@link ApplicationDTO} associated with the release - * @throws ApplicationManagementException If unable to retrieve {@link ApplicationDTO} associated with the given UUID - */ - ApplicationDTO getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException; - /** * To get lifecycle state change flow of a particular Application Release. * diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 30d7beb3e3..32a1d0d83c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -20,7 +20,6 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO; import org.wso2.carbon.device.application.mgt.common.dto.TagDTO; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; @@ -101,18 +100,6 @@ public interface ApplicationDAO { void updateCategory(CategoryDTO categoryDTO, int tenantId) throws ApplicationManagementDAOException; - - - /** - * To check application existence. - * - * @param appName appName that need to identify application. - * @param type type that need to identify application. - * @param tenantId tenantId that need to identify application. - * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. - */ - boolean isExistApplication(String appName, String type, int tenantId) throws ApplicationManagementDAOException; - /** * To get the applications that satisfy the given criteria. * @@ -132,27 +119,6 @@ public interface ApplicationDAO { */ String getUuidOfLatestRelease(int appId) throws ApplicationManagementDAOException; - /** - * To get the application with the given uuid - * - * @param appName name of the application to be retrieved. - * @param tenantId ID of the tenant. - * @param appType Type of the application. - * @return the application - * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. - */ - ApplicationDTO getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException; - - /** - * To get the application with the given id - * - * @param id ID of the application. - * @param tenantId ID of the tenant. - * @return the application - * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. - */ - ApplicationDTO getApplicationById(String id, int tenantId) throws ApplicationManagementDAOException; - /** * To get the application with the given id * @@ -173,16 +139,6 @@ public interface ApplicationDAO { */ ApplicationDTO getApplicationByUUID(String releaseUuid, int tenantId) throws ApplicationManagementDAOException; - /** - * To get the application with the given uuid - * - * @param appId ID of the application - * @param tenantId Tenant Id - * @return the boolean value - * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. - */ - boolean verifyApplicationExistenceById(int appId, int tenantId) throws ApplicationManagementDAOException; - /** * Verify whether application exist for given application name and device type. Because a name and device type is * unique for an application. @@ -193,7 +149,7 @@ public interface ApplicationDAO { * @return ID of the ApplicationDTO. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - boolean isValidAppName(String appName, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException; + boolean isExistingAppName(String appName, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException; /** * To edit the given application. @@ -236,16 +192,6 @@ public interface ApplicationDAO { */ void deleteTags(List tags, int applicationId, int tenantId) throws ApplicationManagementDAOException; - /** - * To get an {@link ApplicationDTO} associated with the given release - * - * @param appReleaseUUID UUID of the {@link ApplicationReleaseDTO} - * @param tenantId ID of the tenant - * @return {@link ApplicationDTO} associated with the given release UUID - * @throws ApplicationManagementDAOException if unable to fetch the ApplicationDTO from the data store. - */ - ApplicationDTO getApplicationByRelease(String appReleaseUUID, int tenantId) throws ApplicationManagementDAOException; - String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException; void deleteApplication(int appId, int tenantId) throws ApplicationManagementDAOException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index d3f79ab433..d69897fc4c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory; import org.json.JSONException; import org.wso2.carbon.device.application.mgt.common.AppLifecycleState; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.dto.TagDTO; @@ -94,36 +93,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - @Override - public boolean isExistApplication(String appName, String type, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Request received in DAO Layer to verify whether the registering app is registered or not"); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - String sql = "SELECT * FROM AP_APP WHERE NAME = ? AND TYPE = ? AND TENANT_ID = ?"; - try { - conn = this.getDBConnection(); - conn.setAutoCommit(false); - stmt = conn.prepareStatement(sql); - stmt.setString(1, appName); - stmt.setString(2, type); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - return rs.next(); - - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException( - "Error occurred while obtaining the DB connection when verifying application existence", e); - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "DB connection error occured while checking whether application exist or not.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); - } - } - @Override public List getApplications(Filter filter,int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { @@ -344,96 +313,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic return count; } - @Override - public ApplicationDTO getApplication(String appName, String appType, int tenantId) throws - ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Getting application with the type(" + appType + " and Name " + appName + - " ) from the database"); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getDBConnection(); - String sql = - "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " - + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY," - + " AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " - + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND " - + "AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, appName); - stmt.setString(2, appType); - stmt.setInt(3, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application with the type " - + appType + "and app name " + appName); - } - - return DAOUtil.loadApplication(rs); - - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application details with app name " + appName + - " while executing query.", e); - } catch (JSONException e) { - throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } catch (UnexpectedServerErrorException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public ApplicationDTO getApplicationById(String id, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Getting application with the id:" + id); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getDBConnection(); - String sql = - "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " - + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY," - + " AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " - + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND " - + "AP_APP.APP_ID= ? AND AP_APP.TENANT_ID=?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, id); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application with the id:" + id); - } - - return DAOUtil.loadApplication(rs); - - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application details with app id " + id + - " while executing query.", e); - } catch (JSONException e) { - throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } catch (UnexpectedServerErrorException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); - } - } - @Override public ApplicationDTO getApplicationByUUID(String releaseUuid, int tenantId) throws ApplicationManagementDAOException { @@ -581,39 +460,6 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - @Override - public boolean verifyApplicationExistenceById(int appId, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Getting application with the application ID(" + appId + " ) from the database"); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getDBConnection(); - String sql = - "SELECT AP_APP.ID AS APP_ID FROM AP_APP WHERE AP_APP.ID = ? AND AP_APP.TENANT_ID=?;"; - - stmt = conn.prepareStatement(sql); - stmt.setInt(1, appId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved basic details of the application with the application ID " + appId); - } - return rs.next(); - } catch (SQLException e) { - throw new ApplicationManagementDAOException( - "Error occurred while getting application details with app ID " + appId + " while executing query.", - e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); - } - } - @Override public boolean updateApplication(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementDAOException { @@ -1532,76 +1378,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public ApplicationDTO getApplicationByRelease(String appReleaseUUID, int tenantId) - throws ApplicationManagementDAOException { - if (log.isDebugEnabled()) { - log.debug("Getting application with the UUID (" + appReleaseUUID + ") from the database"); - } - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - conn = this.getDBConnection(); - String sql = "SELECT AP_APP_RELEASE.ID AS RELEASE_ID, AP_APP_RELEASE.VERSION, AP_APP_RELEASE.TENANT_ID," - + "AP_APP_RELEASE.UUID, AP_APP_RELEASE.RELEASE_TYPE, AP_APP_RELEASE.APP_PRICE, " - + "AP_APP_RELEASE.STORED_LOCATION, AP_APP_RELEASE.BANNER_LOCATION, AP_APP_RELEASE.SC_1_LOCATION," - + "AP_APP_RELEASE.SC_2_LOCATION, AP_APP_RELEASE.SC_3_LOCATION, AP_APP_RELEASE.APP_HASH_VALUE," - + "AP_APP_RELEASE.SHARED_WITH_ALL_TENANTS, AP_APP_RELEASE.APP_META_INFO, AP_APP_RELEASE.CREATED_BY," - + "AP_APP_RELEASE.CREATED_AT, AP_APP_RELEASE.PUBLISHED_BY, AP_APP_RELEASE.PUBLISHED_AT, " - + "AP_APP_RELEASE.STARS," - + "AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, " - + "AP_APP.APP_CATEGORY AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP.CURRENCY AS CURRENCY, " - + "AP_UNRESTRICTED_ROLE.ROLE AS ROLE FROM AP_APP, AP_UNRESTRICTED_ROLE, AP_APP_RELEASE " - + "WHERE AP_APP_RELEASE.UUID=? AND AP_APP.TENANT_ID=?;"; - - stmt = conn.prepareStatement(sql); - stmt.setString(1, appReleaseUUID); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved details of the application with the UUID " + appReleaseUUID); - } - - ApplicationDTO application = null; - while (rs.next()) { - ApplicationReleaseDTO appRelease = DAOUtil.loadApplicationRelease(rs); - application = new ApplicationDTO(); - - application.setId(rs.getInt("APP_ID")); - application.setName(rs.getString("APP_NAME")); - application.setType(rs.getString("APP_TYPE")); -// application.setAppCategories(rs.getString("APP_CATEGORY")); - application.setSubType(rs.getString("SUB_TYPE")); - application.setPaymentCurrency(rs.getString("CURRENCY")); -// application.setIsRestricted(rs.getBoolean("RESTRICTED")); - - String unrestrictedRole = rs.getString("ROLE").toLowerCase(); - List unrestrictedRoleList = new ArrayList<>(); - unrestrictedRoleList.add(unrestrictedRole); - - application.setUnrestrictedRoles(unrestrictedRoleList); - - List applicationReleaseList = new ArrayList<>(); - applicationReleaseList.add(appRelease); - - application.setApplicationReleaseDTOs(applicationReleaseList); - } - return application; - } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting application details with UUID " - + appReleaseUUID + " while executing query.", e); - } catch (JSONException e) { - throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); - } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); - } finally { - DAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public boolean isValidAppName(String appName, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { + public boolean isExistingAppName(String appName, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; 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 c940fa7ab4..4b3fff7d4a 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 @@ -25,7 +25,6 @@ import org.apache.commons.validator.routines.UrlValidator; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; @@ -91,7 +90,6 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -132,12 +130,6 @@ public class ApplicationManagerImpl implements ApplicationManager { this.subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); } - /*** - * The responsbility of this method is the creating an application. - * @param applicationWrapper ApplicationDTO that need to be created. - * @return {@link ApplicationDTO} - * @throws ApplicationManagementException Catch all other throwing exceptions and throw {@link ApplicationManagementException} - */ @Override public Application createApplication(ApplicationWrapper applicationWrapper, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { @@ -145,7 +137,7 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (log.isDebugEnabled()) { - log.debug("Application create request is received for the tenant : " + tenantId + " and" + " the user : " + log.debug("Application create request is received for the tenant : " + tenantId + " and the user: " + userName); } @@ -176,7 +168,6 @@ public class ApplicationManagerImpl implements ApplicationManager { private void deleteApplicationArtifacts(List directoryPaths) throws ApplicationManagementException { ApplicationStorageManager applicationStorageManager = DAOUtil.getApplicationStorageManager(); - try { applicationStorageManager.deleteAllApplicationReleaseArtifacts(directoryPaths); } catch (ApplicationStorageManagementException e) { @@ -1114,107 +1105,6 @@ public class ApplicationManagerImpl implements ApplicationManager { return roleList; } - //todo no usage - public ApplicationDTO getApplication(String appType, String appName) throws ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationDTO application; - boolean isAppAllowed = false; - List applicationReleases; - try { - ConnectionManagerUtil.openDBConnection(); - application = this.applicationDAO.getApplication(appName, appType, tenantId); - if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - applicationReleases = getReleases(application, null); - application.setApplicationReleaseDTOs(applicationReleases); - return application; - } - - if (!application.getUnrestrictedRoles().isEmpty()) { - if (hasUserRole(application.getUnrestrictedRoles(), userName)) { - isAppAllowed = true; - } - } else { - isAppAllowed = true; - } - - if (!isAppAllowed) { - return null; - } - - applicationReleases = getReleases(application, null); - application.setApplicationReleaseDTOs(applicationReleases); - return application; - } catch (UserStoreException e) { - throw new ApplicationManagementException( - "User-store exception while getting application with the " + "application name " + appName); - } catch (ApplicationManagementDAOException e) { - //todo - throw new ApplicationManagementException(""); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - @Override public ApplicationDTO getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - ApplicationDTO application; - try { - ConnectionManagerUtil.openDBConnection(); - application = this.applicationDAO.getApplicationByRelease(appReleaseUUID, tenantId); - - if (application.getUnrestrictedRoles().isEmpty() || hasUserRole(application.getUnrestrictedRoles(), - userName)) { - return application; - } - return null; - } catch (UserStoreException e) { - throw new ApplicationManagementException( - "User-store exception while getting application with the application UUID " + appReleaseUUID); - } catch (ApplicationManagementDAOException e) { - //todo - throw new ApplicationManagementException(""); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - } - - // todo rethink about this method - private List getReleases(ApplicationDTO application, String releaseState) - throws ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - List applicationReleases; - if (log.isDebugEnabled()) { - log.debug("Request is received to retrieve all the releases related with the application " + application - .toString()); - } - //todo - applicationReleases = null; - try { - applicationReleases = this.applicationReleaseDAO.getReleases(application.getId(), tenantId); - } catch (ApplicationManagementDAOException e) { - //todo - throw new ApplicationManagementException(""); - } - for (ApplicationReleaseDTO applicationRelease : applicationReleases) { - LifecycleState lifecycleState = null; - try { - lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); - } catch (LifeCycleManagementDAOException e) { - throw new ApplicationManagementException( - "Error occurred while getting the latest lifecycle state for the application release UUID: " - + applicationRelease.getUuid(), e); - } - if (lifecycleState != null) { - log.error("todo"); -// applicationRelease.setLifecycleState(lifecycleState); - } - } - return applicationReleases; -// return filterAppReleaseByCurrentState(applicationReleases, releaseState); - } - @Override public void deleteApplication(int applicationId) throws ApplicationManagementException { if (log.isDebugEnabled()) { @@ -1364,39 +1254,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - /** - * To check whether current user has the permission to do some secured operation. - * - * @param username Name of the User. - * @param tenantId ID of the tenant. - * @param permission Permission that need to be checked. - * @return true if the current user has the permission, otherwise false. - * @throws UserStoreException UserStoreException - */ - private boolean isAdminUser(String username, int tenantId, String permission) throws UserStoreException { - UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); - return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager() - .isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), permission, - CarbonConstants.UI_PERMISSION_ACTION); - } - - /*** - * To verify whether application type is valid one or not - * @param appType application type {@link ApplicationType} - * @return true returns if appType is valid on, otherwise returns false - */ - private boolean isValidAppType(String appType) { - if (appType == null) { - return false; - } - for (ApplicationType applicationType : ApplicationType.values()) { - if (applicationType.toString().equals(appType)) { - return true; - } - } - return false; - } - @Override public void updateApplicationImageArtifact(String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { @@ -2295,7 +2152,11 @@ public class ApplicationManagerImpl implements ApplicationManager { String supportedOSVersions = applicationReleaseWrapper.getSupportedOsVersions(); if (!StringUtils.isEmpty(supportedOSVersions)) { - //todo check OS versions are supported or not + if (!isValidOsVersions(supportedOSVersions, deviceType)){ + String msg = "You are trying to update application release which has invalid or unsupported OS " + + "versions in the supportedOsVersions section. Hence, please re-evaluate the request payload."; + log.error(msg); + throw new BadRequestException(msg); } applicationReleaseDTO.setSupportedOsVersions(supportedOSVersions); } if (!StringUtils.isEmpty(applicationReleaseWrapper.getDescription())) { @@ -2593,7 +2454,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override public void validateImageArtifacts(Attachment iconFile, Attachment bannerFile, List attachmentList) throws RequestValidatingException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java index 9ec92e1820..3942735da2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java @@ -35,190 +35,190 @@ public class ApplicationManagementTest extends BaseTestCase { private static final Log log = LogFactory.getLog(ApplicationManagementTest.class); - @Test - public void testAddApplication() throws Exception { - - ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); - ConnectionManagerUtil.beginDBTransaction(); - applicationDAO.createApplication(ApplicationsDTO.getApp1(), -1234); - ConnectionManagerUtil.commitDBTransaction(); - ConnectionManagerUtil.closeDBConnection(); - } - - @Test(dependsOnMethods = ("addAplicationCategories")) - public void createApplication() throws Exception { - - log.debug("Creating the first application ....!"); - - ApplicationWrapper applicationWrapper = new ApplicationWrapper(); - - List categories = new ArrayList<>(); - categories.add("Test Category"); - applicationWrapper.setAppCategories(categories); - - applicationWrapper.setDescription("Test Description"); - applicationWrapper.setDeviceType("android"); - applicationWrapper.setName("Test Application"); - applicationWrapper.setSubType("Test Sub type"); - - List tags = new ArrayList<>(); - tags.add("abc"); - tags.add("pqr"); - tags.add("xyz"); - applicationWrapper.setTags(tags); - applicationWrapper.setPaymentCurrency("USD"); - - List applicationReleaseWrappers = new ArrayList<>(); - ApplicationReleaseWrapper releaseWrapper = new ApplicationReleaseWrapper(); - releaseWrapper.setDescription("First release"); - releaseWrapper.setIsSharedWithAllTenants(false); - releaseWrapper.setMetaData("Just meta data"); - releaseWrapper.setReleaseType("free"); - releaseWrapper.setPrice(5.7); - releaseWrapper.setSupportedOsVersions("4.0-7.0"); - applicationReleaseWrappers.add(releaseWrapper); - - applicationWrapper.setApplicationReleaseWrappers(applicationReleaseWrappers); - - ApplicationArtifact applicationArtifact = new ApplicationArtifact(); - applicationArtifact.setBannerName("My First Banner"); - File banner = new File("src/test/resources/samples/app1/banner1.jpg"); - InputStream bannerStream = new FileInputStream(banner); - applicationArtifact.setBannerStream(bannerStream); - applicationArtifact.setIconName("My First Icon"); - applicationArtifact.setIconStream(new FileInputStream(new File("src/test/resources/samples/app1/icon.png"))); - applicationArtifact.setInstallerName("Test Android App"); - applicationArtifact.setInstallerStream(new FileInputStream(new File("src/test/resources/samples/app1/sample.apk"))); - - Map screenshots = new HashMap<>(); - screenshots.put("shot1", new FileInputStream(new File("src/test/resources/samples/app1/shot1.png"))); - screenshots.put("shot2", new FileInputStream(new File("src/test/resources/samples/app1/shot2.png"))); - screenshots.put("shot3", new FileInputStream(new File("src/test/resources/samples/app1/shot3.png"))); - - applicationArtifact.setScreenshots(screenshots); - - ApplicationManager manager = new ApplicationManagerImpl(); - manager.createApplication(applicationWrapper, applicationArtifact); - } - - @Test - public void updateApplication(int applicationId, ApplicationUpdateWrapper applicationUpdateWrapper) throws ApplicationManagementException { - - } - - @Test - public void deleteApplication(int applicationId) throws ApplicationManagementException { - - } - - @Test - public void retireApplication(int applicationId) throws ApplicationManagementException { - - } - - @Test - public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException { - - } - - @Test - public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { - return null; - } - - @Test - public Application getApplicationById(int id, String state) throws ApplicationManagementException { - return null; - } - - @Test - public ApplicationRelease getApplicationReleaseByUUID(String uuid) throws ApplicationManagementException { - return null; - } - - @Test - public ApplicationDTO getApplicationByUuid(String uuid, String state) throws ApplicationManagementException { - return null; - } - - @Test - public ApplicationDTO getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException { - return null; - } - - @Test - public List getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException { - return null; - } - - @Test - public void changeLifecycleState(String releaseUuid, String stateName) throws ApplicationManagementException { - - } - - @Test - public void updateApplicationImageArtifact(String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { - - } - - @Test - public void updateApplicationArtifact(String deviceType, String appType, String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { - - } - - @Test - public ApplicationRelease createRelease(int applicationId, ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { - return null; - } - - @Test - public boolean updateRelease(String deviceType, String applicationType, String releaseUuid, ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { - return false; - } - - @Test - public void validateAppCreatingRequest(ApplicationWrapper applicationWrapper) throws RequestValidatingException { - - } - - @Test - public void validateReleaseCreatingRequest(ApplicationReleaseWrapper applicationReleaseWrapper, String applicationType) throws RequestValidatingException { - - } - - @Test - public void validateImageArtifacts(Attachment iconFile, Attachment bannerFile, List attachmentList) throws RequestValidatingException { - - } - - @Test - public void validateBinaryArtifact(Attachment binaryFile, String applicationType) throws RequestValidatingException { - - } - - @Test - public void addAplicationCategories() throws ApplicationManagementException { - - List categories = new ArrayList<>(); - categories.add("Test Category"); - categories.add("Test Category2"); - ApplicationManager manager = new ApplicationManagerImpl(); - manager.addApplicationCategories(categories); - - } - - @Test - public List getRegisteredTags() throws ApplicationManagementException { - return null; - } - - @Test - public List getRegisteredCategories() throws ApplicationManagementException { - return null; - } - - @Test - public void deleteTagMapping(int appId, String tagName) throws ApplicationManagementException { - - } +// @Test +// public void testAddApplication() throws Exception { +// +// ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); +// ConnectionManagerUtil.beginDBTransaction(); +// applicationDAO.createApplication(ApplicationsDTO.getApp1(), -1234); +// ConnectionManagerUtil.commitDBTransaction(); +// ConnectionManagerUtil.closeDBConnection(); +// } +// +// @Test(dependsOnMethods = ("addAplicationCategories")) +// public void createApplication() throws Exception { +// +// log.debug("Creating the first application ....!"); +// +// ApplicationWrapper applicationWrapper = new ApplicationWrapper(); +// +// List categories = new ArrayList<>(); +// categories.add("Test Category"); +// applicationWrapper.setAppCategories(categories); +// +// applicationWrapper.setDescription("Test Description"); +// applicationWrapper.setDeviceType("android"); +// applicationWrapper.setName("Test Application"); +// applicationWrapper.setSubType("Test Sub type"); +// +// List tags = new ArrayList<>(); +// tags.add("abc"); +// tags.add("pqr"); +// tags.add("xyz"); +// applicationWrapper.setTags(tags); +// applicationWrapper.setPaymentCurrency("USD"); +// +// List applicationReleaseWrappers = new ArrayList<>(); +// ApplicationReleaseWrapper releaseWrapper = new ApplicationReleaseWrapper(); +// releaseWrapper.setDescription("First release"); +// releaseWrapper.setIsSharedWithAllTenants(false); +// releaseWrapper.setMetaData("Just meta data"); +// releaseWrapper.setReleaseType("free"); +// releaseWrapper.setPrice(5.7); +// releaseWrapper.setSupportedOsVersions("4.0-7.0"); +// applicationReleaseWrappers.add(releaseWrapper); +// +// applicationWrapper.setApplicationReleaseWrappers(applicationReleaseWrappers); +// +// ApplicationArtifact applicationArtifact = new ApplicationArtifact(); +// applicationArtifact.setBannerName("My First Banner"); +// File banner = new File("src/test/resources/samples/app1/banner1.jpg"); +// InputStream bannerStream = new FileInputStream(banner); +// applicationArtifact.setBannerStream(bannerStream); +// applicationArtifact.setIconName("My First Icon"); +// applicationArtifact.setIconStream(new FileInputStream(new File("src/test/resources/samples/app1/icon.png"))); +// applicationArtifact.setInstallerName("Test Android App"); +// applicationArtifact.setInstallerStream(new FileInputStream(new File("src/test/resources/samples/app1/sample.apk"))); +// +// Map screenshots = new HashMap<>(); +// screenshots.put("shot1", new FileInputStream(new File("src/test/resources/samples/app1/shot1.png"))); +// screenshots.put("shot2", new FileInputStream(new File("src/test/resources/samples/app1/shot2.png"))); +// screenshots.put("shot3", new FileInputStream(new File("src/test/resources/samples/app1/shot3.png"))); +// +// applicationArtifact.setScreenshots(screenshots); +// +// ApplicationManager manager = new ApplicationManagerImpl(); +// manager.createApplication(applicationWrapper, applicationArtifact); +// } +// +// @Test +// public void updateApplication(int applicationId, ApplicationUpdateWrapper applicationUpdateWrapper) throws ApplicationManagementException { +// +// } +// +// @Test +// public void deleteApplication(int applicationId) throws ApplicationManagementException { +// +// } +// +// @Test +// public void retireApplication(int applicationId) throws ApplicationManagementException { +// +// } +// +// @Test +// public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException { +// +// } +// +// @Test +// public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public Application getApplicationById(int id, String state) throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public ApplicationRelease getApplicationReleaseByUUID(String uuid) throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public ApplicationDTO getApplicationByUuid(String uuid, String state) throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public ApplicationDTO getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public List getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public void changeLifecycleState(String releaseUuid, String stateName) throws ApplicationManagementException { +// +// } +// +// @Test +// public void updateApplicationImageArtifact(String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { +// +// } +// +// @Test +// public void updateApplicationArtifact(String deviceType, String appType, String uuid, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { +// +// } +// +// @Test +// public ApplicationRelease createRelease(int applicationId, ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public boolean updateRelease(String deviceType, String applicationType, String releaseUuid, ApplicationReleaseWrapper applicationReleaseWrapper, ApplicationArtifact applicationArtifact) throws ApplicationManagementException { +// return false; +// } +// +// @Test +// public void validateAppCreatingRequest(ApplicationWrapper applicationWrapper) throws RequestValidatingException { +// +// } +// +// @Test +// public void validateReleaseCreatingRequest(ApplicationReleaseWrapper applicationReleaseWrapper, String applicationType) throws RequestValidatingException { +// +// } +// +// @Test +// public void validateImageArtifacts(Attachment iconFile, Attachment bannerFile, List attachmentList) throws RequestValidatingException { +// +// } +// +// @Test +// public void validateBinaryArtifact(Attachment binaryFile, String applicationType) throws RequestValidatingException { +// +// } +// +// @Test +// public void addAplicationCategories() throws ApplicationManagementException { +// +// List categories = new ArrayList<>(); +// categories.add("Test Category"); +// categories.add("Test Category2"); +// ApplicationManager manager = new ApplicationManagerImpl(); +// manager.addApplicationCategories(categories); +// +// } +// +// @Test +// public List getRegisteredTags() throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public List getRegisteredCategories() throws ApplicationManagementException { +// return null; +// } +// +// @Test +// public void deleteTagMapping(int appId, String tagName) throws ApplicationManagementException { +// +// } }