Search in sources :

Example 1 with PutSecurityOptions

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

the class PutSecurityOptionsTest method testPutSecurityOptions.

@Test
public void testPutSecurityOptions() throws Throwable {
    SecurityObject securityObjectModel = new SecurityObject.Builder().names(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).roles(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
    assertEquals(securityObjectModel.names(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(securityObjectModel.roles(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    PutSecurityOptions putSecurityOptionsModel = new PutSecurityOptions.Builder().db("testString").admins(securityObjectModel).members(securityObjectModel).cloudant(new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    }).couchdbAuthOnly(true).build();
    assertEquals(putSecurityOptionsModel.db(), "testString");
    assertEquals(putSecurityOptionsModel.admins(), securityObjectModel);
    assertEquals(putSecurityOptionsModel.members(), securityObjectModel);
    assertEquals(putSecurityOptionsModel.cloudant(), new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    });
    assertEquals(putSecurityOptionsModel.couchdbAuthOnly(), Boolean.valueOf(true));
}
Also used : SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) PutSecurityOptions(com.ibm.cloud.cloudant.v1.model.PutSecurityOptions) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 2 with PutSecurityOptions

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

the class CloudantTest method testPutSecurityWOptions.

@Test
public void testPutSecurityWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"ok\": true}";
    String putSecurityPath = "/testString/_security";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the SecurityObject model
    SecurityObject securityObjectModel = new SecurityObject.Builder().names(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).roles(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
    // Construct an instance of the PutSecurityOptions model
    PutSecurityOptions putSecurityOptionsModel = new PutSecurityOptions.Builder().db("testString").admins(securityObjectModel).members(securityObjectModel).cloudant(new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    }).couchdbAuthOnly(true).build();
    // Invoke operation with valid options model (positive test)
    Response<Ok> response = cloudantService.putSecurity(putSecurityOptionsModel).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);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, putSecurityPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) ArrayList(java.util.ArrayList) PutSecurityOptions(com.ibm.cloud.cloudant.v1.model.PutSecurityOptions) ArrayList(java.util.ArrayList) List(java.util.List) Ok(com.ibm.cloud.cloudant.v1.model.Ok) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with PutSecurityOptions

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

the class Cloudant method putSecurity.

/**
 * Modify database permissions.
 *
 * Modify who has permission to read, write, or manage a database. This endpoint can be used to modify both Cloudant
 * and CouchDB related permissions. Be careful: by removing a Cloudant API key, a member or an admin from the list of
 * users that have access permissions, you remove it from the list of users that have access to the database.
 *
 * ### Note about nobody role
 *
 * The `nobody` username applies to all unauthenticated connection attempts. For example, if an application tries to
 * read data from a database, but did not identify itself, the task can continue only if the `nobody` user has the
 * role `_reader`.
 *
 * @param putSecurityOptions the {@link PutSecurityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Ok}
 */
public ServiceCall<Ok> putSecurity(PutSecurityOptions putSecurityOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(putSecurityOptions, "putSecurityOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", putSecurityOptions.db());
    RequestBuilder builder = RequestBuilder.put(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_security", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "putSecurity");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    if (putSecurityOptions.admins() != null) {
        contentJson.add("admins", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(putSecurityOptions.admins()));
    }
    if (putSecurityOptions.members() != null) {
        contentJson.add("members", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(putSecurityOptions.members()));
    }
    if (putSecurityOptions.cloudant() != null) {
        contentJson.add("cloudant", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(putSecurityOptions.cloudant()));
    }
    if (putSecurityOptions.couchdbAuthOnly() != null) {
        contentJson.addProperty("couchdb_auth_only", putSecurityOptions.couchdbAuthOnly());
    }
    builder.bodyJson(contentJson);
    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) JsonObject(com.google.gson.JsonObject) Ok(com.ibm.cloud.cloudant.v1.model.Ok)

Aggregations

Ok (com.ibm.cloud.cloudant.v1.model.Ok)2 PutSecurityOptions (com.ibm.cloud.cloudant.v1.model.PutSecurityOptions)2 SecurityObject (com.ibm.cloud.cloudant.v1.model.SecurityObject)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.testng.annotations.Test)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 HashMap (java.util.HashMap)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1