Search in sources :

Example 1 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testIntArrayOrder.

@Test
public void testIntArrayOrder() 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("Int array should not have changed");
    o1.setIntArray(new int[] { 3, 2, 1 });
    o2.setIntArray(new int[] { 1, 2, 3 });
    changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("Int array should have changed");
    int[] changed = (int[]) changes.get("intArray");
    Assert.assertNotNull("Int array should have changed", changed);
    Assert.assertEquals((int) 3, changed.length);
    Assert.assertEquals(1, changed[0]);
    Assert.assertEquals(2, changed[1]);
    Assert.assertEquals(3, changed[2]);
}
Also used : JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) Test(org.junit.Test)

Example 2 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testwriterListLength.

// TODO Test writerMap
@Test
public void testwriterListLength() 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(1));
    annotatedList2.add(new Integer(2));
    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);
    // No Changes
    if (changes.size() != 0)
        Assert.fail("List should not have changed");
    // Modify the List
    annotatedList2.remove(1);
    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) 1, changedList.size());
    Assert.assertEquals((int) 1, changedList.get(0));
}
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 3 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testWriterListOrder.

@Test
public void testWriterListOrder() 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(1));
    writerList2.add(new Integer(2));
    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);
    // No Changes
    if (changes.size() != 0)
        Assert.fail("List should not have changed");
    // Modify the List
    writerList2.clear();
    writerList2.add(new Integer(2));
    writerList2.add(new Integer(1));
    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) 1, 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 4 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testObjectArray.

@Test
public void testObjectArray() 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("Object array should not have changed");
    o1.setObjectArray(new Object[] { new Integer(0), new Integer(1) });
    o2.setObjectArray(new Object[] { new Integer(2), new Integer(3) });
    changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("Object array should have changed");
    Object[] changed = (Object[]) changes.get("objectArray");
    Assert.assertNotNull("Object 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 5 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testAnnotatedMap.

@Test
public void testAnnotatedMap() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    Map<String, Object> annotatedMap1 = new HashMap<>();
    annotatedMap1.put("int", new Integer(1));
    annotatedMap1.put("string", new String("one"));
    Map<String, Object> annotatedMap2 = new HashMap<>();
    annotatedMap2.put("int", new Integer(1));
    annotatedMap2.put("string", new String("one"));
    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("annotatedMap should not have changed.");
    // Make change
    annotatedMap2.put("string", new String("two"));
    annotatedMap2.put("int", new Integer(2));
    changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("annotatedMap should have changed");
    @SuppressWarnings("unchecked") Map<String, Object> changedMap = (Map<String, Object>) changes.get("annotatedMap");
    Assert.assertNotNull("annotatedMap change should have found", changedMap);
    Assert.assertEquals("two", changedMap.get("string"));
    Assert.assertEquals((int) 2, (int) changedMap.get("int"));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) 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