@Test public void writeJSONConvertsCodeFields() throws Exception { @SuppressWarnings("unchecked") Provider<ComponentManager> cmProvider = mock(Provider.class); ComponentManager contextComponentManager = mock(ComponentManager.class); VocabularyManager vm = mock(VocabularyManager.class); VocabularyTerm term = mock(VocabularyTerm.class); ReflectionUtils.setFieldValue(new ComponentManagerRegistry(), "cmProvider", cmProvider); doReturn(contextComponentManager).when(cmProvider).get(); doReturn(vm).when(contextComponentManager).getInstance(VocabularyManager.class); doReturn(term).when(vm).resolveTerm("HP:0009927"); doReturn("Congenital absence of nose").when(term).getName(); term = mock(VocabularyTerm.class); doReturn(term).when(vm).resolveTerm("HP:0002223"); doReturn("Absent eyebrow").when(term).getName(); Map<String, List<VocabularyProperty>> map = new LinkedHashMap<>(); List<VocabularyProperty> list = new LinkedList<>(); list.add(new AbstractComplexController.QuickVocabularyProperty("HP:0009927")); map.put(PROPERTY_1, list); list = new LinkedList<>(); list.add(new AbstractComplexController.QuickVocabularyProperty("HP:0002223")); map.put(PROPERTY_2, list); PatientData<List<VocabularyProperty>> patientData = new DictionaryPatientData<>(DATA_NAME, map); doReturn(patientData).when(this.patient).getData(CODE_FIELDS_DATA_NAME); JSONObject json = new JSONObject(); this.codeFieldImplMocker.getComponentUnderTest().writeJSON(this.patient, json, null); Assert.assertNotNull(json.get(CODE_FIELDS_DATA_NAME)); Assert.assertTrue(json.get(CODE_FIELDS_DATA_NAME) instanceof JSONObject); JSONObject container = json.getJSONObject(CODE_FIELDS_DATA_NAME); JSONObject item1 = container.getJSONArray(PROPERTY_1).getJSONObject(0); JSONObject item2 = container.getJSONArray(PROPERTY_2).getJSONObject(0); Assert.assertNotNull(item1); Assert.assertEquals("HP:0009927", item1.get("id")); Assert.assertEquals("Congenital absence of nose", item1.get("label")); Assert.assertNotNull(item2); Assert.assertEquals("HP:0002223", item2.get("id")); Assert.assertEquals("Absent eyebrow", item2.get("label")); }
@Override public void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames) { PatientData<T> data = patient.getData(getName()); String jsonPropertyName = getJsonPropertyName(); if (data == null || data.size() == 0) { return; } Iterator<Map.Entry<String, T>> iterator = data.dictionaryIterator(); JSONObject container = json.optJSONObject(jsonPropertyName); while (iterator.hasNext()) { Map.Entry<String, T> item = iterator.next(); String itemKey = item.getKey(); Object formattedValue = format(itemKey, item.getValue()); if (selectedFieldNames == null || selectedFieldNames.contains(getControllingFieldName(item.getKey()))) { if (container == null) { json.put(jsonPropertyName, new JSONObject()); container = json.optJSONObject(jsonPropertyName); } container.put(itemKey, formattedValue); } } }
AbstractComplexController implements PatientDataController<T> { @Override public void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames) { PatientData<T> data = patient.getData(getName()); String jsonPropertyName = getJsonPropertyName(); if (data == null || data.size() == 0) { return; } Iterator<Map.Entry<String, T>> iterator = data.dictionaryIterator(); JSONObject container = json.optJSONObject(jsonPropertyName); while (iterator.hasNext()) { Map.Entry<String, T> item = iterator.next(); String itemKey = item.getKey(); Object formattedValue = format(itemKey, item.getValue()); if (selectedFieldNames == null || selectedFieldNames.contains(getControllingFieldName(item.getKey()))) { if (container == null) { json.put(jsonPropertyName, new JSONObject()); container = json.optJSONObject(jsonPropertyName); } container.put(itemKey, formattedValue); } } } }
AbstractComplexController implements PatientDataController<T> { @Override public void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames) { PatientData<T> data = patient.getData(getName()); String jsonPropertyName = getJsonPropertyName(); if (data == null || data.size() == 0) { return; } Iterator<Map.Entry<String, T>> iterator = data.dictionaryIterator(); JSONObject container = json.optJSONObject(jsonPropertyName); while (iterator.hasNext()) { Map.Entry<String, T> item = iterator.next(); String itemKey = item.getKey(); Object formattedValue = format(itemKey, item.getValue()); if (selectedFieldNames == null || selectedFieldNames.contains(getControllingFieldName(item.getKey()))) { if (container == null) { json.put(jsonPropertyName, new JSONObject()); container = json.optJSONObject(jsonPropertyName); } container.put(itemKey, formattedValue); } } }  }
AbstractComplexController implements PatientDataController<T> { @Override public void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames) { PatientData<T> data = patient.getData(getName()); String jsonPropertyName = getJsonPropertyName(); if (data == null || data.size() == 0) { return; } Iterator<Map.Entry<String, T>> iterator = data.dictionaryIterator(); JSONObject container = json.optJSONObject(jsonPropertyName); while (iterator.hasNext()) { Map.Entry<String, T> item = iterator.next(); String itemKey = item.getKey(); Object formattedValue = format(itemKey, item.getValue()); if (selectedFieldNames == null || selectedFieldNames.contains(getControllingFieldName(item.getKey()))) { if (container == null) { json.put(jsonPropertyName, new JSONObject()); container = json.optJSONObject(jsonPropertyName); } container.put(itemKey, formattedValue); } } }  @Override @SuppressWarnings("unchecked") PatientData<T> load(Patient patient); @Override void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames); @Override void writeJSON(Patient patient, JSONObject json); @Override void save(Patient patient); @Override void save(@Nonnull final Patient patient, @Nonnull final PatientWritePolicy policy); @Override PatientData<T> readJSON(JSONObject json); }
AbstractComplexController implements PatientDataController<T> { @Override public void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames) { PatientData<T> data = patient.getData(getName()); String jsonPropertyName = getJsonPropertyName(); if (data == null || data.size() == 0) { return; } Iterator<Map.Entry<String, T>> iterator = data.dictionaryIterator(); JSONObject container = json.optJSONObject(jsonPropertyName); while (iterator.hasNext()) { Map.Entry<String, T> item = iterator.next(); String itemKey = item.getKey(); Object formattedValue = format(itemKey, item.getValue()); if (selectedFieldNames == null || selectedFieldNames.contains(getControllingFieldName(item.getKey()))) { if (container == null) { json.put(jsonPropertyName, new JSONObject()); container = json.optJSONObject(jsonPropertyName); } container.put(itemKey, formattedValue); } } }  @Override @SuppressWarnings("unchecked") PatientData<T> load(Patient patient); @Override void writeJSON(Patient patient, JSONObject json, Collection<String> selectedFieldNames); @Override void writeJSON(Patient patient, JSONObject json); @Override void save(Patient patient); @Override void save(@Nonnull final Patient patient, @Nonnull final PatientWritePolicy policy); @Override PatientData<T> readJSON(JSONObject json);  }