Skip to content

Commit

Permalink
Adding a test to confirm behavior of a nested list
Browse files Browse the repository at this point in the history
  • Loading branch information
mveitas committed Feb 13, 2014
1 parent 7a8d232 commit 69011e4
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.dropwizard.jackson.Jackson;
import io.dropwizard.validation.ConstraintViolations;
import io.dropwizard.validation.Validated;
import org.hibernate.validator.constraints.NotEmpty;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -59,6 +60,13 @@ public boolean equals(Object obj) {
}
}

public static class ListExample {
@NotEmpty
@Valid
@JsonProperty
List<Example> examples;
}

public interface Partial1{}
public interface Partial2{}

Expand Down Expand Up @@ -519,4 +527,50 @@ public void throwsAnInvalidEntityExceptionForInvalidMapRequestEntities() throws
}
}

@Test
public void returnsValidatedEmbeddedListRequestEntities() throws IOException {
final Annotation valid = mock(Annotation.class);
doReturn(Valid.class).when(valid).annotationType();

final ByteArrayInputStream entity =
new ByteArrayInputStream("[ {\"examples\": [ {\"id\":1 } ] } ]".getBytes());
Class<?> klass = List.class;

final Object obj = provider.readFrom((Class<Object>) klass,
new TypeToken<List<ListExample>>() {}.getType(),
new Annotation[]{ valid },
MediaType.APPLICATION_JSON_TYPE,
new MultivaluedMapImpl(),
entity);

assertThat(obj)
.isInstanceOf(klass);

Iterator<ListExample> iterator = ((Iterable<ListExample>)obj).iterator();
assertThat(iterator.next().examples.get(0).id).isEqualTo(1);
}

@Test
public void throwsAnInvalidEntityExceptionForInvalidEmbeddedListRequestEntities() throws Exception {
final Annotation valid = mock(Annotation.class);
doReturn(Valid.class).when(valid).annotationType();

final ByteArrayInputStream entity =
new ByteArrayInputStream("[ {\"examples\": [ {\"id\":1 } ] }, { } ]".getBytes());

try {
final Class<?> klass = List.class;
provider.readFrom((Class<Object>) klass,
new TypeToken<List<ListExample>>() {}.getType(),
new Annotation[]{ valid },
MediaType.APPLICATION_JSON_TYPE,
new MultivaluedMapImpl(),
entity);
failBecauseExceptionWasNotThrown(ConstraintViolationException.class);
} catch (ConstraintViolationException e) {
assertThat(ConstraintViolations.formatUntyped(e.getConstraintViolations()))
.containsOnly("examples may not be empty (was null)");
}
}

}

0 comments on commit 69011e4

Please sign in to comment.