Search in sources :

Example 46 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class HttpIamTest method iamRenewalFailureOnSessionCookie.

/**
 * Test IAM token and cookie flow, where session expires and subsequent session cookie fails:
 * - GET a resource on the cloudant server
 * - Cookie jar empty, so get IAM token followed by session cookie
 * - GET now proceeds as normal, expected cookie value is sent in header
 * - second GET on cloudant server, re-using session cookie
 * - third GET on cloudant server, cookie expired, get IAM token, subsequent session cookie
 * request fails, no more requests are made
 *
 * @throws Exception
 */
@Test
public void iamRenewalFailureOnSessionCookie() throws Exception {
    // Request sequence
    mockWebServer.enqueue(OK_IAM_COOKIE);
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(hello));
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(hello));
    // cookie expired
    mockWebServer.enqueue(new MockResponse().setResponseCode(401).setBody("{\"error\":\"credentials_expired\"}"));
    // mock 500 on cookie renewal
    mockWebServer.enqueue(new MockResponse().setResponseCode(500));
    mockIamServer.enqueue(new MockResponse().setResponseCode(200).setBody(IAM_TOKEN));
    mockIamServer.enqueue(new MockResponse().setResponseCode(200).setBody(IAM_TOKEN_2));
    CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).iamApiKey(iamApiKey).build();
    String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
    assertEquals(hello, response, "The expected response should be received");
    String response2 = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
    assertEquals(hello, response2, "The expected response should be received");
    // unhelpful
    try {
        c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
        fail("Should get CouchDbException when trying to get response");
    } catch (CouchDbException cdbe) {
        ;
    }
    // cloudant mock server
    // assert that there were 5 calls
    RecordedRequest[] recordedRequests = takeN(mockWebServer, 5);
    assertEquals("/_iam_session", recordedRequests[0].getPath(), "The request should have been for /_iam_session");
    assertThat("The request body should contain the IAM token", recordedRequests[0].getBody().readString(Charset.forName("UTF-8")), containsString(IAM_TOKEN));
    // first request
    assertEquals("/", recordedRequests[1].getPath(), "The request should have been for /");
    // The cookie may or may not have the session id quoted, so check both
    assertThat("The Cookie header should contain the expected session value", recordedRequests[1].getHeader("Cookie"), anyOf(containsString(iamSession(EXPECTED_OK_COOKIE)), containsString(iamSessionUnquoted(EXPECTED_OK_COOKIE))));
    // second request
    assertEquals("/", recordedRequests[2].getPath(), "The request should have been for /");
    // third request, will be rejected due to cookie expiry
    assertEquals("/", recordedRequests[3].getPath(), "The request should have been for /");
    // try to renew cookie but get 500
    assertEquals("/_iam_session", recordedRequests[4].getPath(), "The request should have been for /_iam_session");
    assertThat("The request body should contain the IAM token", recordedRequests[4].getBody().readString(Charset.forName("UTF-8")), containsString(IAM_TOKEN_2));
    // iam mock server
    // assert that there were 2 calls
    RecordedRequest[] recordedIamRequests = takeN(mockIamServer, 2);
    // first time, automatically fetch because cookie jar is empty
    assertEquals(iamTokenEndpoint, recordedIamRequests[0].getPath(), "The request should have been for " + "/identity/token");
    assertThat("The request body should contain the IAM API key", recordedIamRequests[0].getBody().readString(Charset.forName("UTF-8")), containsString("apikey=" + iamApiKey));
    // second time, refresh because the cloudant session cookie has expired
    assertEquals(iamTokenEndpoint, recordedIamRequests[1].getPath(), "The request should have been for " + "/identity/token");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.jupiter.api.Test)

Example 47 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class IndexTests method testIndexMovieFindByIndexDesignDoc.

@Test
public void testIndexMovieFindByIndexDesignDoc() {
    QueryResult<Movie> movies = db.query(new QueryBuilder(and(gt("Movie_year", 1960), eq("Person_name", "Alec Guinness"))).sort(Sort.desc("Movie_year")).fields("Movie_name", "Movie_year").limit(1).skip(1).useIndex("Movie_year").build(), Movie.class);
    assertNotNull(movies);
    assert (movies.getDocs().size() == 1);
    for (Movie m : movies.getDocs()) {
        assertNotNull(m.getMovie_name());
        assertNotNull(m.getMovie_year());
    }
}
Also used : QueryBuilder(com.cloudant.client.api.query.QueryBuilder) Test(org.junit.jupiter.api.Test)

Example 48 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class IndexTests method useIndexNotSpecified.

/**
 * Test that use_index is not specified if no index is provided
 *
 * @throws Exception
 */
@Test
public void useIndexNotSpecified() throws Exception {
    JsonElement useIndex = getUseIndexFromRequest(new QueryBuilder(empty()));
    assertNull(useIndex, "The use_index property should be null (i.e. was not specified)");
}
Also used : JsonElement(com.google.gson.JsonElement) QueryBuilder(com.cloudant.client.api.query.QueryBuilder) Test(org.junit.jupiter.api.Test)

Example 49 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class IndexTests method testNotNullIndexNamesAndFields.

@Test
public void testNotNullIndexNamesAndFields() {
    List<JsonIndex> indices = db.listIndexes().jsonIndexes();
    assertNotNull(indices);
    assert (indices.size() > 0);
    for (JsonIndex i : indices) {
        assertNotNull(i.getName());
        assertNotNull(i.getFields());
        List<JsonIndex.Field> flds = i.getFields();
        assertTrue(flds.size() > 0, "The fields should not be empty");
        for (JsonIndex.Field field : flds) {
            assertNotNull(field.getName(), "The field name should not be null");
            assertNotNull(field.getOrder(), "The sort order should not be null");
        }
    }
}
Also used : IndexField(com.cloudant.client.api.model.IndexField) JsonIndex(com.cloudant.client.api.query.JsonIndex) Test(org.junit.jupiter.api.Test)

Example 50 with Test

use of org.junit.jupiter.api.Test in project java-cloudant by cloudant.

the class IndexTests method useIndexDesignDocAndIndexNameJsonTypeIsArray.

/**
 * Test that a design document and index name is passed as an array.
 *
 * @throws Exception
 */
@Test
public void useIndexDesignDocAndIndexNameJsonTypeIsArray() throws Exception {
    JsonElement useIndex = getUseIndexFromRequest(new QueryBuilder(empty()).useIndex("Movie_year", "Person_name"));
    assertNotNull(useIndex, "The use_index property should not be null");
    assertTrue(useIndex.isJsonArray(), "The use_index property should be a JsonArray");
    JsonArray useIndexArray = useIndex.getAsJsonArray();
    assertEquals(2, useIndexArray.size(), "The use_index array should have two elements");
    assertEquals("Movie_year", useIndexArray.get(0).getAsString(), "The use_index design " + "document should be Movie_year");
    assertEquals("Person_name", useIndexArray.get(1).getAsString(), "The use_index index name" + " should be Person_name");
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) QueryBuilder(com.cloudant.client.api.query.QueryBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)67450 lombok.val (lombok.val)3880 File (java.io.File)2228 HashMap (java.util.HashMap)2180 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2164 ArrayList (java.util.ArrayList)2137 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2027 SqlSession (org.apache.ibatis.session.SqlSession)1845 List (java.util.List)1799 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1484 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1184 Map (java.util.Map)1143 IOException (java.io.IOException)1048 Path (java.nio.file.Path)1006 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)935 Date (java.util.Date)914 Method (java.lang.reflect.Method)862 TestBean (org.springframework.beans.testfixture.beans.TestBean)822 Transaction (org.neo4j.graphdb.Transaction)752 BaseDataTest (org.apache.ibatis.BaseDataTest)740