tpalfy commented on a change in pull request #5319:
URL: https://github.com/apache/nifi/pull/5319#discussion_r703503213
##########
File path:
nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/test/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProviderTest.java
##########
@@ -109,40 +119,251 @@ public void testAcquireNewToken() throws Exception {
@Test
public void testRefreshToken() throws Exception {
// GIVEN
- Request mockRequest = new Request.Builder()
- .url("http://unimportant_but_required")
- .build();
+ String firstToken = "first_token";
+ String expectedToken = "second_token";
- String accessTokenValue1 = "access_token_value1";
- String accessTokenValue2 = "access_token_value2";
+ Response response1 = buildResponse(
+ 200,
+ "{ \"access_token\":\"" + firstToken + "\", \"expires_in\":\"0\",
\"refresh_token\":\"not_checking_in_this_test\" }"
+ );
- Response response1 = new Response.Builder()
- .request(mockRequest)
- .protocol(Protocol.HTTP_2)
- .message("unimportant_but_required")
- .code(200)
- .body(ResponseBody.create(
- ("{ \"access_token\":\"" + accessTokenValue1 + "\",
\"expires_in\":\"0\", \"refresh_token\":\"not_checking_in_this_test\"
}").getBytes(),
- MediaType.parse("application/json")))
- .build();
+ Response response2 = buildResponse(
+ 200,
+ "{ \"access_token\":\"" + expectedToken + "\" }"
+ );
+
+
when(mockHttpClient.newCall(any(Request.class)).execute()).thenReturn(response1,
response2);
+
+ // WHEN
+ testSubject.getAccessDetails();
+ String actualToken = testSubject.getAccessDetails().getAccessToken();
+
+ // THEN
+ assertEquals(expectedToken, actualToken);
+ }
+
+ @Test
+ public void testIOExceptionDuringRefreshAndSubsequentAcquire() throws
Exception {
+ // GIVEN
+ String refreshErrorMessage = "refresh_error";
+ String acquireErrorMessage = "acquire_error";
+
+ AtomicInteger callCounter = new AtomicInteger(0);
+
when(mockHttpClient.newCall(any(Request.class)).execute()).thenAnswer(invocation
-> {
+ callCounter.incrementAndGet();
+
+ if (callCounter.get() == 1) {
+ return buildSuccessfulInitResponse();
+ } else if (callCounter.get() == 2) {
+ throw new IOException(refreshErrorMessage);
+ } else if (callCounter.get() == 3) {
+ throw new IOException(acquireErrorMessage);
+ }
+
+ throw new IllegalStateException("Test improperly defined mock HTTP
responses.");
+ });
+
+ // Get a good accessDetails so we can have a refresh a second time
+ testSubject.getAccessDetails();
+
+ // WHEN
+ try {
+ testSubject.getAccessDetails();
+ fail();
+ } catch (UncheckedIOException e) {
+ // THEN
+ checkLoggedDebugWhenRefreshThrowsIOException();
+
+ checkLoggedRefreshError(new UncheckedIOException("OAuth2 access
token request failed", new IOException(refreshErrorMessage)));
+
+ checkError(new UncheckedIOException("OAuth2 access token request
failed", new IOException(acquireErrorMessage)), e);
+ }
+ }
+
+ @Test
+ public void testIOExceptionDuringRefreshSuccessfulSubsequentAcquire()
throws Exception {
+ // GIVEN
+ String refreshErrorMessage = "refresh_error";
+ String expectedToken = "expected_token";
+
+ Response successfulAcquireResponse = buildResponse(
+ 200,
+ "{ \"access_token\":\"" + expectedToken + "\",
\"expires_in\":\"0\", \"refresh_token\":\"not_checking_in_this_test\" }"
+ );
+
+ AtomicInteger callCounter = new AtomicInteger(0);
+
when(mockHttpClient.newCall(any(Request.class)).execute()).thenAnswer(invocation
-> {
+ callCounter.incrementAndGet();
+
+ if (callCounter.get() == 1) {
+ return buildSuccessfulInitResponse();
+ } else if (callCounter.get() == 2) {
+ throw new IOException(refreshErrorMessage);
+ } else if (callCounter.get() == 3) {
+ return successfulAcquireResponse;
+ }
+
+ throw new IllegalStateException("Test improperly defined mock HTTP
responses.");
+ });
+
+ // Get a good accessDetails so we can have a refresh a second time
+ testSubject.getAccessDetails();
+
+ // WHEN
+ String actualToken = testSubject.getAccessDetails().getAccessToken();
+
+ // THEN
+ checkLoggedDebugWhenRefreshThrowsIOException();
+
+ checkLoggedRefreshError(new UncheckedIOException("OAuth2 access token
request failed", new IOException(refreshErrorMessage)));
+
+ assertEquals(expectedToken, actualToken);
+ }
+
+ @Test
+ public void testHTTPErrorDuringRefreshAndSubsequentAcquire() throws
Exception {
+ // GIVEN
+ String errorRefreshResponseBody = "{
\"error_response\":\"refresh_error\" }";
+ String errorAcquireResponseBody = "{
\"error_response\":\"acquire_error\" }";
+
+ Response errorRefreshResponse = buildResponse(500,
errorRefreshResponseBody);
+ Response errorAcquireResponse = buildResponse(503,
errorAcquireResponseBody);
+
+ AtomicInteger callCounter = new AtomicInteger(0);
+
when(mockHttpClient.newCall(any(Request.class)).execute()).thenAnswer(invocation
-> {
+ callCounter.incrementAndGet();
+
+ if (callCounter.get() == 1) {
+ return buildSuccessfulInitResponse();
+ } else if (callCounter.get() == 2) {
+ return errorRefreshResponse;
+ } else if (callCounter.get() == 3) {
+ return errorAcquireResponse;
+ }
+
+ throw new IllegalStateException("Test improperly defined mock HTTP
responses.");
+ });
+
+ List<String> expectedLoggedInfo = Arrays.asList(
+ String.format("OAuth2 access token request failed [HTTP %d],
response:%n%s", 500, errorRefreshResponseBody),
+ String.format("OAuth2 access token request failed [HTTP %d],
response:%n%s", 503, errorAcquireResponseBody)
+ );
+
+ // Get a good accessDetails so we can have a refresh a second time
+ testSubject.getAccessDetails();
+
+ // WHEN
+ try {
+ testSubject.getAccessDetails();
+ fail();
+ } catch (ProcessException e) {
+ // THEN
+ checkLoggedDebugWhenRefreshThrowsIOException();
+
+ checkLoggedRefreshError(new ProcessException("OAuth2 access token
request failed [HTTP 500]"));
+
+
checkedLoggedErrorWhenRefreshReturnsBadHTTPResponse(expectedLoggedInfo);
+
+ checkError(new ProcessException("OAuth2 access token request
failed [HTTP 503]"), e);
+ }
Review comment:
Updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]