Search in sources :

Example 1 with Book

use of org.ecabrerar.examples.javax.rs.validation.domain.Book in project javaee7-firstcup by ecabrerar.

the class BooksCollectionTest method setup.

@BeforeClass
public static void setup() {
    books.add(new Book("Java EE development using GlassFish Aplication Server", "782345689", "David Heffinger"));
    books.add(new Book("Java 7 JAX-WS Web Services", "123456789", "Deepak Vohra"));
    books.add(new Book("Netbeans IDE7 CookBook", "2234555567", "Rhawi Dantas"));
    books.add(new Book("Getting Started with RESTful WebServices", "11233333", "Bhakti Mehta, Masoud Kalali"));
}
Also used : Book(org.ecabrerar.examples.javax.rs.validation.domain.Book) BeforeClass(org.junit.BeforeClass)

Example 2 with Book

use of org.ecabrerar.examples.javax.rs.validation.domain.Book in project javaee7-firstcup by ecabrerar.

the class SampleMessageBodyReader method readFrom.

@Override
public Book readFrom(Class<Book> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Book book = (Book) unmarshaller.unmarshal(entityStream);
        return book;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Book(org.ecabrerar.examples.javax.rs.validation.domain.Book) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 3 with Book

use of org.ecabrerar.examples.javax.rs.validation.domain.Book in project javaee7-firstcup by ecabrerar.

the class BooksCollectionTest method shouldReturnAValidationError.

@Test
public void shouldReturnAValidationError() {
    Book book = new Book("Effective Java", "2234555568", "");
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Book>> violations = validator.validate(book);
    Assert.assertEquals(2, violations.size());
    System.out.println(violations.stream().map(error -> error.getMessage()).collect(Collectors.joining(", ")));
}
Also used : Book(org.ecabrerar.examples.javax.rs.validation.domain.Book) ConstraintViolation(javax.validation.ConstraintViolation) Validator(javax.validation.Validator) Test(org.junit.Test)

Aggregations

Book (org.ecabrerar.examples.javax.rs.validation.domain.Book)3 ConstraintViolation (javax.validation.ConstraintViolation)1 Validator (javax.validation.Validator)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1