Search in sources :

Example 11 with JsonSerializableUtility

use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.

the class JsonSerializableUtilityTest method testIntegerArray.

@Test
public void testIntegerArray() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    JsonSerializableTestObject o1 = new JsonSerializableTestObject();
    JsonSerializableTestObject o2 = new JsonSerializableTestObject();
    JsonSerializableUtility util = new JsonSerializableUtility();
    Map<String, Object> changes = util.findChanges(o1, o2);
    if (changes.size() != 0)
        Assert.fail("Integer array should not have changed");
    o1.setIntegerArray(new Integer[] { new Integer(0), new Integer(1) });
    o2.setIntegerArray(new Integer[] { new Integer(2), new Integer(3) });
    changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("Integer array should have changed");
    Object[] changed = (Object[]) changes.get("integerArray");
    Assert.assertNotNull("Integer array should have changed", changed);
    Assert.assertEquals((int) 2, changed.length);
    Assert.assertEquals(2, changed[0]);
    Assert.assertEquals(3, changed[1]);
}
Also used : JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) Test(org.junit.Test)

Example 12 with JsonSerializableUtility

use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.

the class JsonSerializableUtilityTest method testAnnotatedMapOrder.

@Test
public void testAnnotatedMapOrder() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    Map<String, Object> annotatedMap1 = new LinkedHashMap<>();
    annotatedMap1.put("int", new Integer(1));
    annotatedMap1.put("string", new String("one"));
    Map<String, Object> annotatedMap2 = new LinkedHashMap<>();
    annotatedMap2.put("string", new String("one"));
    annotatedMap2.put("int", new Integer(1));
    JsonSerializableTestObject o1 = new JsonSerializableTestObject();
    o1.setAnnotatedMap(annotatedMap1);
    JsonSerializableTestObject o2 = new JsonSerializableTestObject();
    o2.setAnnotatedMap(annotatedMap2);
    JsonSerializableUtility util = new JsonSerializableUtility();
    Map<String, Object> changes = util.findChanges(o1, o2);
    if (changes.size() != 0)
        Assert.fail("Map order should not matter");
}
Also used : JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 13 with JsonSerializableUtility

use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.

the class JsonSerializableUtilityTest method testInnerEnum.

@Test
public void testInnerEnum() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    JsonSerializableTestObject o1 = new JsonSerializableTestObject();
    o1.setWriterInnerEnum(JsonSerializableTestEnum.ONE);
    JsonSerializableTestObject o2 = new JsonSerializableTestObject();
    o2.setWriterInnerEnum(JsonSerializableTestEnum.TWO);
    JsonSerializableUtility util = new JsonSerializableUtility();
    Map<String, Object> changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("Should be a change of innerEnum");
    if (changes.get("innerEnum") != JsonSerializableTestEnum.TWO)
        Assert.fail("Should be a change of innerEnum to TWO");
}
Also used : JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) Test(org.junit.Test)

Example 14 with JsonSerializableUtility

use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.

the class JsonSerializableUtilityTest method testAnnotatedList.

@Test
public void testAnnotatedList() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    List<Object> annotatedList1 = new ArrayList<>();
    annotatedList1.add(new Integer(1));
    annotatedList1.add(new Integer(2));
    List<Object> annotatedList2 = new ArrayList<>();
    annotatedList2.add(new Integer(2));
    annotatedList2.add(new Integer(3));
    JsonSerializableTestObject o1 = new JsonSerializableTestObject();
    o1.setAnnotatedList(annotatedList1);
    JsonSerializableTestObject o2 = new JsonSerializableTestObject();
    o2.setAnnotatedList(annotatedList2);
    JsonSerializableUtility util = new JsonSerializableUtility();
    Map<String, Object> changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("List should have changed");
    @SuppressWarnings("unchecked") List<Object> changedList = (List<Object>) changes.get("annotatedList");
    Assert.assertNotNull("List should have changed", changedList);
    Assert.assertEquals((int) 2, changedList.size());
    Assert.assertEquals((int) 2, changedList.get(0));
    Assert.assertEquals((int) 3, changedList.get(1));
}
Also used : ArrayList(java.util.ArrayList) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 15 with JsonSerializableUtility

use of com.serotonin.m2m2.util.JsonSerializableUtility in project ma-core-public by infiniteautomation.

the class JsonSerializableUtilityTest method testWriterList.

@Test
public void testWriterList() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    List<Object> writerList1 = new ArrayList<>();
    writerList1.add(new Integer(1));
    writerList1.add(new Integer(2));
    List<Object> writerList2 = new ArrayList<>();
    writerList2.add(new Integer(2));
    writerList2.add(new Integer(3));
    JsonSerializableTestObject o1 = new JsonSerializableTestObject();
    o1.setWriterList(writerList1);
    JsonSerializableTestObject o2 = new JsonSerializableTestObject();
    o2.setWriterList(writerList2);
    JsonSerializableUtility util = new JsonSerializableUtility();
    Map<String, Object> changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("List should have changed");
    @SuppressWarnings("unchecked") List<Object> changedList = (List<Object>) changes.get("writerList");
    Assert.assertNotNull("List should have changed", changedList);
    Assert.assertEquals((int) 2, changedList.size());
    Assert.assertEquals((int) 2, changedList.get(0));
    Assert.assertEquals((int) 3, changedList.get(1));
}
Also used : ArrayList(java.util.ArrayList) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

JsonSerializableUtility (com.serotonin.m2m2.util.JsonSerializableUtility)19 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)7 List (java.util.List)7 HashMap (java.util.HashMap)4 JsonException (com.serotonin.json.JsonException)3 JsonObject (com.serotonin.json.type.JsonObject)3 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 LinkedHashMap (java.util.LinkedHashMap)2 DayOfWeek (java.time.DayOfWeek)1 Map (java.util.Map)1