Search in sources :

Example 1 with PutDatabaseOptions

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

the class Cloudant method putDatabase.

/**
 * Create a database.
 *
 * @param putDatabaseOptions the {@link PutDatabaseOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Ok}
 */
public ServiceCall<Ok> putDatabase(PutDatabaseOptions putDatabaseOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(putDatabaseOptions, "putDatabaseOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", putDatabaseOptions.db());
    RequestBuilder builder = RequestBuilder.put(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "putDatabase");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (putDatabaseOptions.partitioned() != null) {
        builder.query("partitioned", String.valueOf(putDatabaseOptions.partitioned()));
    }
    if (putDatabaseOptions.q() != null) {
        builder.query("q", String.valueOf(putDatabaseOptions.q()));
    }
    ResponseConverter<Ok> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Ok>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Ok(com.ibm.cloud.cloudant.v1.model.Ok)

Example 2 with PutDatabaseOptions

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

the class PutDatabaseOptionsTest method testPutDatabaseOptions.

@Test
public void testPutDatabaseOptions() throws Throwable {
    PutDatabaseOptions putDatabaseOptionsModel = new PutDatabaseOptions.Builder().db("testString").partitioned(false).q(Long.valueOf("26")).build();
    assertEquals(putDatabaseOptionsModel.db(), "testString");
    assertEquals(putDatabaseOptionsModel.partitioned(), Boolean.valueOf(false));
    assertEquals(putDatabaseOptionsModel.q(), Long.valueOf("26"));
}
Also used : PutDatabaseOptions(com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions) Test(org.testng.annotations.Test)

Example 3 with PutDatabaseOptions

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

the class CreateDbAndDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Create a database ===============================================
    // Create a database object with "orders" id
    String exampleDbName = "orders";
    PutDatabaseOptions putDbOptions = new PutDatabaseOptions.Builder().db(exampleDbName).build();
    // Try to create database if it doesn't exist
    try {
        Ok putDatabaseResult = client.putDatabase(putDbOptions).execute().getResult();
        if (putDatabaseResult.isOk()) {
            System.out.println("\"" + exampleDbName + "\" database created.");
        }
    } catch (ServiceResponseException sre) {
        if (sre.getStatusCode() == 412)
            System.out.println("Cannot create \"" + exampleDbName + "\" database, it already exists.");
    }
    // 3. Create a document ===============================================
    // Create a document object with "example" id
    String exampleDocId = "example";
    Document exampleDocument = new Document();
    // Setting id for the document is optional when "postDocument" method is used for CREATE.
    // When id is not provided the server will generate one for your document.
    exampleDocument.setId(exampleDocId);
    // Add "name" and "joined" fields to the document
    exampleDocument.put("name", "Bob Smith");
    exampleDocument.put("joined", "2019-01-24T10:42:59.000Z");
    // Save the document in the database with "postDocument" method
    PostDocumentOptions createDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(exampleDocument).build();
    DocumentResult createDocumentResponse = client.postDocument(createDocumentOptions).execute().getResult();
    // ====================================================================
    // Note: saving the document can also be done with the "putDocument"
    // method. In this case `docId` is required for a CREATE operation:
    /*
        PutDocumentOptions createDocumentOptions =
            new PutDocumentOptions.Builder()
                .db(exampleDbName)
                .docId(exampleDocId)
                .document(exampleDocument)
                .build();
        DocumentResult createDocumentResponse = client
            .putDocument(createDocumentOptions)
            .execute()
            .getResult();
        */
    // ====================================================================
    // Keeping track of the revision number of the document object
    // is necessary for further UPDATE/DELETE operations:
    exampleDocument.setRev(createDocumentResponse.getRev());
    System.out.println("You have created the document:\n" + exampleDocument);
}
Also used : PutDatabaseOptions(com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) ServiceResponseException(com.ibm.cloud.sdk.core.service.exception.ServiceResponseException) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Ok(com.ibm.cloud.cloudant.v1.model.Ok) Document(com.ibm.cloud.cloudant.v1.model.Document) PostDocumentOptions(com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)

Example 4 with PutDatabaseOptions

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

the class CloudantTest method testPutDatabaseWOptions.

@Test
public void testPutDatabaseWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"ok\": true}";
    String putDatabasePath = "/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PutDatabaseOptions model
    PutDatabaseOptions putDatabaseOptionsModel = new PutDatabaseOptions.Builder().db("testString").partitioned(false).q(Long.valueOf("26")).build();
    // Invoke operation with valid options model (positive test)
    Response<Ok> response = cloudantService.putDatabase(putDatabaseOptionsModel).execute();
    assertNotNull(response);
    Ok responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "PUT");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    // Get query params
    assertEquals(Boolean.valueOf(query.get("partitioned")), Boolean.valueOf(false));
    assertEquals(Long.valueOf(query.get("q")), Long.valueOf("26"));
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, putDatabasePath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) PutDatabaseOptions(com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions) Ok(com.ibm.cloud.cloudant.v1.model.Ok) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Ok (com.ibm.cloud.cloudant.v1.model.Ok)3 PutDatabaseOptions (com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions)3 Test (org.testng.annotations.Test)2 Cloudant (com.ibm.cloud.cloudant.v1.Cloudant)1 Document (com.ibm.cloud.cloudant.v1.model.Document)1 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)1 PostDocumentOptions (com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 ServiceResponseException (com.ibm.cloud.sdk.core.service.exception.ServiceResponseException)1 HashMap (java.util.HashMap)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1