Fixing code formatting issues in SearchManagementTestCases.

revert-70aa11f8
Menaka Jayawardena 7 years ago
parent a9f2a0a228
commit 46555cfe3a

@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.core.search;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.powermock.api.mockito.PowerMockito;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; 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.TestDeviceManagementService;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; 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.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.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import org.wso2.carbon.device.mgt.core.search.mgt.InvalidOperatorException; import org.wso2.carbon.device.mgt.core.search.mgt.InvalidOperatorException;
@ -43,18 +41,13 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import javax.sql.DataSource;
import java.lang.reflect.Field; 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.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
* This class holds unit test cases for org.wso2.carbon.device.mgt.core.search.mgt.impl.ProcessorImpl * 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 DeviceAccessAuthorizationService deviceAccessAuthorizationService;
@ -67,7 +60,6 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{
public void init() throws Exception { public void init() throws Exception {
deviceAccessAuthorizationService = DeviceManagementDataHolder.getInstance() deviceAccessAuthorizationService = DeviceManagementDataHolder.getInstance()
.getDeviceAccessAuthorizationService(); .getDeviceAccessAuthorizationService();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
deviceIdentifiers.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE)); deviceIdentifiers.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE));
} }
@ -76,7 +68,6 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService);
deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE,
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME));
List<Device> devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers); List<Device> devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers);
for (Device device : devices) { for (Device device : devices) {
device.setDeviceInfo(Utils.getDeviceInfo()); device.setDeviceInfo(Utils.getDeviceInfo());
@ -91,27 +82,23 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{
} }
@Test(description = "Test the Search Processor") @Test(description = "Test the Search Processor")
public void testWithNoDeviceAccessAuthorization() throws Exception { public void testWithNoDeviceAccessAuthorization() throws NoSuchFieldException, IllegalAccessException,
SearchMgtException {
SearchContext context = new SearchContext(); SearchContext context = new SearchContext();
List<Condition> conditions = new ArrayList<>(); List<Condition> conditions = new ArrayList<>();
Condition cond = new Condition(); Condition cond = new Condition();
cond.setKey("batteryLevel"); cond.setKey("batteryLevel");
cond.setOperator("="); cond.setOperator("=");
cond.setValue("40"); cond.setValue("40");
cond.setState(Condition.State.AND); cond.setState(Condition.State.AND);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
ProcessorImpl processor = new ProcessorImpl(); ProcessorImpl processor = new ProcessorImpl();
Field deviceAccessAuthorizationServiceField = ProcessorImpl.class.getDeclaredField Field deviceAccessAuthorizationServiceField = ProcessorImpl.class.getDeclaredField
("deviceAccessAuthorizationService"); ("deviceAccessAuthorizationService");
deviceAccessAuthorizationServiceField.setAccessible(true); deviceAccessAuthorizationServiceField.setAccessible(true);
deviceAccessAuthorizationServiceField.set(processor, null); deviceAccessAuthorizationServiceField.set(processor, null);
List<Device> searchedDevices = processor.execute(context); List<Device> searchedDevices = processor.execute(context);
Assert.assertEquals(0, searchedDevices.size()); Assert.assertEquals(0, searchedDevices.size());
} }
@ -120,9 +107,7 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{
public void testInvalidState() throws SearchMgtException { public void testInvalidState() throws SearchMgtException {
SearchContext context = new SearchContext(); SearchContext context = new SearchContext();
List<Condition> conditions = new ArrayList<>(); List<Condition> conditions = new ArrayList<>();
ChangeEnumValues.addEnum(Condition.State.class, "BLA"); ChangeEnumValues.addEnum(Condition.State.class, "BLA");
Condition.State state = Condition.State.valueOf("BLA"); Condition.State state = Condition.State.valueOf("BLA");
Condition cond = new Condition(); Condition cond = new Condition();
@ -147,7 +132,6 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{
conditions.add(cond3); conditions.add(cond3);
context.setConditions(conditions); context.setConditions(conditions);
ProcessorImpl processor = new ProcessorImpl(); ProcessorImpl processor = new ProcessorImpl();
try { try {
processor.execute(context); processor.execute(context);
@ -161,13 +145,11 @@ public class ProcessorImplTest extends BaseDeviceManagementTest{
@Test(description = "Test when Device Access Authorization is null", expectedExceptions = {IllegalStateException @Test(description = "Test when Device Access Authorization is null", expectedExceptions = {IllegalStateException
.class}, dependsOnMethods = {"testWithNoDeviceAccessAuthorization", "testInvalidState"}) .class}, dependsOnMethods = {"testWithNoDeviceAccessAuthorization", "testInvalidState"})
public void testProcessorInitializationError() throws ClassNotFoundException, NoSuchMethodException, public void testProcessorInitializationError() throws ClassNotFoundException, NoSuchMethodException,
NoSuchFieldException, NoSuchFieldException, IllegalAccessException, SearchMgtException {
IllegalAccessException, SearchMgtException {
DeviceManagementDataHolder deviceManagementDataHolder = DeviceManagementDataHolder.getInstance(); DeviceManagementDataHolder deviceManagementDataHolder = DeviceManagementDataHolder.getInstance();
Field field = DeviceManagementDataHolder.class.getDeclaredField("deviceAccessAuthorizationService"); Field field = DeviceManagementDataHolder.class.getDeclaredField("deviceAccessAuthorizationService");
field.setAccessible(true); field.setAccessible(true);
field.set(deviceManagementDataHolder, null); field.set(deviceManagementDataHolder, null);
ProcessorImpl processor = new ProcessorImpl(); ProcessorImpl processor = new ProcessorImpl();
processor.execute(null); processor.execute(null);
} }

@ -72,8 +72,8 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE,
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME));
DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl(); DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl();
List<Device> devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers); List<Device> devices = TestDataHolder.generateDummyDeviceData(deviceIdentifiers);
for (Device device : devices) { for (Device device : devices) {
deviceMgtService.enrollDevice(device); deviceMgtService.enrollDevice(device);
} }
@ -90,6 +90,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
} }
List<Device> returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE, true); List<Device> returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE, true);
for (Device device : returnedDevices) { for (Device device : returnedDevices) {
if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) { if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) {
throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!"); throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!");
@ -108,9 +109,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
cond.setValue("40"); cond.setValue("40");
cond.setState(Condition.State.OR); cond.setState(Condition.State.OR);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices != null); Assert.assertTrue(devices != null);
@ -127,9 +126,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
cond.setValue("Karandeniya"); cond.setValue("Karandeniya");
cond.setState(Condition.State.AND); cond.setState(Condition.State.AND);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices != null); Assert.assertTrue(devices != null);
@ -146,9 +143,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
cond.setValue("Colombo"); cond.setValue("Colombo");
cond.setState(Condition.State.AND); cond.setState(Condition.State.AND);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices.size() == 0); Assert.assertTrue(devices.size() == 0);
@ -165,12 +160,9 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
cond.setValue("SM-T520"); cond.setValue("SM-T520");
cond.setState(Condition.State.AND); cond.setState(Condition.State.AND);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices != null); Assert.assertTrue(devices != null);
} }
@ -185,12 +177,9 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
cond.setValue("3.56"); cond.setValue("3.56");
cond.setState(Condition.State.AND); cond.setState(Condition.State.AND);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices != null); Assert.assertTrue(devices != null);
} }
@ -205,12 +194,9 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
cond.setValue("SM-T520"); cond.setValue("SM-T520");
cond.setState(Condition.State.OR); cond.setState(Condition.State.OR);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices != null); Assert.assertTrue(devices != null);
} }
@ -232,9 +218,7 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
cond.setValue("bbb"); cond.setValue("bbb");
cond.setState(Condition.State.OR); cond.setState(Condition.State.OR);
conditions.add(cond); conditions.add(cond);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
try { try {
service.search(context); service.search(context);
@ -293,7 +277,6 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
conditions.add(cond6); conditions.add(cond6);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices != null); Assert.assertTrue(devices != null);
@ -333,7 +316,6 @@ public class SearchManagementServiceTest extends BaseDeviceManagementTest {
conditions.add(condition4); conditions.add(condition4);
context.setConditions(conditions); context.setConditions(conditions);
SearchManagerService service = new SearchManagerServiceImpl(); SearchManagerService service = new SearchManagerServiceImpl();
List<Device> devices = service.search(context); List<Device> devices = service.search(context);
Assert.assertTrue(devices != null); Assert.assertTrue(devices != null);

@ -66,7 +66,6 @@ public class SearchMgtUtilTest {
public void testConvertStringToList() { public void testConvertStringToList() {
List<String> stringList = Utils.convertStringToList("some string"); List<String> stringList = Utils.convertStringToList("some string");
List<String> expected = this.getStringList(); List<String> expected = this.getStringList();
Assert.assertEquals(stringList, expected); Assert.assertEquals(stringList, expected);
} }

@ -41,16 +41,12 @@ public class ChangeEnumValues {
private static void setFailSafeFieldValue(Field field, Object target, Object value) throws NoSuchFieldException, private static void setFailSafeFieldValue(Field field, Object target, Object value) throws NoSuchFieldException,
IllegalAccessException { IllegalAccessException {
field.setAccessible(true); field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers"); Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true); modifiersField.setAccessible(true);
int modifiers = modifiersField.getInt(field); int modifiers = modifiersField.getInt(field);
modifiers &= ~Modifier.FINAL; modifiers &= ~Modifier.FINAL;
modifiersField.setInt(field, modifiers); modifiersField.setInt(field, modifiers);
FieldAccessor fa = reflectionFactory.newFieldAccessor(field, false); FieldAccessor fa = reflectionFactory.newFieldAccessor(field, false);
fa.set(target, value); fa.set(target, value);
} }
@ -115,15 +111,10 @@ public class ChangeEnumValues {
try { try {
T[] previousValues = (T[]) valuesField.get(enumType); T[] previousValues = (T[]) valuesField.get(enumType);
List<T> values = new ArrayList<T>(Arrays.asList(previousValues)); List<T> values = new ArrayList<T>(Arrays.asList(previousValues));
T newValue = (T) makeEnum(enumType, enumName, values.size(), new Class<?>[]{}, new Object[]{}); T newValue = (T) makeEnum(enumType, enumName, values.size(), new Class<?>[]{}, new Object[]{});
values.add(newValue); values.add(newValue);
setFailSafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0))); setFailSafeFieldValue(valuesField, null, values.toArray((T[]) Array.newInstance(enumType, 0)));
cleanEnumCache(enumType); cleanEnumCache(enumType);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e);
} }

@ -51,7 +51,6 @@ public class Utils {
deviceInfo.setLocation(getSampleDeviceLocation()); deviceInfo.setLocation(getSampleDeviceLocation());
Map<String, String> propertyMap = new HashMap<>(); Map<String, String> propertyMap = new HashMap<>();
propertyMap.put("BATTERY_VOLTAGE", "40"); propertyMap.put("BATTERY_VOLTAGE", "40");
propertyMap.put("BATTERY_HEALTH", "Good"); propertyMap.put("BATTERY_HEALTH", "Good");
propertyMap.put("BATTERY_STATUS", "SWElLED"); propertyMap.put("BATTERY_STATUS", "SWElLED");
@ -61,14 +60,11 @@ public class Utils {
propertyMap.put("CPU_IRQ", "1"); propertyMap.put("CPU_IRQ", "1");
propertyMap.put("IMEI", "e6f236ac82537a8e"); propertyMap.put("IMEI", "e6f236ac82537a8e");
propertyMap.put("IMSI", "432659632123654845"); propertyMap.put("IMSI", "432659632123654845");
deviceInfo.setDeviceDetailsMap(propertyMap); deviceInfo.setDeviceDetailsMap(propertyMap);
return deviceInfo; return deviceInfo;
} }
public static DeviceLocation getSampleDeviceLocation(){
private static DeviceLocation getSampleDeviceLocation(){
DeviceLocation deviceLocation = new DeviceLocation(); DeviceLocation deviceLocation = new DeviceLocation();
deviceLocation.setDeviceIdentifier(Utils.getDeviceIdentifier()); deviceLocation.setDeviceIdentifier(Utils.getDeviceIdentifier());
deviceLocation.setLatitude(76.2422); deviceLocation.setLatitude(76.2422);
@ -80,7 +76,6 @@ public class Utils {
deviceLocation.setZip("80360"); deviceLocation.setZip("80360");
deviceLocation.setCountry("Sri Lanka"); deviceLocation.setCountry("Sri Lanka");
deviceLocation.setDeviceId(1); deviceLocation.setDeviceId(1);
return deviceLocation; return deviceLocation;
} }
@ -88,11 +83,6 @@ public class Utils {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(TestDataHolder.TEST_DEVICE_TYPE); deviceIdentifier.setType(TestDataHolder.TEST_DEVICE_TYPE);
deviceIdentifier.setId("12345"); deviceIdentifier.setId("12345");
return deviceIdentifier; return deviceIdentifier;
} }
} }

Loading…
Cancel
Save