Search in sources :

Example 1 with Response

use of org.folio.rest.tools.client.Response in project raml-module-builder by folio-org.

the class HTTPMockTest method test.

@Test
public void test() {
    HttpClientInterface client = HttpClientFactory.getHttpClient("localhost", 8080, "zxc");
    try {
        CompletableFuture<Response> cf = client.request("auth_test2");
        System.out.println("-------------------------------------->" + cf.get().getBody());
        assertEquals("1", cf.get().getBody().getString("id"));
    } catch (Exception e) {
        assertTrue(false);
        e.printStackTrace();
    }
}
Also used : Response(org.folio.rest.tools.client.Response) HttpClientInterface(org.folio.rest.tools.client.interfaces.HttpClientInterface) Test(org.junit.Test)

Example 2 with Response

use of org.folio.rest.tools.client.Response in project raml-module-builder by folio-org.

the class ResponseTest method check.

@Test
public void check() {
    JsonObject j1 = null;
    JsonObject j2 = null;
    try {
        j1 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("pathTest.json"), "UTF-8"));
        j2 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("pathTest.json"), "UTF-8"));
        Response test1 = new Response();
        test1.setBody(j1);
        Response test2 = new Response();
        test2.setBody(j2);
        // replace value at c.a1 with the value at c.arr[1] and check
        JsonPathParser jpp = new JsonPathParser(test1.joinOn("c.a1", test2, "a", "c.arr[1]").getBody());
        assertEquals("true", jpp.getValueAt("c.a1.ignore"));
        // ///////////////////////////////////////////////////////////
        // non existent path creation test - when passing in a Response as the first parameter
        // we are asking to use the two response parameters as the values to join on, and then populate
        // the response object with a specific value from from the 2nd response parameter
        Response testSetNonExistPath1 = new Response();
        Response r1 = new Response();
        JsonObject j3 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("pathTest.json"), "UTF-8"));
        testSetNonExistPath1.setBody(j3);
        // populate r1 with the content from c.arr[1] - this will be the only content in r1
        r1.joinOn(testSetNonExistPath1, "c.a1", test2, "a", "c.arr[1]");
        assertEquals(new JsonObject("{\"c\":{\"a1\":{\"ignore\":\"true\"}}}").encode(), r1.getBody().encode());
        // //////////////////////////////////////////////////////////////
        // try to replace value at c.a1 with a value from a non existent field (c.a[5]) with the
        // param allowNulls set to false so that the value is not updated
        new JsonPathParser(test1.joinOn("c.a1", test2, "a", "c.arr[5]", false).getBody());
        assertEquals(jpp.getValueAt("c.a1.ignore"), "true");
        // non existent insertField with allowNulls set to true (default)
        Response r2 = new Response();
        r2.setBody(j1);
        assertNull(new JsonPathParser(r2.joinOn("c.a1", test2, "a", "c.arr[5]").getBody()).getValueAt("c.a1"));
        // //////////////////////////////////////////////////////////////////
        // check non existent path
        assertNull(jpp.getValueAt("c.a1.ignores!SA"));
        // do not allow nulls , so that the c.a9 field which does not exist
        // will not be added since the c.arr[5] is null
        assertFalse(test1.joinOn("c.a9", test2, "a", "c.arr[5]", false).getBody().getJsonObject("c").containsKey("a9"));
        assertTrue(test1.joinOn("c.a9", test2, "a", "c.arr[5]", true).getBody().getJsonObject("c").containsKey("a9"));
        // pass all params to joinOn
        r2.joinOn("b", test2, "c.arr[0].a2.arr2[1].a30", "c.arr[0].a2.arr2[2].arr3[0]", "a", false);
        assertEquals("5", r2.getBody().getJsonObject("a").getString("a32"));
        String val = (String) new JsonPathParser(r2.joinOn("b", test2, "c.arr[0].a2.arr2[1].a30", "c.arr[0]", "a", false).getBody()).getValueAt("a.a3");
        assertEquals("a23", val);
        // one to many join (a json with one object joined with an array of jsons - where the one json
        // matches multiple entries in the array.
        // json to join on has the same id multiple times with different values for each id.
        // match with this json so that all values are inserted as an array
        j2 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("joinTest.json"), "UTF-8"));
        j1 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("pathTest.json"), "UTF-8"));
        Response resp1 = new Response();
        resp1.setBody(j1);
        Response resp2 = new Response();
        resp2.setBody(j2);
        JsonArray arr = (JsonArray) new JsonPathParser(resp1.joinOn("a", resp2, "arr2[*].id", "../a31", "b", false).getBody()).getValueAt("b");
        assertEquals(3, arr.size());
        // test non existent paths (populate new response with array content) one to many
        Response r = new Response();
        r.joinOn(resp1, "a", resp2, "arr2[*].id", "../a31", "b", false);
        JsonArray arr2 = (JsonArray) new JsonPathParser(r.getBody()).getValueAt("b");
        assertEquals(new JsonObject("{\"b\":[\"1\",\"2\",\"3\"]}").encode(), r.getBody().encode());
        // ///////////////////////////////////////////////////////////////////////
        // test many to many -
        // in the case below will create an array of values for every joined match
        j2 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("joinTest.json"), "UTF-8"));
        j1 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("joinTest.json"), "UTF-8"));
        resp1 = new Response();
        resp1.setBody(j1);
        resp2 = new Response();
        resp2.setBody(j2);
        JsonObject jo1 = resp1.joinOn("arr2[*].id", resp2, "arr2[*].id", "../a31", "../id", false).getBody();
        // will produce (notice that the a31 field value is pushed into the id field for every entry
        // {"arr2":[
        // {"id":["1","2","3"],"a31":"1"},
        // {"id":["1","2","3"],"a31":"2"},
        // {"id":["1","2","3"],"a31":"3"},
        // {"id":["1","2","3"],"a32":"4"},
        // {"id":"4","a31":"4","a32":"5"}
        // ]}
        System.out.println(jo1.encode());
        List<?> listOfVals = (List<?>) new JsonPathParser(jo1).getValueAt("arr2[*].id");
        assertEquals(5, listOfVals.size());
        assertEquals("4", jo1.getJsonArray("arr2").getJsonObject(4).getString("id"));
        // test with numbers / boolean
        j2 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("pathTest.json"), "UTF-8"));
        j1 = new JsonObject(IOUtils.toString(JsonPathParser.class.getClassLoader().getResourceAsStream("pathTest.json"), "UTF-8"));
        resp1 = new Response();
        resp1.setBody(j1);
        resp2 = new Response();
        resp2.setBody(j2);
        resp1.joinOn("number", resp2, "number", "boolean", "number", false);
        assertTrue(resp1.getBody().getBoolean("number"));
        resp1.joinOn("boolean", resp2, "boolean", "number", "number", false);
        assertTrue(99 == resp1.getBody().getInteger("number"));
        /*      j1 = new JsonObject(
        IOUtils.toString(JsonPathParser.class.getClassLoader().
          getResourceAsStream("compositeTest1.json"), "UTF-8"));*/
        // resp2.setBody(j1);
        Response r2d2 = new Response();
        r2d2.mapFrom(resp1, "c.arr[*]", "compositeUsers.compositeUser", "users", true);
        r2d2.joinOn(r2d2, "compositeUsers.compositeUser[*].users.a3", resp1, "c.arr[*].a3", "c.a1", "../../groups", true);
        assertNull(new JsonPathParser(r2d2.getBody()).getValueAt("compositeUsers.compositeUser[2].groups"));
        Response testMap1 = new Response();
        testMap1.mapFrom(resp1, "c.arr[0].a2", "mapped", null, true);
        assertEquals("aaa.bbb", new JsonPathParser(testMap1.getBody()).getValueAt("mapped.'aaa.ccc'"));
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : Response(org.folio.rest.tools.client.Response) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonPathParser(org.folio.rest.tools.parser.JsonPathParser) List(java.util.List) Test(org.junit.Test)

Example 3 with Response

use of org.folio.rest.tools.client.Response in project raml-module-builder by folio-org.

the class BuildCQLTest method check.

@Test
public void check() {
    try {
        JsonObject j = new JsonObject();
        JsonArray j22 = new JsonArray();
        JsonArray j33 = new JsonArray();
        j.put("arr", j22);
        j.put("arr2", j33);
        j22.add("librarian3");
        j22.add("librarian2");
        JsonObject j44 = new JsonObject();
        j44.put("o", new JsonObject("{\"bbb\":\"aaa\"}"));
        j33.add(j44);
        Response r = new Response();
        r.setBody(j);
        String g1 = new BuildCQL(r, "arr2[0]", "group").buildCQL();
        assertEquals("?query=group=={\"o\":{\"bbb\":\"aaa\"}}", URLDecoder.decode(g1, "UTF-8"));
        String g2 = new BuildCQL(r, "arr", "group").buildCQL();
        assertEquals("?query=group==[\"librarian3\",\"librarian2\"]", URLDecoder.decode(g2, "UTF-8"));
        String g3 = new BuildCQL(r, "arr[0]", "group").buildCQL();
        assertEquals("?query=group==librarian3", URLDecoder.decode(g3, "UTF-8"));
        String g4 = new BuildCQL(r, "arr[*]", "group").buildCQL();
        assertEquals("?query=group==librarian3 or group==librarian2", URLDecoder.decode(g4, "UTF-8"));
        String g5 = new BuildCQL(r, "arr[*]", "group", "query1").buildCQL();
        assertEquals("?query1=group==librarian3 or group==librarian2", URLDecoder.decode(g5, "UTF-8"));
        String g6 = new BuildCQL(r, "arr[*]", "group", "query1", false, "and").buildCQL();
        assertEquals("&query1=group==librarian3 and group==librarian2", URLDecoder.decode(g6, "UTF-8"));
        // build cql by requesting values from a non existent field in the json, should be an emptry string ""
        String g7 = new BuildCQL(r, "arr3", "group").buildCQL();
        assertEquals("", URLDecoder.decode(g7, "UTF-8"));
        String g8 = new BuildCQL(r, "arr[*]", "group", "query1", false, "and", "=").buildCQL();
        assertEquals("&query1=group=librarian3 and group=librarian2", URLDecoder.decode(g8, "UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Response(org.folio.rest.tools.client.Response) BuildCQL(org.folio.rest.tools.client.BuildCQL) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 4 with Response

use of org.folio.rest.tools.client.Response in project raml-module-builder by folio-org.

the class HttpClientMock2 method getCF.

private CompletableFuture<Response> getCF(HttpMethod method, String endpoint) throws Exception {
    log.info("MOCKING URL: " + endpoint);
    JsonArray jar = mockJson.getJsonArray("mocks");
    int size = jar.size();
    for (int i = 0; i < size; i++) {
        JsonObject j = jar.getJsonObject(i);
        String url = j.getString("url");
        String method1 = j.getString("method");
        if (endpoint.equalsIgnoreCase(url) && method.toString().equalsIgnoreCase(method1)) {
            Response r = new Response();
            JsonObject body = null;
            String path2data = j.getString("receivedPath");
            try {
                if (path2data != null && path2data.trim().length() > 0) {
                    body = new JsonObject(IOUtils.toString(HttpClientMock2.class.getClassLoader().getResourceAsStream(path2data), "UTF-8"));
                }
            } catch (IOException e) {
                log.warn("unable to read in json content at " + path2data);
            }
            if (body == null || body.isEmpty()) {
                try {
                    body = j.getJsonObject("receivedData");
                } catch (Exception e) {
                    log.warn("unable to read in json content from receivedData field for endpoint" + endpoint);
                }
            }
            r.setBody(body);
            Integer status = j.getInteger("status");
            if (status != null) {
                r.setCode(status);
            }
            r.setEndpoint(endpoint);
            JsonArray headers = j.getJsonArray("headers");
            if (headers != null) {
                MultiMap mm = MultiMap.caseInsensitiveMultiMap();
                // List<JsonObject> hList = headers.getList();
                int size2 = headers.size();
                for (int j1 = 0; j1 < size2; j1++) {
                    String headerName = headers.getJsonObject(j1).getString("name");
                    String headerValue = headers.getJsonObject(j1).getString("value");
                    mm.add(headerName, headerValue);
                }
                r.setHeaders(mm);
            }
            CompletableFuture<Response> cf = new CompletableFuture<>();
            cf.complete(r);
            return cf;
        }
    }
    CompletableFuture<Response> cf = new CompletableFuture<>();
    Response r = new Response();
    r.setCode(0);
    r.setEndpoint(endpoint);
    r.setError(new JsonObject("{\"endpoint\":\"" + endpoint + "\",\"statusCode\": 0 , \"errorMessage\":\"mocked error\"}"));
    cf.complete(r);
    return cf;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Response(org.folio.rest.tools.client.Response) MultiMap(io.vertx.core.MultiMap) CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

Response (org.folio.rest.tools.client.Response)4 JsonArray (io.vertx.core.json.JsonArray)3 JsonObject (io.vertx.core.json.JsonObject)3 Test (org.junit.Test)3 MultiMap (io.vertx.core.MultiMap)1 IOException (java.io.IOException)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 BuildCQL (org.folio.rest.tools.client.BuildCQL)1 HttpClientInterface (org.folio.rest.tools.client.interfaces.HttpClientInterface)1 JsonPathParser (org.folio.rest.tools.parser.JsonPathParser)1