diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java index 66e7756118..f958dd87a1 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/device/mgt/mobile/android/api/impl/DeviceManagementAPIImpl.java @@ -34,42 +34,24 @@ */ package org.wso2.carbon.device.mgt.mobile.android.api.impl; -import org.apache.commons.lang.StringUtils; +import com.google.api.client.http.HttpStatusCodes; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; -import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; -import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; -import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; -import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; -import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; -import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; -import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.mobile.android.api.DeviceManagementAPI; -import org.wso2.carbon.device.mgt.mobile.android.common.AndroidConstants; import org.wso2.carbon.device.mgt.mobile.android.common.Message; import org.wso2.carbon.device.mgt.mobile.android.common.bean.ErrorResponse; import org.wso2.carbon.device.mgt.mobile.android.common.bean.wrapper.AndroidApplication; import org.wso2.carbon.device.mgt.mobile.android.common.bean.wrapper.AndroidDevice; -import org.wso2.carbon.device.mgt.mobile.android.common.bean.wrapper.EnterpriseUser; import org.wso2.carbon.device.mgt.mobile.android.common.exception.BadRequestException; -import org.wso2.carbon.device.mgt.mobile.android.common.exception.EnterpriseServiceException; -import org.wso2.carbon.device.mgt.mobile.android.common.exception.NotFoundException; import org.wso2.carbon.device.mgt.mobile.android.common.exception.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.mobile.android.common.spi.AndroidService; import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidAPIUtils; import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidDeviceUtils; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import javax.validation.Valid; import javax.validation.constraints.NotNull; @@ -87,7 +69,6 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; @Path("/devices") @@ -129,31 +110,24 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } - DeviceIdentifier deviceIdentifier = AndroidDeviceUtils.convertToDeviceIdentifierObject(id); try { AndroidService androidService = AndroidAPIUtils.getAndroidService(); - androidService.getPendingOperations(id, resultOperations); - } catch (DeviceManagementException e) { - String msg = "Issue in retrieving device management service instance"; + List pendingOperations = androidService + .getPendingOperations(id, resultOperations, disableGoogleApps); + return Response.status(Response.Status.CREATED).entity(pendingOperations).build(); + } catch (InvalidDeviceException e) { + String msg = "Device identifier is invalid. Device identifier " + id; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); - } - - List pendingOperations; - try { - pendingOperations = AndroidDeviceUtils.getPendingOperations(deviceIdentifier, !disableGoogleApps); - } catch (OperationManagementException e) { - String msg = "Issue in retrieving operation management service instance"; + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting pending operations of the device."; log.error(msg, e); throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(msg).build()); } - return Response.status(Response.Status.CREATED).entity(pendingOperations).build(); } - - @POST @Override public Response enrollDevice(@Valid AndroidDevice androidDevice) { @@ -184,13 +158,13 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { try { AndroidService androidService = AndroidAPIUtils.getAndroidService(); Message responseMessage = androidService.isEnrolled(id, deviceIdentifier); - return Response.status(Response.Status.NOT_FOUND).entity(responseMessage).build(); - + return Response.status(Integer.parseInt(responseMessage.getResponseCode())).entity(responseMessage).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while checking enrollment status of the device."; log.error(msg, e); throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(msg).build()); } } @@ -198,13 +172,9 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { @Path("/{id}") @Override public Response modifyEnrollment(@PathParam("id") String id, @Valid AndroidDevice androidDevice) { - AndroidService androidService = AndroidAPIUtils.getAndroidService(); - Device device = androidService.modifyEnrollment(id, androidDevice); - boolean result; try { - device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); - result = AndroidAPIUtils.getDeviceManagementService().modifyEnrollment(device); - if (result) { + AndroidService androidService = AndroidAPIUtils.getAndroidService(); + if (androidService.modifyEnrollment(id, androidDevice)) { Message responseMessage = new Message(); responseMessage.setResponseCode(Response.Status.ACCEPTED.toString()); responseMessage.setResponseMessage("Enrollment of Android device that " + @@ -230,11 +200,9 @@ public class DeviceManagementAPIImpl implements DeviceManagementAPI { @Path("/{id}") @Override public Response disEnrollDevice(@PathParam("id") String id) { - try { AndroidService androidService = AndroidAPIUtils.getAndroidService(); - boolean result = androidService.disEnrollDevice(id); - if (result) { + if (androidService.disEnrollDevice(id)) { String msg = "Android device that carries id '" + id + "' is successfully "; Message responseMessage = new Message(); responseMessage.setResponseCode(Response.Status.OK.toString()); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/exception/NotFoundExceptionDup.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/exception/NotFoundExceptionDup.java new file mode 100644 index 0000000000..6d0f90cef2 --- /dev/null +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/exception/NotFoundExceptionDup.java @@ -0,0 +1,32 @@ +/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.mobile.android.common.exception; + +/** + * Represents the exception thrown during validating the request. + */ +public class NotFoundExceptionDup extends AndroidDeviceMgtPluginException { + + public NotFoundExceptionDup(String message, Throwable ex) { + super(message, ex); + } + + public NotFoundExceptionDup(String message) { + super(message); + } +} diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java index b9bb0923a2..04e5039a3a 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/spi/AndroidService.java @@ -22,6 +22,8 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; +import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; @@ -119,11 +121,12 @@ public interface AndroidService { //DeviceManagementAPIImpl Message updateApplicationList(String id, List androidApplications) throws ApplicationManagementException; - void getPendingOperations(String id, List resultOperations) throws DeviceManagementException; + List getPendingOperations(String id, List resultOperations, boolean disableGoogleApps) + throws DeviceManagementException, InvalidDeviceException; Response enrollDevice(AndroidDevice androidDevice) throws DeviceManagementException; - Device modifyEnrollment(String id, AndroidDevice androidDevice); + boolean modifyEnrollment(String id, AndroidDevice androidDevice) throws DeviceManagementException; boolean disEnrollDevice(String id) throws DeviceManagementException; diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java index b05728fe5f..aed7c41705 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java @@ -17,6 +17,7 @@ package org.wso2.carbon.device.mgt.mobile.android.core.impl; +import com.google.api.client.http.HttpStatusCodes; import com.google.api.services.androidenterprise.model.ProductsListResponse; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -35,6 +36,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; @@ -261,9 +263,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setPayLoad(file.toJSON()); try { - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(fileTransferBeanWrapper.getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers ( " + fileTransferBeanWrapper.getDeviceIDs() + " ) found."; log.error(errorMessage, e); @@ -288,8 +289,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setPayLoad(lock.toJSON()); try { - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceLockBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceLockBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -305,8 +305,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.DEVICE_UNLOCK); operation.setType(Operation.Type.COMMAND); operation.setEnabled(true); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -321,8 +320,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.DEVICE_LOCATION); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -337,8 +335,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.CLEAR_PASSWORD); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -361,8 +358,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.CAMERA); operation.setType(Operation.Type.COMMAND); operation.setEnabled(camera.isEnabled()); - Activity activity = AndroidDeviceUtils.getOperationResponse(cameraBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(cameraBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -377,8 +373,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.DEVICE_INFO); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -393,8 +388,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.LOGCAT); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -409,8 +403,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.ENTERPRISE_WIPE); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -433,8 +426,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.WIPE_DATA); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(wipeData.toJSON()); - Activity activity = AndroidDeviceUtils.getOperationResponse(wipeDataBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(wipeDataBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -449,8 +441,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.APPLICATION_LIST); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -465,8 +456,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.DEVICE_RING); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -481,8 +471,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.DEVICE_REBOOT); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -497,8 +486,7 @@ public class AndroidServiceImpl implements AndroidService { CommandOperation operation = new CommandOperation(); operation.setCode(AndroidConstants.OperationCodes.CHANGE_LOCK_TASK_MODE); operation.setType(Operation.Type.COMMAND); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -514,8 +502,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.DEVICE_MUTE); operation.setType(Operation.Type.COMMAND); operation.setEnabled(true); - Activity activity = AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(deviceIDs, operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -545,9 +532,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.INSTALL_APPLICATION); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(applicationInstallation.toJSON()); - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(applicationInstallationBeanWrapper.getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -575,9 +561,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setType(Operation.Type.PROFILE); operation.setPayLoad(applicationUpdate.toJSON()); - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(applicationUpdateBeanWrapper.getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -603,9 +588,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.UNINSTALL_APPLICATION); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(applicationUninstallation.toJSON()); - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(applicationUninstallationBeanWrapper.getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -628,9 +612,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.BLACKLIST_APPLICATIONS); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(blacklistApplications.toJSON()); - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(blacklistApplicationsBeanWrapper.getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -655,9 +638,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.UPGRADE_FIRMWARE); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(upgradeFirmware.toJSON()); - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(upgradeFirmwareBeanWrapper.getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -680,8 +662,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.VPN); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(vpn.toJSON()); - Activity activity = AndroidDeviceUtils.getOperationResponse(vpnConfiguration.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(vpnConfiguration.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -704,8 +685,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.NOTIFICATION); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(notification.toJSON()); - Activity activity = AndroidDeviceUtils.getOperationResponse(notificationBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(notificationBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -729,8 +709,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setType(Operation.Type.PROFILE); operation.setPayLoad(wifi.toJSON()); - Activity activity = AndroidDeviceUtils.getOperationResponse(wifiBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(wifiBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -753,8 +732,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.ENCRYPT_STORAGE); operation.setType(Operation.Type.COMMAND); operation.setEnabled(deviceEncryption.isEncrypted()); - Activity activity = AndroidDeviceUtils.getOperationResponse(encryptionBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(encryptionBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -777,8 +755,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setCode(AndroidConstants.OperationCodes.CHANGE_LOCK_CODE); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(lockCode.toJSON()); - Activity activity = AndroidDeviceUtils.getOperationResponse(lockCodeBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(lockCodeBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -802,9 +779,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setType(Operation.Type.PROFILE); operation.setPayLoad(passcodePolicy.toJSON()); - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(passwordPolicyBeanWrapper.getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -829,8 +805,7 @@ public class AndroidServiceImpl implements AndroidService { operation.setType(Operation.Type.PROFILE); operation.setPayLoad(webClip.toJSON()); - Activity activity = AndroidDeviceUtils.getOperationResponse(webClipBeanWrapper.getDeviceIDs(), operation); - return activity; + return AndroidDeviceUtils.getOperationResponse(webClipBeanWrapper.getDeviceIDs(), operation); } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; log.error(errorMessage, e); @@ -856,9 +831,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setType(Operation.Type.PROFILE); operation.setPayLoad(globalProxy.toJSON()); - Activity activity = AndroidDeviceUtils + return AndroidDeviceUtils .getOperationResponse(globalProxyBeanWrapper.getDeviceIDs(), operation); - return activity; } else { String errorMessage = "The payload of the global proxy operation is incorrect"; log.error(errorMessage); @@ -890,9 +864,8 @@ public class AndroidServiceImpl implements AndroidService { operation.setType(Operation.Type.PROFILE); operation.setPayLoad(configureDisplayMessage.toJSON()); - Activity activity = AndroidDeviceUtils.getOperationResponse(displayMessageBeanWrapper. + return AndroidDeviceUtils.getOperationResponse(displayMessageBeanWrapper. getDeviceIDs(), operation); - return activity; } catch (InvalidDeviceException e) { String errorMessage = "Invalid Device Identifiers found."; @@ -935,47 +908,67 @@ public class AndroidServiceImpl implements AndroidService { } @Override - public void getPendingOperations(String id, List resultOperations) throws DeviceManagementException { - DeviceIdentifier deviceIdentifier = AndroidDeviceUtils.convertToDeviceIdentifierObject(id); + public List getPendingOperations(String deviceId, List resultOperations, + boolean disableGoogleApps) throws DeviceManagementException, InvalidDeviceException { + DeviceIdentifier deviceIdentifier = AndroidDeviceUtils.convertToDeviceIdentifierObject(deviceId); if (!AndroidDeviceUtils.isValidDeviceIdentifier(deviceIdentifier)) { - String msg = "Device not found for identifier '" + id + "'"; + String msg = "Device not found for identifier '" + deviceId + "'"; log.error(msg); - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()); + throw new InvalidDeviceException(msg); } if (log.isDebugEnabled()) { - log.debug("Invoking Android pending operations:" + id); + log.debug("Invoking Android pending operations:" + deviceId); } if (resultOperations != null && !resultOperations.isEmpty()) { try { - updateOperations(id, resultOperations); + for (org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation : resultOperations) { + AndroidDeviceUtils.updateOperation(deviceId, operation); + if (OPERATION_ERROR_STATUS.equals(operation.getStatus().toString())) { + org.wso2.carbon.device.mgt.common.notification.mgt.Notification notification = new org.wso2.carbon.device.mgt.common.notification.mgt.Notification(); + DeviceIdentifier id = new DeviceIdentifier(); + id.setId(deviceId); + id.setType(AndroidConstants.DEVICE_TYPE_ANDROID); + String deviceName = AndroidAPIUtils.getDeviceManagementService().getDevice(id, false).getName(); + notification.setOperationId(operation.getId()); + notification.setStatus(org.wso2.carbon.device.mgt.common.notification.mgt.Notification. + Status.NEW.toString()); + notification.setDescription( + operation.getCode() + " operation failed to execute on device " + deviceName + " (ID: " + + deviceId + ")"); + AndroidAPIUtils.getNotificationManagementService().addNotification(id, notification); + } + if (log.isDebugEnabled()) { + log.debug("Updating operation '" + operation.toString() + "'"); + } + } } catch (OperationManagementException e) { String msg = "Issue in retrieving operation management service instance"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + throw new DeviceManagementException(msg, e); } catch (PolicyComplianceException e) { String msg = "Issue in updating Monitoring operation"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); - } catch (DeviceManagementException e) { - String msg = "Issue in retrieving device management service instance"; - log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + throw new DeviceManagementException(msg, e); } catch (org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException e) { String msg = "Issue in retrieving application management service instance"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + throw new DeviceManagementException(msg, e); } catch (NotificationManagementException e) { String msg = "Issue in retrieving Notification management service instance"; log.error(msg, e); - throw new UnexpectedServerErrorException( - new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(msg).build()); + throw new DeviceManagementException(msg, e); } } + + try { + return AndroidDeviceUtils.getPendingOperations(deviceIdentifier, !disableGoogleApps); + } catch (OperationManagementException e) { + String msg = "Issue in retrieving operation management service instance"; + log.error(msg, e); + throw new UnexpectedServerErrorException( + new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR) + .setMessage(msg).build()); + } } @Override @@ -1114,7 +1107,7 @@ public class AndroidServiceImpl implements AndroidService { } @Override - public Device modifyEnrollment(String id, AndroidDevice androidDevice){ + public boolean modifyEnrollment(String id, AndroidDevice androidDevice) throws DeviceManagementException { Device device; DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(id); @@ -1161,16 +1154,14 @@ public class AndroidServiceImpl implements AndroidService { if(androidDevice.getProperties() != null) { device.setProperties(androidDevice.getProperties()); } - return device; + return AndroidAPIUtils.getDeviceManagementService().modifyEnrollment(device); } @Override public boolean disEnrollDevice(String id) throws DeviceManagementException{ - boolean result; DeviceIdentifier deviceIdentifier = AndroidDeviceUtils.convertToDeviceIdentifierObject(id); AndroidDeviceUtils.updateDisEnrollOperationStatus(deviceIdentifier); - result = AndroidAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier); - return result; + return AndroidAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier); } @Override @@ -1292,31 +1283,6 @@ public class AndroidServiceImpl implements AndroidService { } } - private void updateOperations(String deviceId, List operations) - throws OperationManagementException, PolicyComplianceException, - org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException, NotificationManagementException, DeviceManagementException { - for (org.wso2.carbon.device.mgt.common.operation.mgt.Operation operation : operations) { - AndroidDeviceUtils.updateOperation(deviceId, operation); - if (OPERATION_ERROR_STATUS.equals(operation.getStatus().toString())) { - org.wso2.carbon.device.mgt.common.notification.mgt.Notification notification = new - org.wso2.carbon.device.mgt.common.notification.mgt.Notification(); - DeviceIdentifier id = new DeviceIdentifier(); - id.setId(deviceId); - id.setType(AndroidConstants.DEVICE_TYPE_ANDROID); - String deviceName = AndroidAPIUtils.getDeviceManagementService().getDevice(id, false).getName(); - notification.setOperationId(operation.getId()); - notification.setStatus(org.wso2.carbon.device.mgt.common.notification.mgt.Notification. - Status.NEW.toString()); - notification.setDescription(operation.getCode() + " operation failed to execute on device " + - deviceName + " (ID: " + deviceId + ")"); - AndroidAPIUtils.getNotificationManagementService().addNotification(id, notification); - } - if (log.isDebugEnabled()) { - log.debug("Updating operation '" + operation.toString() + "'"); - } - } - } - /** * Extracts the device location * @@ -1402,7 +1368,7 @@ public class AndroidServiceImpl implements AndroidService { List androidEnterpriseUsers = AndroidAPIUtils.getAndroidPluginService() .getEnterpriseUser(CarbonContext.getThreadLocalCarbonContext().getUsername()); GoogleAPIInvoker googleAPIInvoker = new GoogleAPIInvoker(enterpriseConfigs.getEsa()); - if (androidEnterpriseUsers != null && androidEnterpriseUsers.size() > 0) { + if (androidEnterpriseUsers != null && !androidEnterpriseUsers.isEmpty()) { googleUserId = androidEnterpriseUsers.get(0).getGoogleUserId(); // If this device is also present, only need to provide a token for this request. for (AndroidEnterpriseUser enterprise : androidEnterpriseUsers) { @@ -1430,9 +1396,7 @@ public class AndroidServiceImpl implements AndroidService { AndroidAPIUtils.getAndroidPluginService().addEnterpriseUser(androidEnterpriseUser); } - return token; - } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementAdminServiceTests.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementAdminServiceTests.java index 4c1e432b40..d67ad8ab01 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementAdminServiceTests.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementAdminServiceTests.java @@ -18,294 +18,290 @@ package org.wso2.carbon.device.mgt.mobile.android.core; +import org.junit.Assert; +import org.junit.Test; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.testng.PowerMockObjectFactory; -import org.testng.Assert; import org.testng.IObjectFactory; import org.testng.annotations.BeforeClass; import org.testng.annotations.ObjectFactory; -import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.mobile.android.core.impl.AndroidServiceImpl; import org.wso2.carbon.device.mgt.mobile.android.core.mokcs.DeviceManagementProviderServiceMock; -import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidAPIUtils; import org.wso2.carbon.device.mgt.mobile.android.core.mokcs.utils.TestUtils; - -import javax.ws.rs.core.Response; +import org.wso2.carbon.device.mgt.mobile.android.core.util.AndroidAPIUtils; @PowerMockIgnore({"javax.ws.rs.*", "org.apache.log4j.*"}) @PrepareForTest(AndroidAPIUtils.class) public class DeviceManagementAdminServiceTests { -// private DeviceManagementAdminServiceImpl deviceManagementAdminService; + private AndroidServiceImpl androidService; @ObjectFactory public IObjectFactory getObjectFactory() { return new PowerMockObjectFactory(); } -// @BeforeClass -// public void init() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// MockitoAnnotations.initMocks(this); -// deviceManagementAdminService = new DeviceManagementAdminServiceImpl(); -// } + @BeforeClass + public void init() { + MockitoAnnotations.initMocks(this); + androidService = new AndroidServiceImpl(); + } - private void mockDeviceManagementService() - throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + private void mockDeviceManagementService() { PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getDeviceManagementService")) .toReturn(new DeviceManagementProviderServiceMock()); } -// @Test -// public void testConfigureDeviceLock() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.configureDeviceLock(TestUtils.getDeviceLockBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testConfigureDeviceUnlock() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.configureDeviceUnlock(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetDeviceLocation() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.getDeviceLocation(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testRemovePassword() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.removePassword(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testConfigureCamera() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.configureCamera(TestUtils.getCamerabeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetDeviceInformation() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.getDeviceInformation(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetDeviceLogcat() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.getDeviceLogcat(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testWipeDevice() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.wipeDevice(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testWipeData() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.wipeData(TestUtils.getWipeDataBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetApplications() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.getApplications(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testRingDevice() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.ringDevice(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testRebootDevice() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.rebootDevice(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testChangeLockTask() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.changeLockTask(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testMuteDevice() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.muteDevice(TestUtils.getDeviceIds()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testInstallApplication() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService -// .installApplication(TestUtils.getApplicationInstallationBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testUpdateApplication() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.updateApplication(TestUtils.getApplicationUpdateBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testUninstallApplicationPublic() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService -// .uninstallApplication(TestUtils.getApplicationUninstallationBeanWrapperPublic()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testUninstallApplicationWebApp() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService -// .uninstallApplication(TestUtils.getApplicationUninstallationBeanWrapperWebApp()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testBlacklistApplications() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService -// .blacklistApplications(TestUtils.getBlacklistApplicationsBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testUpgradeFirmware() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.upgradeFirmware(TestUtils.getUpgradeFirmwareBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testConfigureVPN() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.configureVPN(TestUtils.getVpnBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testSendNotification() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.sendNotification(TestUtils.getNotificationBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testConfigureWifi() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.configureWifi(TestUtils.getWifiBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testEncryptStorage() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.encryptStorage(TestUtils.getEncryptionBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testChangeLockCode() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.changeLockCode(TestUtils.getLockCodeBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testSetPasswordPolicy() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.setPasswordPolicy(TestUtils.getPasswordPolicyBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testSetWebClip() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementAdminService.setWebClip(TestUtils.getWebClipBeanWrapper()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } + @Test + public void testConfigureDeviceLock() throws OperationManagementException { + mockDeviceManagementService(); + Activity activity = androidService.configureDeviceLock(TestUtils.getDeviceLockBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } -} + @Test + public void testConfigureDeviceUnlock() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.configureDeviceUnlock(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testGetDeviceLocation() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.getDeviceLocation(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testRemovePassword() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.removePassword(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testConfigureCamera() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.configureCamera(TestUtils.getCamerabeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testGetDeviceInformation() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.getDeviceInformation(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testGetDeviceLogcat() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.getDeviceLogcat(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testWipeDevice() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.wipeDevice(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testWipeData() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.wipeData(TestUtils.getWipeDataBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testGetApplications() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.getApplications(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testRingDevice() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.ringDevice(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testRebootDevice() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.rebootDevice(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testChangeLockTask() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.changeLockTask(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + @Test + public void testMuteDevice() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.muteDevice(TestUtils.getDeviceIds()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testInstallApplication() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService + .installApplication(TestUtils.getApplicationInstallationBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testUpdateApplication() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.updateApplication(TestUtils.getApplicationUpdateBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testUninstallApplicationPublic() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService + .uninstallApplication(TestUtils.getApplicationUninstallationBeanWrapperPublic()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testUninstallApplicationWebApp() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService + .uninstallApplication(TestUtils.getApplicationUninstallationBeanWrapperWebApp()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testBlacklistApplications() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService + .blacklistApplications(TestUtils.getBlacklistApplicationsBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testUpgradeFirmware() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.upgradeFirmware(TestUtils.getUpgradeFirmwareBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testConfigureVPN() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.configureVPN(TestUtils.getVpnBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testSendNotification() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.sendNotification(TestUtils.getNotificationBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testConfigureWifi() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.configureWifi(TestUtils.getWifiBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testEncryptStorage() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.encryptStorage(TestUtils.getEncryptionBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testChangeLockCode() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.changeLockCode(TestUtils.getLockCodeBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testSetPasswordPolicy() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.setPasswordPolicy(TestUtils.getPasswordPolicyBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } + + @Test + public void testSetWebClip() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Activity activity = androidService.setWebClip(TestUtils.getWebClipBeanWrapper()); + Assert.assertNotNull(activity); + Assert.assertNotNull(activity.getActivityId()); + } +} diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementServiceTests.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementServiceTests.java index 9ce61820da..ead8db2d25 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementServiceTests.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/DeviceManagementServiceTests.java @@ -29,10 +29,14 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.mobile.android.common.Message; import org.wso2.carbon.device.mgt.mobile.android.common.bean.wrapper.AndroidDevice; +import org.wso2.carbon.device.mgt.mobile.android.core.impl.AndroidServiceImpl; import org.wso2.carbon.device.mgt.mobile.android.core.mokcs.ApplicationManagementProviderServiceMock; import org.wso2.carbon.device.mgt.mobile.android.core.mokcs.DeviceInformationManagerServiceMock; import org.wso2.carbon.device.mgt.mobile.android.core.mokcs.DeviceManagementProviderServiceMock; @@ -49,285 +53,275 @@ import java.util.List; @PrepareForTest(AndroidAPIUtils.class) public class DeviceManagementServiceTests { -// private DeviceManagementServiceImpl deviceManagementService; - -// @ObjectFactory -// public IObjectFactory getObjectFactory() { -// return new PowerMockObjectFactory(); -// } - -// @BeforeClass -// public void init() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// MockitoAnnotations.initMocks(this); -// deviceManagementService = new DeviceManagementServiceImpl(); -// } - -// private void mockDeviceManagementService() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getDeviceManagementService")) -// .toReturn(new DeviceManagementProviderServiceMock()); -// } -// -// private void mockApplicationManagerService() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getApplicationManagerService")) -// .toReturn(new ApplicationManagementProviderServiceMock()); -// } -// -// private void mockPolicyManagerService() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getPolicyManagerService")) -// .toReturn(new PolicyManagerServiceMock()); -// } -// -// private void mockDeviceInformationManagerService() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getDeviceInformationManagerService")) -// .toReturn(new DeviceInformationManagerServiceMock()); -// } -// -// private void mockNotificationManagementService() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getNotificationManagementService")) -// .toReturn(new NotificationManagementServiceMock()); -// } -// -// private void mockUser() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getAuthenticatedUser")) -// .toReturn("admin"); -// } -// -// @Test -// public void testUpdateApplicationList() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockApplicationManagerService(); -// Response response = deviceManagementService -// .updateApplicationList(TestUtils.getDeviceId(), TestUtils.getAndroidApplications()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.ACCEPTED.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsForNullDevice() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// Response response = deviceManagementService -// .getPendingOperations(true, null, null, null); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsInvalidDevice() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService -// .getPendingOperations(true, "1234", null, null); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsNullResponse() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService -// .getPendingOperations(true, TestUtils.getDeviceId(), null, null); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsWithMonitorResponse() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockPolicyManagerService(); -// Response response = deviceManagementService -// .getPendingOperations(true, TestUtils.getDeviceId(), null, -// TestUtils.getSuccessMonitorOperationResponse()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsWithApplicationResponse() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockApplicationManagerService(); -// Response response = deviceManagementService -// .getPendingOperations(true, TestUtils.getDeviceId(), null, -// TestUtils.getSuccessApplicationOperationResponse()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsWithDeviceInfoResponse() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockDeviceInformationManagerService(); -// Response response = deviceManagementService -// .getPendingOperations(true, TestUtils.getDeviceId(), null, -// TestUtils.getSuccessInfoOperationResponse()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsWithInProgressResponse() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService -// .getPendingOperations(true, TestUtils.getDeviceId(), null, -// TestUtils.getInProgressOperationResponse()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testGetPendingOperationsWithErrorResponse() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockNotificationManagementService(); -// Response response = deviceManagementService -// .getPendingOperations(true, TestUtils.getDeviceId(), null, -// TestUtils.getErrorOperationResponse()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode()); -// } -// -// @Test -// public void testEnrollDeviceWithoutLocationSuccess() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockPolicyManagerService(); -// mockUser(); -// Response response = deviceManagementService.enrollDevice(TestUtils.getBasicAndroidDevice()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); -// } -// -// @Test -// public void testEnrollDeviceWithLocationSuccess() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockDeviceInformationManagerService(); -// mockPolicyManagerService(); -// mockUser(); -// AndroidDevice androidDevice = TestUtils.getBasicAndroidDevice(); -// -// List properties = new ArrayList<>(); -// Device.Property property = new Device.Property(); -// property.setName("LATITUDE"); -// property.setValue("79.5"); -// properties.add(property); -// property = new Device.Property(); -// property.setName("LONGITUDE"); -// property.setValue("6.9"); -// properties.add(property); -// property = new Device.Property(); -// property.setName("ALTITUDE"); -// property.setValue("-59.8373726"); -// properties.add(property); -// property = new Device.Property(); -// property.setName("SPEED"); -// property.setValue("0.5123423333"); -// properties.add(property); -// property = new Device.Property(); -// property.setName("BEARING"); -// property.setValue("44.0"); -// properties.add(property); -// property = new Device.Property(); -// property.setName("DISTANCE"); -// property.setValue("44.0"); -// properties.add(property); -// androidDevice.setProperties(properties); -// -// Response response = deviceManagementService.enrollDevice(androidDevice); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); -// } -// -// @Test -// public void testEnrollDeviceUnSuccess() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockUser(); -// AndroidDevice androidDevice = TestUtils.getBasicAndroidDevice(); -// androidDevice.setDeviceIdentifier("1234"); -// Response response = deviceManagementService.enrollDevice(androidDevice); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); -// } -// -// @Test -// public void testIsEnrolledExists() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService.isEnrolled(TestUtils.getDeviceId(), null); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); -// } -// -// @Test -// public void testIsEnrolledNonExist() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService.isEnrolled("1234", null); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); -// } -// -// @Test -// public void testIsEnrolledNull() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService.isEnrolled(null, null); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); -// } -// -// @Test -// public void testModifyEnrollmentSuccess() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockUser(); -// Response response = deviceManagementService -// .modifyEnrollment(TestUtils.getDeviceId(), TestUtils.getBasicAndroidDevice()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.ACCEPTED.getStatusCode()); -// } -// -// @Test -// public void testModifyEnrollmentUnSuccess() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// mockUser(); -// AndroidDevice androidDevice = TestUtils.getBasicAndroidDevice(); -// androidDevice.setDeviceIdentifier("1234"); -// Response response = deviceManagementService -// .modifyEnrollment(TestUtils.getDeviceId(), androidDevice); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode()); -// } -// -// @Test -// public void testDisEnrollDeviceSuccess() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService.disEnrollDevice(TestUtils.getDeviceId()); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); -// } -// -// @Test -// public void testDisenrollUnSuccess() -// throws DeviceManagementException, OperationManagementException, InvalidDeviceException { -// mockDeviceManagementService(); -// Response response = deviceManagementService.disEnrollDevice("1234"); -// Assert.assertNotNull(response); -// Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); -// } + private AndroidServiceImpl androidService; + @ObjectFactory + public IObjectFactory getObjectFactory() { + return new PowerMockObjectFactory(); + } + + @BeforeClass + public void init() throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + MockitoAnnotations.initMocks(this); + androidService = new AndroidServiceImpl(); + } + + private void mockDeviceManagementService() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getDeviceManagementService")) + .toReturn(new DeviceManagementProviderServiceMock()); + } + + private void mockApplicationManagerService() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getApplicationManagerService")) + .toReturn(new ApplicationManagementProviderServiceMock()); + } + + private void mockPolicyManagerService() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getPolicyManagerService")) + .toReturn(new PolicyManagerServiceMock()); + } + + private void mockDeviceInformationManagerService() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getDeviceInformationManagerService")) + .toReturn(new DeviceInformationManagerServiceMock()); + } + + private void mockNotificationManagementService() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getNotificationManagementService")) + .toReturn(new NotificationManagementServiceMock()); + } + + private void mockUser() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + PowerMockito.stub(PowerMockito.method(AndroidAPIUtils.class, "getAuthenticatedUser")) + .toReturn("admin"); + } + + @Test + public void testUpdateApplicationList() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException, + ApplicationManagementException { + mockApplicationManagerService(); + Message message = androidService + .updateApplicationList(TestUtils.getDeviceId(), TestUtils.getAndroidApplications()); + Assert.assertNotNull(message); + Assert.assertEquals(message.getResponseCode(), Response.Status.ACCEPTED.toString()); + } + + @Test (expectedExceptions = {InvalidDeviceException.class}) + public void testGetPendingOperationsForNullDevice() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + List resultOperations = new ArrayList<>(); + androidService.getPendingOperations(null, resultOperations, true); + } + + @Test (expectedExceptions = {InvalidDeviceException.class}) + public void testGetPendingOperationsForInvalidDevice() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + List resultOperations = new ArrayList<>(); + androidService.getPendingOperations("1234", resultOperations, true); + } + + @Test + public void testGetPendingOperationsNullResponse() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + List pendingOperations = androidService + .getPendingOperations(TestUtils.getDeviceId(), null, true); + Assert.assertNotNull(pendingOperations); + Assert.assertFalse((pendingOperations.isEmpty())); + } + + @Test + public void testGetPendingOperationsWithMonitorResponse() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockPolicyManagerService(); + List pendingOperations = androidService + .getPendingOperations(TestUtils.getDeviceId(), TestUtils.getSuccessMonitorOperationResponse(), true); + Assert.assertNotNull(pendingOperations); + Assert.assertFalse((pendingOperations.isEmpty())); + } + + @Test + public void testGetPendingOperationsWithApplicationResponse() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockApplicationManagerService(); + List pendingOperations = androidService + .getPendingOperations(TestUtils.getDeviceId(), TestUtils.getSuccessApplicationOperationResponse(), + true); + Assert.assertNotNull(pendingOperations); + Assert.assertFalse((pendingOperations.isEmpty())); + } + + @Test + public void testGetPendingOperationsWithDeviceInfoResponse() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockDeviceInformationManagerService(); + List pendingOperations = androidService + .getPendingOperations(TestUtils.getDeviceId(), + TestUtils.getSuccessInfoOperationResponse(), true); + Assert.assertNotNull(pendingOperations); + Assert.assertFalse((pendingOperations.isEmpty())); + } + + @Test + public void testGetPendingOperationsWithInProgressResponse() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + List pendingOperations = androidService + .getPendingOperations(TestUtils.getDeviceId(), + TestUtils.getInProgressOperationResponse(), true); + Assert.assertNotNull(pendingOperations); + Assert.assertFalse((pendingOperations.isEmpty())); + } + + @Test + public void testGetPendingOperationsWithErrorResponse() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockNotificationManagementService(); + List pendingOperations = androidService + .getPendingOperations(TestUtils.getDeviceId(), + TestUtils.getErrorOperationResponse(), true); + Assert.assertNotNull(pendingOperations); + Assert.assertFalse((pendingOperations.isEmpty())); + } + + @Test + public void testEnrollDeviceWithoutLocationSuccess() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockPolicyManagerService(); + mockUser(); + Response response = androidService.enrollDevice(TestUtils.getBasicAndroidDevice()); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + } + + @Test + public void testEnrollDeviceWithLocationSuccess() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockDeviceInformationManagerService(); + mockPolicyManagerService(); + mockUser(); + AndroidDevice androidDevice = TestUtils.getBasicAndroidDevice(); + + List properties = new ArrayList<>(); + Device.Property property = new Device.Property(); + property.setName("LATITUDE"); + property.setValue("79.5"); + properties.add(property); + property = new Device.Property(); + property.setName("LONGITUDE"); + property.setValue("6.9"); + properties.add(property); + property = new Device.Property(); + property.setName("ALTITUDE"); + property.setValue("-59.8373726"); + properties.add(property); + property = new Device.Property(); + property.setName("SPEED"); + property.setValue("0.5123423333"); + properties.add(property); + property = new Device.Property(); + property.setName("BEARING"); + property.setValue("44.0"); + properties.add(property); + property = new Device.Property(); + property.setName("DISTANCE"); + property.setValue("44.0"); + properties.add(property); + androidDevice.setProperties(properties); + + Response response = androidService.enrollDevice(androidDevice); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); + } + + @Test + public void testEnrollDeviceUnSuccess() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockUser(); + AndroidDevice androidDevice = TestUtils.getBasicAndroidDevice(); + androidDevice.setDeviceIdentifier("1234"); + Response response = androidService.enrollDevice(androidDevice); + Assert.assertNotNull(response); + Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } + + @Test + public void testIsEnrolledExists() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Message message = androidService.isEnrolled(TestUtils.getDeviceId(), null); + Assert.assertNotNull(message); + Assert.assertEquals(message.getResponseCode(), Response.Status.OK.toString()); + } + + @Test + public void testIsEnrolledNonExist() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Message message = androidService.isEnrolled("1234", null); + Assert.assertNotNull(message); + Assert.assertEquals(message.getResponseCode(), Response.Status.NOT_FOUND.toString()); + } + + @Test + public void testIsEnrolledNull() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + Message response = androidService.isEnrolled(null, null); + Assert.assertNotNull(response); + Assert.assertEquals(response.getResponseCode(), Response.Status.NOT_FOUND.toString()); + } + + @Test + public void testModifyEnrollmentSuccess() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockUser(); + boolean status = androidService.modifyEnrollment(TestUtils.getDeviceId(), TestUtils.getBasicAndroidDevice()); + Assert.assertTrue(status); + } + + @Test + public void testModifyEnrollmentUnSuccess() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + mockUser(); + AndroidDevice androidDevice = TestUtils.getBasicAndroidDevice(); + androidDevice.setDeviceIdentifier("1234"); + boolean status = androidService + .modifyEnrollment(TestUtils.getDeviceId(), androidDevice); + Assert.assertFalse(status); + } + + @Test + public void testDisEnrollDeviceSuccess() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + boolean status = androidService.disEnrollDevice(TestUtils.getDeviceId()); + Assert.assertTrue(status); + } + + @Test + public void testDisEnrollUnSuccess() + throws DeviceManagementException, OperationManagementException, InvalidDeviceException { + mockDeviceManagementService(); + boolean status = androidService.disEnrollDevice("1234"); + Assert.assertFalse(status); + } } diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/mokcs/utils/TestUtils.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/mokcs/utils/TestUtils.java index 2a0bb71ea5..61169ecfa3 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/mokcs/utils/TestUtils.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/test/java/org/wso2/carbon/device/mgt/mobile/android/core/mokcs/utils/TestUtils.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.mobile.android.core.mokcs.utils; -import javafx.scene.Camera; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -32,6 +31,7 @@ import org.wso2.carbon.device.mgt.mobile.android.common.bean.ApplicationInstalla import org.wso2.carbon.device.mgt.mobile.android.common.bean.ApplicationUninstallation; import org.wso2.carbon.device.mgt.mobile.android.common.bean.ApplicationUpdate; import org.wso2.carbon.device.mgt.mobile.android.common.bean.BlacklistApplications; +import org.wso2.carbon.device.mgt.mobile.android.common.bean.Camera; import org.wso2.carbon.device.mgt.mobile.android.common.bean.DeviceEncryption; import org.wso2.carbon.device.mgt.mobile.android.common.bean.DeviceLock; import org.wso2.carbon.device.mgt.mobile.android.common.bean.LockCode; @@ -108,14 +108,14 @@ public class TestUtils { return deviceLockBeanWrapper; } -// public static CameraBeanWrapper getCamerabeanWrapper() { -// CameraBeanWrapper cameraBeanWrapper = new CameraBeanWrapper(); -// cameraBeanWrapper.setDeviceIDs(getDeviceIds()); -// Camera camera = new Camera(); -// camera.setEnabled(false); -// cameraBeanWrapper.setOperation(camera); -// return cameraBeanWrapper; -// } + public static CameraBeanWrapper getCamerabeanWrapper() { + CameraBeanWrapper cameraBeanWrapper = new CameraBeanWrapper(); + cameraBeanWrapper.setDeviceIDs(getDeviceIds()); + Camera camera = new Camera(); + camera.setEnabled(false); + cameraBeanWrapper.setOperation(camera); + return cameraBeanWrapper; + } public static WipeDataBeanWrapper getWipeDataBeanWrapper() { WipeDataBeanWrapper wipeDataBeanWrapper = new WipeDataBeanWrapper();