Search in sources :

Example 1 with PostIndexOptions

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

the class CloudantTest method testPostIndexWOptions.

@Test
public void testPostIndexWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"id\": \"id\", \"name\": \"name\", \"result\": \"created\"}";
    String postIndexPath = "/testString/_index";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the Analyzer model
    Analyzer analyzerModel = new Analyzer.Builder().name("classic").stopwords(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
    // Construct an instance of the IndexTextOperatorDefaultField model
    IndexTextOperatorDefaultField indexTextOperatorDefaultFieldModel = new IndexTextOperatorDefaultField.Builder().analyzer(analyzerModel).enabled(true).build();
    // Construct an instance of the IndexField model
    IndexField indexFieldModel = new IndexField.Builder().name("testString").type("boolean").add("foo", "asc").build();
    // Construct an instance of the IndexDefinition model
    IndexDefinition indexDefinitionModel = new IndexDefinition.Builder().defaultAnalyzer(analyzerModel).defaultField(indexTextOperatorDefaultFieldModel).fields(new java.util.ArrayList<IndexField>(java.util.Arrays.asList(indexFieldModel))).indexArrayLengths(true).partialFilterSelector(new java.util.HashMap<String, Object>() {

        {
            put("foo", "testString");
        }
    }).build();
    // Construct an instance of the PostIndexOptions model
    PostIndexOptions postIndexOptionsModel = new PostIndexOptions.Builder().db("testString").index(indexDefinitionModel).ddoc("testString").def(indexDefinitionModel).name("testString").partitioned(true).type("json").build();
    // Invoke operation with valid options model (positive test)
    Response<IndexResult> response = cloudantService.postIndex(postIndexOptionsModel).execute();
    assertNotNull(response);
    IndexResult responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, postIndexPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Analyzer(com.ibm.cloud.cloudant.v1.model.Analyzer) PostIndexOptions(com.ibm.cloud.cloudant.v1.model.PostIndexOptions) GeoIndexDefinition(com.ibm.cloud.cloudant.v1.model.GeoIndexDefinition) IndexDefinition(com.ibm.cloud.cloudant.v1.model.IndexDefinition) SearchIndexDefinition(com.ibm.cloud.cloudant.v1.model.SearchIndexDefinition) IndexResult(com.ibm.cloud.cloudant.v1.model.IndexResult) IndexTextOperatorDefaultField(com.ibm.cloud.cloudant.v1.model.IndexTextOperatorDefaultField) IndexField(com.ibm.cloud.cloudant.v1.model.IndexField) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with PostIndexOptions

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

the class PostIndexOptionsTest method testPostIndexOptions.

@Test
public void testPostIndexOptions() throws Throwable {
    Analyzer analyzerModel = new Analyzer.Builder().name("classic").stopwords(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
    assertEquals(analyzerModel.name(), "classic");
    assertEquals(analyzerModel.stopwords(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    IndexTextOperatorDefaultField indexTextOperatorDefaultFieldModel = new IndexTextOperatorDefaultField.Builder().analyzer(analyzerModel).enabled(true).build();
    assertEquals(indexTextOperatorDefaultFieldModel.analyzer(), analyzerModel);
    assertEquals(indexTextOperatorDefaultFieldModel.enabled(), Boolean.valueOf(true));
    IndexField indexFieldModel = new IndexField.Builder().name("testString").type("boolean").add("foo", "asc").build();
    assertEquals(indexFieldModel.getName(), "testString");
    assertEquals(indexFieldModel.getType(), "boolean");
    assertEquals(indexFieldModel.get("foo"), "asc");
    IndexDefinition indexDefinitionModel = new IndexDefinition.Builder().defaultAnalyzer(analyzerModel).defaultField(indexTextOperatorDefaultFieldModel).fields(new java.util.ArrayList<IndexField>(java.util.Arrays.asList(indexFieldModel))).indexArrayLengths(true).partialFilterSelector(new java.util.HashMap<String, Object>() {

        {
            put("foo", "testString");
        }
    }).build();
    assertEquals(indexDefinitionModel.defaultAnalyzer(), analyzerModel);
    assertEquals(indexDefinitionModel.defaultField(), indexTextOperatorDefaultFieldModel);
    assertEquals(indexDefinitionModel.fields(), new java.util.ArrayList<IndexField>(java.util.Arrays.asList(indexFieldModel)));
    assertEquals(indexDefinitionModel.indexArrayLengths(), Boolean.valueOf(true));
    assertEquals(indexDefinitionModel.partialFilterSelector(), new java.util.HashMap<String, Object>() {

        {
            put("foo", "testString");
        }
    });
    PostIndexOptions postIndexOptionsModel = new PostIndexOptions.Builder().db("testString").index(indexDefinitionModel).ddoc("testString").def(indexDefinitionModel).name("testString").partitioned(true).type("json").build();
    assertEquals(postIndexOptionsModel.db(), "testString");
    assertEquals(postIndexOptionsModel.index(), indexDefinitionModel);
    assertEquals(postIndexOptionsModel.ddoc(), "testString");
    assertEquals(postIndexOptionsModel.def(), indexDefinitionModel);
    assertEquals(postIndexOptionsModel.name(), "testString");
    assertEquals(postIndexOptionsModel.partitioned(), Boolean.valueOf(true));
    assertEquals(postIndexOptionsModel.type(), "json");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Analyzer(com.ibm.cloud.cloudant.v1.model.Analyzer) PostIndexOptions(com.ibm.cloud.cloudant.v1.model.PostIndexOptions) IndexDefinition(com.ibm.cloud.cloudant.v1.model.IndexDefinition) IndexTextOperatorDefaultField(com.ibm.cloud.cloudant.v1.model.IndexTextOperatorDefaultField) IndexField(com.ibm.cloud.cloudant.v1.model.IndexField) Test(org.testng.annotations.Test)

Example 3 with PostIndexOptions

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

the class Cloudant method postIndex.

/**
 * Create a new index on a database.
 *
 * Create a new index on a database.
 *
 * @param postIndexOptions the {@link PostIndexOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link IndexResult}
 */
public ServiceCall<IndexResult> postIndex(PostIndexOptions postIndexOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postIndexOptions, "postIndexOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postIndexOptions.db());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_index", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postIndex");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    contentJson.add("index", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postIndexOptions.index()));
    if (postIndexOptions.ddoc() != null) {
        contentJson.addProperty("ddoc", postIndexOptions.ddoc());
    }
    if (postIndexOptions.def() != null) {
        contentJson.add("def", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postIndexOptions.def()));
    }
    if (postIndexOptions.name() != null) {
        contentJson.addProperty("name", postIndexOptions.name());
    }
    if (postIndexOptions.partitioned() != null) {
        contentJson.addProperty("partitioned", postIndexOptions.partitioned());
    }
    if (postIndexOptions.type() != null) {
        contentJson.addProperty("type", postIndexOptions.type());
    }
    builder.bodyJson(contentJson);
    ResponseConverter<IndexResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<IndexResult>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) IndexResult(com.ibm.cloud.cloudant.v1.model.IndexResult)

Aggregations

HashMap (java.util.HashMap)3 Analyzer (com.ibm.cloud.cloudant.v1.model.Analyzer)2 IndexDefinition (com.ibm.cloud.cloudant.v1.model.IndexDefinition)2 IndexField (com.ibm.cloud.cloudant.v1.model.IndexField)2 IndexResult (com.ibm.cloud.cloudant.v1.model.IndexResult)2 IndexTextOperatorDefaultField (com.ibm.cloud.cloudant.v1.model.IndexTextOperatorDefaultField)2 PostIndexOptions (com.ibm.cloud.cloudant.v1.model.PostIndexOptions)2 ArrayList (java.util.ArrayList)2 Test (org.testng.annotations.Test)2 JsonObject (com.google.gson.JsonObject)1 GeoIndexDefinition (com.ibm.cloud.cloudant.v1.model.GeoIndexDefinition)1 SearchIndexDefinition (com.ibm.cloud.cloudant.v1.model.SearchIndexDefinition)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1