Search in sources :

Example 1 with Books

use of org.folio.rest.jaxrs.model.Books in project raml-module-builder by folio-org.

the class DemoRamlRestTest method getBookWithRoutingContext.

@Test
public void getBookWithRoutingContext(TestContext context) throws Exception {
    Buffer buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test?query=nullpointer%3Dtrue", 500);
    context.assertEquals("java.lang.NullPointerException", buf.toString());
    Books books;
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test", 200);
    books = Json.decodeValue(buf, Books.class);
    context.assertEquals(0, books.getTotalRecords());
    context.assertEquals(0, books.getResultInfo().getDiagnostics().size());
    context.assertEquals(0, books.getResultInfo().getTotalRecords());
    context.assertEquals(0, books.getBooks().size());
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test?query=title%3Dwater", 200);
    books = Json.decodeValue(buf, Books.class);
    context.assertEquals(0, books.getTotalRecords());
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test?query=a%3D", 400);
    context.assertTrue(buf.toString().contains("expected index or term, got EOF"));
    Data d = new Data();
    d.setAuthor("a");
    d.setGenre("g");
    d.setDescription("description1");
    d.setTitle("title");
    d.setDatetime(new Datetime());
    d.setLink("link");
    Book b = new Book();
    b.setData(d);
    b.setStatus(0);
    b.setSuccess(true);
    ObjectMapper om = new ObjectMapper();
    String book = om.writerWithDefaultPrettyPrinter().writeValueAsString(b);
    postData(context, "http://localhost:" + port + "/rmbtests/test", Buffer.buffer(book), 201, HttpMethod.POST, null, TENANT, false);
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test", 200);
    books = Json.decodeValue(buf, Books.class);
    context.assertEquals(1, books.getTotalRecords());
    context.assertEquals(0, books.getResultInfo().getDiagnostics().size());
    context.assertEquals(1, books.getResultInfo().getTotalRecords());
    context.assertEquals(1, books.getBooks().size());
    d.setDescription("description2");
    book = om.writerWithDefaultPrettyPrinter().writeValueAsString(b);
    postData(context, "http://localhost:" + port + "/rmbtests/test", Buffer.buffer(book), 201, HttpMethod.POST, "application/json", TENANT, false);
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test", 200);
    books = Json.decodeValue(buf, Books.class);
    context.assertEquals(2, books.getTotalRecords());
    context.assertEquals(0, books.getResultInfo().getDiagnostics().size());
    context.assertEquals(2, books.getResultInfo().getTotalRecords());
    context.assertEquals(2, books.getBooks().size());
    // need at least one record in result before we can trigger this error
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test?query=badclass%3Dtrue", 200);
    books = Json.decodeValue(buf, Books.class);
    context.assertEquals(2, books.getTotalRecords());
    context.assertEquals(1, books.getResultInfo().getDiagnostics().size());
    context.assertTrue(books.getResultInfo().getDiagnostics().get(0).getMessage().contains("Cannot deserialize"));
    context.assertEquals(2, books.getResultInfo().getTotalRecords());
    context.assertEquals(0, books.getBooks().size());
    // see that we can handle a subset of the Book properties: id and status
    // use case: replace "SELECT jsonb FROM ..." by "SELECT jsonb_build_object('id', id, 'status', jsonb->'status') FROM ..."
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test?query=slim%3Dtrue", 200);
    JsonObject jo = new JsonObject(buf);
    context.assertEquals(2, jo.getInteger("totalRecords"));
    SlimBook sb = jo.getJsonArray("books").getJsonObject(0).mapTo(SlimBook.class);
    context.assertEquals(0, sb.getStatus());
    context.assertNotNull(sb.getId());
    sb = jo.getJsonArray("books").getJsonObject(1).mapTo(SlimBook.class);
    context.assertEquals(0, sb.getStatus());
    context.assertNotNull(sb.getId());
    buf = checkURLs(context, "http://localhost:" + port + "/rmbtests/test?query=wrapper%3Dtrue", 200);
    books = Json.decodeValue(buf, Books.class);
    context.assertEquals(2, books.getTotalRecords());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Book(org.folio.rest.jaxrs.model.Book) JsonObject(io.vertx.core.json.JsonObject) Data(org.folio.rest.jaxrs.model.Data) Books(org.folio.rest.jaxrs.model.Books) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Datetime(org.folio.rest.jaxrs.model.Datetime) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonObject (io.vertx.core.json.JsonObject)1 Book (org.folio.rest.jaxrs.model.Book)1 Books (org.folio.rest.jaxrs.model.Books)1 Data (org.folio.rest.jaxrs.model.Data)1 Datetime (org.folio.rest.jaxrs.model.Datetime)1 Test (org.junit.Test)1