Search in sources :

Example 1 with GetGeoOptions

use of com.ibm.cloud.cloudant.v1.model.GetGeoOptions in project cloudant-java-sdk by IBM.

the class CloudantTest method testGetGeoAsStreamWOptions.

@Test
public void testGetGeoAsStreamWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"foo\": \"this is a mock response for JSON streaming\"}";
    String getGeoAsStreamPath = "/testString/_design/testString/_geo/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetGeoOptions model
    GetGeoOptions getGeoOptionsModel = new GetGeoOptions.Builder().db("testString").ddoc("testString").index("testString").bbox("testString").bookmark("testString").format("view").g("testString").includeDocs(false).lat(Double.valueOf("-90")).limit(Long.valueOf("0")).lon(Double.valueOf("-180")).nearest(false).radius(Double.valueOf("0")).rangex(Double.valueOf("0")).rangey(Double.valueOf("0")).relation("intersects").skip(Long.valueOf("0")).stale("ok").build();
    // Invoke operation with valid options model (positive test)
    Response<InputStream> response = cloudantService.getGeoAsStream(getGeoOptionsModel).execute();
    assertNotNull(response);
    InputStream responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("bbox"), "testString");
    assertEquals(query.get("bookmark"), "testString");
    assertEquals(query.get("format"), "view");
    assertEquals(query.get("g"), "testString");
    assertEquals(Boolean.valueOf(query.get("include_docs")), Boolean.valueOf(false));
    assertEquals(Double.valueOf(query.get("lat")), Double.valueOf("-90"));
    assertEquals(Long.valueOf(query.get("limit")), Long.valueOf("0"));
    assertEquals(Double.valueOf(query.get("lon")), Double.valueOf("-180"));
    assertEquals(Boolean.valueOf(query.get("nearest")), Boolean.valueOf(false));
    assertEquals(Double.valueOf(query.get("radius")), Double.valueOf("0"));
    assertEquals(Double.valueOf(query.get("rangex")), Double.valueOf("0"));
    assertEquals(Double.valueOf(query.get("rangey")), Double.valueOf("0"));
    assertEquals(query.get("relation"), "intersects");
    assertEquals(Long.valueOf(query.get("skip")), Long.valueOf("0"));
    assertEquals(query.get("stale"), "ok");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getGeoAsStreamPath);
    // Verify streamed JSON response
    java.util.Scanner s = new java.util.Scanner(responseObj).useDelimiter("\\A");
    String streamedResponseBody = s.hasNext() ? s.next() : "";
    assertEquals(streamedResponseBody, mockResponseBody);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) InputStream(java.io.InputStream) GetGeoOptions(com.ibm.cloud.cloudant.v1.model.GetGeoOptions) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with GetGeoOptions

use of com.ibm.cloud.cloudant.v1.model.GetGeoOptions in project cloudant-java-sdk by IBM.

the class Cloudant method getGeo.

/**
 * Query a geospatial index.
 *
 * Executes a query against the requested geospatial index from the specified design document.
 *
 * @param getGeoOptions the {@link GetGeoOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link GeoResult}
 */
public ServiceCall<GeoResult> getGeo(GetGeoOptions getGeoOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getGeoOptions, "getGeoOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", getGeoOptions.db());
    pathParamsMap.put("ddoc", getGeoOptions.ddoc());
    pathParamsMap.put("index", getGeoOptions.index());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_design/{ddoc}/_geo/{index}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getGeo");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (getGeoOptions.bbox() != null) {
        builder.query("bbox", String.valueOf(getGeoOptions.bbox()));
    }
    if (getGeoOptions.bookmark() != null) {
        builder.query("bookmark", String.valueOf(getGeoOptions.bookmark()));
    }
    if (getGeoOptions.format() != null) {
        builder.query("format", String.valueOf(getGeoOptions.format()));
    }
    if (getGeoOptions.g() != null) {
        builder.query("g", String.valueOf(getGeoOptions.g()));
    }
    if (getGeoOptions.includeDocs() != null) {
        builder.query("include_docs", String.valueOf(getGeoOptions.includeDocs()));
    }
    if (getGeoOptions.lat() != null) {
        builder.query("lat", String.valueOf(getGeoOptions.lat()));
    }
    if (getGeoOptions.limit() != null) {
        builder.query("limit", String.valueOf(getGeoOptions.limit()));
    }
    if (getGeoOptions.lon() != null) {
        builder.query("lon", String.valueOf(getGeoOptions.lon()));
    }
    if (getGeoOptions.nearest() != null) {
        builder.query("nearest", String.valueOf(getGeoOptions.nearest()));
    }
    if (getGeoOptions.radius() != null) {
        builder.query("radius", String.valueOf(getGeoOptions.radius()));
    }
    if (getGeoOptions.rangex() != null) {
        builder.query("rangex", String.valueOf(getGeoOptions.rangex()));
    }
    if (getGeoOptions.rangey() != null) {
        builder.query("rangey", String.valueOf(getGeoOptions.rangey()));
    }
    if (getGeoOptions.relation() != null) {
        builder.query("relation", String.valueOf(getGeoOptions.relation()));
    }
    if (getGeoOptions.skip() != null) {
        builder.query("skip", String.valueOf(getGeoOptions.skip()));
    }
    if (getGeoOptions.stale() != null) {
        builder.query("stale", String.valueOf(getGeoOptions.stale()));
    }
    ResponseConverter<GeoResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<GeoResult>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) GeoResult(com.ibm.cloud.cloudant.v1.model.GeoResult)

Example 3 with GetGeoOptions

use of com.ibm.cloud.cloudant.v1.model.GetGeoOptions in project cloudant-java-sdk by IBM.

the class GetGeoOptionsTest method testGetGeoOptions.

@Test
public void testGetGeoOptions() throws Throwable {
    GetGeoOptions getGeoOptionsModel = new GetGeoOptions.Builder().db("testString").ddoc("testString").index("testString").bbox("testString").bookmark("testString").format("view").g("testString").includeDocs(false).lat(Double.valueOf("-90")).limit(Long.valueOf("0")).lon(Double.valueOf("-180")).nearest(false).radius(Double.valueOf("0")).rangex(Double.valueOf("0")).rangey(Double.valueOf("0")).relation("intersects").skip(Long.valueOf("0")).stale("ok").build();
    assertEquals(getGeoOptionsModel.db(), "testString");
    assertEquals(getGeoOptionsModel.ddoc(), "testString");
    assertEquals(getGeoOptionsModel.index(), "testString");
    assertEquals(getGeoOptionsModel.bbox(), "testString");
    assertEquals(getGeoOptionsModel.bookmark(), "testString");
    assertEquals(getGeoOptionsModel.format(), "view");
    assertEquals(getGeoOptionsModel.g(), "testString");
    assertEquals(getGeoOptionsModel.includeDocs(), Boolean.valueOf(false));
    assertEquals(getGeoOptionsModel.lat(), Double.valueOf("-90"));
    assertEquals(getGeoOptionsModel.limit(), Long.valueOf("0"));
    assertEquals(getGeoOptionsModel.lon(), Double.valueOf("-180"));
    assertEquals(getGeoOptionsModel.nearest(), Boolean.valueOf(false));
    assertEquals(getGeoOptionsModel.radius(), Double.valueOf("0"));
    assertEquals(getGeoOptionsModel.rangex(), Double.valueOf("0"));
    assertEquals(getGeoOptionsModel.rangey(), Double.valueOf("0"));
    assertEquals(getGeoOptionsModel.relation(), "intersects");
    assertEquals(getGeoOptionsModel.skip(), Long.valueOf("0"));
    assertEquals(getGeoOptionsModel.stale(), "ok");
}
Also used : GetGeoOptions(com.ibm.cloud.cloudant.v1.model.GetGeoOptions) Test(org.testng.annotations.Test)

Example 4 with GetGeoOptions

use of com.ibm.cloud.cloudant.v1.model.GetGeoOptions in project cloudant-java-sdk by IBM.

the class CloudantTest method testGetGeoWOptions.

@Test
public void testGetGeoWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"bookmark\": \"bookmark\", \"features\": [{\"_id\": \"id\", \"_rev\": \"rev\", \"bbox\": [4], \"geometry\": {\"type\": \"Point\", \"coordinates\": [\"anyValue\"]}, \"properties\": {\"mapKey\": \"anyValue\"}, \"type\": \"Feature\"}], \"rows\": [{\"doc\": {\"_attachments\": {\"mapKey\": {\"content_type\": \"contentType\", \"data\": \"VGhpcyBpcyBhbiBlbmNvZGVkIGJ5dGUgYXJyYXku\", \"digest\": \"digest\", \"encoded_length\": 0, \"encoding\": \"encoding\", \"follows\": false, \"length\": 0, \"revpos\": 1, \"stub\": true}}, \"_conflicts\": [\"conflicts\"], \"_deleted\": false, \"_deleted_conflicts\": [\"deletedConflicts\"], \"_id\": \"id\", \"_local_seq\": \"localSeq\", \"_rev\": \"rev\", \"_revisions\": {\"ids\": [\"ids\"], \"start\": 1}, \"_revs_info\": [{\"rev\": \"rev\", \"status\": \"available\"}]}, \"geometry\": {\"type\": \"Point\", \"coordinates\": [\"anyValue\"]}, \"id\": \"id\", \"rev\": \"rev\"}], \"type\": \"FeatureCollection\"}";
    String getGeoPath = "/testString/_design/testString/_geo/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetGeoOptions model
    GetGeoOptions getGeoOptionsModel = new GetGeoOptions.Builder().db("testString").ddoc("testString").index("testString").bbox("testString").bookmark("testString").format("view").g("testString").includeDocs(false).lat(Double.valueOf("-90")).limit(Long.valueOf("0")).lon(Double.valueOf("-180")).nearest(false).radius(Double.valueOf("0")).rangex(Double.valueOf("0")).rangey(Double.valueOf("0")).relation("intersects").skip(Long.valueOf("0")).stale("ok").build();
    // Invoke operation with valid options model (positive test)
    Response<GeoResult> response = cloudantService.getGeo(getGeoOptionsModel).execute();
    assertNotNull(response);
    GeoResult responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(query.get("bbox"), "testString");
    assertEquals(query.get("bookmark"), "testString");
    assertEquals(query.get("format"), "view");
    assertEquals(query.get("g"), "testString");
    assertEquals(Boolean.valueOf(query.get("include_docs")), Boolean.valueOf(false));
    assertEquals(Double.valueOf(query.get("lat")), Double.valueOf("-90"));
    assertEquals(Long.valueOf(query.get("limit")), Long.valueOf("0"));
    assertEquals(Double.valueOf(query.get("lon")), Double.valueOf("-180"));
    assertEquals(Boolean.valueOf(query.get("nearest")), Boolean.valueOf(false));
    assertEquals(Double.valueOf(query.get("radius")), Double.valueOf("0"));
    assertEquals(Double.valueOf(query.get("rangex")), Double.valueOf("0"));
    assertEquals(Double.valueOf(query.get("rangey")), Double.valueOf("0"));
    assertEquals(query.get("relation"), "intersects");
    assertEquals(Long.valueOf(query.get("skip")), Long.valueOf("0"));
    assertEquals(query.get("stale"), "ok");
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getGeoPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GeoResult(com.ibm.cloud.cloudant.v1.model.GeoResult) GetGeoOptions(com.ibm.cloud.cloudant.v1.model.GetGeoOptions) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

GetGeoOptions (com.ibm.cloud.cloudant.v1.model.GetGeoOptions)3 Test (org.testng.annotations.Test)3 GeoResult (com.ibm.cloud.cloudant.v1.model.GeoResult)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1