From 707d8851071de0687e24308dad3298cf62a6a41e Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 24 Apr 2019 14:31:43 +0530 Subject: [PATCH 1/2] Improve APPM proxy response --- .../mgt/handler/util/HandlerUtil.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java index b138c7686bf..cf14b96651c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.application.mgt.handler.util; import com.google.gson.Gson; import com.google.gson.JsonObject; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; @@ -27,6 +28,8 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.json.JSONException; +import org.json.JSONObject; import org.wso2.carbon.device.application.mgt.common.ProxyResponse; import javax.servlet.http.HttpServletRequest; @@ -201,18 +204,30 @@ public class HandlerUtil { return; } - Gson gson = new Gson(); resp.setStatus(proxyResponse.getCode()); resp.setContentType("application/json"); resp.setCharacterEncoding("UTF-8"); - proxyResponse.setExecutorResponse(null); - try (PrintWriter writer = resp.getWriter()) { - if (proxyResponse.getCode() == HttpStatus.SC_OK){ - writer.write(gson.toJson(proxyResponse.getData())); - } else{ - writer.write(proxyResponse.getData()); + + JSONObject response = new JSONObject(); + String redirectUrl = proxyResponse.getUrl(); + String responseData = proxyResponse.getData(); + + if (!StringUtils.isEmpty(redirectUrl)){ + response.put("url", redirectUrl); + } + if (!StringUtils.isEmpty(responseData)){ + try { + JSONObject responseDataJsonObj = new JSONObject(responseData); + response.put("data", responseDataJsonObj); + } catch (JSONException e) { + log.debug("Response data is not valid json string"); + response.put("data", responseData); } } + + try (PrintWriter writer = resp.getWriter()) { + writer.write(response.toString()); + } } } From 38c7f6211ee43f4866de01fe6b8f3d6e165aea5f Mon Sep 17 00:00:00 2001 From: Gathika Ratnayaka Date: Wed, 24 Apr 2019 09:10:58 +0000 Subject: [PATCH 2/2] Additional methods and unit tests to LifeCycleManager --- .../core/lifecycle/LifecycleStateManager.java | 39 ++++++ .../LifecycleManagementTest.java | 111 +++++++++++++----- 2 files changed, 119 insertions(+), 31 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java index 140383fb02e..2691b4153ea 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java @@ -155,6 +155,28 @@ public class LifecycleStateManager { } } + public boolean isInitialState(String state) throws LifecycleManagementException { + State currentState = getMatchingState(state); + if (currentState != null) { + return currentState.isInitialState(); + } else { + String msg = "Couldn't find a lifecycle state that matches with " + state + " state."; + log.error(msg); + throw new LifecycleManagementException(msg); + } + } + + public boolean isEndState(String state) throws LifecycleManagementException { + State currentState = getMatchingState(state); + if (currentState != null) { + return currentState.isEndState(); + } else { + String msg = "Couldn't find a lifecycle state that matches with " + state + " state."; + log.error(msg); + throw new LifecycleManagementException(msg); + } + } + public String getInitialState() throws LifecycleManagementException { String initialState = null; for (Map.Entry stringStateEntry : lifecycleStates.entrySet()) { @@ -198,6 +220,23 @@ public class LifecycleStateManager { return false; } + public boolean isUpdatable(String state) { + State currentState = getMatchingState(state); + if (currentState.isAppUpdatable()) { + return true; + } + return false; + } + + public boolean isInstallable(String state) { + State currentState = getMatchingState(state); + if (currentState.isAppInstallable()) { + return true; + } + return false; + } + + public void setLifecycleStates(Map lifecycleStates) { this.lifecycleStates = lifecycleStates; } 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 index db7bdd2c6fb..42f1b41f11c 100644 --- 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 @@ -24,6 +24,8 @@ public class LifecycleManagementTest { private final String NON_UPDATABLE_STATE = "Removed"; private final String INSTALLABLE_STATE = "Published"; private final String UNINSTALlABLE_STATE = "Removed"; + private final String INITIAL_STATE = "Created"; + private final String END_STATE = "Removed"; @BeforeClass @@ -49,36 +51,83 @@ public class LifecycleManagementTest { proceedingStates.contains(BOGUS_STATE.toUpperCase())); } -// @Test -// public void CheckUpdatableState() { -// boolean isUpdatableState = lifecycleStateManager.isUpdatableState(UPDATABLE_STATE); -// System.out.println(isUpdatableState); -// Assert.assertTrue("Updatable state: " + UPDATABLE_STATE, isUpdatableState); -// } -// -// @Test -// public void CheckNonUpdatableState() { -// boolean isUpdatableState = lifecycleStateManager.isUpdatableState(NON_UPDATABLE_STATE); -// Assert.assertFalse("Non Updatable state: " + NON_UPDATABLE_STATE, isUpdatableState); -// } -// -// @Test -// public void CheckInstallableState() { -// boolean isInstallableState = lifecycleStateManager.isInstallableState(INSTALLABLE_STATE); -// Assert.assertTrue("Installable state: " + INSTALLABLE_STATE, isInstallableState); -// } -// -// @Test -// public void CheckUnInstallableState() { -// boolean isInstallableState = lifecycleStateManager.isInstallableState(UNINSTALlABLE_STATE); -// Assert.assertFalse("UnInstallable state: " + UNINSTALlABLE_STATE, isInstallableState); -// } -// -// @Test -// public void check() { -// Set proceedingStates = lifecycleStateManager.getNextLifecycleStates(CURRENT_STATE); -// Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, -// proceedingStates.contains(BOGUS_STATE.toUpperCase())); -// } + @Test + public void CheckUpdatableState() throws LifecycleManagementException { + boolean isUpdatable = lifecycleStateManager.isUpdatable(UPDATABLE_STATE); + System.out.println(isUpdatable); + Assert.assertTrue("Updatable state: " + UPDATABLE_STATE, isUpdatable); + } + + @Test + public void CheckNonUpdatableState() throws LifecycleManagementException { + boolean isUpdatable = lifecycleStateManager.isUpdatable(NON_UPDATABLE_STATE); + Assert.assertFalse("Non Updatable state: " + NON_UPDATABLE_STATE, isUpdatable); + } + + @Test + public void CheckInstallableState() throws LifecycleManagementException { + boolean isInstallable = lifecycleStateManager.isInstallable(INSTALLABLE_STATE); + Assert.assertTrue("Installable state: " + INSTALLABLE_STATE, isInstallable); + } + + @Test + public void CheckUnInstallableState() throws LifecycleManagementException { + boolean isInstallable = lifecycleStateManager.isInstallable(UNINSTALlABLE_STATE); + Assert.assertFalse("UnInstallable state: " + UNINSTALlABLE_STATE, isInstallable); + } + + @Test + public void CheckGetInitialState() throws LifecycleManagementException { + boolean isInitialState = lifecycleStateManager.getInitialState().equalsIgnoreCase(INITIAL_STATE); + Assert.assertTrue("Initial state: " + INITIAL_STATE, isInitialState); + } + + @Test + public void CheckGetNonInitialState() throws LifecycleManagementException { + boolean isInitialState = lifecycleStateManager.getInitialState().equalsIgnoreCase(END_STATE); + Assert.assertFalse("Non initial state: " + END_STATE, isInitialState); + } + + @Test + public void CheckGetEndState() throws LifecycleManagementException { + boolean isEndState = lifecycleStateManager.getEndState().equalsIgnoreCase(END_STATE); + Assert.assertTrue("End State: " + END_STATE, isEndState); + } + @Test + public void CheckGetNonEndState() throws LifecycleManagementException { + boolean isEndState = lifecycleStateManager.getEndState().equalsIgnoreCase(INITIAL_STATE); + Assert.assertFalse("Non End State : " + INITIAL_STATE, isEndState); + } + + @Test + public void CheckIsInitialState() throws LifecycleManagementException { + boolean isInitialState = lifecycleStateManager.isInitialState(INITIAL_STATE); + Assert.assertTrue("Initial state: " + INITIAL_STATE, isInitialState); + } + + @Test + public void CheckIsNonInitialState() throws LifecycleManagementException { + boolean isInitialState = lifecycleStateManager.isInitialState(END_STATE); + Assert.assertFalse("Non Initial state: " + END_STATE, isInitialState); + } + + @Test + public void CheckIsEndState() throws LifecycleManagementException { + boolean isEndState = lifecycleStateManager.isEndState(END_STATE); + Assert.assertTrue("End state: " + END_STATE, isEndState); + } + + @Test + public void CheckIsNonEndState() throws LifecycleManagementException { + boolean isEndState = lifecycleStateManager.isEndState(INITIAL_STATE); + Assert.assertFalse("Non End state: " + INITIAL_STATE, isEndState); + } + + @Test + public void check() { + Set proceedingStates = lifecycleStateManager.getNextLifecycleStates(CURRENT_STATE); + Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, + proceedingStates.contains(BOGUS_STATE.toUpperCase())); + } }