From 78057b2992ca97680b002e075edd9180abcadf5d Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 5 Jul 2016 15:09:11 +0530 Subject: [PATCH 1/4] Adding an API to return the list of device types registered in the underlying device management platform --- .../mgt/jaxrs/beans/BasePaginatedResult.java | 2 +- .../mgt/jaxrs/beans/DeviceTypeList.java | 101 ++++++++++++++++++ .../admin/DeviceTypeManagementService.java | 93 ++++++++++++++++ .../DeviceTypeManagementServiceImpl.java | 55 ++++++++++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 + .../device/mgt/core/dao/DeviceTypeDAO.java | 6 +- .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 22 ++-- .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 21 ++-- 9 files changed, 274 insertions(+), 30 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java index 9ae3ade67e1..e423f567084 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java @@ -28,7 +28,7 @@ public class BasePaginatedResult { private String previous; /** - * Number of Devices returned. + * Number of Resources returned. */ @ApiModelProperty(value = "Number of resources returned.") @JsonProperty("count") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java new file mode 100644 index 00000000000..0398b6c7cfc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.Device; + +import java.util.ArrayList; +import java.util.List; + +public class DeviceTypeList { + + private int count; + private String next; + private String previous; + private List deviceTypes = new ArrayList<>(); + + /** + * Number of Devices Types returned. + */ + @ApiModelProperty(value = "Number of resources returned.") + @JsonProperty("count") + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + + /** + * Link to the next subset of resources qualified. \nEmpty if no more resources are to be returned. + */ + @ApiModelProperty(value = "Link to the next subset of resources qualified. \n " + + "Empty if no more resources are to be returned.") + @JsonProperty("next") + public String getNext() { + return next; + } + + public void setNext(String next) { + this.next = next; + } + + /** + * Link to the previous subset of resources qualified. \nEmpty if current subset is the first subset returned. + */ + @ApiModelProperty(value = "Link to the previous subset of resources qualified. \n" + + "Empty if current subset is the first subset returned.") + @JsonProperty("previous") + public String getPrevious() { + return previous; + } + + public void setPrevious(String previous) { + this.previous = previous; + } + + @ApiModelProperty(value = "List of device types returned") + @JsonProperty("devicesTypes") + public List getList() { + return deviceTypes; + } + + public void setList(List deviceTypes) { + this.deviceTypes = deviceTypes; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" next: ").append(getNext()).append(",\n"); + sb.append(" previous: ").append(getPrevious()).append(",\n"); + sb.append(" deviceTypes: [").append(deviceTypes).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java new file mode 100644 index 00000000000..2d0794e2996 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.jaxrs.service.api.admin; + +import io.swagger.annotations.*; +import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeList; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@API(name = "Device Type Management", version = "1.0.0", context = "/devicemgt_admin/configuration", tags = {"devicemgt_admin"}) + +@Path("/admin/device-types") +@Api(value = "Device Type Management", description = "This API corresponds to all tasks related to device " + + "type management") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface DeviceTypeManagementService { + + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get the list of device types registered.", + notes = "Retrieves the list of device types of which the devices intended to be managed.", + tags = "Device Type Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the list of supported device types.", + response = DeviceTypeList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the " + + "list of supported device types.", + response = ErrorResponse.class) + } + ) + @Permission( + scope = "read:device-types", + permissions = {"/permission/admin/device-mgt/admin/device-types/view"} + ) + Response getDeviceTypes( + @ApiParam( + name = "If-Modified-Since", + value = "Validates if the requested variant has not been modified since the time specified", + required = false) + @HeaderParam("If-Modified-Since") + String ifModifiedSince); + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java new file mode 100644 index 00000000000..00029244756 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.jaxrs.service.impl.admin; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeList; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceTypeManagementService; +import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; + +import javax.ws.rs.HeaderParam; +import javax.ws.rs.core.Response; +import java.util.List; + +public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementService { + + private static Log log = LogFactory.getLog(DeviceTypeManagementServiceImpl.class); + + @Override + public Response getDeviceTypes(@HeaderParam("If-Modified-Since") String ifModifiedSince) { + List deviceTypes; + try { + deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes(); + + DeviceTypeList deviceTypeList = new DeviceTypeList(); + deviceTypeList.setCount(deviceTypes.size()); + deviceTypeList.setList(deviceTypes); + return Response.status(Response.Status.OK).entity(deviceTypeList).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while fetching the list of device types."; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 42eb1188dda..1b128fb4507 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -38,6 +38,7 @@ + @@ -77,6 +78,7 @@ + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index 53827abd6ec..0e34fc1ab08 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -51,17 +51,17 @@ public interface DeviceTypeDAO { List getDeviceTypes(int tenantId) throws DeviceManagementDAOException; /** - * @param tenandId of the device type provider. + * @param tenantId of the device type provider. * @return return only the device types that are associated with the provider tenant. * @throws DeviceManagementDAOException */ - List getDeviceTypesByProvider(int tenandId) throws DeviceManagementDAOException; + List getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException; /** * @return sharedWithAllDeviceTypes This returns public shared device types. * @throws DeviceManagementDAOException */ - List getSharedDeviceTypes() throws DeviceManagementDAOException; + List getSharedDeviceTypes() throws DeviceManagementDAOException; /** * @param id retrieve the device type with its id. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index 8ef838ef8ea..c7e4952328e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -89,24 +89,21 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } @Override - public List getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException { + public List getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - List deviceTypes = new ArrayList<>(); + List deviceTypes = new ArrayList<>(); try { conn = this.getConnection(); String sql = - "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =?"; + "SELECT NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); rs = stmt.executeQuery(); while (rs.next()) { - DeviceType deviceType = new DeviceType(); - deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); - deviceType.setName(rs.getString("DEVICE_TYPE")); - deviceTypes.add(deviceType); + deviceTypes.add(rs.getString("DEVICE_TYPE")); } return deviceTypes; } catch (SQLException e) { @@ -117,25 +114,22 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } @Override - public List getSharedDeviceTypes() throws DeviceManagementDAOException { + public List getSharedDeviceTypes() throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - List deviceTypes = new ArrayList<>(); + List deviceTypes = new ArrayList<>(); try { conn = this.getConnection(); String sql = - "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where " + + "SELECT NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where " + "SHARED_WITH_ALL_TENANTS = ?"; stmt = conn.prepareStatement(sql); stmt.setBoolean(1, true); rs = stmt.executeQuery(); while (rs.next()) { - DeviceType deviceType = new DeviceType(); - deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); - deviceType.setName(rs.getString("DEVICE_TYPE")); - deviceTypes.add(deviceType); + deviceTypes.add(rs.getString("DEVICE_TYPE")); } return deviceTypes; } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 95e8716af13..d0211bcd941 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -207,7 +207,7 @@ public interface DeviceManagementProviderService { Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; - List getAvailableDeviceTypes() throws DeviceManagementException; + List getAvailableDeviceTypes() throws DeviceManagementException; boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 536901f821f..087f3eb50c8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -857,10 +857,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getAvailableDeviceTypes() throws DeviceManagementException { - List deviceTypesProvidedByTenant; - List publicSharedDeviceTypesInDB; - List deviceTypesResponse = new ArrayList<>(); + public List getAvailableDeviceTypes() throws DeviceManagementException { + List deviceTypesProvidedByTenant; + List publicSharedDeviceTypesInDB; + List deviceTypesResponse = new ArrayList<>(); try { DeviceManagementDAOFactory.openConnection(); int tenantId = this.getTenantId(); @@ -872,21 +872,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (registeredTypes != null) { if (deviceTypesProvidedByTenant != null) { - for (DeviceType deviceType : deviceTypesProvidedByTenant) { - DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName(), tenantId); + for (String deviceType : deviceTypesProvidedByTenant) { + DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType, tenantId); if (registeredTypes.get(providerKey) != null) { deviceTypesResponse.add(deviceType); - deviceTypeSetForTenant.add(deviceType.getName()); + deviceTypeSetForTenant.add(deviceType); } } } // Get the device from the public space, however if there is another device with same name then give // priority to that if (publicSharedDeviceTypesInDB != null) { - for (DeviceType deviceType : publicSharedDeviceTypesInDB) { - DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName()); - if (registeredTypes.get(providerKey) != null && !deviceTypeSetForTenant.contains( - deviceType.getName())) { + for (String deviceType : publicSharedDeviceTypesInDB) { + DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType); + if (registeredTypes.get(providerKey) != null && !deviceTypeSetForTenant.contains(deviceType)) { deviceTypesResponse.add(deviceType); } } From 0125310440202648a387f50dc0c7540b244b01e3 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 5 Jul 2016 15:39:50 +0530 Subject: [PATCH 2/4] Prepending the url template configured for updating notifications with a '/' --- .../mgt/jaxrs/service/api/NotificationManagementService.java | 2 +- .../jaxrs/service/impl/NotificationManagementServiceImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java index 47ab30198c9..5e4d06f10c1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/NotificationManagementService.java @@ -125,7 +125,7 @@ public interface NotificationManagementService { int limit); @PUT - @Path("{id}/mark-checked") + @Path("/{id}/mark-checked") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "PUT", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java index ec01db35cd2..c504da33d1b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java @@ -76,7 +76,7 @@ public class NotificationManagementServiceImpl implements NotificationManagement } @PUT - @Path("{id}/mark-checked") + @Path("/{id}/mark-checked") public Response updateNotificationStatus( @PathParam("id") int id) { String msg; From 9aa54f2ead88096be448d9815bb6fc9626119ee9 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 7 Jul 2016 13:10:19 +0530 Subject: [PATCH 3/4] Improving policy management functionalities to use deviceType in place of deviceTypeId for persisting policy information --- .../device/mgt/jaxrs/beans/Profile.java | 6 +- .../mgt/jaxrs/beans/ProfileFeature.java | 10 +-- .../admin/DeviceTypeManagementService.java | 2 +- .../DeviceTypeManagementServiceImpl.java | 4 + .../device/mgt/jaxrs/util/DeviceMgtUtil.java | 2 +- .../src/main/webapp/META-INF/permissions.xml | 9 +++ .../policy/evaluator/PolicyFilterImpl.java | 6 +- .../carbon/policy/mgt/common/Profile.java | 6 +- .../policy/mgt/common/ProfileFeature.java | 10 +-- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 2 +- .../policy/mgt/core/dao/ProfileDAO.java | 2 +- .../mgt/core/dao/impl/FeatureDAOImpl.java | 21 +++-- .../mgt/core/dao/impl/PolicyDAOImpl.java | 46 +++++------ .../mgt/core/dao/impl/ProfileDAOImpl.java | 27 +++---- .../mgt/core/enforcement/DelegationTask.java | 6 +- .../mgt/core/impl/PolicyFilterImpl.java | 2 +- .../policy/mgt/core/mgt/PolicyManager.java | 2 +- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 6 +- .../mgt/core/mgt/impl/ProfileManagerImpl.java | 54 +------------ .../policy/mgt/core/MonitoringTestCase.java | 18 ++++- .../policy/mgt/core/PolicyDAOTestCase.java | 81 ++++++++++++++----- .../mgt/core/PolicyEvaluationTestCase.java | 28 ++++++- .../policy/mgt/core/util/ProfileCreator.java | 35 ++------ .../mgt/core/util/ProfileFeatureCreator.java | 4 +- .../src/test/resources/sql/CreateH2TestDB.sql | 16 ++-- .../src/main/resources/dbscripts/cdm/h2.sql | 16 ++-- 26 files changed, 209 insertions(+), 212 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/Profile.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/Profile.java index 17f8b905bfc..27aebb2abf2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/Profile.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/Profile.java @@ -40,7 +40,7 @@ public class Profile { private int tenantId; @ApiModelProperty(name = "deviceType", value = "Contains the device type details the policy was created " + "for", required = true) - private DeviceType deviceType; + private String deviceType; @ApiModelProperty(name = "createdDate", value = "The date the policy was created", required = true) private Timestamp createdDate; @ApiModelProperty(name = "updatedDate", value = "The date the changes made to the policy was published to" @@ -50,11 +50,11 @@ public class Profile { + "in the policy", required = true) private List profileFeaturesList; // Features included in the policies. - public DeviceType getDeviceType() { + public String getDeviceType() { return deviceType; } - public void setDeviceType(DeviceType deviceType) { + public void setDeviceType(String deviceType) { this.deviceType = deviceType; } @XmlElement diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java index 81d8032799c..7a214c308c6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java @@ -37,7 +37,7 @@ public class ProfileFeature implements Serializable { private int profileId; @ApiModelProperty(name = "deviceTypeId", value = "The ID used to define the type of the device platform", required = true) - private int deviceTypeId; + private String deviceType; @ApiModelProperty(name = "content", value = "The list of parameters that define the policy", required = true) private Object content; @@ -69,12 +69,12 @@ public class ProfileFeature implements Serializable { this.profileId = profileId; } - public int getDeviceTypeId() { - return deviceTypeId; + public String getDeviceTypeId() { + return deviceType; } - public void setDeviceTypeId(int deviceTypeId) { - this.deviceTypeId = deviceTypeId; + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java index 2d0794e2996..4ce4ac2147a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java @@ -28,7 +28,7 @@ import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -@API(name = "Device Type Management", version = "1.0.0", context = "/devicemgt_admin/configuration", tags = {"devicemgt_admin"}) +@API(name = "Device Type Management", version = "1.0.0", context = "/admin/device-types", tags = {"devicemgt_admin"}) @Path("/admin/device-types") @Api(value = "Device Type Management", description = "This API corresponds to all tasks related to device " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java index 00029244756..fdda3bdd887 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java @@ -26,14 +26,18 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceTypeManagementService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; import javax.ws.rs.core.Response; import java.util.List; +@Path("/admin/device-types") public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementService { private static Log log = LogFactory.getLog(DeviceTypeManagementServiceImpl.class); + @GET @Override public Response getDeviceTypes(@HeaderParam("If-Modified-Since") String ifModifiedSince) { List deviceTypes; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java index c2f5c2b7017..98461d153a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java @@ -52,7 +52,7 @@ public class DeviceMgtUtil { new org.wso2.carbon.policy.mgt.common.ProfileFeature(); profileFeature.setProfileId(mdmProfileFeature.getProfileId()); profileFeature.setContent(mdmProfileFeature.getPayLoad()); - profileFeature.setDeviceTypeId(mdmProfileFeature.getDeviceTypeId()); + profileFeature.setDeviceType(mdmProfileFeature.getDeviceTypeId()); profileFeature.setFeatureCode(mdmProfileFeature.getFeatureCode()); profileFeature.setId(mdmProfileFeature.getId()); return profileFeature; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml index a92a3c184b3..30bc4bf8864 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -981,4 +981,13 @@ GET + + + + Get device types registered in the system + /device-mgt/admin/device-types + /admin/device-types + GET + + diff --git a/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java index a4bc89bfe7d..0cbcdb8dd2d 100644 --- a/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.complex.policy.decision.point/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java @@ -36,7 +36,7 @@ public class PolicyFilterImpl implements PolicyFilter { @Override public List extractPoliciesRelatedToRoles(List policyList, List roles) { - List policies = new ArrayList(); + List policies = new ArrayList<>(); for (Policy policy : policyList) { List roleList = policy.getRoles(); @@ -60,10 +60,10 @@ public class PolicyFilterImpl implements PolicyFilter { */ @Override public List extractPoliciesRelatedToDeviceType(List policyList, String deviceType) { - List policies = new ArrayList(); + List policies = new ArrayList<>(); for (Policy policy : policyList) { - if (policy.getProfile().getDeviceType().getName().equalsIgnoreCase(deviceType)) { + if (policy.getProfile().getDeviceType().equalsIgnoreCase(deviceType)) { policies.add(policy); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java index 259a1e19bca..0d3d08026d2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java @@ -33,17 +33,17 @@ public class Profile implements Serializable { private int profileId; private String profileName; private int tenantId; - private DeviceType deviceType; + private String deviceType; private Timestamp createdDate; private Timestamp updatedDate; // private List featuresList; // Features included in the policies. private List profileFeaturesList; // Features included in the policies. - public DeviceType getDeviceType() { + public String getDeviceType() { return deviceType; } - public void setDeviceType(DeviceType deviceType) { + public void setDeviceType(String deviceType) { this.deviceType = deviceType; } @XmlElement diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java index 5de755a35d4..723f5fb49c6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java @@ -38,7 +38,7 @@ public class ProfileFeature implements Serializable { private int profileId; @ApiModelProperty(name = "deviceTypeId", value = "The ID used to define the type of the device platform", required = true) - private int deviceTypeId; + private String deviceType; @ApiModelProperty(name = "content", value = "The list of parameters that define the policy", required = true) private Object content; @@ -67,12 +67,12 @@ public class ProfileFeature implements Serializable { this.profileId = profileId; } - public int getDeviceTypeId() { - return deviceTypeId; + public String getDeviceType() { + return deviceType; } - public void setDeviceTypeId(int deviceTypeId) { - this.deviceTypeId = deviceTypeId; + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; } public Object getContent() { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index ce519e135f0..db36c6ec998 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -31,7 +31,7 @@ public interface PolicyDAO { Policy addPolicy(Policy policy) throws PolicyManagerDAOException; - Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException; +// Policy addPolicyToDeviceType(String deviceType, Policy policy) throws PolicyManagerDAOException; /** * This method is used to add/update the roles associated with the policy. diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java index 5d91650d7e4..c5fe41a9456 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java @@ -85,6 +85,6 @@ public interface ProfileDAO { * @return retruns list of profiles. * @throws ProfileManagerDAOException */ - List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException; + List getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 196001c041d..80e56af1401 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -66,14 +66,14 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT, " + + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, new String[] {"id"}); for (ProfileFeature feature : features) { stmt.setInt(1, profileId); stmt.setString(2, feature.getFeatureCode()); - stmt.setInt(3, feature.getDeviceTypeId()); + stmt.setString(3, feature.getDeviceType()); // if (conn.getMetaData().getDriverName().contains("H2")) { // stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); // } else { @@ -145,10 +145,7 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query); stmt.setInt(1, profile.getProfileId()); stmt.setInt(2, tenantId); - if (stmt.executeUpdate() > 0) { - return true; - } - return false; + return stmt.executeUpdate() > 0; } catch (SQLException e) { throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e); } finally { @@ -211,7 +208,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); - String query = "SELECT ID, PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT FROM DM_PROFILE_FEATURES " + + String query = "SELECT ID, PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT FROM DM_PROFILE_FEATURES " + "WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, tenantId); @@ -221,7 +218,7 @@ public class FeatureDAOImpl implements FeatureDAO { ProfileFeature profileFeature = new ProfileFeature(); profileFeature.setFeatureCode(resultSet.getString("FEATURE_CODE")); - profileFeature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID")); + profileFeature.setDeviceType(resultSet.getString("DEVICE_TYPE")); profileFeature.setId(resultSet.getInt("ID")); profileFeature.setProfileId(resultSet.getInt("PROFILE_ID")); @@ -272,9 +269,9 @@ public class FeatureDAOImpl implements FeatureDAO { List featureList = new ArrayList(); try { conn = this.getConnection(); - String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE_ID DEVICE_TYPE_ID," + + String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE DEVICE_TYPE," + " f.EVALUATION_RULE EVALUATION_RULE FROM DM_FEATURES f INNER JOIN DM_DEVICE_TYPE d " + - "ON d.ID=f.DEVICE_TYPE_ID WHERE d.NAME = ?"; + "ON d.ID=f.DEVICE_TYPE WHERE d.NAME = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, deviceType); resultSet = stmt.executeQuery(); @@ -306,7 +303,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); - String query = "SELECT ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT FROM DM_PROFILE_FEATURES " + + String query = "SELECT ID, FEATURE_CODE, DEVICE_TYPE, CONTENT FROM DM_PROFILE_FEATURES " + "WHERE PROFILE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); @@ -317,7 +314,7 @@ public class FeatureDAOImpl implements FeatureDAO { ProfileFeature profileFeature = new ProfileFeature(); profileFeature.setId(resultSet.getInt("ID")); profileFeature.setFeatureCode(resultSet.getString("FEATURE_CODE")); - profileFeature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID")); + profileFeature.setDeviceType(resultSet.getString("DEVICE_TYPE")); ByteArrayInputStream bais = null; ObjectInputStream ois = null; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 9641dbc5167..1530b8edfcc 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -48,25 +48,25 @@ public class PolicyDAOImpl implements PolicyDAO { return persistPolicy(policy); } - @Override - public Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException { - Connection conn; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, getDeviceTypeId(deviceType)); - stmt.setInt(2, policy.getId()); - stmt.executeQuery(); - } catch (SQLException e) { - throw new PolicyManagerDAOException("Error occurred while adding the device type policy to database.", e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - return policy; - - } +// @Override +// public Policy addPolicyToDeviceType(String deviceType, Policy policy) throws PolicyManagerDAOException { +// Connection conn; +// PreparedStatement stmt = null; +// try { +// conn = this.getConnection(); +// String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)"; +// stmt = conn.prepareStatement(query); +// stmt.setInt(1, getDeviceTypeId(deviceType)); +// stmt.setInt(2, policy.getId()); +// stmt.executeQuery(); +// } catch (SQLException e) { +// throw new PolicyManagerDAOException("Error occurred while adding the device type policy to database.", e); +// } finally { +// PolicyManagementDAOUtil.cleanupResources(stmt, null); +// } +// return policy; +// +// } @Override public Policy addPolicyToRole(List rolesToAdd, Policy policy) throws PolicyManagerDAOException { @@ -831,10 +831,10 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_CHANGE_MGT (POLICY_ID, DEVICE_TYPE_ID, TENANT_ID) VALUES (?, ?, ?)"; + String query = "INSERT INTO DM_POLICY_CHANGE_MGT (POLICY_ID, DEVICE_TYPE, TENANT_ID) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); - stmt.setInt(2, policy.getProfile().getDeviceType().getId()); + stmt.setString(2, policy.getProfile().getDeviceType()); stmt.setInt(3, tenantId); stmt.executeUpdate(); @@ -855,11 +855,11 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_CHANGE_MGT (POLICY_ID, DEVICE_TYPE_ID, TENANT_ID) VALUES (?, ?, ?)"; + String query = "INSERT INTO DM_POLICY_CHANGE_MGT (POLICY_ID, DEVICE_TYPE, TENANT_ID) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(query); for (Policy policy : policies) { stmt.setInt(1, policy.getId()); - stmt.setInt(2, policy.getProfile().getDeviceType().getId()); + stmt.setString(2, policy.getProfile().getDeviceType()); stmt.setInt(3, tenantId); stmt.addBatch(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index 371c545b35e..253cc27a3ae 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -50,12 +50,12 @@ public class ProfileDAOImpl implements ProfileDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_PROFILE " + - "(PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; + "(PROFILE_NAME, TENANT_ID, DEVICE_TYPE, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, new String[] {"id"}); stmt.setString(1, profile.getProfileName()); stmt.setInt(2, tenantId); - stmt.setLong(3, profile.getDeviceType().getId()); + stmt.setString(3, profile.getDeviceType()); stmt.setTimestamp(4, profile.getCreatedDate()); stmt.setTimestamp(5, profile.getUpdatedDate()); @@ -95,11 +95,11 @@ public class ProfileDAOImpl implements ProfileDAO { try { conn = this.getConnection(); - String query = "UPDATE DM_PROFILE SET PROFILE_NAME = ? , DEVICE_TYPE_ID = ? , UPDATED_TIME = ? " + + String query = "UPDATE DM_PROFILE SET PROFILE_NAME = ? , DEVICE_TYPE = ? , UPDATED_TIME = ? " + "WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, profile.getProfileName()); - stmt.setLong(2, profile.getDeviceType().getId()); + stmt.setString(2, profile.getDeviceType()); stmt.setTimestamp(3, profile.getUpdatedDate()); stmt.setInt(4, profile.getProfileId()); stmt.setInt(5, tenantId); @@ -183,8 +183,6 @@ public class ProfileDAOImpl implements ProfileDAO { PreparedStatement stmt = null; ResultSet resultSet = null; Profile profile = new Profile(); - DeviceType deviceType = new DeviceType(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_PROFILE WHERE ID = ?"; @@ -194,11 +192,10 @@ public class ProfileDAOImpl implements ProfileDAO { while (resultSet.next()) { - deviceType.setId(resultSet.getInt("DEVICE_TYPE_ID")); profile.setProfileId(profileId); profile.setProfileName(resultSet.getString("PROFILE_NAME")); profile.setTenantId(resultSet.getInt("TENANT_ID")); - profile.setDeviceType(deviceType); + profile.setDeviceType(resultSet.getString("DEVICE_TYPE")); profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME")); profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME")); } @@ -236,11 +233,7 @@ public class ProfileDAOImpl implements ProfileDAO { profile.setTenantId(resultSet.getInt("TENANT_ID")); profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME")); profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME")); - - DeviceType deviceType = new DeviceType(); - deviceType.setId(resultSet.getInt("DEVICE_TYPE_ID")); - - profile.setDeviceType(deviceType); + profile.setDeviceType(resultSet.getString("DEVICE_TYPE")); profileList.add(profile); } @@ -256,16 +249,16 @@ public class ProfileDAOImpl implements ProfileDAO { } @Override - public List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException { + public List getProfilesOfDeviceType(String deviceType) throws ProfileManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List profileList = new ArrayList<>(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_PROFILE WHERE DEVICE_TYPE_ID = ?"; + String query = "SELECT * FROM DM_PROFILE WHERE DEVICE_TYPE = ?"; stmt = conn.prepareStatement(query); - stmt.setInt(1, deviceType.getId()); + stmt.setString(1, deviceType); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -273,7 +266,7 @@ public class ProfileDAOImpl implements ProfileDAO { profile.setProfileId(resultSet.getInt("ID")); profile.setProfileName(resultSet.getString("PROFILE_NAME")); profile.setTenantId(resultSet.getInt("TENANT_ID")); - profile.setDeviceType(deviceType); + profile.setDeviceType(resultSet.getString("DEVICE_TYPE")); profile.setCreatedDate(resultSet.getTimestamp("CREATED_TIME")); profile.setUpdatedDate(resultSet.getTimestamp("UPDATED_TIME")); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java index 2de040e451d..9a7eab1c6d5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -54,7 +54,7 @@ public class DelegationTask implements Task { try { PolicyManager policyManager = new PolicyManagerImpl(); - List deviceTypes = policyManager.applyChangesMadeToPolicies(); + List deviceTypes = policyManager.applyChangesMadeToPolicies(); PolicyCacheManagerImpl.getInstance().rePopulateCache(); @@ -65,9 +65,9 @@ public class DelegationTask implements Task { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance() .getDeviceManagementService(); List devices = new ArrayList<>(); - for (DeviceType deviceType : deviceTypes) { + for (String deviceType : deviceTypes) { try { - devices.addAll(service.getAllDevices(deviceType.getName())); + devices.addAll(service.getAllDevices(deviceType)); } catch (DeviceManagementException e) { throw new PolicyManagementException("Error occurred while taking the devices", e); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index 0d4770a14c2..bb2edf3586c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -179,7 +179,7 @@ public class PolicyFilterImpl implements PolicyFilter { List temp = new ArrayList(); for (Policy policy : policies) { - if (deviceType.equalsIgnoreCase(policy.getProfile().getDeviceType().getName())) { + if (deviceType.equalsIgnoreCase(policy.getProfile().getDeviceType())) { temp.add(policy); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index fb8a8df5bf5..5e02d988595 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -68,7 +68,7 @@ public interface PolicyManager { void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; - List applyChangesMadeToPolicies() throws PolicyManagementException; + List applyChangesMadeToPolicies() throws PolicyManagementException; void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 778de88dad0..a8e8250c56d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -681,7 +681,7 @@ public class PolicyManagerImpl implements PolicyManager { List allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); for (Policy policy : allPolicies) { - if (policy.getProfile().getDeviceType().getName().equalsIgnoreCase(deviceTypeName)) { + if (policy.getProfile().getDeviceType().equalsIgnoreCase(deviceTypeName)) { policies.add(policy); } } @@ -843,9 +843,9 @@ public class PolicyManagerImpl implements PolicyManager { } @Override - public List applyChangesMadeToPolicies() throws PolicyManagementException { + public List applyChangesMadeToPolicies() throws PolicyManagementException { - List changedDeviceTypes = new ArrayList<>(); + List changedDeviceTypes = new ArrayList<>(); try { //HashMap map = policyDAO.getUpdatedPolicyIdandDeviceTypeId(); List updatedPolicies = new ArrayList<>(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index bdfe27c420e..f99de9bc956 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -144,8 +144,6 @@ public class ProfileManagerImpl implements ProfileManager { public Profile getProfile(int profileId) throws ProfileManagementException { Profile profile; List featureList; - DeviceType deviceType = null; - try { PolicyManagementDAOFactory.openConnection(); profile = profileDAO.getProfile(profileId); @@ -161,41 +159,12 @@ public class ProfileManagerImpl implements ProfileManager { } finally { PolicyManagementDAOFactory.closeConnection(); } - - try { - DeviceManagementDAOFactory.openConnection(); - deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); - } catch (DeviceManagementDAOException e) { - throw new ProfileManagementException("Error occurred while getting features related profile id (" + - profileId + ")", e); - } catch (SQLException e) { - throw new ProfileManagementException("SQL exception occurred while getting features related profile id (" + - profileId + ")", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - profile.setDeviceType(deviceType); return profile; } @Override public List getAllProfiles() throws ProfileManagementException { List profileList; - List deviceTypes; - - try { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceManagementDAOFactory.openConnection(); - deviceTypes = deviceTypeDAO.getDeviceTypes(tenantId); - } catch (SQLException e) { - throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); - } catch (DeviceManagementDAOException e) { - throw new ProfileManagementException("Error occurred while retrieving device type information", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - try { PolicyManagementDAOFactory.openConnection(); profileList = profileDAO.getAllProfiles(); @@ -210,12 +179,6 @@ public class ProfileManagerImpl implements ProfileManager { } } profile.setProfileFeaturesList(list); - - for (DeviceType deviceType : deviceTypes) { - if (profile.getDeviceType().getId() == deviceType.getId()) { - profile.setDeviceType(deviceType); - } - } } } catch (ProfileManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profiles", e); @@ -225,29 +188,14 @@ public class ProfileManagerImpl implements ProfileManager { throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); - // DeviceManagementDAOFactory.closeConnection(); } return profileList; } @Override - public List getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException { + public List getProfilesOfDeviceType(String deviceType) throws ProfileManagementException { List profileList; List featureList; - DeviceType deviceType; - - try { - DeviceManagementDAOFactory.openConnection(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - deviceType = deviceTypeDAO.getDeviceType(deviceTypeName, tenantId); - } catch (DeviceManagementDAOException e) { - throw new ProfileManagementException("Error occurred while retrieving device type information", e); - } catch (SQLException e) { - throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - try { PolicyManagementDAOFactory.openConnection(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java index 49fe8c062cd..5f583740202 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java @@ -59,13 +59,25 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { } @Test - public void testMonitorDao() throws PolicyManagementException, DeviceManagementException { + public void testMonitorDao() { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); - List policies = policyManagerService.getPolicies(ANDROID); - List devices = service.getAllDevices(ANDROID); + List policies = null; + List devices = null; + try { + policies = policyManagerService.getPolicies(ANDROID); + devices = service.getAllDevices(ANDROID); + } catch (PolicyManagementException e) { + log.error("Error occurred while retrieving the list of policies defined against the device type '" + + ANDROID + "'", e); + Assert.fail(); + } catch (DeviceManagementException e) { + log.error("Error occurred while retrieving the list of devices pertaining to the type '" + + ANDROID + "'", e); + Assert.fail(); + } for (Policy policy : policies) { log.debug("Policy Name : " + policy.getPolicyName()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 9e44132e06d..b323735b693 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.policy.mgt.core; +import junit.framework.Assert; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.annotations.BeforeClass; @@ -63,10 +64,11 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { deviceTypeDAO.addDeviceType(DeviceTypeCreator.getDeviceType(), -1234, true); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementDAOException("Error occurred while adding dummy device type", e); + log.error("Error occurred while adding dummy device type", e); + Assert.fail(); } catch (TransactionManagementException e) { - throw new DeviceManagementDAOException("Error occurred while initiating a transaction to add dummy " + - "device type", e); + log.error("Error occurred while initiating a transaction to add dummy device type", e); + Assert.fail(); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -94,10 +96,12 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { enrollmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234); } } catch (TransactionManagementException e) { - throw new PolicyManagementException("Error occurred while adding device enrolment", e); + log.error("Error occurred while adding device enrolment", e); + Assert.fail(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new PolicyManagementException("Error occurred while adding device information", e); + log.error("Error occurred while adding device information", e); + Assert.fail(); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -107,7 +111,9 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { PolicyManagementDataHolder.getInstance().setDeviceManagementService(service); - log.debug("Printing device taken by calling the service layer with device type."); + if (log.isDebugEnabled()) { + log.debug("Printing device taken by calling the service layer with device type."); + } List devices3 = service.getAllDevices("android"); log.debug("Device list size ...! " + devices3.size()); @@ -141,14 +147,19 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("addProfileFeatures")) - public void addPolicy() throws PolicyManagementException, ProfileManagementException { + public void addPolicy() { // ProfileManager profileManager = new ProfileManagerImpl(); - Profile profile = ProfileCreator.getProfile5(FeatureCreator.getFeatureList5()); + try { + Profile profile = ProfileCreator.getProfile5(FeatureCreator.getFeatureList5()); // profileManager.addProfile(profile); - PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); - policy = PolicyCreator.createPolicy(profile); - policy = pap.addPolicy(policy); - pap.activatePolicy(policy.getId()); + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); + policy = PolicyCreator.createPolicy(profile); + policy = pap.addPolicy(policy); + pap.activatePolicy(policy.getId()); + } catch (PolicyManagementException e) { + log.error("Error occurred while adding the policy", e); + Assert.fail(); + } } @Test(dependsOnMethods = ("addPolicy")) @@ -205,9 +216,15 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("addNewPolicy")) - public void getPolicies() throws PolicyManagementException { + public void getPolicies() { PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); - List policyList = policyAdministratorPoint.getPolicies(); + List policyList = null; + try { + policyList = policyAdministratorPoint.getPolicies(); + } catch (PolicyManagementException e) { + log.error("Error occurred while retrieving all the policies registered in the system", e); + Assert.fail(); + } log.debug("----------All policies---------"); @@ -228,10 +245,16 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("getPolicies")) - public void getDeviceTypeRelatedPolicy() throws PolicyManagementException { + public void getDeviceTypeRelatedPolicy() { PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); - List policyList = policyAdministratorPoint.getPoliciesOfDeviceType("android"); + List policyList = null; + try { + policyList = policyAdministratorPoint.getPoliciesOfDeviceType("android"); + } catch (PolicyManagementException e) { + log.error("Error occurred while retrieving the list of policies configured upon the platform 'android'", e); + Assert.fail(); + } log.debug("----------Device type related policy---------"); @@ -253,10 +276,17 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { @Test(dependsOnMethods = ("getDeviceTypeRelatedPolicy")) - public void getUserRelatedPolicy() throws PolicyManagementException { - + public void getUserRelatedPolicy() { + String targetUser = "Dilshan"; PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); - List policyList = policyAdministratorPoint.getPoliciesOfUser("Dilshan"); + List policyList = null; + try { + policyList = policyAdministratorPoint.getPoliciesOfUser(targetUser); + } catch (PolicyManagementException e) { + log.error("Error occurred while retrieving the list of policies assigned to the user '" + + targetUser + "'", e); + Assert.fail(); + } log.debug("----------User related policy---------"); @@ -277,10 +307,17 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("getDeviceTypeRelatedPolicy")) - public void getRoleRelatedPolicy() throws PolicyManagementException { - + public void getRoleRelatedPolicy() { + String targetRole = "Test_ROLE_01"; PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); - List policyList = policyAdministratorPoint.getPoliciesOfRole("Test_ROLE_01"); + List policyList = null; + try { + policyList = policyAdministratorPoint.getPoliciesOfRole(targetRole); + } catch (PolicyManagementException e) { + log.error("Error occurred while retrieving the list of policies defined against the role '" + + targetRole + "'", e); + Assert.fail(); + } log.debug("----------Roles related policy---------"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java index 28732dad38c..af0c3d604cf 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java @@ -19,6 +19,7 @@ package org.wso2.carbon.policy.mgt.core; +import junit.framework.Assert; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.annotations.BeforeClass; @@ -54,18 +55,37 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { } @Test - public void activatePolicies() throws PolicyManagementException, TaskException { + public void activatePolicies() { PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); - PolicyAdministratorPoint administratorPoint = policyManagerService.getPAP(); + PolicyAdministratorPoint administratorPoint = null; + try { + administratorPoint = policyManagerService.getPAP(); + } catch (PolicyManagementException e) { + log.error("Error occurred while loading the policy administration point", e); + Assert.fail(); + } - List policies = policyManagerService.getPolicies(ANDROID); + List policies = null; + try { + policies = policyManagerService.getPolicies(ANDROID); + } catch (PolicyManagementException e) { + log.error("Error occurred while retrieving the list of policies defined against the device type '" + + ANDROID + "'", e); + Assert.fail(); + } for (Policy policy : policies) { log.debug("Policy status : " + policy.getPolicyName() + " - " + policy.isActive() + " - " + policy .isUpdated() + " Policy id : " + policy.getId()); if (!policy.isActive()) { - administratorPoint.activatePolicy(policy.getId()); + try { + administratorPoint.activatePolicy(policy.getId()); + } catch (PolicyManagementException e) { + log.error("Error occurred while activating the policy, which carries the id '" + + policy.getId() + "'", e); + Assert.fail(); + } } } // This cannot be called due to task service cannot be started from the diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java index 19574abdf6e..9e92c6b7f9d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java @@ -29,60 +29,40 @@ public class ProfileCreator { public static Profile getProfile(List features) { Profile profile = new Profile(); - DeviceType deviceType = new DeviceType(); - - deviceType.setId(1); - deviceType.setName("android"); - profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features)); profile.setProfileName("Test Profile"); profile.setTenantId(MultitenantConstants.SUPER_TENANT_ID); - profile.setDeviceType(deviceType); + profile.setDeviceType("android"); return profile; } public static Profile getProfile2(List features) { Profile profile = new Profile(); - DeviceType deviceType = new DeviceType(); - - deviceType.setId(1); - deviceType.setName("android"); - profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features)); profile.setProfileName("Test Profile 2"); profile.setTenantId(MultitenantConstants.SUPER_TENANT_ID); - profile.setDeviceType(deviceType); + profile.setDeviceType("android"); return profile; } public static Profile getProfile3(List features) { Profile profile = new Profile(); - DeviceType deviceType = new DeviceType(); - - deviceType.setId(1); - deviceType.setName("android"); - profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features)); profile.setProfileName("Test Profile 3"); profile.setTenantId(MultitenantConstants.SUPER_TENANT_ID); - profile.setDeviceType(deviceType); + profile.setDeviceType("android"); return profile; } public static Profile getProfile4(List features) { Profile profile = new Profile(); - DeviceType deviceType = new DeviceType(); - - deviceType.setId(1); - deviceType.setName("android"); - profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features)); profile.setProfileName("Test Profile 4"); profile.setTenantId(MultitenantConstants.SUPER_TENANT_ID); - profile.setDeviceType(deviceType); + profile.setDeviceType("android"); return profile; } @@ -90,15 +70,10 @@ public class ProfileCreator { public static Profile getProfile5(List features) { Profile profile = new Profile(); - DeviceType deviceType = new DeviceType(); - - deviceType.setId(1); - deviceType.setName("android"); - profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features)); profile.setProfileName("Test Profile 5"); profile.setTenantId(MultitenantConstants.SUPER_TENANT_ID); - profile.setDeviceType(deviceType); + profile.setDeviceType("android"); return profile; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java index e4add057f4f..d36d069eb2d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java @@ -38,7 +38,9 @@ public class ProfileFeatureCreator { } else { profileFeature.setContent(getJSON2()); } - profileFeature.setDeviceTypeId(1); + //TODO why assigning a random number below? + //profileFeature.setDeviceTypeId(1); + profileFeature.setDeviceType("android"); profileFeature.setFeatureCode(feature.getCode()); // profileFeature.setContent("mm"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index bf0be00ccc2..efc3005e2ef 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -141,13 +141,13 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE ( ID INT NOT NULL AUTO_INCREMENT , PROFILE_NAME VARCHAR(45) NOT NULL , TENANT_ID INT NOT NULL , - DEVICE_TYPE_ID INT NOT NULL , + DEVICE_TYPE VARCHAR(20) NOT NULL , CREATED_TIME DATETIME NOT NULL , UPDATED_TIME DATETIME NOT NULL , PRIMARY KEY (ID) , CONSTRAINT DM_PROFILE_DEVICE_TYPE - FOREIGN KEY (DEVICE_TYPE_ID ) - REFERENCES DM_DEVICE_TYPE (ID ) + FOREIGN KEY (DEVICE_TYPE ) + REFERENCES DM_DEVICE_TYPE (NAME ) ON DELETE NO ACTION ON UPDATE NO ACTION ); @@ -202,7 +202,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( ID INT(11) NOT NULL , - DEVICE_TYPE_ID INT(11) NOT NULL , + DEVICE_TYPE VARCHAR(20) NOT NULL , POLICY_ID INT(11) NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_DEVICE_TYPE_POLICY @@ -211,8 +211,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT FK_DEVICE_TYPE_POLICY_DEVICE_TYPE - FOREIGN KEY (DEVICE_TYPE_ID ) - REFERENCES DM_DEVICE_TYPE (ID ) + FOREIGN KEY (DEVICE_TYPE ) + REFERENCES DM_DEVICE_TYPE (NAME) ON DELETE NO ACTION ON UPDATE NO ACTION ); @@ -225,7 +225,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( ID INT(11) NOT NULL AUTO_INCREMENT, PROFILE_ID INT(11) NOT NULL, FEATURE_CODE VARCHAR(100) NOT NULL, - DEVICE_TYPE_ID INT NOT NULL, + DEVICE_TYPE VARCHAR(20) NOT NULL, TENANT_ID INT(11) NOT NULL , CONTENT BLOB NULL DEFAULT NULL, PRIMARY KEY (ID), @@ -346,7 +346,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT ( ID INT NOT NULL AUTO_INCREMENT, POLICY_ID INT NOT NULL, - DEVICE_TYPE_ID INT NOT NULL, + DEVICE_TYPE VARCHAR(20) NOT NULL, TENANT_ID INT(11) NOT NULL, PRIMARY KEY (ID) ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index bf0be00ccc2..3e40d109bd7 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -141,13 +141,13 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE ( ID INT NOT NULL AUTO_INCREMENT , PROFILE_NAME VARCHAR(45) NOT NULL , TENANT_ID INT NOT NULL , - DEVICE_TYPE_ID INT NOT NULL , + DEVICE_TYPE VARCHAR(20) NOT NULL , CREATED_TIME DATETIME NOT NULL , UPDATED_TIME DATETIME NOT NULL , PRIMARY KEY (ID) , CONSTRAINT DM_PROFILE_DEVICE_TYPE - FOREIGN KEY (DEVICE_TYPE_ID ) - REFERENCES DM_DEVICE_TYPE (ID ) + FOREIGN KEY (DEVICE_TYPE ) + REFERENCES DM_DEVICE_TYPE (NAME) ON DELETE NO ACTION ON UPDATE NO ACTION ); @@ -202,7 +202,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( ID INT(11) NOT NULL , - DEVICE_TYPE_ID INT(11) NOT NULL , + DEVICE_TYPE VARCHAR(20) NOT NULL , POLICY_ID INT(11) NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_DEVICE_TYPE_POLICY @@ -211,8 +211,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT FK_DEVICE_TYPE_POLICY_DEVICE_TYPE - FOREIGN KEY (DEVICE_TYPE_ID ) - REFERENCES DM_DEVICE_TYPE (ID ) + FOREIGN KEY (DEVICE_TYPE ) + REFERENCES DM_DEVICE_TYPE (NAME) ON DELETE NO ACTION ON UPDATE NO ACTION ); @@ -225,7 +225,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( ID INT(11) NOT NULL AUTO_INCREMENT, PROFILE_ID INT(11) NOT NULL, FEATURE_CODE VARCHAR(100) NOT NULL, - DEVICE_TYPE_ID INT NOT NULL, + DEVICE_TYPE VARCHAR(20) NOT NULL, TENANT_ID INT(11) NOT NULL , CONTENT BLOB NULL DEFAULT NULL, PRIMARY KEY (ID), @@ -346,7 +346,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT ( ID INT NOT NULL AUTO_INCREMENT, POLICY_ID INT NOT NULL, - DEVICE_TYPE_ID INT NOT NULL, + DEVICE_TYPE VARCHAR(20) NOT NULL, TENANT_ID INT(11) NOT NULL, PRIMARY KEY (ID) ); From ce2a8019343806449255f2a2674890e6a86c825a Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 7 Jul 2016 14:10:03 +0530 Subject: [PATCH 4/4] Fixing https://wso2.org/jira/browse/EMM-1448 which fixes issues while persisting certificates when PostgreSQL is used as the underlying certificate management repository database --- .../mgt/core/dao/impl/GenericCertificateDAOImpl.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java index 3c3278f89f0..9d1129d0676 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java @@ -67,10 +67,9 @@ public class GenericCertificateDAOImpl implements CertificateDAO { serialNumber = String.valueOf(certificate.getCertificate().getSerialNumber()); } byte[] bytes = Serializer.serialize(certificate.getCertificate()); - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); stmt.setString(1, serialNumber); - stmt.setObject(2, byteArrayInputStream); + stmt.setBytes(2, bytes); stmt.setInt(3, certificate.getTenantId()); stmt.setString(4, username); stmt.addBatch(); @@ -102,7 +101,7 @@ public class GenericCertificateDAOImpl implements CertificateDAO { stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); - while (resultSet.next()) { + if (resultSet.next()) { certificateResponse = new CertificateResponse(); byte [] certificateBytes = resultSet.getBytes("CERTIFICATE"); certificateResponse.setCertificate(certificateBytes); @@ -110,7 +109,6 @@ public class GenericCertificateDAOImpl implements CertificateDAO { certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setUsername(resultSet.getString("USERNAME")); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); - break; } } catch (SQLException e) { String errorMsg =