### Question:
ConceptNameSaveHandler implements SaveHandler<ConceptName> { @Override public void handle(ConceptName conceptName, User currentUser, Date currentDate, String reason) { if (conceptName.getTags() != null) { Collection<ConceptNameTag> replacementTags = new ArrayList<>(); Iterator<ConceptNameTag> tagsIt = conceptName.getTags().iterator(); while (tagsIt.hasNext()) { ConceptNameTag tag = tagsIt.next(); if (tag.getConceptNameTagId() == null) { ConceptNameTag replacementTag = Context.getConceptService().getConceptNameTagByName(tag.getTag()); if (replacementTag != null) { tagsIt.remove(); replacementTags.add(replacementTag); } } } if (!replacementTags.isEmpty()) { conceptName.getTags().addAll(replacementTags); } } }  @Override void handle(ConceptName conceptName, User currentUser, Date currentDate, String reason);  }### Answer:
@Test public void handle_shouldNotFailIfTagsIsNull() { ConceptNameSaveHandler handler = new ConceptNameSaveHandler(); ConceptName name = new ConceptName(); name.setTags(null); handler.handle(name, null, null, null); }
@Test public void handle_shouldReplaceTagsWithoutIdsWithDatabaseFetchedTag() { ConceptNameSaveHandler handler = new ConceptNameSaveHandler(); ConceptName name = new ConceptName(); name.addTag("preferred"); name.addTag("short"); handler.handle(name, null, null, null); for (ConceptNameTag tag : name.getTags()) { if (tag.getTag().equals("preferred")) { Assert.assertEquals(4, tag.getConceptNameTagId().intValue()); } else if (tag.getTag().equals("short")) { Assert.assertEquals(2, tag.getConceptNameTagId().intValue()); } } }
@Test public void handle_shouldNotReplaceTagsWithoutIdsThatAreNotInTheDatabase() { ConceptNameSaveHandler handler = new ConceptNameSaveHandler(); ConceptName name = new ConceptName(); name.addTag(new ConceptNameTag("Some randome tag name", "")); handler.handle(name, null, null, null); ConceptNameTag newTag = name.getTags().iterator().next(); Assert.assertNull(newTag.getConceptNameTagId()); }
@Test public void handle_shouldNotReplaceTagsThatHaveIds() { ConceptNameSaveHandler handler = new ConceptNameSaveHandler(); ConceptName name = new ConceptName(); ConceptNameTag tag = new ConceptNameTag("some randome tag name with an id", ""); tag.setConceptNameTagId(34); name.addTag(tag); handler.handle(name, null, null, null); ConceptNameTag newTag = name.getTags().iterator().next(); Assert.assertEquals(34, newTag.getConceptNameTagId().intValue()); }