@Test public void testGetAllOperationsFor_withEmtpyMessage() { String json = "{ \"head\" : {}, \"operations\" : [] }"; ClientMessage message = new ClientMessage(JsonObject.readFrom(json)); List<Operation> operations = message.getAllOperationsFor("w5"); assertTrue(operations.isEmpty()); }
@Test public void testConstructor_JsonObject_createsIndex() { String json = "{ \"head\" : {}, \"operations\" : [" + "[ \"set\", \"w3\", { \"foo\" : 23 } ]" + "] }"; ClientMessage message = new ClientMessage(JsonObject.readFrom(json)); List<Operation> operations = message.getAllOperationsFor("w3"); assertFalse(operations.isEmpty()); }
@Test public void testGetAllOperationsFor_withoutMatchingOperations() { String json = "{ \"head\" : {}, \"operations\" : [" + "[ \"set\", \"w3\", { \"p1\" : \"foo\" } ]," + "[ \"set\", \"w4\", { \"p2\" : \"bar\" } ]," + "[ \"notify\", \"w3\", \"widgetSelected\", {} ]" + "] }"; ClientMessage message = new ClientMessage(JsonObject.readFrom(json)); List<Operation> operations = message.getAllOperationsFor("w5"); assertTrue(operations.isEmpty()); }
@Test public void testGetLastNotifyOperation_withNullTarget() { String json = "{ \"head\" : {}, \"operations\" : [" + "[ \"notify\", \"w3\", \"foo\", {} ]," + "[ \"notify\", \"w4\", \"foo\", {} ]," // <--- + "[ \"notify\", \"w5\", \"bar\", {} ]" + "] }"; ClientMessage message = new ClientMessage(JsonObject.readFrom(json)); NotifyOperation operation = message.getLastNotifyOperationFor(null, "foo"); assertEquals("w4", operation.getTarget()); }
@Test public void testGetAllOperationsFor_selectsMatchingOperations() { String json = "{ \"head\" : {}, \"operations\" : [" + "[ \"set\", \"w3\", { \"foo\" : 23 } ]," + "[ \"set\", \"w4\", { \"foo\" : 42 } ]," + "[ \"notify\", \"w3\", \"event\", {} ]" + "] }"; ClientMessage message = new ClientMessage(JsonObject.readFrom(json)); List<Operation> operations = message.getAllOperationsFor("w3"); assertEquals(2, operations.size()); assertTrue(operations.get(0) instanceof SetOperation); assertTrue(operations.get(1) instanceof NotifyOperation); }
@Test public void testGetAllCallOperations_withoutTargetAndMethodName() { String json = "{ \"head\" : {}, \"operations\" : [" + "[ \"set\", \"w3\", { \"count\" : 1 } ]," + "[ \"call\", \"w3\", \"foo\", { \"count\" : 2 } ]," // <--- + "[ \"call\", \"w4\", \"bar\", { \"count\" : 3 } ]" // <--- + "] }"; ClientMessage message = new ClientMessage(JsonObject.readFrom(json)); List<CallOperation> operations = message.getAllCallOperationsFor(null, null); assertEquals(2, operations.size()); assertEquals(2, operations.get(0).getParameters().get("count").asInt()); assertEquals(3, operations.get(1).getParameters().get("count").asInt()); }
@Test public void testGetLastNotifyOperation() { String json = "{ \"head\" : {}, \"operations\" : [" + "[ \"notify\", \"w3\", \"foo\", { \"count\" : 1 } ]," + "[ \"notify\", \"w3\", \"bar\", { \"count\" : 2 } ]," + "[ \"notify\", \"w3\", \"foo\", { \"count\" : 3 } ]," // <--- + "[ \"notify\", \"w3\", \"bar\", { \"count\" : 4 } ]," + "[ \"notify\", \"w4\", \"foo\", { \"count\" : 5 } ]," + "[ \"notify\", \"w4\", \"bar\", { \"count\" : 6 } ]" + "] }"; ClientMessage message = new ClientMessage(JsonObject.readFrom(json)); NotifyOperation operation = message.getLastNotifyOperationFor("w3", "foo"); assertEquals(3, operation.getProperties().get("count").asInt()); }