PsqlEventMapper { Event<Map<String, Object>> parseResponse(String responsePayload) { try { JSONObject jsonResponse = new JSONObject(responsePayload); String eventType = jsonResponse.getString(EVENT_TYPE_KEY); String tableName = jsonResponse.getString(TABLE_NAME_KEY); Map<String, Object> newRow = jsonObjectToMap(jsonResponse.getJSONObject(NEW_ENTITY_KEY)); Map<String, Object> oldRow = jsonObjectToMap(jsonResponse.getJSONObject(OLD_ENTITY_KEY)); Event<Map<String, Object>> event = new Event<>(eventType, tableName, newRow, oldRow); validateEvent(event); return event; } catch (JSONException je) { throw new InvalidPayloadException("Payload is not a valid json payload", je); } catch (InvalidPayloadException ipe) { throw new InvalidPayloadException("Payload is not valid.", ipe); } } PsqlEventMapper();   }
@Test(groups = SMALL, expectedExceptions = InvalidPayloadException.class) public void testParsingThrowsExceptionForInvalidPayload() { mapper.parseResponse(NONPARSABLE_JSON); }
@Test(groups = SMALL, expectedExceptions = InvalidPayloadException.class) public void testParsingPayloadWithMissingEventTypeThrowsException() { mapper.parseResponse(JSON_MISSING_EVENT_TYPE); }
@Test(groups = SMALL, expectedExceptions = InvalidPayloadException.class) public void testParsingPayloadWithMissingTableNameThrowsException() { mapper.parseResponse(JSON_MISSING_TABLE_NAME); }
@Test(groups = SMALL, expectedExceptions = InvalidPayloadException.class) public void testParsingPayloadWithMissingOldEntityThrowsException() { mapper.parseResponse(JSON_MISSING_OLD_DATA); }
@Test(groups = SMALL, expectedExceptions = InvalidPayloadException.class) public void testParsingPayloadWithMissingNewEntityThrowsException() { mapper.parseResponse(JSON_MISSING_NEW_DATA); }
@Test(groups = SMALL, expectedExceptions = InvalidPayloadException.class) public void testParsingPayloadWithNullOldEntityThrowsException() { mapper.parseResponse(JSON_NULL_OLD_ENTITY); }
@Test(groups = SMALL) public void testParsingPayloadDoesntFailForEmptyRow() { Event<Map<String, Object>> event = mapper.parseResponse(JSON_CORRECT_RESPONSE_INSERT); assertEquals(event.getOldEntity(), Maps.newHashMap()); }
@Test(groups = SMALL) public void testCanHandleNestedObjectsInTheRows() { Event<Map<String, Object>> event = mapper.parseResponse(JSON_NESTED_RESPONSE_INSERT); assertTrue(Map.class.isAssignableFrom(event.getNewEntity().get("value").getClass())); }
@Test(groups = SMALL) public void tesParsingCorrectPayloadReturnsCorrectEventObject() { Map<String, Object> expectedNewEntity = Maps.newHashMap(); expectedNewEntity.put("id", 1); expectedNewEntity.put("value", "def"); Map<String, Object> expectedOldEntity = Maps.newHashMap(); expectedOldEntity.put("id", 1); expectedOldEntity.put("value", "abc"); Event<Map<String, Object>> event = mapper.parseResponse(JSON_CORRECT_RESPONSE); assertEquals(event.getEventType(), Event.UPDATE_TYPE); assertEquals(event.getEntityName(), "test"); assertEquals(event.getNewEntity(), expectedNewEntity); assertEquals(event.getOldEntity(), expectedOldEntity); }