Below are the positive and negative test cases for the given project code:
Positive Test Cases (Junit5):
- Test Case: Validate successful message sending to Kafka topic
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.kafka.core.KafkaTemplate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
public class KafkaProducerServiceTest {
@Test
public void testSendJsonMessage_Success() {
// Mock KafkaTemplate
KafkaTemplate<String, Object> kafkaTemplate = Mockito.mock(KafkaTemplate.class);
KafkaProducerService kafkaProducerService = new KafkaProducerService(kafkaTemplate);
// Create a valid SpaceCollector object
SpaceCollector spaceCollector = new SpaceCollector();
spaceCollector.setId("1");
spaceCollector.setUrl("http://example.com");
spaceCollector.setPort(8080);
spaceCollector.setConnectionType("API");
spaceCollector.setUsername("admin");
spaceCollector.setPassword("password");
// Call the method under test
kafkaProducerService.sendJsonMessage(spaceCollector);
// Verify that kafkaTemplate.send() is called with the correct parameters
verify(kafkaTemplate).send("topic-name", spaceCollector);
}
}- Test Case: Validate error message sending when id is missing
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.kafka.core.KafkaTemplate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
public class KafkaProducerServiceTest {
@Test
public void testSendJsonMessage_MissingId() {
// Mock KafkaTemplate
KafkaTemplate<String, Object> kafkaTemplate = Mockito.mock(KafkaTemplate.class);
KafkaProducerService kafkaProducerService = new KafkaProducerService(kafkaTemplate);
// Create a SpaceCollector object with missing id
SpaceCollector spaceCollector = new SpaceCollector();
spaceCollector.setUrl("http://example.com");
spaceCollector.setPort(8080);
spaceCollector.setConnectionType("API");
spaceCollector.setUsername("admin");
spaceCollector.setPassword("password");
// Call the method under test
kafkaProducerService.sendJsonMessage(spaceCollector);
// Verify that kafkaTemplate.send() is called with the error message
verify(kafkaTemplate).send("topic-name", createErrorMessage("Validation Failed: Missing id"));
}
private Map<String, Object> createErrorMessage(String message) {
Map<String, Object> map = new HashMap<>();
map.put("STATUS", "FAIL");
map.put("MESSAGE", message);
return map;
}
}Negative Test Cases (Junit5):
- Test Case: Validate error message sending when url is missing
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.kafka.core.KafkaTemplate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
public class KafkaProducerServiceTest {
@Test
public void testSendJsonMessage_MissingUrl() {
// Mock KafkaTemplate
KafkaTemplate<String, Object> kafkaTemplate = Mockito.mock(KafkaTemplate.class);
KafkaProducerService kafkaProducerService = new KafkaProducerService(kafkaTemplate);
// Create a SpaceCollector object with missing url
SpaceCollector spaceCollector = new SpaceCollector();
spaceCollector.setId("1");
spaceCollector.setPort(8080);
spaceCollector.setConnectionType("API");
spaceCollector.setUsername("admin");
spaceCollector.setPassword("password");
// Call the method under test
kafkaProducerService.sendJsonMessage(spaceCollector);
// Verify that kafkaTemplate.send() is called with the error message
verify(kafkaTemplate).send("topic-name", createErrorMessage("Validation Failed: Missing url"));
}
private Map<String, Object> createErrorMessage(String message) {
Map<String, Object> map = new HashMap<>();
map.put("STATUS", "FAIL");
map.put("MESSAGE", message);
return map;
}
}- Test Case: Validate error message sending when port is zero
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.kafka.core.KafkaTemplate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
public class KafkaProducerServiceTest {
@Test
public void testSendJsonMessage_ZeroPort() {
// Mock KafkaTemplate
KafkaTemplate<String, Object> kafkaTemplate = Mockito.mock(KafkaTemplate.class);
KafkaProducerService kafkaProducerService = new KafkaProducerService(kafkaTemplate);
// Create a SpaceCollector object with zero port
SpaceCollector spaceCollector = new SpaceCollector();
spaceCollector.setId("1");
spaceCollector.setUrl("http://example.com");
spaceCollector.setPort(0);
spaceCollector.setConnectionType("API");
spaceCollector.setUsername("admin");
spaceCollector.setPassword("password");
// Call the method under test
kafkaProducerService.sendJsonMessage(spaceCollector);
// Verify that kafkaTemplate.send() is called with the error message
verify(kafkaTemplate).send("topic-name", createErrorMessage("Validation Failed: Missing port"));
}
private Map<String, Object> createErrorMessage(String message) {
Map<String, Object> map = new HashMap<>();
map.put("STATUS", "FAIL");
map.put("MESSAGE", message);
return map;
}
}Modifications to make the entire project executable:
Application.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}The above modifications enable the Spring Boot application to be executed using the main() method in the Application class.