@Test public void returnsPartialValidatedRequestEntities() throws Exception { final Validated valid = Mockito.mock(Validated.class); Mockito.doReturn(Validated.class).when(valid).annotationType(); Mockito.when(valid.value()).thenReturn(new Class[] {Partial1.class, Partial2.class}); final ByteArrayInputStream entity = new ByteArrayInputStream("<PartialExample xmlns=\"\"><id>1</id><text>hello Cemo</text></PartialExample>".getBytes()); final Class<?> klass = PartialExample.class; final Object obj = provider.readFrom((Class<Object>) klass, PartialExample.class, new Annotation[] {valid}, MediaType.APPLICATION_XML_TYPE, new MultivaluedHashMap<>(), entity); assertThat(obj) .isInstanceOf(PartialExample.class); assertThat(((PartialExample) obj).id) .isEqualTo(1); }
@Test public void returnsPartialValidatedByGroupRequestEntities() throws Exception { final Validated valid = Mockito.mock(Validated.class); Mockito.doReturn(Validated.class).when(valid).annotationType(); Mockito.when(valid.value()).thenReturn(new Class[] {Partial1.class}); final ByteArrayInputStream entity = new ByteArrayInputStream("<Example xmlns=\"\"><id>1</id></Example>".getBytes()); final Class<?> klass = PartialExample.class; final Object obj = provider.readFrom((Class<Object>) klass, PartialExample.class, new Annotation[] {valid}, MediaType.APPLICATION_XML_TYPE, new MultivaluedHashMap<>(), entity); assertThat(obj) .isInstanceOf(PartialExample.class); assertThat(((PartialExample) obj).id) .isEqualTo(1); }
@Test public void throwsAnInvalidEntityExceptionForPartialValidatedRequestEntities() throws Exception { final Validated valid = Mockito.mock(Validated.class); Mockito.doReturn(Validated.class).when(valid).annotationType(); Mockito.when(valid.value()).thenReturn(new Class[] {Partial1.class, Partial2.class}); final ByteArrayInputStream entity = new ByteArrayInputStream("<Example xmlns=\"\"><id>1</id></Example>".getBytes()); try { final Class<?> klass = PartialExample.class; provider.readFrom((Class<Object>) klass, PartialExample.class, new Annotation[] {valid}, MediaType.APPLICATION_XML_TYPE, new MultivaluedHashMap<>(), entity); failBecauseExceptionWasNotThrown(ConstraintViolationException.class); } catch (ConstraintViolationException e) { assertThat(ConstraintViolations.formatUntyped(e.getConstraintViolations())) .containsOnly("text may not be null"); } }
@PUT @Path("/{id:\\d+}") public Device update( @PathParam("id") final Long id, @Validated(Validations.Update.class) final Device device ) { return deviceRepo.update(device); }
@PUT @Path("/{id}") public User updateUser( @PathParam("id") final long id, @Validated(Validations.Update.class) final User user ) { return userRepo.update(user); }
@PUT @Path("/{id}") public Claim update( @PathParam("id") final Long id, @Validated(Validations.Update.class) final Claim claim ) { return claimRepo.update(claim); }
private Class<?>[] findValidationGroups(Annotation[] annotations) { for (Annotation annotation : annotations) { if (annotation.annotationType() == Valid.class) { return DEFAULT_GROUP_ARRAY; } else if (annotation.annotationType() == Validated.class) { return ((Validated) annotation).value(); } } return null; }
/** * Copied from com.yammer.dropwizard.jersey.jackson.JacksonMessageBodyProvider#findValidationGroups() */ private Class<?>[] findValidationGroups(Annotation[] annotations) { for (Annotation annotation : annotations) { if (annotation.annotationType() == Valid.class) { return DEFAULT_GROUP_ARRAY; } else if (annotation.annotationType() == Validated.class) { return ((Validated) annotation).value(); } } return null; }
@POST public Device create(@Validated(Validations.Create.class) final Device device) { return deviceRepo.create(device); }
@POST public User createUser(@Validated(Validations.Create.class) final User device) { return userRepo.create(device); }
@POST public Claim create(@Validated(Validations.Create.class) final Claim claim) { return claimRepo.create(claim); }
public void withDropwizardValidation(@Validated() String foo) { }