/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldPassWhenSavingAConceptAfterRemovingAName() { executeDataSet("org/openmrs/api/include/ConceptServiceTest-names.xml"); Concept concept = conceptService.getConcept(3000); concept.addDescription(new ConceptDescription("some description",null)); Assert.assertFalse(concept.getSynonyms().isEmpty()); concept.removeName(concept.getSynonyms().iterator().next()); conceptService.saveConcept(concept); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldAddNewConceptName() { Concept concept = conceptService.getConcept(3); ConceptName name = new ConceptName("new name", Locale.US); concept.addName(name); conceptService.saveConcept(concept); assertNotNull(name.getConceptNameId()); }
/** * @see org.openmrs.api.ConceptService#convertBooleanConceptToCoded(org.openmrs.Concept) */ @Override public void convertBooleanConceptToCoded(Concept conceptToChange) throws APIException { if (conceptToChange != null) { if (!conceptToChange.getDatatype().isBoolean()) { throw new APIException("Concept.datatype.invalid", (Object[]) null); } conceptToChange.setDatatype(getConceptDatatypeByName("Coded")); conceptToChange.addAnswer(new ConceptAnswer(getTrueConcept())); conceptToChange.addAnswer(new ConceptAnswer(getFalseConcept())); Context.getConceptService().saveConcept(conceptToChange); } }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldCreateAReferenceTermForAConceptMappingOnTheFlyWhenEditingAConcept() { int initialTermCount = conceptService.getAllConceptReferenceTerms().size(); Concept concept = conceptService.getConcept(5497); ConceptMap map = new ConceptMap(); map.getConceptReferenceTerm().setCode("unique code"); map.getConceptReferenceTerm().setConceptSource(conceptService.getConceptSource(1)); concept.addConceptMapping(map); conceptService.saveConcept(concept); Assert.assertEquals(initialTermCount + 1, conceptService.getAllConceptReferenceTerms().size()); }
@Override public void execute() { ConceptNumeric weight = Context.getConceptService().getConceptNumeric(5089); Date dateChangedBefore = weight.getDateChanged(); weight.setHiAbsolute(75d); Context.getConceptService().saveConcept(weight); Assert.assertNotSame(dateChangedBefore, weight.getDateChanged()); } }).runTheTask();
@Test public void saveConcept_shouldUpdateConceptIfConceptIsAttachedToAnObservationAndItIsANonDatatypeChange() { executeDataSet(INITIAL_CONCEPTS_XML); Concept concept = conceptService.getConcept(1); assertNotNull(concept); ObsService obsService = Context.getObsService(); Obs obs = new Obs(new Person(1), concept, new Date(), new Location(1)); obs.setValueCoded(Context.getConceptService().getConcept(7)); obsService.saveObs(obs, "Creating a new observation with a concept"); conceptService.saveConcept(concept); }
/** * @see ConceptServiceImpl#saveConcept(Concept) */ @Test public void saveConcept_shouldReturnTheConceptWithNewConceptIDIfCreatingNewConcept() { Concept c = new Concept(); ConceptName fullySpecifiedName = new ConceptName("requires one name min", new Locale("fr", "CA")); c.addName(fullySpecifiedName); c.addDescription(new ConceptDescription("some description", null)); c.setDatatype(new ConceptDatatype(1)); c.setConceptClass(new ConceptClass(1)); Concept savedC = Context.getConceptService().saveConcept(c); assertNotNull(savedC); assertTrue(savedC.getConceptId() > 0); }
@Test public void saveConcept_shouldCreateNewConceptInDatabase() { executeDataSet(INITIAL_CONCEPTS_XML); Concept conceptToAdd = new Concept(); ConceptName cn = new ConceptName("new name", Context.getLocale()); conceptToAdd.addName(cn); conceptToAdd.addDescription(new ConceptDescription("some description",null)); conceptToAdd.setDatatype(new ConceptDatatype(1)); conceptToAdd.setConceptClass(new ConceptClass(1)); assertFalse(conceptService.getAllConcepts().contains(conceptToAdd)); conceptService.saveConcept(conceptToAdd); assertTrue(conceptService.getAllConcepts().contains(conceptToAdd)); }
/** * Tests if {@link org.openmrs.api.ConceptService#saveConcept(Concept)} saves a ConceptNumeric with allowDecimal value */ @Test public void shouldSaveAConceptNumericWithAllowDecimalValue() { Concept c = Context.getConceptService().getConcept(22); ConceptNumeric cn = new ConceptNumeric(c); cn.addDescription(new ConceptDescription("some description", null)); Context.getConceptService().saveConcept(cn); Assert.assertFalse(Context.getConceptService().getConceptNumeric(22).getAllowDecimal()); cn.setAllowDecimal(true); Context.getConceptService().saveConcept(cn); Assert.assertTrue(Context.getConceptService().getConceptNumeric(22).getAllowDecimal()); } }
@Test(expected = ConceptInUseException.class) public void saveConcept_shouldNotUpdateConceptDataTypeIfConceptIsAttachedToAnObservation() { executeDataSet(INITIAL_CONCEPTS_XML); Concept concept = conceptService.getConcept(2); assertNotNull(concept); ObsService obsService = Context.getObsService(); Obs obs = new Obs(Context.getPersonService().getPerson(1), concept, new Date(), Context.getLocationService() .getLocation(1)); obs.setValueCoded(Context.getConceptService().getConcept(7)); obsService.saveObs(obs, "Creating a new observation with a concept"); ConceptDatatype newDatatype = conceptService.getConceptDatatypeByName("Text"); concept.setDatatype(newDatatype); conceptService.saveConcept(concept); }
@Test public void validate_shouldPassIfTheConceptWithADuplicateNameIsRetired() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; concept.setRetired(true); conceptService.saveConcept(concept); String duplicateName = concept.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassIfTheConceptBeingValidatedIsRetiredAndHasADuplicateName() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; conceptService.saveConcept(concept); String duplicateName = concept.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.setRetired(true); anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void saveConcept_shouldTrimWhitespacesInConceptName() { //Given Concept concept = new Concept(); String nameWithSpaces = " jwm "; concept.addName(new ConceptName(nameWithSpaces, new Locale("en", "US"))); concept.addDescription(new ConceptDescription("some description", null)); concept.setDatatype(new ConceptDatatype(1)); concept.setConceptClass(new ConceptClass(1)); //When Context.getConceptService().saveConcept(concept); //Then assertNotEquals(concept.getName().getName(), nameWithSpaces); assertEquals(concept.getName().getName(), "jwm"); }
@Test public void saveConcept_shouldUpdateConceptAlreadyExistingInDatabase() { executeDataSet(INITIAL_CONCEPTS_XML); // using isSet() as a value to check and change assertFalse(conceptService.getConcept(2).getSet()); Concept concept = conceptService.getConcept(2); // change a value concept.setSet(true); // save the concept conceptService.saveConcept(concept); // see if the value was updated in the database assertTrue(conceptService.getConcept(2).getSet()); }
/** * @see ConceptService#saveConcept(Concept) */ @Test @Ignore public void saveConcept_shouldNotSetAuditInfoIfTheConceptIsNotEdited() { Concept concept = conceptService.getConcept(3); Assert.assertNull(concept.getDateChanged()); Assert.assertNull(concept.getChangedBy()); conceptService.saveConcept(concept); Assert.assertNull(concept.getDateChanged()); Assert.assertNull(concept.getChangedBy()); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldGenerateIdForNewConceptIfNoneIsSpecified() { Concept concept = new Concept(); ConceptName cn = new ConceptName("Weight", Context.getLocale()); concept.addName(cn); concept.addDescription(new ConceptDescription("some description",null)); concept.setConceptId(null); concept.setDatatype(Context.getConceptService().getConceptDatatypeByName("Numeric")); concept.setConceptClass(Context.getConceptService().getConceptClassByName("Finding")); concept = Context.getConceptService().saveConcept(concept); assertFalse(concept.getConceptId().equals(5089)); }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredPreferredNameInTheSameLocale() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; ConceptName preferredName = new ConceptName("preferred name", Context.getLocale()); concept.setPreferredName(preferredName); conceptService.saveConcept(concept); Assert.assertEquals("preferred name", concept.getPreferredName(Context.getLocale()).getName()); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName("preferred name"); Errors errors = new BindException(anotherConcept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + preferredName + "' is a duplicate name in locale '" + Context.getLocale() + "'"); validator.validate(anotherConcept, errors); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldSetAuditInfoIfAnyItemInTheChildCollectionsIsEdited() { Concept concept = conceptService.getConcept(3); Assert.assertNull(concept.getDateChanged()); Assert.assertNull(concept.getChangedBy()); ConceptDescription description = concept.getDescription(); Assert.assertNotNull(description); description.setDescription("changed to something else"); conceptService.saveConcept(concept); Assert.assertNotNull(concept.getDateChanged()); Assert.assertNotNull(concept.getChangedBy()); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldKeepIdForNewConceptIfOneIsSpecified() { Integer conceptId = 343434; // a nonexistent concept id; Assert.assertNull(conceptService.getConcept(conceptId)); // sanity check Concept concept = new Concept(); ConceptName cn = new ConceptName("Weight", Context.getLocale()); concept.addName(cn); concept.addDescription(new ConceptDescription("some description",null)); concept.setConceptId(conceptId); concept.setDatatype(Context.getConceptService().getConceptDatatypeByName("Numeric")); concept.setConceptClass(Context.getConceptService().getConceptClassByName("Finding")); concept = Context.getConceptService().saveConcept(concept); assertTrue(concept.getConceptId().equals(conceptId)); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldSetAuditInfoIfAnItemIsAddedToAnyOfItsChildCollections() { Concept concept = conceptService.getConcept(3); Assert.assertNull(concept.getDateChanged()); Assert.assertNull(concept.getChangedBy()); ConceptDescription description = new ConceptDescription("new description",Context.getLocale()); concept.addDescription(description); conceptService.saveConcept(concept); Assert.assertNotNull(description.getConceptDescriptionId()); Assert.assertNotNull(concept.getDateChanged()); Assert.assertNotNull(concept.getChangedBy()); }