Skip to content

Commit

Permalink
polish junits
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Jan 10, 2025
1 parent aece594 commit f17a48e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageRestController {
class MessageRestController {

private final Sender sender;
private final MessageService messageService;

public MessageRestController(Sender sender, MessageService messageService) {
MessageRestController(Sender sender, MessageService messageService) {
this.sender = sender;
this.messageService = messageService;
}

@GetMapping("/listeners")
public ResponseEntity<Map<String, Boolean>> getListeners() {
ResponseEntity<Map<String, Boolean>> getListeners() {
return ResponseEntity.ok(messageService.getListenersState());
}

Expand All @@ -48,18 +48,18 @@ public ResponseEntity<Map<String, Boolean>> getListeners() {
@ApiResponse(responseCode = "404", description = "Listener not found", content = @Content)
})
@PostMapping("/listeners")
public ResponseEntity<Map<String, Boolean>> updateListenerState(
ResponseEntity<Map<String, Boolean>> updateListenerState(
@RequestBody @Valid final KafkaListenerRequest kafkaListenerRequest) {
return ResponseEntity.ok(messageService.updateListenerState(kafkaListenerRequest));
}

@PostMapping("/messages")
public void sendMessage(@RequestBody MessageDTO messageDTO) {
void sendMessage(@RequestBody MessageDTO messageDTO) {
this.sender.send(messageDTO);
}

@GetMapping("/topics")
public List<TopicInfo> getTopicsWithPartitionsCount(
List<TopicInfo> getTopicsWithPartitionsCount(
@RequestParam(required = false, defaultValue = "false") boolean showInternalTopics) {
return messageService.getTopicsWithPartitions(showInternalTopics);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public class Receiver1 {

private static final Logger logger = LoggerFactory.getLogger(Receiver1.class);

@KafkaListener(topics = TOPIC_TEST_1, groupId = "foo", errorHandler = "validationErrorHandler")
@KafkaListener(
id = "topic_1_Listener",
topics = TOPIC_TEST_1,
groupId = "foo",
errorHandler = "validationErrorHandler")
@SendTo(TOPIC_TEST_2)
public MessageDTO listen(@Header(KafkaHeaders.RECEIVED_KEY) UUID key, ConsumerRecord<String, MessageDTO> cr) {
logger.info("Received message : {} with Key :{} in topic :{}", cr.toString(), key, cr.topic());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public CountDownLatch getDeadLetterLatch() {
backoff = @Backoff(delay = 1000, multiplier = 2.0),
exclude = {MethodArgumentNotValidException.class},
topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE)
@KafkaListener(topics = TOPIC_TEST_2, groupId = "foo")
@KafkaListener(id = "topic_2_Listener", topics = TOPIC_TEST_2, groupId = "foo")
public void listenTopic2(@Payload @Valid MessageDTO messageDTO, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
logger.info("Received message : {} in topic :{}", messageDTO.toString(), topic);
latch.countDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.example.springbootkafkasample.dto.MessageDTO;
import com.example.springbootkafkasample.service.listener.Receiver2;
import java.util.UUID;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
Expand All @@ -17,7 +14,6 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.MessageListenerContainer;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.context.EmbeddedKafka;
Expand All @@ -38,12 +34,6 @@ class KafkaSampleApplicationTests {
@Autowired
private EmbeddedKafkaBroker embeddedKafkaBroker;

@Autowired
private KafkaTemplate<UUID, MessageDTO> template;

@Autowired
private Receiver2 receiver2;

@Autowired
private MockMvc mockMvc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ void getListOfContainers() {
String expectedJson =
"""
{
"org.springframework.kafka.KafkaListenerEndpointContainer#1-retry": true,
"org.springframework.kafka.KafkaListenerEndpointContainer#1-dlt": true,
"org.springframework.kafka.KafkaListenerEndpointContainer#0": true,
"org.springframework.kafka.KafkaListenerEndpointContainer#1": true
"topic_2_Listener": true,
"topic_2_Listener-retry": true,
"topic_1_Listener": true,
"topic_2_Listener-dlt": true
}
""";
this.mockMvcTester
Expand All @@ -147,17 +147,17 @@ void stopAndStartContainers() throws Exception {
String expectedJson =
"""
{
"org.springframework.kafka.KafkaListenerEndpointContainer#1-retry": true,
"org.springframework.kafka.KafkaListenerEndpointContainer#1-dlt": %s,
"org.springframework.kafka.KafkaListenerEndpointContainer#0": true,
"org.springframework.kafka.KafkaListenerEndpointContainer#1": true
"topic_2_Listener-dlt": %s,
"topic_2_Listener": true,
"topic_2_Listener-retry": true,
"topic_1_Listener": true
}
""";
this.mockMvcTester
.post()
.uri("/listeners")
.content(this.objectMapper.writeValueAsString(new KafkaListenerRequest(
"org.springframework.kafka.KafkaListenerEndpointContainer#1-dlt", Operation.STOP)))
.content(this.objectMapper.writeValueAsString(
new KafkaListenerRequest("topic_2_Listener-dlt", Operation.STOP)))
.contentType(MediaType.APPLICATION_JSON)
.assertThat()
.hasStatusOk()
Expand All @@ -167,8 +167,8 @@ void stopAndStartContainers() throws Exception {
this.mockMvcTester
.post()
.uri("/listeners")
.content(this.objectMapper.writeValueAsString(new KafkaListenerRequest(
"org.springframework.kafka.KafkaListenerEndpointContainer#1-dlt", Operation.START)))
.content(this.objectMapper.writeValueAsString(
new KafkaListenerRequest(" topic_2_Listener-dlt", Operation.START)))
.contentType(MediaType.APPLICATION_JSON)
.assertThat()
.hasStatusOk()
Expand Down

0 comments on commit f17a48e

Please sign in to comment.