Search in sources :

Example 16 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testNoChange.

@Test
public void testNoChange() 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("Should be no changes.");
    }
}
Also used : JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) Test(org.junit.Test)

Example 17 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testListJsonSerializable.

@Test
public void testListJsonSerializable() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    List<Object> writerList1 = new ArrayList<>();
    writerList1.add(new JsonSerializableTestObject());
    writerList1.add(new JsonSerializableTestObject());
    List<Object> writerList2 = new ArrayList<>();
    writerList2.add(new JsonSerializableTestObject());
    writerList2.add(new JsonSerializableTestObject());
    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() != 0)
        Assert.fail("List should not have changed");
    ((JsonSerializableTestObject) o2.getWriterList().get(0)).setDayOfWeekEnum(DayOfWeek.SUNDAY);
    changes = util.findChanges(o1, o2);
    if (changes.size() != 1)
        Assert.fail("List should have changed");
    @SuppressWarnings("unchecked") List<JsonSerializableTestObject> changedList = (List<JsonSerializableTestObject>) changes.get("writerList");
    Assert.assertNotNull("List should have changed", changedList);
    Assert.assertEquals((int) 2, changedList.size());
    Assert.assertEquals(DayOfWeek.SUNDAY, changedList.get(0).getDayOfWeekEnum());
    Assert.assertEquals(DayOfWeek.MONDAY, changedList.get(1).getDayOfWeekEnum());
}
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 18 with JsonSerializableUtility

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

the class JsonSerializableUtilityTest method testAnnotatedListOrder.

@Test
public void testAnnotatedListOrder() 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.clear();
    annotatedList2.add(new Integer(2));
    annotatedList2.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("annotatedList");
    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 19 with JsonSerializableUtility

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

the class AuditEventType method raiseChangedEvent.

public static void raiseChangedEvent(String auditEventType, AbstractVO<?> from, AbstractVO<?> to) {
    Map<String, Object> context = new HashMap<String, Object>();
    // Find the annotated properties
    JsonSerializableUtility scanner = new JsonSerializableUtility();
    try {
        context = scanner.findChanges(from, to);
        if (context.size() == 0)
            // If the object wasn't in fact changed, don't raise an event.
            return;
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | JsonException | IOException e) {
        LOG.error(e.getMessage(), e);
    }
    raiseEvent(AuditEventInstanceVO.CHANGE_TYPE_MODIFY, auditEventType, to, "event.audit.extended.changed", 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)

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