Search in sources :

Example 1 with CreateCredentialsOptions

use of com.ibm.watson.discovery.v1.model.CreateCredentialsOptions in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method createCredentialsIsSuccessful.

/**
 * Creates the credentials is successful.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void createCredentialsIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(credentialsResp));
    CredentialDetails details = new CredentialDetails.Builder().credentialType(CredentialDetails.CredentialType.USERNAME_PASSWORD).url("url").username("username").build();
    Credentials credentials = new Credentials.Builder().sourceType(Credentials.SourceType.SALESFORCE).credentialDetails(details).build();
    CreateCredentialsOptions options = new CreateCredentialsOptions.Builder().environmentId(environmentId).sourceType(Credentials.SourceType.SALESFORCE).credentials(credentials).credentialDetails(details).build();
    Credentials credentialsResponse = discoveryService.createCredentials(options).execute().getResult();
    RecordedRequest request = server.takeRequest();
    assertEquals(CREATE_CREDENTIALS_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(credentialsResp, credentialsResponse);
    assertEquals(credentialsResp.credentialDetails(), credentialsResponse.credentialDetails());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) CreateCredentialsOptions(com.ibm.watson.discovery.v1.model.CreateCredentialsOptions) CredentialDetails(com.ibm.watson.discovery.v1.model.CredentialDetails) Credentials(com.ibm.watson.discovery.v1.model.Credentials) DeleteCredentials(com.ibm.watson.discovery.v1.model.DeleteCredentials) WatsonServiceUnitTest(com.ibm.watson.common.WatsonServiceUnitTest)

Example 2 with CreateCredentialsOptions

use of com.ibm.watson.discovery.v1.model.CreateCredentialsOptions in project java-sdk by watson-developer-cloud.

the class DiscoveryTest method testCreateCredentialsWOptions.

// Test the createCredentials operation with a valid options model parameter
@Test
public void testCreateCredentialsWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"credential_id\": \"credentialId\", \"source_type\": \"box\", \"credential_details\": {\"credential_type\": \"oauth2\", \"client_id\": \"clientId\", \"enterprise_id\": \"enterpriseId\", \"url\": \"url\", \"username\": \"username\", \"organization_url\": \"organizationUrl\", \"site_collection.path\": \"siteCollectionPath\", \"client_secret\": \"clientSecret\", \"public_key_id\": \"publicKeyId\", \"private_key\": \"privateKey\", \"passphrase\": \"passphrase\", \"password\": \"password\", \"gateway_id\": \"gatewayId\", \"source_version\": \"online\", \"web_application_url\": \"webApplicationUrl\", \"domain\": \"domain\", \"endpoint\": \"endpoint\", \"access_key_id\": \"accessKeyId\", \"secret_access_key\": \"secretAccessKey\"}, \"status\": {\"authenticated\": false, \"error_message\": \"errorMessage\"}}";
    String createCredentialsPath = "/v1/environments/testString/credentials";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the CredentialDetails model
    CredentialDetails credentialDetailsModel = new CredentialDetails.Builder().credentialType("oauth2").clientId("testString").enterpriseId("testString").url("testString").username("testString").organizationUrl("testString").siteCollectionPath("testString").clientSecret("testString").publicKeyId("testString").privateKey("testString").passphrase("testString").password("testString").gatewayId("testString").sourceVersion("online").webApplicationUrl("testString").domain("testString").endpoint("testString").accessKeyId("testString").secretAccessKey("testString").build();
    // Construct an instance of the StatusDetails model
    StatusDetails statusDetailsModel = new StatusDetails.Builder().authenticated(true).errorMessage("testString").build();
    // Construct an instance of the CreateCredentialsOptions model
    CreateCredentialsOptions createCredentialsOptionsModel = new CreateCredentialsOptions.Builder().environmentId("testString").sourceType("box").credentialDetails(credentialDetailsModel).status(statusDetailsModel).build();
    // Invoke createCredentials() with a valid options model and verify the result
    Response<Credentials> response = discoveryService.createCredentials(createCredentialsOptionsModel).execute();
    assertNotNull(response);
    Credentials responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, createCredentialsPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CreateCredentialsOptions(com.ibm.watson.discovery.v1.model.CreateCredentialsOptions) CredentialDetails(com.ibm.watson.discovery.v1.model.CredentialDetails) StatusDetails(com.ibm.watson.discovery.v1.model.StatusDetails) Credentials(com.ibm.watson.discovery.v1.model.Credentials) DeleteCredentials(com.ibm.watson.discovery.v1.model.DeleteCredentials) Test(org.testng.annotations.Test)

Example 3 with CreateCredentialsOptions

use of com.ibm.watson.discovery.v1.model.CreateCredentialsOptions in project java-sdk by watson-developer-cloud.

the class Discovery method createCredentials.

/**
 * Create credentials.
 *
 * <p>Creates a set of credentials to connect to a remote source. Created credentials are used in
 * a configuration to associate a collection with the remote source.
 *
 * <p>**Note:** All credentials are sent over an encrypted connection and encrypted at rest.
 *
 * @param createCredentialsOptions the {@link CreateCredentialsOptions} containing the options for
 *     the call
 * @return a {@link ServiceCall} with a result of type {@link Credentials}
 */
public ServiceCall<Credentials> createCredentials(CreateCredentialsOptions createCredentialsOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createCredentialsOptions, "createCredentialsOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("environment_id", createCredentialsOptions.environmentId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/environments/{environment_id}/credentials", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "createCredentials");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    final JsonObject contentJson = new JsonObject();
    if (createCredentialsOptions.sourceType() != null) {
        contentJson.addProperty("source_type", createCredentialsOptions.sourceType());
    }
    if (createCredentialsOptions.credentialDetails() != null) {
        contentJson.add("credential_details", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createCredentialsOptions.credentialDetails()));
    }
    if (createCredentialsOptions.status() != null) {
        contentJson.add("status", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createCredentialsOptions.status()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Credentials> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Credentials>() {
    }.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) Credentials(com.ibm.watson.discovery.v1.model.Credentials) DeleteCredentials(com.ibm.watson.discovery.v1.model.DeleteCredentials)

Aggregations

Credentials (com.ibm.watson.discovery.v1.model.Credentials)3 DeleteCredentials (com.ibm.watson.discovery.v1.model.DeleteCredentials)3 CreateCredentialsOptions (com.ibm.watson.discovery.v1.model.CreateCredentialsOptions)2 CredentialDetails (com.ibm.watson.discovery.v1.model.CredentialDetails)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)1 StatusDetails (com.ibm.watson.discovery.v1.model.StatusDetails)1 HashMap (java.util.HashMap)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Test (org.testng.annotations.Test)1