Polish "Replace Mockito argument captors with assertArg"

Co-authored-by: Andy Wilkinson <wilkinsona@vmware.com>

See gh-35015
This commit is contained in:
Moritz Halbritter
2023-04-20 07:37:58 +01:00
parent 80ca37984a
commit 441ed30ee4
7 changed files with 149 additions and 153 deletions

View File

@@ -33,11 +33,10 @@ import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.assertj.core.api.ThrowingConsumer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@@ -78,9 +77,6 @@ class HttpClientTransportTests {
@Mock
private InputStream content;
@Captor
private ArgumentCaptor<HttpUriRequest> requestCaptor;
private HttpClientTransport http;
private URI uri;
@@ -117,13 +113,14 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri);
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull();
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull();
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@@ -132,13 +129,15 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, "auth token");
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER).getValue()).isEqualTo("auth token");
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER).getValue())
.isEqualTo("auth token");
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@@ -147,13 +146,14 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, "");
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull();
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull();
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@@ -164,18 +164,19 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, APPLICATION_JSON,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(content.length());
assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(content.length());
assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@@ -186,18 +187,19 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, APPLICATION_X_TAR,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@@ -208,18 +210,18 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200);
Response response = this.http.put(this.uri, APPLICATION_JSON,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(content.length());
assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPut>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(content.length());
assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@@ -230,18 +232,18 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200);
Response response = this.http.put(this.uri, APPLICATION_X_TAR,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPut>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@@ -250,12 +252,14 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200);
Response response = this.http.delete(this.uri);
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue();
assertThat(request).isInstanceOf(HttpDelete.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(response.getContent()).isSameAs(this.content);
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpDelete>) (request) -> {
assertThat(request).isInstanceOf(HttpDelete.class);
assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test

View File

@@ -20,8 +20,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.cli.command.status.ExitStatus;
@@ -31,6 +29,7 @@ import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.BDDMockito.then;
/**
@@ -43,9 +42,6 @@ class EncodePasswordCommandTests {
private MockLog log;
@Captor
private ArgumentCaptor<String> message;
@BeforeEach
void setup() {
this.log = MockLog.attach();
@@ -60,10 +56,10 @@ class EncodePasswordCommandTests {
void encodeWithNoAlgorithmShouldUseBcrypt() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand();
ExitStatus status = command.run("boot");
then(this.log).should().info(this.message.capture());
assertThat(this.message.getValue()).startsWith("{bcrypt}");
assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", this.message.getValue()))
.isTrue();
then(this.log).should().info(assertArg((message) -> {
assertThat(message).startsWith("{bcrypt}");
assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", message)).isTrue();
}));
assertThat(status).isEqualTo(ExitStatus.OK);
}
@@ -71,9 +67,10 @@ class EncodePasswordCommandTests {
void encodeWithBCryptShouldUseBCrypt() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand();
ExitStatus status = command.run("-a", "bcrypt", "boot");
then(this.log).should().info(this.message.capture());
assertThat(this.message.getValue()).doesNotStartWith("{");
assertThat(new BCryptPasswordEncoder().matches("boot", this.message.getValue())).isTrue();
then(this.log).should().info(assertArg((message) -> {
assertThat(message).doesNotStartWith("{");
assertThat(new BCryptPasswordEncoder().matches("boot", message)).isTrue();
}));
assertThat(status).isEqualTo(ExitStatus.OK);
}
@@ -81,10 +78,10 @@ class EncodePasswordCommandTests {
void encodeWithPbkdf2ShouldUsePbkdf2() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand();
ExitStatus status = command.run("-a", "pbkdf2", "boot");
then(this.log).should().info(this.message.capture());
assertThat(this.message.getValue()).doesNotStartWith("{");
assertThat(Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8().matches("boot", this.message.getValue()))
.isTrue();
then(this.log).should().info(assertArg((message) -> {
assertThat(message).doesNotStartWith("{");
assertThat(Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8().matches("boot", message)).isTrue();
}));
assertThat(status).isEqualTo(ExitStatus.OK);
}