Writing Unit tests for an added method

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldFindAConceptByItsConceptId() {
	String id = "3";
	executeDataSet(GET_CONCEPTS_BY_SET_XML);
	Assert.assertEquals("3", conceptService.getConceptByNameOrIdOrMap(id).getConceptId().toString());
}

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldGetConceptByName() {
	String nameToFetch = "Some non numeric concept name";
	executeDataSet(INITIAL_CONCEPTS_XML);
	Concept conceptByName = conceptService.getConceptByNameOrIdOrMap(nameToFetch);
	assertEquals("Unable to fetch concept by name", 1, conceptByName.getId().intValue());
}

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldFindAConceptByItsMapping() {
	String mapping = "XIC:2345";
	executeDataSet(INITIAL_CONCEPTS_XML);
	Concept concept = conceptService.getConceptByNameOrIdOrMap(mapping);
	Assert.assertEquals("Unable to fetch concept by map", 5, concept.getId().intValue());
}

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldFindAConceptByItsUuid() {
	String uniqueId = "0ade2ed3-cd5f-4f46-9459-26127c9265ab";
	executeDataSet(INITIAL_CONCEPTS_XML);
	Assert.assertEquals(uniqueId, conceptService.getConceptByNameOrIdOrMap(uniqueId).getUuid());
}

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldFindAConceptWithNonStandardUuid() {
	String nonStandId = "1000AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	executeDataSet(INITIAL_CONCEPTS_XML);
	Assert.assertEquals(nonStandId, conceptService.getConceptByNameOrIdOrMap(nonStandId).getUuid());
}

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldNotFindAConceptWithInvalidUuid() {
	String invalidUuid = "1000";
	executeDataSet(INITIAL_CONCEPTS_XML);
	Assert.assertNull(conceptService.getConceptByNameOrIdOrMap(invalidUuid));
}

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldFindAConceptByStaticConstant() {
	assertThat(conceptService.getConceptByNameOrIdOrMap("org.openmrs.api.ConceptServiceTest.TEST_CONCEPT_CONSTANT_ID"), notNullValue());
	assertThat(conceptService.getConceptByNameOrIdOrMap("org.openmrs.api.ConceptServiceTest.TEST_CONCEPT_CONSTANT_UUID"), notNullValue());
	assertThat(conceptService.getConceptByNameOrIdOrMap("org.openmrs.api.ConceptServiceTest.TEST_CONCEPT_CONSTANT_MAPPING"), notNullValue());
}

/**
 * @see ConceptService#getConceptByNameOrIdOrMap(String)
 */
@Test
public void getConceptByNameOrIdOrMap_shouldReturnNullOtherwise() {
	executeDataSet(INITIAL_CONCEPTS_XML);
	String conceptRef = null;
	Assert.assertNull(conceptService.getConceptByNameOrIdOrMap(conceptRef));
	conceptRef = "";
	Assert.assertNull(conceptService.getConceptByNameOrIdOrMap(conceptRef));
	conceptRef = "name does not exist in initialConcepts";
	Assert.assertNull(conceptService.getConceptByNameOrIdOrMap(conceptRef));
}

that is the unit tests i managed to add the method getConceptByNameOrIdOrMap(String)

The error log is pasted at pastebin. Your assistance will be much of imortance. Thanks!

Thanks @jnsereko for the work. I need some more clarification , was this a ticket or some thing ?? can you mention the ticket here

Basically, the error is saying that whatever is in your INITIAL_CONCEPTS_XML has a name column for a concept, something like this:

<concept id="..." ... name="My Concept" ... />

The problem is that concept names are not stored in the concept table, but in a separate table, ConceptName so you’d want something like the below (taken from OpenMRS-Core:

<concept concept_id="3000" retired="0" datatype_id="4" class_id="3" is_set="false" creator="1" date_created="2008-08-15 15:27:51.0" uuid="568b58c8-e878-11e0-950d-00248140a5eb"/>
<concept_name concept_id="3000" name="TRUST ALWAYS" locale="en" creator="1" date_created="2004-08-12 00:00:00.0" concept_name_id="9997" voided="false" locale_preferred="0" uuid="3e1b3848-e879-11e0-950d-00248140a5eb"/>

Also, you probably want the shouldFindAConceptByStaticConstant test to look like this:

assertThat(conceptService.getConceptByNameOrIdOrMap(org.openmrs.api.ConceptServiceTest.TEST_CONCEPT_CONSTANT_ID), notNullValue());
assertThat(conceptService.getConceptByNameOrIdOrMap(org.openmrs.api.ConceptServiceTest.TEST_CONCEPT_CONSTANT_UUID), notNullValue());
assertThat(conceptService.getConceptByNameOrIdOrMap(org.openmrs.api.ConceptServiceTest.TEST_CONCEPT_CONSTANT_MAPPING), notNullValue()
2 Likes

Thanks @jnsereko for the work. I need some more clarification , was this a ticket or some thing ?? can you mention the ticket here

yes @mozzy it was a ticket TRUNK-5655

String INITIAL_CONCEPTS_XML = “org/openmrs/api/include/ConceptServiceTest-initialConcepts.xml”; Thanks for the help @mozzy and @ibacher. The expression above shows the value of the constant. And had added two lines of code in ConceptServiceTest-initialConcepts.xml

`