From 24e69d5492b1be2c76c26e754769c6082f527650 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 21:06:30 +0530 Subject: [PATCH] Fix test cases related to hierarchical group changes --- .../impl/GroupManagementServiceImplTest.java | 46 +++++++++++++------ ...ManagementProviderServiceNegativeTest.java | 21 ++++++++- .../GroupManagementProviderServiceTest.java | 23 ++++++++-- .../src/test/resources/sql/h2.sql | 1 + .../src/test/resources/sql/CreateH2TestDB.sql | 3 +- 5 files changed, 73 insertions(+), 21 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java index 20136b172d7..2d430c09f6d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java @@ -15,11 +15,26 @@ * specific language governing permissions and limitations * under the License. * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.jaxrs.service.impl; -import org.mockito.Mock; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -130,13 +145,14 @@ public class GroupManagementServiceImplTest { .toReturn(groupManagementProviderService); PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext")) .toReturn(context); - Mockito.doReturn(2).when(groupManagementProviderService).getGroupCount(Mockito.anyString()); + Mockito.doReturn(2).when(groupManagementProviderService) + .getGroupCount(Mockito.anyString(), Mockito.anyString()); Response response = groupManagementService.getGroupCount(); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetGroupCount request failed with valid parameters"); Mockito.reset(groupManagementProviderService); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService) - .getGroupCount(Mockito.anyString()); + .getGroupCount(Mockito.anyString(), Mockito.anyString()); response = groupManagementService.getGroupCount(); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "GetGroupCount request succeeded with in-valid parameters"); @@ -174,16 +190,16 @@ public class GroupManagementServiceImplTest { public void testGetGroup() throws GroupManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); - Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false); - Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, false); - Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3, false); - Response response = groupManagementService.getGroup(1, false); + Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false, 1); + Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, false, 1); + Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3, false, 1); + Response response = groupManagementService.getGroup(1, false, 1); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "getGroup request failed for a request with valid parameters"); - response = groupManagementService.getGroup(2, false); + response = groupManagementService.getGroup(2, false, 1); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "getGroup request returned a group for a non-existing group"); - response = groupManagementService.getGroup(3, false); + response = groupManagementService.getGroup(3, false, 1); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "getGroup request returned a group for a in-valid request"); } @@ -216,16 +232,16 @@ public class GroupManagementServiceImplTest { public void testDeleteGroup() throws GroupManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); - Mockito.doReturn(true).when(groupManagementProviderService).deleteGroup(1); - Mockito.doReturn(false).when(groupManagementProviderService).deleteGroup(2); - Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).deleteGroup(3); - Response response = groupManagementService.deleteGroup(1); + Mockito.doReturn(true).when(groupManagementProviderService).deleteGroup(1, false); + Mockito.doReturn(false).when(groupManagementProviderService).deleteGroup(2, false); + Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).deleteGroup(3, false); + Response response = groupManagementService.deleteGroup(1, false); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "delete group request failed for a request with valid parameters"); - response = groupManagementService.deleteGroup(2); + response = groupManagementService.deleteGroup(2, false); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Non-existing group was successfully deleted"); - response = groupManagementService.deleteGroup(3); + response = groupManagementService.deleteGroup(3, false); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Deletion succeeded with an erroneous condition."); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java index fd341650310..ec8004e475c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java @@ -8,6 +8,23 @@ * * 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. + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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 @@ -103,7 +120,7 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Error occurred " + "while retrieving group count of user.*") public void testGetGroupCountWithUserName() throws GroupManagementException { - groupManagementProviderService.getGroupCount("test"); + groupManagementProviderService.getGroupCount("test", null); } @@ -145,7 +162,7 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Received empty " + "user name for getGroupCount.*") public void testGetGroupCountWithUserName2() throws GroupManagementException { - groupManagementProviderService.getGroupCount(null); + groupManagementProviderService.getGroupCount(null, null); } @Test(description = "This method tests getGroups method under negative circumstances", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java index 4ffb675fe7d..b645eac9a0d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.service; @@ -119,13 +136,13 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup")) public void deleteGroup() throws GroupManagementException { DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup4().getName(), false); - Assert.assertTrue(groupManagementProviderService.deleteGroup(deviceGroup.getGroupId())); + Assert.assertTrue(groupManagementProviderService.deleteGroup(deviceGroup.getGroupId(), false)); } @Test(dependsOnMethods = ("createGroup")) public void deleteGroupNotExists() throws GroupManagementException { - groupManagementProviderService.deleteGroup(8); + groupManagementProviderService.deleteGroup(8, false); } @@ -190,7 +207,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup")) public void getGroupCountByUsername(String username) throws GroupManagementException { - int x = groupManagementProviderService.getGroupCount(username); + int x = groupManagementProviderService.getGroupCount(username, null); Assert.assertNotNull(x); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index a380bf52048..2fc9fb4b29a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( STATUS VARCHAR(50) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, OWNER VARCHAR(45) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); 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 e53054a6a00..bce9121e734 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 @@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( DATE_OF_CREATE BIGINT DEFAULT NULL, DATE_OF_LAST_UPDATE BIGINT DEFAULT NULL, OWNER VARCHAR(45) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); @@ -607,4 +608,4 @@ DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_DEVICE_DETAIL.DEVICE_ID ORDER BY TENANT_ID, DEVICE_ID; --- END OF DASHBOARD RELATED VIEWS -- \ No newline at end of file +-- END OF DASHBOARD RELATED VIEWS --