StringToCodedOrFreeTextValueConverter implements Converter<String, CodedOrFreeTextValue> { @Override public CodedOrFreeTextValue convert(String str) { if (StringUtils.isNotBlank(str)) { if (str.startsWith(CONCEPT_PREFIX)) { String uuidOrConceptId = str.substring(str.indexOf(CONCEPT_PREFIX) + CONCEPT_PREFIX.length()); if (StringUtils.isNotBlank(uuidOrConceptId)) { Concept concept = conversionServiceFactory.getObject().convert(uuidOrConceptId, Concept.class); return new CodedOrFreeTextValue(concept); } } else if (str.startsWith(CONCEPT_NAME_PREFIX)) { String uuidOrConceptNameId = str.substring(str.indexOf(CONCEPT_NAME_PREFIX) + CONCEPT_NAME_PREFIX.length()); if (StringUtils.isNotBlank(uuidOrConceptNameId)) { ConceptName name = conversionServiceFactory.getObject().convert(uuidOrConceptNameId, ConceptName.class); return new CodedOrFreeTextValue(name); } } else if (str.startsWith(NON_CODED_PREFIX)) { return new CodedOrFreeTextValue(str.substring(str.indexOf(NON_CODED_PREFIX) + NON_CODED_PREFIX.length())); } } return new CodedOrFreeTextValue(); }  @Override CodedOrFreeTextValue convert(String str);  }
@Test public void convert_shouldConvertAValidConceptIdString() throws Exception { int conceptId = 3; CodedOrFreeTextValue codedValue = converter.convert(CONCEPT_PREFIX + conceptId); Concept concept = codedValue.getCodedValue(); assertEquals(conceptId, concept.getConceptId().intValue()); }
@Test public void convert_shouldConvertAValidConceptUuidString() throws Exception { String conceptUuid = "0cbe2ed3-cd5f-4f46-9459-26127c9265ab"; CodedOrFreeTextValue codedValue = converter.convert(CONCEPT_PREFIX + conceptUuid); Concept concept = codedValue.getCodedValue(); assertEquals(conceptUuid, concept.getUuid()); }
@Test public void convert_shouldConvertAValidConceptNameIdString() throws Exception { int conceptNameId = 1439; CodedOrFreeTextValue codedValue = converter.convert(CONCEPT_NAME_PREFIX + conceptNameId); ConceptName name = codedValue.getCodedNameValue(); assertEquals(conceptNameId, name.getConceptNameId().intValue()); }
@Test public void convert_shouldConvertAValidConceptNameUuidString() throws Exception { String conceptNameUuid = "9bc5693a-f558-40c9-8177-145a4b119ca7"; CodedOrFreeTextValue codedValue = converter.convert(CONCEPT_NAME_PREFIX + conceptNameUuid); ConceptName name = codedValue.getCodedNameValue(); assertEquals(conceptNameUuid, name.getUuid()); }
@Test public void convert_shouldHandleFreeText() throws Exception { String text = "some random text"; CodedOrFreeTextValue codedValue = converter.convert(NON_CODED_PREFIX + text); assertEquals(text, codedValue.getValue()); }
@Test public void convert_shouldNotReturnNull() throws Exception { String conceptUuid = "some invalid concept uuid"; CodedOrFreeTextValue codedValue = converter.convert(CONCEPT_PREFIX + conceptUuid); assertNotNull(codedValue); assertNull(codedValue.getValue()); }