Search in sources :

Example 1 with IndexResult

use of com.ibm.cloud.cloudant.v1.model.IndexResult 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 IndexResult

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

the class IndexResultTest method testIndexResult.

@Test
public void testIndexResult() throws Throwable {
    IndexResult indexResultModel = new IndexResult();
    assertNull(indexResultModel.getId());
    assertNull(indexResultModel.getName());
    assertNull(indexResultModel.getResult());
}
Also used : IndexResult(com.ibm.cloud.cloudant.v1.model.IndexResult) Test(org.testng.annotations.Test)

Example 3 with IndexResult

use of com.ibm.cloud.cloudant.v1.model.IndexResult 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

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