Search in sources :

Example 1 with Security

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

the class Cloudant method getSecurity.

/**
 * Retrieve database permissions information.
 *
 * See who has permission to read, write, and manage the database. The credentials you use to log in to the dashboard
 * automatically include `_admin` permissions to all databases you create. Everyone and everything else, including
 * users you share databases with and API keys you create, must be given a permission level explicitly.
 *
 * @param getSecurityOptions the {@link GetSecurityOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Security}
 */
public ServiceCall<Security> getSecurity(GetSecurityOptions getSecurityOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getSecurityOptions, "getSecurityOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", getSecurityOptions.db());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_security", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getSecurity");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    ResponseConverter<Security> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Security>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) Security(com.ibm.cloud.cloudant.v1.model.Security)

Example 2 with Security

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

the class SecurityTest method testSecurity.

@Test
public void testSecurity() 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")));
    Security securityModel = new Security.Builder().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(securityModel.admins(), securityObjectModel);
    assertEquals(securityModel.members(), securityObjectModel);
    assertEquals(securityModel.cloudant(), new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    });
    assertEquals(securityModel.couchdbAuthOnly(), Boolean.valueOf(true));
    String json = TestUtilities.serialize(securityModel);
    Security securityModelNew = TestUtilities.deserialize(json, Security.class);
    assertTrue(securityModelNew instanceof Security);
    assertEquals(securityModelNew.admins().toString(), securityObjectModel.toString());
    assertEquals(securityModelNew.members().toString(), securityObjectModel.toString());
    assertEquals(securityModelNew.couchdbAuthOnly(), Boolean.valueOf(true));
}
Also used : SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Security(com.ibm.cloud.cloudant.v1.model.Security) Test(org.testng.annotations.Test)

Example 3 with Security

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

the class CloudantTest method testGetSecurityWOptions.

@Test
public void testGetSecurityWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"admins\": {\"names\": [\"names\"], \"roles\": [\"roles\"]}, \"members\": {\"names\": [\"names\"], \"roles\": [\"roles\"]}, \"cloudant\": {\"mapKey\": [\"_reader\"]}, \"couchdb_auth_only\": false}";
    String getSecurityPath = "/testString/_security";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the GetSecurityOptions model
    GetSecurityOptions getSecurityOptionsModel = new GetSecurityOptions.Builder().db("testString").build();
    // Invoke operation with valid options model (positive test)
    Response<Security> response = cloudantService.getSecurity(getSecurityOptionsModel).execute();
    assertNotNull(response);
    Security responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "GET");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, getSecurityPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetSecurityOptions(com.ibm.cloud.cloudant.v1.model.GetSecurityOptions) Security(com.ibm.cloud.cloudant.v1.model.Security) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

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