RegimenItemAdapter implements JsonSerializer<RegimenItem>, JsonDeserializer<RegimenItem> { @Override public RegimenItem deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { RegimenItem regimenItem = gson.fromJson(json, RegimenItem.class); try { Regimen regimen = regimenRepository.getByCode(json.getAsJsonObject().get("code").getAsString()); if (regimen == null) { regimen = createRegimen(json); } regimenItem.setRegimen(regimen); } catch (LMISException e) { new LMISException(e,"RegimenItemAdapter.deserialize").reportToFabric(); throw new JsonParseException("can not find RegimenItem by name and category"); } return regimenItem; } RegimenItemAdapter(); @Override RegimenItem deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(RegimenItem src, Type typeOfSrc, JsonSerializationContext context); @Inject
    public RegimenRepository regimenRepository; }
@Test public void shouldDeSerializeRegimenItem() throws Exception { when(regimenRepository.getByNameAndCategory("d4T 30+3TC+EFV", Regimen.RegimeType.Adults)).thenReturn(null); String json = JsonFileReader.readJson(getClass(), "RegimenItemResponse.json"); RegimenItemAdapter regimenItemAdapter = new RegimenItemAdapter(); RegimenItem deserialize = regimenItemAdapter.deserialize(new JsonParser().parse(json), null, null); Regimen regimen = deserialize.getRegimen(); assertThat(regimen.getType(), is(Regimen.RegimeType.Adults)); assertThat(regimen.getCode(), is("020")); assertThat(regimen.getName(), is("d4T 30+3TC+EFV")); assertThat(regimen.isCustom(), is(true)); verify(regimenRepository).create(regimen); }