com.cloudant.client.api.ClientBuilder Java Examples

The following examples show how to use com.cloudant.client.api.ClientBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestUtility.java    From chatbot with Apache License 2.0 6 votes vote down vote up
public static Application.Helper getHelper() throws Exception {
    if(systemProperties.containsKey("PROP_FILE")) {
        properties.load(new FileInputStream(systemProperties.get("PROP_FILE")));
    }

    CloudantClient cloudantClient = ClientBuilder
            .url(new URL(getProperty("cloudant.url")))
            .username(getProperty("cloudant.username"))
            .password(getProperty("cloudant.password"))
            .build();
    WolframRepository wolframRepository = new WolframRepository(getProperty("wolfram.apiKey"));
    return new Application.Helper(
            cloudantClient,
            wolframRepository,
            getProperty("cloudant.chatDB"),
            getProperty("cloudant.feedbackDB"),
            getProperty("cloudant.explorerDB"),
            getProperty("tmdb.apiKey")
    );
}
 
Example #2
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 6 votes vote down vote up
@Test
public void vcapValidServiceNameSpecified() throws Exception {
    //server.enqueue(MockWebServerResources.OK_COOKIE);
    String serviceName = "serviceFoo";
    VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator(serviceName);
    vcap.createNewLegacyService("test_bluemix_service_1",
            String.format("%s:%s/", server.getHostName(), server.getPort()),
            "user", "password");
    CloudantClient client = ClientBuilder
            .bluemix(vcap.toJson(), serviceName, "test_bluemix_service_1")
            .disableSSLAuthentication()
            .build();
    this.testMockInfoRequest(client, true);
    // One request to _session then one to get server info
    assertEquals(2, server.getRequestCount(), "There should be two requests");
}
 
Example #3
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 6 votes vote down vote up
@Test
public void vcapMultiServiceEmptyCredentials() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyService("test_bluemix_service_1", "foo1.bar", "admin1",
                    "pass1");
            vcap.createNewLegacyService("test_bluemix_service_2", "foo2.bar", "admin2",
                    "pass2");
            vcap.createNewLegacyServiceWithEmptyCredentials("test_bluemix_service_3",
                    CloudantClientHelper.SERVER_HOST);
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_3");
        }
    });
}
 
Example #4
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 6 votes vote down vote up
@Test
public void vcapMultiServiceMissingNamedService() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyService("test_bluemix_service_1", "foo1.bar", "admin1",
                    "pass1");
            vcap.createNewLegacyService("test_bluemix_service_2", "foo2.bar", "admin2",
                    "pass2");
            vcap.createNewLegacyService("test_bluemix_service_3", "foo3.bar", "admin3",
                    "pass3");
            vcap.createNewService("test_bluemix_service_4", "admin4", "api1234key");
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_5");
        }
    });
}
 
Example #5
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 6 votes vote down vote up
@Test
public void vcapMultiServiceNoNameSpecified() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyService("test_bluemix_service_1", "foo1.bar", "admin1",
                    "pass1");
            vcap.createNewLegacyService("test_bluemix_service_2", "foo2.bar", "admin2",
                    "pass2");
            vcap.createNewLegacyService("test_bluemix_service_3", "foo3.bar", "admin3",
                    "pass3");
            vcap.createNewService("test_bluemix_service_4", "admin4", "api1234key");
            ClientBuilder.bluemix(vcap.toJson());
        }
    });
}
 
Example #6
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 6 votes vote down vote up
@Test
public void vcapMultiServiceUsingIAM() throws Exception{
    VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
    vcap.createNewLegacyService("test_bluemix_service_1", "foo1.bar", "admin1", "pass1");
    vcap.createNewLegacyService("test_bluemix_service_2", "foo2.bar", "admin2", "pass2");
    vcap.createNewLegacyService("test_bluemix_service_3",
            mockServerHostPort,
            "user", "password");
    vcap.createNewService("test_bluemix_service_4", "admin4", "api1234key");
    vcap.createNewService("test_bluemix_service_5", mockServerHostPort,
            IAM_API_KEY);
    CloudantClient client = ClientBuilder
            .bluemix(vcap.toJson(), "test_bluemix_service_5")
            .disableSSLAuthentication()
            .build();
    this.testMockInfoRequest(client, false);
    // One request to _iam_session then one to get server info
    assertEquals(2, server.getRequestCount(), "There should be two requests");
}
 
Example #7
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 6 votes vote down vote up
@Test
public void vcapMultiService() throws Exception {
    VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
    vcap.createNewLegacyService("test_bluemix_service_1", "foo1.bar", "admin1", "pass1");
    vcap.createNewLegacyService("test_bluemix_service_2", "foo2.bar", "admin2", "pass2");
    vcap.createNewLegacyService("test_bluemix_service_3",
            mockServerHostPort,
            "user", "password");
    vcap.createNewService("test_bluemix_service_4", "admin4", "api1234key");
    vcap.createNewService("test_bluemix_service_5", mockServerHostPort,
            "api1234key");
    CloudantClient client = ClientBuilder
            .bluemix(vcap.toJson(), "test_bluemix_service_3")
            .disableSSLAuthentication()
            .build();
    this.testMockInfoRequest(client, true);
    // One request to _session then one to get server info
    assertEquals(2, server.getRequestCount(), "There should be two requests");
}
 
Example #8
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceEmptyIAMAndHost() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewServiceWithEmptyIAM("test_bluemix_service_1", null);
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_1");
        }
    });
}
 
Example #9
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceWithName() throws Exception{
    VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
    vcap.createNewLegacyService("test_bluemix_service_1",
            mockServerHostPort,
            "user", "password");
    CloudantClient client = ClientBuilder
            .bluemix(vcap.toJson(), "test_bluemix_service_1")
            .disableSSLAuthentication()
            .build();
    this.testMockInfoRequest(client, true);
    // One request to _session then one to get server info
    assertEquals(2, server.getRequestCount(), "There should be two requests");
}
 
Example #10
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceNoNameSpecified() throws Exception {
    VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
    vcap.createNewLegacyService("test_bluemix_service_1",
            mockServerHostPort,
            "user", "password");
    CloudantClient client = ClientBuilder
            .bluemix(vcap.toJson())
            .disableSSLAuthentication()
            .build();
    this.testMockInfoRequest(client, true);
    // One request to _session then one to get server info
    assertEquals(2, server.getRequestCount(), "There should be two requests");
}
 
Example #11
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceWithIAM() throws Exception{
    VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
    vcap.createNewService("test_bluemix_service_1",
            mockServerHostPort,
            IAM_API_KEY);
    CloudantClient client = ClientBuilder
            .bluemix(vcap.toJson(), "test_bluemix_service_1")
            .disableSSLAuthentication()
            .build();
    this.testMockInfoRequest(client, false);
    // One request to _iam_session then one to get server info
    assertEquals(2, server.getRequestCount(), "There should be two requests");
}
 
Example #12
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceMissingNamedService() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyService("test_bluemix_service_1", "foo1.bar", "admin1",
                    "pass1");
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_2");
        }
    });
}
 
Example #13
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceEmptyCredentials() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyServiceWithEmptyCredentials("test_bluemix_service_1",
                    CloudantClientHelper.SERVER_HOST);
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_1");
        }
    });
}
 
Example #14
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceEmptyCredentialsAndHost() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyServiceWithEmptyCredentials("test_bluemix_service_1",null);
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_1");
        }
    });
}
 
Example #15
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapSingleServiceEmptyIAM() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewServiceWithEmptyIAM("test_bluemix_service_1",
                    CloudantClientHelper.SERVER_HOST);
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_1");
        }
    });
}
 
Example #16
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapMissingServiceNameSpecified() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyService("test_bluemix_service_1",
                    TEST_HOST, TEST_USER, TEST_PASSWORD);
            ClientBuilder.bluemix(vcap.toJson(), "missingService", "test_bluemix_service_1")
                    .build();
        }
    });
}
 
Example #17
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapMultiServiceEmptyIAM() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewService("test_bluemix_service_1", "admin1", "apikey1");
            vcap.createNewService("test_bluemix_service_2", "admin2", "apikey2");
            vcap.createNewServiceWithEmptyIAM("test_bluemix_service_3",
                    CloudantClientHelper.SERVER_HOST);
            ClientBuilder.bluemix(vcap.toJson(), "test_bluemix_service_3");
        }
    });
}
 
Example #18
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapNoServicesPresent() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            ClientBuilder.bluemix(new CloudFoundryServiceTest.VCAPGenerator().toJson());
        }
    });
}
 
Example #19
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapInvalidJSON() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            ClientBuilder.bluemix("{\"cloudantNoSQLDB\":[]"); // invalid JSON
        }
    });
}
 
Example #20
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapNotPresent() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            ClientBuilder.bluemix(null);
        }
    });
}
 
Example #21
Source File: CloudantClientTests.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that a {@code null} URL causes an IllegalArgumentException to be thrown.
 *
 * @throws Exception
 */
@Test
public void nullURLThrowsIAE() throws Exception {
    assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            ClientBuilder.url(null);
        }
    });
}
 
Example #22
Source File: CloudantConfiguration.java    From greeting-cloudant-spring with Apache License 2.0 5 votes vote down vote up
@Bean
public CloudantClient client() {
	ClientBuilder builder = ClientBuilder
		.url(config.getUrl())
		.username(config.getUsername())
		.password(config.getPassword());
	return builder.build();
}
 
Example #23
Source File: URIBaseTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void buildAccountUri_noTrailingPathSeparator() throws Exception {
    CloudantClient client = ClientBuilder.url(clientResource.get().getBaseUri().toURL())
            .build();
    Assertions.assertFalse(client.getBaseUri().toString().endsWith("/"));
    URI clientUri = new URIBase(client.getBaseUri()).build();
    Assertions.assertFalse(clientUri.toString().endsWith("/"));

    //Check that path is not missing / separators
    clientUri = new URIBase(client.getBaseUri()).path("").path("api").path("couch").build();
    URI expectedAccountUri = new URI(clientResource.get().getBaseUri().toString()
            + "/api/couch");
    Assertions.assertEquals(expectedAccountUri, clientUri);
}
 
Example #24
Source File: HttpTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@TestTemplate
public void testCookieAuthWithPath() throws Exception {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(OK_COOKIE);
    mockWebServer.enqueue(MockWebServerResources.JSON_OK);
    CloudantClient client = ClientBuilder.url(mockWebServer.url("/pathex").url())
            .username("user")
            .password("password")
            .build();
    //send single request
    client.database("test", true);
    assertThat("_session is the second element of the path",
            mockWebServer.takeRequest().getPath(),
            is("/pathex/_session"));
}
 
Example #25
Source File: CloudantClientTests.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
private void credentialsCheck(ClientBuilder b, String encodedUser, String encodedPassword)
        throws Exception {
    CloudantClient c = b.build();

    server.enqueue(MockWebServerResources.OK_COOKIE);
    server.enqueue(MockWebServerResources.JSON_OK);

    HttpConnection conn = c.executeRequest(Http.GET(c.getBaseUri()));
    // Consume response stream and assert ok: true
    String responseStr = conn.responseAsString();
    assertNotNull(responseStr);

    // One request to _session then one to get info
    assertEquals(2, server.getRequestCount(), "There should be two requests");

    // Get the _session request
    RecordedRequest request = server.takeRequest();
    String body = request.getBody().readUtf8();
    // body should be of form:
    // name=YourUserName&password=YourPassword
    Matcher m = CREDENTIALS.matcher(body);
    assertTrue(m.matches(), "The _session request should match the regex");
    assertEquals(2, m.groupCount(), "There should be a username group and a password group in" +
            " the creds");
    assertEquals(encodedUser, m.group(1), "The username should match");
    assertEquals(encodedPassword, m.group(2), "The password should match");

    //ensure that building a URL from it does not throw any exceptions
    new URL(c.getBaseUri().toString());
}
 
Example #26
Source File: CloudantClientTests.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
/**
 * Test that adding the Basic Authentication interceptor to CloudantClient works.
 */
@Test
@DisabledWithIam
@RequiresCloudant
public void testBasicAuth() throws IOException {
    BasicAuthInterceptor interceptor =
            new BasicAuthInterceptor(CloudantClientHelper.SERVER_USER
                    + ":" + CloudantClientHelper.SERVER_PASSWORD);

    CloudantClient client = ClientBuilder.account(CloudantClientHelper.SERVER_USER)
            .interceptors(interceptor).build();

    // Test passes if there are no exceptions
    client.getAllDbs();
}
 
Example #27
Source File: CloudantClientTests.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void gatewayStyleURL() throws Exception {

    final String gatewayPath = "/gateway";

    // Set a dispatcher that returns 200 if the requests have the correct path /gateway/_all_dbs
    // Otherwise return 400.
    server.setDispatcher(new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            if (request.getPath().equals(gatewayPath + "/_all_dbs")) {
                return new MockResponse();
            } else {
                return new MockResponse().setResponseCode(400);
            }
        }
    });

    // Build a client with a URL that includes a path
    CloudantClient c = ClientBuilder.url(new URL(server.url(gatewayPath).toString())).build();
    // If the request path is wrong this call will return 400 and throw an exception failing the
    // test.
    c.getAllDbs();

    // Build a client with a URL that includes a path with a trailing /
    c = ClientBuilder.url(new URL(server.url(gatewayPath + "/").toString())).build();
    // If the request path is wrong this call will return 400 and throw an exception failing the
    // test.
    c.getAllDbs();
}
 
Example #28
Source File: CloudFoundryServiceTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void vcapNullServiceNameSpecified() {
    Assertions.assertThrows(IllegalArgumentException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            VCAPGenerator vcap = new CloudFoundryServiceTest.VCAPGenerator();
            vcap.createNewLegacyService("test_bluemix_service_1",
                    TEST_HOST, TEST_USER, TEST_PASSWORD);
            ClientBuilder.bluemix(vcap.toJson(), null, "test_bluemix_service_1").build();
        }
    });
}
 
Example #29
Source File: CloudantClientHelper.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
public static ClientBuilder getClientBuilder() {
    ClientBuilder builder = ClientBuilder.url(SERVER_URL);
    if (IamAuthCondition.IS_IAM_ENABLED) {
        builder.iamApiKey(IamAuthCondition.IAM_API_KEY);
    } else {
        builder.username(SERVER_USER)
                .password(SERVER_PASSWORD);
    }
    return builder;
}
 
Example #30
Source File: LoggingTest.java    From java-cloudant with Apache License 2.0 5 votes vote down vote up
@Test
public void clientBuilderLogging() throws Exception {
    logger = setupLogger(ClientBuilder.class, Level.CONFIG);
    CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).build();
    assertEquals(5, handler.logEntries.size(), "There should be 5 log entries");
    // Validate each of the 5 entries are what we expect
    assertLogMessage("URL: .*", 0);
    assertLogMessage("Building client using URL: .*", 1);
    assertLogMessage("Connect timeout: .*", 2);
    assertLogMessage("Read timeout: .*", 3);
    assertLogMessage("Using default GSON builder", 4);
}