### Question:
ConceptTypeValidator implements Validator { @Override public void validate(Object target, Errors errors) { ConceptTypeForm conceptTypeForm = (ConceptTypeForm) target; IUser user = (IUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (!errors.hasErrors()) { IConceptType type = conceptTypeManager.getByUriAndOwner(conceptTypeForm.getUri(), user); if (conceptTypeForm.getConceptTypeId() != null) { IConceptType dbConceptType = conceptTypeManager.get(conceptTypeForm.getConceptTypeId()); if (!dbConceptType.getOwner().getUsername().equals(user.getUsername())) { errors.rejectValue("uri", "uri", "Only the owner can edit a Concept Type."); } else { if (!dbConceptType.getUri().equals(conceptTypeForm.getUri()) && type != null) { errors.rejectValue("uri", "uri", "Concept Type with this uri already exists!"); } } } else { if (type != null) { errors.rejectValue("uri", "uri", "Concept Type with this uri already exists!"); } } } }  @Override boolean supports(Class<?> paramClass); @Override void validate(Object target, Errors errors);  }### Answer:
@Test public void test_addConceptType_success() { Mockito.when(conceptTypeManager.getByUriAndOwner(conceptTypeForm.getUri(), user)).thenReturn(null); conceptTypeValidator.validate(conceptTypeForm, errors); assertTrue(!errors.hasErrors()); }
@Test public void test_addConceptType_uriExists() { Mockito.when(conceptTypeManager.getByUriAndOwner(conceptTypeForm.getUri(), user)).thenReturn(conceptType); conceptTypeValidator.validate(conceptTypeForm, errors); assertNotNull(errors.getFieldError(URI_STRING)); }
@Test public void test_editConceptType_success() { conceptTypeForm.setConceptTypeId(CONCEPT_TYPE_ID); dbConceptType.setUri(URI_EXISTING); Mockito.when(conceptTypeManager.get(CONCEPT_TYPE_ID)).thenReturn(dbConceptType); Mockito.when(conceptTypeManager.getByUriAndOwner(conceptTypeForm.getUri(), user)).thenReturn(null); conceptTypeValidator.validate(conceptTypeForm, errors); assertTrue(!errors.hasErrors()); }
@Test public void test_editConceptType_uriExists() { conceptTypeForm.setConceptTypeId(CONCEPT_TYPE_ID); dbConceptType.setUri(URI_EXISTING); Mockito.when(conceptTypeManager.get(CONCEPT_TYPE_ID)).thenReturn(dbConceptType); Mockito.when(conceptTypeManager.getByUriAndOwner(conceptTypeForm.getUri(), user)).thenReturn(conceptType); conceptTypeValidator.validate(conceptTypeForm, errors); assertNotNull(errors.getFieldError(URI_STRING)); }
@Test public void test_editConceptType_ownerOnly() { User user1 = new User(); user1.setUsername("user1"); conceptTypeForm.setConceptTypeId(CONCEPT_TYPE_ID); dbConceptType.setOwner(user1); Mockito.when(conceptTypeManager.get(CONCEPT_TYPE_ID)).thenReturn(dbConceptType); Mockito.when(conceptTypeManager.getByUriAndOwner(conceptTypeForm.getUri(), user)).thenReturn(conceptType); conceptTypeValidator.validate(conceptTypeForm, errors); assertNotNull(errors.getFieldError(URI_STRING)); }