Search in sources :

Example 1 with Credentials

use of com.ibm.watson.discovery.v1.model.Credentials in project hale by halestudio.

the class HaleConnectServiceImpl method login.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#login(java.lang.String,
 *      java.lang.String)
 */
@Override
public boolean login(String username, String password) throws HaleConnectException {
    LoginApi loginApi = UserServiceHelper.getLoginApi(this);
    Credentials credentials = UserServiceHelper.buildCredentials(username, password);
    try {
        Token token = loginApi.login(credentials);
        if (token != null) {
            UsersApi usersApi = UserServiceHelper.getUsersApi(this, token.getToken());
            // First get the current user's profile to obtain the user ID
            // required to fetch the extended profile (including the user's
            // roles/organisations) in the next step
            UserInfo shortProfile = usersApi.getProfileOfCurrentUser();
            session = new HaleConnectSessionImpl(username, token.getToken(), usersApi.getProfile(shortProfile.getId()));
            notifyLoginStateChanged();
        } else {
            clearSession();
        }
    } catch (ApiException e) {
        if (e.getCode() == 401) {
            clearSession();
        } else {
            throw new HaleConnectException(e.getMessage(), e);
        }
    }
    return isLoggedIn();
}
Also used : UsersApi(com.haleconnect.api.user.v1.api.UsersApi) Token(com.haleconnect.api.user.v1.model.Token) UserInfo(com.haleconnect.api.user.v1.model.UserInfo) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) LoginApi(com.haleconnect.api.user.v1.api.LoginApi) Credentials(com.haleconnect.api.user.v1.model.Credentials) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 2 with Credentials

use of com.ibm.watson.discovery.v1.model.Credentials in project hale by halestudio.

the class UserServiceHelper method buildCredentials.

/**
 * Build a {@link Credentials} object. Any null values passed in will be
 * converted to an empty string.
 *
 * @param username the user name
 * @param password the password
 * @return a Credentials object with the given credentials
 */
public static Credentials buildCredentials(String username, String password) {
    Credentials credentials = new Credentials();
    credentials.setUsername(Optional.ofNullable(username).orElse(""));
    credentials.setPassword(Optional.ofNullable(password).orElse(""));
    return credentials;
}
Also used : Credentials(com.haleconnect.api.user.v1.model.Credentials)

Example 3 with Credentials

use of com.ibm.watson.discovery.v1.model.Credentials 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 4 with Credentials

use of com.ibm.watson.discovery.v1.model.Credentials 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 5 with Credentials

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

the class DiscoveryTest method testDeleteCredentialsWOptions.

// Test the deleteCredentials operation with a valid options model parameter
@Test
public void testDeleteCredentialsWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"credential_id\": \"credentialId\", \"status\": \"deleted\"}";
    String deleteCredentialsPath = "/v1/environments/testString/credentials/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    // Construct an instance of the DeleteCredentialsOptions model
    DeleteCredentialsOptions deleteCredentialsOptionsModel = new DeleteCredentialsOptions.Builder().environmentId("testString").credentialId("testString").build();
    // Invoke deleteCredentials() with a valid options model and verify the result
    Response<DeleteCredentials> response = discoveryService.deleteCredentials(deleteCredentialsOptionsModel).execute();
    assertNotNull(response);
    DeleteCredentials 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(), "DELETE");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, deleteCredentialsPath);
    // 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) DeleteCredentials(com.ibm.watson.discovery.v1.model.DeleteCredentials) DeleteCredentialsOptions(com.ibm.watson.discovery.v1.model.DeleteCredentialsOptions) Test(org.testng.annotations.Test)

Aggregations

DeleteCredentials (com.ibm.watson.discovery.v1.model.DeleteCredentials)12 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 Credentials (com.ibm.watson.discovery.v1.model.Credentials)9 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)5 WatsonServiceUnitTest (com.ibm.watson.common.WatsonServiceUnitTest)5 HashMap (java.util.HashMap)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 Test (org.testng.annotations.Test)5 CredentialDetails (com.ibm.watson.discovery.v1.model.CredentialDetails)4 CredentialsList (com.ibm.watson.discovery.v1.model.CredentialsList)3 JsonObject (com.google.gson.JsonObject)2 Credentials (com.haleconnect.api.user.v1.model.Credentials)2 CreateCredentialsOptions (com.ibm.watson.discovery.v1.model.CreateCredentialsOptions)2 DeleteCredentialsOptions (com.ibm.watson.discovery.v1.model.DeleteCredentialsOptions)2 GetCredentialsOptions (com.ibm.watson.discovery.v1.model.GetCredentialsOptions)2 ListCredentialsOptions (com.ibm.watson.discovery.v1.model.ListCredentialsOptions)2 StatusDetails (com.ibm.watson.discovery.v1.model.StatusDetails)2 UpdateCredentialsOptions (com.ibm.watson.discovery.v1.model.UpdateCredentialsOptions)2 ApiException (com.haleconnect.api.user.v1.ApiException)1 LoginApi (com.haleconnect.api.user.v1.api.LoginApi)1