HookNotification implements JsonDeserializer<HookNotification.HookNotificationMessage> { @Override public HookNotificationMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { HookNotificationType type = context.deserialize(((JsonObject) json).get("type"), HookNotificationType.class); switch (type) { case ENTITY_CREATE: return context.deserialize(json, EntityCreateRequest.class); case ENTITY_FULL_UPDATE: return context.deserialize(json, EntityUpdateRequest.class); case ENTITY_PARTIAL_UPDATE: return context.deserialize(json, EntityPartialUpdateRequest.class); case ENTITY_DELETE: return context.deserialize(json, EntityDeleteRequest.class); case TYPE_CREATE: case TYPE_UPDATE: return context.deserialize(json, TypeRequest.class); default: throw new IllegalStateException("Unhandled type " + type); } }  @Override HookNotificationMessage deserialize(JsonElement json, Type typeOfT,
                                               JsonDeserializationContext context);  }
@Test public void testNewMessageSerDe() throws Exception { Referenceable entity1 = new Referenceable("sometype"); entity1.set("attr", "value"); entity1.set("complex", new Referenceable("othertype")); Referenceable entity2 = new Referenceable("newtype"); String user = "user"; HookNotification.EntityCreateRequest request = new HookNotification.EntityCreateRequest(user, entity1, entity2); String notificationJson = AbstractNotification.GSON.toJson(request); HookNotification.HookNotificationMessage actualNotification = HOOK_MESSAGE_DESERIALIZER.deserialize(notificationJson); assertEquals(actualNotification.getType(), HookNotification.HookNotificationType.ENTITY_CREATE); assertEquals(actualNotification.getUser(), user); HookNotification.EntityCreateRequest createRequest = (HookNotification.EntityCreateRequest) actualNotification; assertEquals(createRequest.getEntities().size(), 2); Referenceable actualEntity1 = createRequest.getEntities().get(0); assertEquals(actualEntity1.getTypeName(), "sometype"); assertEquals(((Referenceable)actualEntity1.get("complex")).getTypeName(), "othertype"); assertEquals(createRequest.getEntities().get(1).getTypeName(), "newtype"); }
@Test public void testBackwardCompatibility() throws Exception { Referenceable entity = new Referenceable("sometype"); entity.set("attr", "value"); HookNotification.EntityCreateRequest request = new HookNotification.EntityCreateRequest(null, entity); String notificationJsonFromCode = AbstractNotification.GSON.toJson(request); System.out.println(notificationJsonFromCode); String notificationJson = "{\n" + " \"entities\": [\n" + " {\n" + " \"jsonClass\": \"org.apache.atlas.typesystem.json.InstanceSerialization$_Reference\",\n" + " \"id\": {\n" + " \"jsonClass\": \"org.apache.atlas.typesystem.json.InstanceSerialization$_Id\",\n" + " \"id\": \"-1459493350903186000\",\n" + " \"version\": 0,\n" + " \"typeName\": \"sometype\",\n" + " \"state\": \"ACTIVE\"\n" + " },\n" + " \"typeName\": \"sometype\",\n" + " \"values\": {\n" + " \"attr\": \"value\"\n" + " },\n" + " \"traitNames\": [],\n" + " \"traits\": {}\n" + " }\n" + " ],\n" + " \"type\": \"ENTITY_CREATE\"\n" + "}"; HookNotification.HookNotificationMessage actualNotification = HOOK_MESSAGE_DESERIALIZER.deserialize(notificationJson); assertEquals(actualNotification.getType(), HookNotification.HookNotificationType.ENTITY_CREATE); assertNull(actualNotification.user); assertEquals(actualNotification.getUser(), HookNotification.HookNotificationMessage.UNKNOW_USER); }