Skip to content

Commit 40453c9

Browse files
committed
User-sv kafka tests
1 parent 147c9e9 commit 40453c9

File tree

12 files changed

+512
-7
lines changed

12 files changed

+512
-7
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.mbridge.dac.comp</groupId>
7+
<artifactId>dac-components</artifactId>
8+
<version>3.5.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.mbridge.dac.comp</groupId>
11+
<artifactId>dac-kafka-commons</artifactId>
12+
<version>3.5.0-SNAPSHOT</version>
13+
<licenses>
14+
<license>
15+
<name>Apache License, Version 2.0</name>
16+
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
17+
</license>
18+
</licenses>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.kafka</groupId>
22+
<artifactId>spring-kafka</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.mbridge.dac.comp</groupId>
26+
<artifactId>dac-yaml-property-source</artifactId>
27+
</dependency>
28+
</dependencies>
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>io.repaint.maven</groupId>
33+
<artifactId>tiles-maven-plugin</artifactId>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
</project>

core/kafka-messaging-test/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>kafka-messaging-test</artifactId>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>eda.videoclub</groupId>
12+
<artifactId>core</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-test</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.kafka</groupId>
23+
<artifactId>spring-kafka-test</artifactId>
24+
<scope>compile</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.testcontainers</groupId>
28+
<artifactId>kafka</artifactId>
29+
<scope>compile</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<scope>compile</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.testcontainers</groupId>
38+
<artifactId>junit-jupiter</artifactId>
39+
<scope>compile</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-web</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
48+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package eda.videoclub.messaging.test;
2+
3+
import org.springframework.boot.test.util.TestPropertyValues;
4+
import org.springframework.context.ApplicationContextInitializer;
5+
import org.springframework.context.ConfigurableApplicationContext;
6+
import org.springframework.test.context.ActiveProfiles;
7+
import org.testcontainers.containers.KafkaContainer;
8+
import org.testcontainers.junit.jupiter.Testcontainers;
9+
import org.testcontainers.utility.DockerImageName;
10+
11+
import lombok.experimental.UtilityClass;
12+
import lombok.extern.slf4j.Slf4j;
13+
14+
@ActiveProfiles("test")
15+
@Testcontainers
16+
@Slf4j
17+
@UtilityClass
18+
public class KafkaServerTestProvider {
19+
20+
public static final KafkaContainer KAFKA_CONTAINER =
21+
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.1.1"));
22+
23+
public static class KafkaServerInitializer
24+
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
25+
26+
@Override
27+
public void initialize(final ConfigurableApplicationContext applicationContext) {
28+
KAFKA_CONTAINER.start();
29+
30+
TestPropertyValues.of(
31+
"spring.kafka.bootstrap-servers=" + KAFKA_CONTAINER.getBootstrapServers())
32+
.applyTo(applicationContext.getEnvironment());
33+
34+
log.info("Kafka server for test: {}", KAFKA_CONTAINER.getBootstrapServers());
35+
}
36+
}
37+
}

core/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939

4040
<!-- libs -->
4141
<swagger.version>3.0.0</swagger.version>
42+
<junit.version>5.8.2</junit.version>
43+
<junit4.version>4.13.2</junit4.version>
44+
<testcontainers.version>1.16.2</testcontainers.version>
4245

4346
</properties>
4447

@@ -63,12 +66,35 @@
6366
<artifactId>springfox-swagger2</artifactId>
6467
<version>${swagger.version}</version>
6568
</dependency>
69+
<dependency>
70+
<groupId>org.testcontainers</groupId>
71+
<artifactId>kafka</artifactId>
72+
<version>${testcontainers.version}</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>junit</groupId>
77+
<artifactId>junit</artifactId>
78+
<version>${junit4.version}</version>
79+
<scope>test</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.testcontainers</groupId>
83+
<artifactId>junit-jupiter</artifactId>
84+
<version>${testcontainers.version}</version>
85+
<scope>test</scope>
86+
</dependency>
6687
<!-- CORE - components -->
6788
<dependency>
6889
<groupId>eda.videoclub</groupId>
6990
<artifactId>kafka-messaging</artifactId>
7091
<version>0.0.1-SNAPSHOT</version>
7192
</dependency>
93+
<dependency>
94+
<groupId>eda.videoclub</groupId>
95+
<artifactId>kafka-messaging-test</artifactId>
96+
<version>0.0.1-SNAPSHOT</version>
97+
</dependency>
7298

7399
</dependencies>
74100
</dependencyManagement>
@@ -94,6 +120,7 @@
94120
<artifactId>spring-boot-starter-test</artifactId>
95121
<scope>test</scope>
96122
</dependency>
123+
97124
</dependencies>
98125

99126
<build>
@@ -130,6 +157,7 @@
130157

131158
<modules>
132159
<module>kafka-messaging</module>
160+
<module>kafka-messaging-test</module>
133161
</modules>
134162

135163
</project>

user-sv/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
@@ -38,6 +38,12 @@
3838
<groupId>eda.videoclub</groupId>
3939
<artifactId>kafka-messaging</artifactId>
4040
</dependency>
41+
<dependency>
42+
<groupId>eda.videoclub</groupId>
43+
<artifactId>kafka-messaging-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
4147
</dependencies>
4248

4349
<build>

user-sv/src/main/java/eda/videoclub/service/user/adapter/producer/KafkaProducer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.beans.factory.annotation.Value;
77
import org.springframework.kafka.core.KafkaTemplate;
8+
import org.springframework.kafka.support.SendResult;
89
import org.springframework.stereotype.Component;
910

1011
import eda.videoclub.messaging.message.UserInvalidatedEvent;
@@ -30,21 +31,24 @@ public class KafkaProducer implements EventProducer {
3031
@Autowired private KafkaTemplate<String, Object> kafkaTemplate;
3132

3233
@Override
33-
public void sendMessage(final UserValidationCommand command) {
34+
public SendResult<String, ?> sendMessage(final UserValidationCommand command) {
3435

3536
final UserValidatedEvent event = converter.convert(command);
3637

3738
log.info("Publishing event: " + event);
38-
kafkaTemplate.send(topicName, UUID.randomUUID().toString(), event);
39+
return kafkaTemplate.send(topicName, UUID.randomUUID().toString(), event).completable().join();
3940
}
4041

4142
@Override
42-
public void sendErrorMessage(
43+
public SendResult<String, ?> sendErrorMessage(
4344
final UserValidationCommand command, final RejectionReasonVO reason) {
4445

4546
final UserInvalidatedEvent event = errorConverter.convert(command, reason);
4647

4748
log.info("Publishing event: " + event);
48-
kafkaTemplate.send(errorTopicName, UUID.randomUUID().toString(), event);
49+
return kafkaTemplate
50+
.send(errorTopicName, UUID.randomUUID().toString(), event)
51+
.completable()
52+
.join();
4953
}
5054
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package eda.videoclub.service.user.port.producer;
22

3+
import org.springframework.kafka.support.SendResult;
4+
35
import eda.videoclub.service.user.command.UserValidationCommand;
46
import eda.videoclub.service.user.domain.entity.RejectionReasonVO;
57

68
public interface EventProducer {
79

8-
void sendMessage(UserValidationCommand command);
10+
SendResult<String, ?> sendMessage(UserValidationCommand command);
911

10-
void sendErrorMessage(UserValidationCommand command, RejectionReasonVO reason);
12+
SendResult<String, ?> sendErrorMessage(UserValidationCommand command, RejectionReasonVO reason);
1113
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package eda.videoclub.service.user.consumer;
2+
3+
import java.util.UUID;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.mockito.ArgumentCaptor;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.Mockito;
11+
import org.mockito.junit.jupiter.MockitoExtension;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.beans.factory.annotation.Value;
14+
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.kafka.core.KafkaTemplate;
16+
import org.springframework.kafka.test.context.EmbeddedKafka;
17+
import org.springframework.test.annotation.DirtiesContext;
18+
import org.springframework.test.context.ActiveProfiles;
19+
import org.springframework.test.context.ContextConfiguration;
20+
21+
import eda.videoclub.messaging.config.KafkaConfig;
22+
import eda.videoclub.messaging.message.BookingCreatedEvent;
23+
import eda.videoclub.messaging.message.BookingStatusEnum;
24+
import eda.videoclub.messaging.wrapper.base.Schema;
25+
import eda.videoclub.service.user.adapter.consumer.BookingCreatedEventToUserValidationConverter;
26+
import eda.videoclub.service.user.adapter.consumer.KafkaConsumer;
27+
import eda.videoclub.service.user.adapter.consumer.wrapper.BookingCreatedEventWrapperMessage;
28+
import eda.videoclub.service.user.command.UserValidationCommand;
29+
import eda.videoclub.service.user.command.handler.UserValidationCommandHandler;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
33+
@ActiveProfiles("test")
34+
@SpringBootTest
35+
@DirtiesContext
36+
@EmbeddedKafka
37+
@ContextConfiguration(classes = KafkaConfig.class) // TODO Isolate KafkaConfig
38+
@ExtendWith(MockitoExtension.class)
39+
public class KafkaConsumerEmbeddedTest {
40+
41+
@Value("${user-sv.topic.consume.bookingCreated}")
42+
private String topic;
43+
44+
@InjectMocks private KafkaConsumer kafkaConsumer;
45+
46+
@Autowired private KafkaTemplate<String, Object> kafkaTemplate;
47+
48+
@Mock private BookingCreatedEventToUserValidationConverter converter;
49+
50+
@Mock private UserValidationCommandHandler userValidationCommandHandler;
51+
52+
@Test
53+
public void listenTest() {
54+
// given
55+
final Schema schema = Schema.builder().build();
56+
final BookingCreatedEvent bookingCreatedEvent =
57+
BookingCreatedEvent.builder()
58+
.id(UUID.randomUUID().toString())
59+
.imdbId("12345")
60+
.userId(UUID.randomUUID().toString())
61+
.status(BookingStatusEnum.CREATED)
62+
.build();
63+
64+
final BookingCreatedEventWrapperMessage bookingCreatedWrapper =
65+
BookingCreatedEventWrapperMessage.builder()
66+
.schema(schema)
67+
.payload(bookingCreatedEvent)
68+
.build();
69+
70+
kafkaTemplate.send(topic, bookingCreatedWrapper);
71+
72+
// when
73+
final ArgumentCaptor<BookingCreatedEvent> captor =
74+
ArgumentCaptor.forClass(BookingCreatedEvent.class);
75+
76+
Mockito.when(converter.convert(captor.capture()))
77+
.thenReturn(UserValidationCommand.builder().build());
78+
Mockito.doNothing().when(userValidationCommandHandler).handle(Mockito.any());
79+
80+
kafkaConsumer.listen(bookingCreatedWrapper);
81+
82+
// then
83+
final BookingCreatedEvent eventPayloadCaptured = captor.getValue();
84+
assertEquals(bookingCreatedEvent.getId(), eventPayloadCaptured.getId());
85+
assertEquals(bookingCreatedEvent.getImdbId(), eventPayloadCaptured.getImdbId());
86+
assertEquals(bookingCreatedEvent.getUserId(), eventPayloadCaptured.getUserId());
87+
assertEquals(bookingCreatedEvent.getStatus(), eventPayloadCaptured.getStatus());
88+
}
89+
}

0 commit comments

Comments
 (0)