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);
}
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");
}
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);
}
Aggregations