Search in sources :

Example 6 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testWriterListLength.

@Test
public void testWriterListLength() 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.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("writerList");
    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 7 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testRollupEnum.

@Test
public void testRollupEnum() 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("Day Of Week should not have changed");
    o2.setDayOfWeekEnum(DayOfWeek.FRIDAY);
    changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("Day Of Week should have changed");
    DayOfWeek dow = (DayOfWeek) changes.get("dayOfWeekEnum");
    Assert.assertEquals(DayOfWeek.FRIDAY, dow);
}
Also used : DayOfWeek(java.time.DayOfWeek) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) Test(org.junit.Test)

Example 8 with JsonSerializableUtility

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

the class AuditEventType method raiseDeletedEvent.

public static void raiseDeletedEvent(String auditEventType, AbstractVO<?> o) {
    Map<String, Object> context = new HashMap<String, Object>();
    JsonSerializableUtility scanner = new JsonSerializableUtility();
    try {
        context = scanner.findValues(o);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | JsonException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
    raiseEvent(AuditEventInstanceVO.CHANGE_TYPE_DELETE, auditEventType, o, "event.audit.extended.deleted", context);
}
Also used : JsonException(com.serotonin.json.JsonException) HashMap(java.util.HashMap) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with JsonSerializableUtility

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

the class AuditEventType method raiseAddedEvent.

public static void raiseAddedEvent(String auditEventType, AbstractVO<?> o) {
    Map<String, Object> context = new HashMap<String, Object>();
    JsonSerializableUtility scanner = new JsonSerializableUtility();
    try {
        context = scanner.findValues(o);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | JsonException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
    raiseEvent(AuditEventInstanceVO.CHANGE_TYPE_CREATE, auditEventType, o, "event.audit.extended.added", context);
}
Also used : JsonException(com.serotonin.json.JsonException) HashMap(java.util.HashMap) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testIntArray.

@Test
public void testIntArray() 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");
    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)

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