SpaceWritableValidator implements ConstraintValidator<SpaceWritable, SpaceForm> { @Override public boolean isValid(SpaceForm spaceForm, ConstraintValidatorContext context) { try { ContentStore contentStore = contentStoreFactory.create(spaceForm.getCredentialsForm(), false); String spaceId = spaceForm.getSpaceId(); String contentId = ".duracloud-dummy-item-" + System.currentTimeMillis(); contentStore.deleteContent(spaceId, contentId); return true; } catch (UnauthorizedException e) { log.error(e.getMessage(), e); context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(constraintAnnotation.message()) .addNode("spaceId") .addConstraintViolation(); return false; } catch (NotFoundException ex) { return true; } catch (Exception ex) { throw new RuntimeException("unexpected exception!:" + ex.getMessage(), ex); } } @Autowired  SpaceWritableValidator(ContentStoreFactory contentStoreFactory); @Override boolean isValid(SpaceForm spaceForm,
                           ConstraintValidatorContext context); @Override void initialize(SpaceWritable constraintAnnotation);  }
@Test public void testIsValid() throws Exception { ContentStore cs = createMock(ContentStore.class); ContentStoreFactory csf = createMock(ContentStoreFactory.class); EasyMock.expect(csf.create(EasyMock.anyObject(DuracloudCredentialsForm.class), EasyMock.eq(false))) .andReturn(cs); cs.deleteContent(EasyMock.isA(String.class), EasyMock.isA(String.class)); EasyMock.expectLastCall().once(); ConstraintValidatorContext cvc = createMock(ConstraintValidatorContext.class); replay(); SpaceWritableValidator v = new SpaceWritableValidator(csf); SpaceForm f = new SpaceForm(); f.setSpaceId("testSpace"); Assert.assertTrue(v.isValid(f, cvc)); }