### Question:
CitationConceptValidator implements Validator { @Override public void validate(Object target, Errors errors) { CitationConceptForm conceptForm = (CitationConceptForm) target; IUser user = (IUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (!errors.hasErrors()) { ICitationConcept concept = conceptManager.getByUriAndOwner(conceptForm.getUri(), user); if (conceptForm.getConceptId() != null) { ICitationConcept dbConcept = conceptManager.get(conceptForm.getConceptId()); if (!dbConcept.getOwner().getUsername().equals(user.getUsername())) { errors.rejectValue("uri", "uri", "Only the owner can edit a Concept."); } else { if (!dbConcept.getUri().equals(conceptForm.getUri()) && concept != null) { errors.rejectValue("uri", "uri", "Concept with this uri already exists!"); } } } else { if (concept != null) { errors.rejectValue("uri", "uri", "Concept with this uri already exists!"); } } } }  @Override boolean supports(Class<?> paramClass); @Override void validate(Object target, Errors errors);  }### Answer:
@Test public void test_addConcept_success() { Mockito.when(conceptManager.getByUriAndOwner(conceptForm.getUri(), user)).thenReturn(null); citationConceptValidator.validate(conceptForm, errors); assertTrue(!errors.hasErrors()); }
@Test public void test_addConcept_uriExists() { Mockito.when(conceptManager.getByUriAndOwner(conceptForm.getUri(), user)).thenReturn(concept); citationConceptValidator.validate(conceptForm, errors); assertNotNull(errors.getFieldError(URI_STRING)); }
@Test public void test_editConcept_success() { conceptForm.setConceptId(CONCEPT_ID); dbConcept.setUri(URI_EXISTING); Mockito.when(conceptManager.get(CONCEPT_ID)).thenReturn(dbConcept); Mockito.when(conceptManager.getByUriAndOwner(conceptForm.getUri(), user)).thenReturn(null); citationConceptValidator.validate(conceptForm, errors); assertTrue(!errors.hasErrors()); }
@Test public void test_editConcept_uriExists() { conceptForm.setConceptId(CONCEPT_ID); dbConcept.setUri(URI_EXISTING); Mockito.when(conceptManager.get(CONCEPT_ID)).thenReturn(dbConcept); Mockito.when(conceptManager.getByUriAndOwner(conceptForm.getUri(), user)).thenReturn(concept); citationConceptValidator.validate(conceptForm, errors); assertNotNull(errors.getFieldError(URI_STRING)); }
@Test public void test_editConcept_ownerOnly() { User user1 = new User(); user1.setUsername("user1"); conceptForm.setConceptId(CONCEPT_ID); dbConcept.setOwner(user1); Mockito.when(conceptManager.get(CONCEPT_ID)).thenReturn(dbConcept); Mockito.when(conceptManager.getByUriAndOwner(conceptForm.getUri(), user)).thenReturn(concept); citationConceptValidator.validate(conceptForm, errors); assertNotNull(errors.getFieldError(URI_STRING)); }