|
package com.project.configuration; |
|
|
|
import org.junit.jupiter.api.Test; |
|
import org.junit.jupiter.api.extension.ExtendWith; |
|
import org.mockito.InjectMocks; |
|
import org.mockito.Mock; |
|
import org.mockito.junit.jupiter.MockitoExtension; |
|
import org.springframework.amqp.core.Queue; |
|
import org.springframework.amqp.core.QueueBuilder; |
|
import org.springframework.amqp.rabbit.core.RabbitAdmin; |
|
|
|
import java.util.Properties; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
import static org.assertj.core.api.InstanceOfAssertFactories.map; |
|
import static org.mockito.Mockito.when; |
|
|
|
@ExtendWith(MockitoExtension.class) |
|
class RabbitQueueCheckHealthIndicatorTest { |
|
|
|
@InjectMocks |
|
private RabbitQueueCheckHealthIndicator rabbitQueueCheckHealthIndicator; |
|
|
|
@Mock |
|
private RabbitAdmin rabbitAdmin; |
|
|
|
@Test |
|
void it_should_report_up_when_message_and_consumer_counts_are_within_thresholds() { |
|
Queue queue1 = QueueBuilder.durable("queue.name").build(); |
|
Queue queue2 = QueueBuilder.durable("queue.name2").build(); |
|
rabbitQueueCheckHealthIndicator |
|
.addQueueCheck(queue1, 10, 1) |
|
.addQueueCheck(queue2, Integer.MAX_VALUE, 1); |
|
when(rabbitAdmin.getQueueProperties(queue1.getName())).thenReturn(buildQueueProperties(5, 2)); |
|
when(rabbitAdmin.getQueueProperties(queue2.getName())).thenReturn(buildQueueProperties(0, 1)); |
|
|
|
var health = rabbitQueueCheckHealthIndicator.health(); |
|
|
|
assertThat(health).isNotNull(); |
|
assertThat(health.getStatus().getCode()).isEqualTo("UP"); |
|
assertThat(health.getDetails()) |
|
.containsKey(queue1.getName()) |
|
.containsKey(queue2.getName()); |
|
assertThat(health.getDetails().get(queue1.getName())) |
|
.asInstanceOf(map(String.class, Object.class)) |
|
.containsEntry("messageCount", 5) |
|
.containsEntry("consumerCount", 2) |
|
.containsEntry("maxMessageCount", 10) |
|
.containsEntry("minConsumerCount", 1); |
|
} |
|
|
|
@Test |
|
void it_should_report_down_when_queue_is_not_found() { |
|
Queue queue = QueueBuilder.durable("queue.name").build(); |
|
rabbitQueueCheckHealthIndicator.addQueueCheck(queue, 100, 1); |
|
when(rabbitAdmin.getQueueProperties(queue.getName())).thenReturn(null); |
|
|
|
var health = rabbitQueueCheckHealthIndicator.health(); |
|
|
|
assertThat(health).isNotNull(); |
|
assertThat(health.getStatus().getCode()).isEqualTo("DOWN"); |
|
assertThat(health.getDetails()).containsEntry(queue.getName(), "Queue not found"); |
|
} |
|
|
|
@Test |
|
void it_should_report_down_when_message_count_exceeds_max_threshold() { |
|
Queue queue = QueueBuilder.durable("queue.name").build(); |
|
rabbitQueueCheckHealthIndicator.addQueueCheck(queue, 1000, 1); |
|
when(rabbitAdmin.getQueueProperties(queue.getName())).thenReturn(buildQueueProperties(1001, 1)); |
|
|
|
var health = rabbitQueueCheckHealthIndicator.health(); |
|
|
|
assertThat(health).isNotNull(); |
|
assertThat(health.getStatus().getCode()).isEqualTo("DOWN"); |
|
} |
|
|
|
@Test |
|
void it_should_report_down_when_consumer_count_is_below_min_threshold() { |
|
Queue queue = QueueBuilder.durable("queue.name").build(); |
|
rabbitQueueCheckHealthIndicator.addQueueCheck(queue, 10, 2); |
|
when(rabbitAdmin.getQueueProperties(queue.getName())).thenReturn(buildQueueProperties(1, 1)); |
|
|
|
var health = rabbitQueueCheckHealthIndicator.health(); |
|
|
|
assertThat(health).isNotNull(); |
|
assertThat(health.getStatus().getCode()).isEqualTo("DOWN"); |
|
} |
|
|
|
private Properties buildQueueProperties(int messageCount, int consumerCount) { |
|
Properties properties = new Properties(); |
|
properties.put(RabbitAdmin.QUEUE_MESSAGE_COUNT, messageCount); |
|
properties.put(RabbitAdmin.QUEUE_CONSUMER_COUNT, consumerCount); |
|
return properties; |
|
} |
|
} |