From 46555cfe3a2545ab54ced0a87b01c8aef442e607 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Tue, 24 Oct 2017 17:37:56 +0530 Subject: [PATCH] Fixing code formatting issues in SearchManagementTestCases. --- .../mgt/core/search/ProcessorImplTest.java | 28 ++++--------------- .../search/SearchManagementServiceTest.java | 22 ++------------- .../mgt/core/search/SearchMgtUtilTest.java | 1 - .../core/search/util/ChangeEnumValues.java | 9 ------ .../device/mgt/core/search/util/Utils.java | 12 +------- 5 files changed, 8 insertions(+), 64 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java index 4e5f26a548..92f3eac37d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/ProcessorImplTest.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.core.search; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.powermock.api.mockito.PowerMockito; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -31,7 +30,6 @@ import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.search.mgt.InvalidOperatorException; @@ -43,19 +41,14 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import javax.sql.DataSource; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** * This class holds unit test cases for org.wso2.carbon.device.mgt.core.search.mgt.impl.ProcessorImpl - * */ -public class ProcessorImplTest extends BaseDeviceManagementTest{ + */ +public class ProcessorImplTest extends BaseDeviceManagementTest { private DeviceAccessAuthorizationService deviceAccessAuthorizationService; private static final Log log = LogFactory.getLog(SearchManagementServiceTest.class); @@ -67,7 +60,6 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ public void init() throws Exception { deviceAccessAuthorizationService = DeviceManagementDataHolder.getInstance() .getDeviceAccessAuthorizationService(); - for (int i = 0; i < 5; i++) { deviceIdentifiers.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); } @@ -76,7 +68,6 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); - List devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers); for (Device device : devices) { device.setDeviceInfo(Utils.getDeviceInfo()); @@ -91,27 +82,23 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ } @Test(description = "Test the Search Processor") - public void testWithNoDeviceAccessAuthorization() throws Exception { + public void testWithNoDeviceAccessAuthorization() throws NoSuchFieldException, IllegalAccessException, + SearchMgtException { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); - Condition cond = new Condition(); cond.setKey("batteryLevel"); cond.setOperator("="); cond.setValue("40"); cond.setState(Condition.State.AND); conditions.add(cond); - context.setConditions(conditions); - ProcessorImpl processor = new ProcessorImpl(); Field deviceAccessAuthorizationServiceField = ProcessorImpl.class.getDeclaredField ("deviceAccessAuthorizationService"); deviceAccessAuthorizationServiceField.setAccessible(true); deviceAccessAuthorizationServiceField.set(processor, null); - List searchedDevices = processor.execute(context); - Assert.assertEquals(0, searchedDevices.size()); } @@ -120,9 +107,7 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ public void testInvalidState() throws SearchMgtException { SearchContext context = new SearchContext(); List conditions = new ArrayList<>(); - ChangeEnumValues.addEnum(Condition.State.class, "BLA"); - Condition.State state = Condition.State.valueOf("BLA"); Condition cond = new Condition(); @@ -147,7 +132,6 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ conditions.add(cond3); context.setConditions(conditions); - ProcessorImpl processor = new ProcessorImpl(); try { processor.execute(context); @@ -161,13 +145,11 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{ @Test(description = "Test when Device Access Authorization is null", expectedExceptions = {IllegalStateException .class}, dependsOnMethods = {"testWithNoDeviceAccessAuthorization", "testInvalidState"}) public void testProcessorInitializationError() throws ClassNotFoundException, NoSuchMethodException, - NoSuchFieldException, - IllegalAccessException, SearchMgtException { + NoSuchFieldException, IllegalAccessException, SearchMgtException { DeviceManagementDataHolder deviceManagementDataHolder = DeviceManagementDataHolder.getInstance(); Field field = DeviceManagementDataHolder.class.getDeclaredField("deviceAccessAuthorizationService"); field.setAccessible(true); field.set(deviceManagementDataHolder, null); - ProcessorImpl processor = new ProcessorImpl(); processor.execute(null); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java index 49641d16ab..9a2099b6cc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchManagementServiceTest.java @@ -72,8 +72,8 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl(); - List devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers); + for (Device device : devices) { deviceMgtService.enrollDevice(device); } @@ -90,6 +90,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { } List returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE, true); + for (Device device : returnedDevices) { if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); @@ -108,9 +109,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { cond.setValue("40"); cond.setState(Condition.State.OR); conditions.add(cond); - context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); Assert.assertTrue(devices != null); @@ -127,9 +126,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { cond.setValue("Karandeniya"); cond.setState(Condition.State.AND); conditions.add(cond); - context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); Assert.assertTrue(devices != null); @@ -146,9 +143,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { cond.setValue("Colombo"); cond.setState(Condition.State.AND); conditions.add(cond); - context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); Assert.assertTrue(devices.size() == 0); @@ -165,12 +160,9 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { cond.setValue("SM-T520"); cond.setState(Condition.State.AND); conditions.add(cond); - context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); - Assert.assertTrue(devices != null); } @@ -185,12 +177,9 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { cond.setValue("3.56"); cond.setState(Condition.State.AND); conditions.add(cond); - context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); - Assert.assertTrue(devices != null); } @@ -205,12 +194,9 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { cond.setValue("SM-T520"); cond.setState(Condition.State.OR); conditions.add(cond); - context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); - Assert.assertTrue(devices != null); } @@ -232,9 +218,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { cond.setValue("bbb"); cond.setState(Condition.State.OR); conditions.add(cond); - context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); try { service.search(context); @@ -293,7 +277,6 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { conditions.add(cond6); context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); Assert.assertTrue(devices != null); @@ -333,7 +316,6 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest { conditions.add(condition4); context.setConditions(conditions); - SearchManagerService service = new SearchManagerServiceImpl(); List devices = service.search(context); Assert.assertTrue(devices != null); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java index df99f82bff..7a5ba8836f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/SearchMgtUtilTest.java @@ -66,7 +66,6 @@ public class SearchMgtUtilTest { public void testConvertStringToList() { List stringList = Utils.convertStringToList("some string"); List expected = this.getStringList(); - Assert.assertEquals(stringList, expected); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java index 2d0163e7c3..b646ec75d0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/ChangeEnumValues.java @@ -41,16 +41,12 @@ public class ChangeEnumValues { private static void setFailSafeFieldValue(Field field, Object target, Object value) throws NoSuchFieldException, IllegalAccessException { - field.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); - modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); - FieldAccessor fa = reflectionFactory.newFieldAccessor(field, false); fa.set(target, value); } @@ -115,15 +111,10 @@ public class ChangeEnumValues { try { T[] previousValues = (T[]) valuesField.get(enumType); List values = new ArrayList(Arrays.asList(previousValues)); - T newValue = (T) makeEnum(enumType, enumName, values.size(), new Class[]{}, new Object[]{}); - values.add(newValue); - setFailSafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0))); - cleanEnumCache(enumType); - } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java index ea46635116..f6e7ce7a67 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/search/util/Utils.java @@ -51,7 +51,6 @@ public class Utils { deviceInfo.setLocation(getSampleDeviceLocation()); Map propertyMap = new HashMap<>(); - propertyMap.put("BATTERY_VOLTAGE", "40"); propertyMap.put("BATTERY_HEALTH", "Good"); propertyMap.put("BATTERY_STATUS", "SWElLED"); @@ -61,14 +60,11 @@ public class Utils { propertyMap.put("CPU_IRQ", "1"); propertyMap.put("IMEI", "e6f236ac82537a8e"); propertyMap.put("IMSI", "432659632123654845"); - deviceInfo.setDeviceDetailsMap(propertyMap); - return deviceInfo; } - - private static DeviceLocation getSampleDeviceLocation(){ + public static DeviceLocation getSampleDeviceLocation(){ DeviceLocation deviceLocation = new DeviceLocation(); deviceLocation.setDeviceIdentifier(Utils.getDeviceIdentifier()); deviceLocation.setLatitude(76.2422); @@ -80,7 +76,6 @@ public class Utils { deviceLocation.setZip("80360"); deviceLocation.setCountry("Sri Lanka"); deviceLocation.setDeviceId(1); - return deviceLocation; } @@ -88,11 +83,6 @@ public class Utils { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(TestDataHolder.TEST_DEVICE_TYPE); deviceIdentifier.setId("12345"); - return deviceIdentifier; } - } - - -