Skip to content

Commit

Permalink
refactor: AssertJ best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie and TeamModerne committed Dec 24, 2024
1 parent adb66e4 commit ab350fb
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
*/
@SpringJUnitConfig
@DirtiesContext
public class KinesisMessageDrivenChannelAdapterTests {
class KinesisMessageDrivenChannelAdapterTests {

private static final String STREAM1 = "stream1";

Expand Down Expand Up @@ -118,7 +118,7 @@ void setup() {

@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void testKinesisMessageDrivenChannelAdapter() {
void kinesisMessageDrivenChannelAdapter() {
this.kinesisMessageDrivenChannelAdapter.start();
final Set<KinesisShardOffset> shardOffsets = TestUtils.getPropertyValue(this.kinesisMessageDrivenChannelAdapter,
"shardOffsets", Set.class);
Expand Down Expand Up @@ -168,11 +168,11 @@ void testKinesisMessageDrivenChannelAdapter() {
Map<?, ?> forLocking = TestUtils.getPropertyValue(this.kinesisMessageDrivenChannelAdapter,
"shardConsumerManager.locks", Map.class);

await().untilAsserted(() -> assertThat(forLocking).hasSize(0));
await().untilAsserted(() -> assertThat(forLocking).isEmpty());

final List consumerInvokers = TestUtils.getPropertyValue(this.kinesisMessageDrivenChannelAdapter,
"consumerInvokers", List.class);
await().untilAsserted(() -> assertThat(consumerInvokers).hasSize(0));
await().untilAsserted(() -> assertThat(consumerInvokers).isEmpty());

this.kinesisMessageDrivenChannelAdapter.setListenerMode(ListenerMode.batch);
this.kinesisMessageDrivenChannelAdapter.setCheckpointMode(CheckpointMode.record);
Expand Down Expand Up @@ -236,16 +236,17 @@ void testKinesisMessageDrivenChannelAdapter() {
assertThat(message.getPayload()).isInstanceOf(List.class);
messagePayload = (List<String>) message.getPayload();
assertThat(messagePayload).size().isEqualTo(2);
assertThat(messagePayload).contains("bar");
assertThat(messagePayload).contains("foobar");
assertThat(messagePayload)
.contains("bar")
.contains("foobar");

this.kinesisMessageDrivenChannelAdapter.stop();

}

@Test
@SuppressWarnings("rawtypes")
void testResharding() throws InterruptedException {
void resharding() throws InterruptedException {
this.reshardingChannelAdapter.start();

assertThat(this.kinesisChannel.receive(10000)).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
*/
@SpringJUnitConfig
@DirtiesContext
public class S3InboundChannelAdapterTests implements LocalstackContainerTest {
class S3InboundChannelAdapterTests implements LocalstackContainerTest {

private static final ExpressionParser PARSER = new SpelExpressionParser();

Expand All @@ -84,12 +84,12 @@ static void setup() {
}

@Test
void testS3InboundChannelAdapter() throws IOException {
void s3InboundChannelAdapter() throws IOException {
Message<?> message = this.s3FilesChannel.receive(10000);
assertThat(message).isNotNull();
assertThat(message.getPayload()).isInstanceOf(File.class);
File localFile = (File) message.getPayload();
assertThat(localFile.getName()).isEqualTo("A.TEST.a");
assertThat(localFile).hasName("A.TEST.a");

String content = FileCopyUtils.copyToString(new FileReader(localFile));
assertThat(content).isEqualTo("Hello");
Expand All @@ -98,7 +98,7 @@ void testS3InboundChannelAdapter() throws IOException {
assertThat(message).isNotNull();
assertThat(message.getPayload()).isInstanceOf(File.class);
localFile = (File) message.getPayload();
assertThat(localFile.getName()).isEqualTo("B.TEST.a");
assertThat(localFile).hasName("B.TEST.a");

content = FileCopyUtils.copyToString(new FileReader(localFile));
assertThat(content).isEqualTo("Bye");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
*/
@SpringJUnitConfig
@DirtiesContext
public class S3StreamingChannelAdapterTests implements LocalstackContainerTest {
class S3StreamingChannelAdapterTests implements LocalstackContainerTest {

private static final String S3_BUCKET = "s3-bucket";

Expand All @@ -74,7 +74,7 @@ static void setup() {
}

@Test
void testS3InboundStreamingChannelAdapter() throws IOException {
void s3InboundStreamingChannelAdapter() throws IOException {
Message<?> message = this.s3FilesChannel.receive(10000);
assertThat(message).isNotNull();
assertThat(message.getPayload()).isInstanceOf(InputStream.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*/
@SpringJUnitWebConfig
@DirtiesContext
public class SnsInboundChannelAdapterTests {
class SnsInboundChannelAdapterTests {

@Autowired
private WebApplicationContext context;
Expand Down Expand Up @@ -83,7 +83,7 @@ void setUp() {
}

@Test
void testSubscriptionConfirmation() throws Exception {
void subscriptionConfirmation() throws Exception {
this.mockMvc
.perform(post("/mySampleTopic").header("x-amz-sns-message-type", "SubscriptionConfirmation")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -110,7 +110,7 @@ void testSubscriptionConfirmation() throws Exception {

@Test
@SuppressWarnings("unchecked")
void testNotification() throws Exception {
void notification() throws Exception {
this.mockMvc
.perform(post("/mySampleTopic").header("x-amz-sns-message-type", "Notification")
.contentType(MediaType.TEXT_PLAIN)
Expand All @@ -121,12 +121,13 @@ void testNotification() throws Exception {
assertThat(receive).isNotNull();
Map<String, String> payload = (Map<String, String>) receive.getPayload();

assertThat(payload.get("Subject")).isEqualTo("foo");
assertThat(payload.get("Message")).isEqualTo("bar");
assertThat(payload)
.containsEntry("Subject", "foo")
.containsEntry("Message", "bar");
}

@Test
void testUnsubscribe() throws Exception {
void unsubscribe() throws Exception {
this.mockMvc
.perform(post("/mySampleTopic").header("x-amz-sns-message-type", "UnsubscribeConfirmation")
.contentType(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*/
@SpringJUnitConfig
@DirtiesContext
public class SqsMessageDrivenChannelAdapterTests implements LocalstackContainerTest {
class SqsMessageDrivenChannelAdapterTests implements LocalstackContainerTest {

private static SqsAsyncClient AMAZON_SQS;

Expand All @@ -59,7 +59,7 @@ static void setup() {
}

@Test
void testSqsMessageDrivenChannelAdapter() {
void sqsMessageDrivenChannelAdapter() {
Map<String, MessageAttributeValue> attributes =
Map.of("someAttribute",
MessageAttributeValue.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
*/
@SpringJUnitConfig
@DirtiesContext

public class KclMessageDrivenChannelAdapterMultiStreamTests implements LocalstackContainerTest {
class KclMessageDrivenChannelAdapterMultiStreamTests implements LocalstackContainerTest {

private static final String TEST_STREAM1 = "MultiStreamKcl1";

Expand Down Expand Up @@ -104,7 +103,7 @@ static void tearDown() {
}

@Test
public void kclChannelAdapterMultiStream() {
void kclChannelAdapterMultiStream() {
String testData = "test data";
AMAZON_KINESIS.putRecord(request -> request
.streamName(TEST_STREAM1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private Message<?> verifyRecordReceived(String testData) {
}

@Test
public void metricsLevelOfMetricsConfigShouldBeSetToMetricsLevelOfAdapter() {
void metricsLevelOfMetricsConfigShouldBeSetToMetricsLevelOfAdapter() {
MetricsLevel metricsLevel =
TestUtils.getPropertyValue(this.kclMessageDrivenChannelAdapter,
"scheduler.metricsConfig.metricsLevel",
Expand All @@ -170,7 +170,7 @@ public void metricsLevelOfMetricsConfigShouldBeSetToMetricsLevelOfAdapter() {
}

@Test
public void metricsFactoryOfSchedulerShouldBeSetNullMetricsFactoryIfMetricsLevelIsNone() {
void metricsFactoryOfSchedulerShouldBeSetNullMetricsFactoryIfMetricsLevelIsNone() {
MetricsFactory metricsFactory =
TestUtils.getPropertyValue(this.kclMessageDrivenChannelAdapter,
"scheduler.metricsFactory",
Expand All @@ -179,7 +179,7 @@ public void metricsFactoryOfSchedulerShouldBeSetNullMetricsFactoryIfMetricsLevel
}

@Test
public void maxLeasesForWorkerOverriddenByCustomizer() {
void maxLeasesForWorkerOverriddenByCustomizer() {
Integer maxLeasesForWorker =
TestUtils.getPropertyValue(this.kclMessageDrivenChannelAdapter,
"scheduler.leaseCoordinator.leaseTaker.maxLeasesForWorker",
Expand All @@ -188,7 +188,7 @@ public void maxLeasesForWorkerOverriddenByCustomizer() {
}

@Test
public void shardConsumerDispatchPollIntervalMillisOverriddenByCustomizer() {
void shardConsumerDispatchPollIntervalMillisOverriddenByCustomizer() {
Long shardConsumerDispatchPollIntervalMillis =
TestUtils.getPropertyValue(this.kclMessageDrivenChannelAdapter,
"scheduler.shardConsumerDispatchPollIntervalMillis",
Expand All @@ -197,7 +197,7 @@ public void shardConsumerDispatchPollIntervalMillisOverriddenByCustomizer() {
}

@Test
public void pollingMaxRecordsIsPropagated() {
void pollingMaxRecordsIsPropagated() {
Integer maxRecords =
TestUtils.getPropertyValue(this.kclMessageDrivenChannelAdapter,
"scheduler.retrievalConfig.retrievalSpecificConfig.maxRecords",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
*/
@SpringJUnitConfig
@DirtiesContext
public class KinesisIntegrationTests implements LocalstackContainerTest {
class KinesisIntegrationTests implements LocalstackContainerTest {

private static final String TEST_STREAM = "TestStream";

Expand Down Expand Up @@ -95,7 +95,7 @@ static void tearDown() {
}

@Test
void testKinesisInboundOutbound() {
void kinesisInboundOutbound() {
this.kinesisSendChannel
.send(MessageBuilder.withPayload("foo").setHeader(AwsHeaders.STREAM, TEST_STREAM).build());

Expand Down Expand Up @@ -138,7 +138,7 @@ void testKinesisInboundOutbound() {
assertThat(receivedSequences.add(sequenceNumber)).isTrue();
}

assertThat(receivedSequences.size()).isEqualTo(2);
assertThat(receivedSequences).hasSize(2);

receive = this.kinesisReceiveChannel.receive(10);
assertThat(receive).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
@Disabled("Depends on real call to http://169.254.169.254 through native library")
@SpringJUnitConfig
@DirtiesContext
public class KplKclIntegrationTests implements LocalstackContainerTest {
class KplKclIntegrationTests implements LocalstackContainerTest {

private static final String TEST_STREAM = "TestStreamKplKcl";

Expand Down Expand Up @@ -105,7 +105,7 @@ static void tearDown() {
}

@Test
void testKinesisInboundOutbound() {
void kinesisInboundOutbound() {
this.kinesisSendChannel
.send(MessageBuilder.withPayload("foo").setHeader(AwsHeaders.STREAM, TEST_STREAM).build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void destroy() {
}

@Test
void testDistributedLeaderElection() throws Exception {
void distributedLeaderElection() throws Exception {
CountDownLatch granted = new CountDownLatch(1);
CountingPublisher countingPublisher = new CountingPublisher(granted);
List<DynamoDbLockRepository> repositories = new ArrayList<>();
Expand Down Expand Up @@ -176,7 +176,7 @@ void testDistributedLeaderElection() throws Exception {
}

@Test
void testLostConnection() throws Exception {
void lostConnection() throws Exception {
CountDownLatch granted = new CountDownLatch(1);
CountingPublisher countingPublisher = new CountingPublisher(granted);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*/
@SpringJUnitConfig
@DirtiesContext
public class DynamoDbLockRegistryTests implements LocalstackContainerTest {
class DynamoDbLockRegistryTests implements LocalstackContainerTest {

private static DynamoDbAsyncClient DYNAMO_DB;

Expand Down Expand Up @@ -98,7 +98,7 @@ void clear() throws InterruptedException {

@Test
@SuppressWarnings("unchecked")
void testLock() {
void lock() {
for (int i = 0; i < 10; i++) {
Lock lock = this.dynamoDbLockRegistry.obtain("foo");
lock.lock();
Expand All @@ -113,7 +113,7 @@ void testLock() {

@Test
@SuppressWarnings("unchecked")
void testLockInterruptibly() throws Exception {
void lockInterruptibly() throws Exception {
for (int i = 0; i < 10; i++) {
Lock lock = this.dynamoDbLockRegistry.obtain("foo");
lock.lockInterruptibly();
Expand All @@ -127,7 +127,7 @@ void testLockInterruptibly() throws Exception {
}

@Test
void testReentrantLock() {
void reentrantLock() {
for (int i = 0; i < 10; i++) {
Lock lock1 = this.dynamoDbLockRegistry.obtain("foo");
lock1.lock();
Expand All @@ -144,7 +144,7 @@ void testReentrantLock() {
}

@Test
void testReentrantLockInterruptibly() throws Exception {
void reentrantLockInterruptibly() throws Exception {
for (int i = 0; i < 10; i++) {
Lock lock1 = this.dynamoDbLockRegistry.obtain("foo");
lock1.lockInterruptibly();
Expand All @@ -161,7 +161,7 @@ void testReentrantLockInterruptibly() throws Exception {
}

@Test
void testTwoLocks() throws Exception {
void twoLocks() throws Exception {
for (int i = 0; i < 10; i++) {
Lock lock1 = this.dynamoDbLockRegistry.obtain("foo");
lock1.lockInterruptibly();
Expand All @@ -178,7 +178,7 @@ void testTwoLocks() throws Exception {
}

@Test
void testTwoThreadsSecondFailsToGetLock() throws Exception {
void twoThreadsSecondFailsToGetLock() throws Exception {
final Lock lock1 = this.dynamoDbLockRegistry.obtain("foo");
lock1.lockInterruptibly();
final AtomicBoolean locked = new AtomicBoolean();
Expand Down Expand Up @@ -212,7 +212,7 @@ void testTwoThreadsSecondFailsToGetLock() throws Exception {
}

@Test
void testTwoThreads() throws Exception {
void twoThreads() throws Exception {
final Lock lock1 = this.dynamoDbLockRegistry.obtain("foo");
final AtomicBoolean locked = new AtomicBoolean();
final CountDownLatch latch1 = new CountDownLatch(1);
Expand Down Expand Up @@ -254,7 +254,7 @@ void testTwoThreads() throws Exception {
}

@Test
void testTwoThreadsDifferentRegistries() throws Exception {
void twoThreadsDifferentRegistries() throws Exception {
DynamoDbLockRepository dynamoDbLockRepository = new DynamoDbLockRepository(DYNAMO_DB);
dynamoDbLockRepository.setLeaseDuration(Duration.ofSeconds(10));
dynamoDbLockRepository.afterPropertiesSet();
Expand Down Expand Up @@ -299,7 +299,7 @@ void testTwoThreadsDifferentRegistries() throws Exception {
}

@Test
void testTwoThreadsWrongOneUnlocks() throws Exception {
void twoThreadsWrongOneUnlocks() throws Exception {
final Lock lock = this.dynamoDbLockRegistry.obtain("foo");
lock.lockInterruptibly();
final AtomicBoolean locked = new AtomicBoolean();
Expand Down Expand Up @@ -343,7 +343,7 @@ void abandonedLock() throws Exception {
}

@Test
public void testLockRenew() {
void lockRenew() {
final Lock lock = this.dynamoDbLockRegistry.obtain("foo");

assertThat(lock.tryLock()).isTrue();
Expand Down
Loading

0 comments on commit ab350fb

Please sign in to comment.