Search in sources :

Example 1 with Data

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

the class DemoRamlRestTest method postData.

/**
 * for POST
 */
private void postData(TestContext context, String url, Buffer buffer, int errorCode, int mode, String contenttype, String tenant, boolean userIdHeader) {
    // save stacktrace for async handler
    Exception stacktrace = new RuntimeException();
    Async async = context.async();
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest request = null;
    if (mode == 0) {
        request = client.putAbs(url);
    } else if (mode == 1) {
        request = client.postAbs(url);
    } else {
        request = client.deleteAbs(url);
    }
    request.exceptionHandler(error -> {
        async.complete();
        context.fail(new RuntimeException(error.getMessage(), stacktrace));
    }).handler(response -> {
        int statusCode = response.statusCode();
        // is it 2XX
        log.info(statusCode + ", " + errorCode + " expected status at " + System.currentTimeMillis() + " mode " + mode + " for " + url);
        if (statusCode == errorCode) {
            if (statusCode == 422) {
                String str = response.getHeader("Content-type");
                if (str != null && str.contains("application/json")) {
                    context.assertTrue(true);
                } else {
                    context.fail(new RuntimeException("422 response code should contain a content type header of application/json", stacktrace));
                }
            } else if (statusCode == 201) {
                response.bodyHandler(responseData -> {
                    String date = (String) new JsonPathParser(responseData.toJsonObject()).getValueAt("metadata.createdDate");
                    if (date == null && userIdHeader) {
                        context.fail(new RuntimeException("metaData schema createdDate missing from returned json", stacktrace));
                    }
                });
            }
            context.assertTrue(true);
        } else {
            response.bodyHandler(responseData -> {
                context.fail(new RuntimeException("got unexpected response code, expected: " + errorCode + ", received code: " + statusCode + " mode " + mode + " for url " + url + "\ndata:" + responseData.toString(), stacktrace));
            });
        }
        if (!async.isCompleted()) {
            async.complete();
        }
    });
    request.setChunked(true);
    request.putHeader("X-Okapi-Request-Id", "999999999999");
    if (tenant != null) {
        request.putHeader("x-okapi-tenant", tenant);
    }
    request.putHeader("Accept", "application/json,text/plain");
    if (userIdHeader) {
        request.putHeader("X-Okapi-User-Id", "af23adf0-61ba-4887-bf82-956c4aae2260");
    }
    if (contenttype != null) {
        request.putHeader("Content-type", contenttype);
    } else {
        if (mode == 0 || mode == 2) {
            request.putHeader("Content-type", "application/json");
        } else {
            request.putHeader("Content-type", "multipart/form-data; boundary=MyBoundary");
        }
    }
    if (buffer != null) {
        request.write(buffer);
    }
    request.end();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RestVerticle(org.folio.rest.RestVerticle) BeforeClass(org.junit.BeforeClass) AES(org.folio.rest.security.AES) Date(java.util.Date) MimeBodyPart(javax.mail.internet.MimeBodyPart) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) VertxUtils(org.folio.rest.tools.utils.VertxUtils) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) BodyPart(javax.mail.BodyPart) BigDecimal(java.math.BigDecimal) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Locale(java.util.Locale) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) JsonObject(io.vertx.core.json.JsonObject) Datetime(org.folio.rest.jaxrs.model.Datetime) Metadata(org.folio.rest.jaxrs.model.Metadata) Logger(io.vertx.core.logging.Logger) MimeMultipart(javax.mail.internet.MimeMultipart) AfterClass(org.junit.AfterClass) RmbtestsClient(org.folio.rest.client.RmbtestsClient) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) Test(org.junit.Test) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) JsonPathParser(org.folio.rest.tools.parser.JsonPathParser) Data(org.folio.rest.jaxrs.model.Data) File(java.io.File) NetworkUtils(org.folio.rest.tools.utils.NetworkUtils) Book(org.folio.rest.jaxrs.model.Book) IOUtils(org.apache.commons.io.IOUtils) Messages(org.folio.rest.tools.messages.Messages) List(java.util.List) InternetHeaders(javax.mail.internet.InternetHeaders) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) AdminClient(org.folio.rest.client.AdminClient) HttpMethod(io.vertx.core.http.HttpMethod) SecretKey(javax.crypto.SecretKey) PersistMethod(org.folio.rest.jaxrs.resource.AdminResource.PersistMethod) Handler(io.vertx.core.Handler) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.core.http.HttpClient) JsonPathParser(org.folio.rest.tools.parser.JsonPathParser) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with Data

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

the class DemoRamlRestTest method createBook.

private Book createBook() {
    int ran = ThreadLocalRandom.current().nextInt(0, 11);
    Book b = new Book();
    b.setStatus(99 + ran);
    b.setSuccess(true);
    b.setData(null);
    Data d = new Data();
    d.setAuthor("a" + ran);
    Datetime dt = new Datetime();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS\'Z\'");
    String parsedDate = format.format(new Date());
    dt.set$date(parsedDate);
    d.setDatetime(dt);
    d.setGenre("b");
    d.setDescription("c");
    d.setLink("d");
    d.setTitle("title" + ran);
    b.setData(d);
    return b;
}
Also used : Book(org.folio.rest.jaxrs.model.Book) Data(org.folio.rest.jaxrs.model.Data) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Datetime(org.folio.rest.jaxrs.model.Datetime)

Example 3 with Data

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

the class DemoRamlRestTest method test.

/**
 * just send a get request for books api with and without the required author query param
 * 1. one call should succeed and the other should fail (due to
 * validation aspect that should block the call and return 400)
 * 2. test the built in upload functionality
 * @param context - the test context
 */
@Test
public void test(TestContext context) throws Exception {
    Book b = new Book();
    Data d = new Data();
    d.setAuthor("a");
    d.setGenre("g");
    d.setDescription("asdfss");
    b.setData(d);
    ObjectMapper om = new ObjectMapper();
    String book = om.writerWithDefaultPrettyPrinter().writeValueAsString(b);
    // check File Uploads
    postData(context, "http://localhost:" + port + "/admin/uploadmultipart", getBody("uploadtest.json", true), 200, 1, null, null, false);
    postData(context, "http://localhost:" + port + "/admin/uploadmultipart?file_name=test.json", getBody("uploadtest.json", true), 200, 1, null, null, false);
    postData(context, "http://localhost:" + port + "/rmbtests/test", Buffer.buffer(book), 200, 1, "application/json", TENANT, false);
    d.setDatetime(new Datetime());
    d.setTitle("title");
    d.setLink("link");
    b.setStatus(0);
    b.setSuccess(true);
    book = om.writerWithDefaultPrettyPrinter().writeValueAsString(b);
    postData(context, "http://localhost:" + port + "/rmbtests/books", Buffer.buffer(book), 201, 1, "application/json", TENANT, true);
    postData(context, "http://localhost:" + port + "/rmbtests/books", Buffer.buffer(book), 201, 1, "application/json", TENANT, false);
    // check that additionalProperties (fields not appearing in schema) - returns 422
    JsonObject jo = new JsonObject(book);
    jo.put("lalala", "non existant");
    postData(context, "http://localhost:" + port + "/rmbtests/books", Buffer.buffer(jo.encode()), 422, 1, "application/json", TENANT, false);
    postData(context, "http://localhost:" + port + "/admin/loglevel?level=FINE&java_package=org", null, 200, 0, "application/json", TENANT, false);
    Metadata md = new Metadata();
    md.setCreatedByUserId("12345678-1234-1234-1234-123456789098");
    md.setCreatedByUsername("you");
    md.setCreatedDate(new Date());
    md.setUpdatedDate(new Date());
    md.setUpdatedByUserId("123456789098");
    b.setMetadata(md);
    postData(context, "http://localhost:" + port + "/rmbtests/books", Buffer.buffer(om.writerWithDefaultPrettyPrinter().writeValueAsString(b)), 422, 1, "application/json", TENANT, false);
    md.setUpdatedByUserId("12345678-1234-1234-1234-123456789098");
    postData(context, "http://localhost:" + port + "/rmbtests/books", Buffer.buffer(om.writerWithDefaultPrettyPrinter().writeValueAsString(b)), 201, 1, "application/json", TENANT, false);
    List<Object> list = getListOfBooks();
    // should be 200
    checkURLs(context, "http://localhost:" + port + "/apidocs/index.html", 200);
    // should be 200
    checkURLs(context, "http://localhost:" + port + "/admin/loglevel", 200);
    // use generated client
    checkClientCode(context);
    RmbtestsClient testClient = new RmbtestsClient("localhost", port, "abc", "abc", false);
    String[] facets = new String[] { "author:10", "name:5" };
    testClient.getBooks("aaa", new BigDecimal(1999), null, null, facets, handler -> {
        if (handler.statusCode() != 200) {
            context.fail();
        } else {
            log.info(handler.statusCode() + "----------------------------------------- passed ---------------------------");
        }
    });
}
Also used : Metadata(org.folio.rest.jaxrs.model.Metadata) JsonObject(io.vertx.core.json.JsonObject) Data(org.folio.rest.jaxrs.model.Data) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Book(org.folio.rest.jaxrs.model.Book) JsonObject(io.vertx.core.json.JsonObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Datetime(org.folio.rest.jaxrs.model.Datetime) RmbtestsClient(org.folio.rest.client.RmbtestsClient) Test(org.junit.Test)

Example 4 with Data

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

the class DemoRamlRestTest method postBook.

private void postBook(TestContext context, String parameterString, int expectedStatus) {
    Book b = new Book();
    Data d = new Data();
    d.setAuthor("a");
    d.setGenre("g");
    d.setDescription("asdfss");
    b.setData(d);
    ObjectMapper om = new ObjectMapper();
    String book = "";
    try {
        book = om.writerWithDefaultPrettyPrinter().writeValueAsString(b);
    } catch (JsonProcessingException e) {
        context.fail(e);
    }
    postData(context, "http://localhost:" + port + "/rmbtests/books", Buffer.buffer(book), expectedStatus, 1, "application/json", TENANT, false);
}
Also used : Book(org.folio.rest.jaxrs.model.Book) Data(org.folio.rest.jaxrs.model.Data) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Book (org.folio.rest.jaxrs.model.Book)4 Data (org.folio.rest.jaxrs.model.Data)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Date (java.util.Date)3 Datetime (org.folio.rest.jaxrs.model.Datetime)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonObject (io.vertx.core.json.JsonObject)2 BigDecimal (java.math.BigDecimal)2 SimpleDateFormat (java.text.SimpleDateFormat)2 RmbtestsClient (org.folio.rest.client.RmbtestsClient)2 Metadata (org.folio.rest.jaxrs.model.Metadata)2 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpClient (io.vertx.core.http.HttpClient)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 HttpMethod (io.vertx.core.http.HttpMethod)1 Logger (io.vertx.core.logging.Logger)1