Skip to content

Commit

Permalink
adds IntegrationTests (missing some assertions)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Dec 27, 2024
1 parent fc3d7e8 commit 174d812
Show file tree
Hide file tree
Showing 15 changed files with 2,779 additions and 15 deletions.
27 changes: 24 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,17 @@
<artifactId>spring-modulith-api</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events-core</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>

<!-- optional -->
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
Expand All @@ -142,17 +141,39 @@
</dependency>

<!-- Testing -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-jdbc</artifactId>
<version>${parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import org.springframework.context.annotation.Import;

@Configuration
@Import({AvroEventSerializerConfiguration.class, MessageEventSerializerConfiguration.class, SpringCloudStreamEventExternalizerConfiguration.class})
@Import({SpringCloudStreamEventExternalizerConfiguration.class, MessageEventSerializerConfiguration.class, AvroEventSerializerConfiguration.class})
public @interface EnableSpringCloudStreamEventExternalization {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.zenwave360.modulith.events.scs;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest(classes = { TestsConfiguration.class })
@Import({ AvroEventSerializerConfiguration.class })
@Transactional
public class SCSAvroEventExternalizerTest {

@Autowired
TestsConfiguration.CustomerEventsProducer customerEventsProducer;

@Autowired
JdbcTemplate jdbcTemplate;

@Test
void testExternalizeAvroEvent() {
var event = new io.zenwave360.modulith.events.scs.dtos.avro.CustomerEvent();
event.setName("John Doe");
customerEventsProducer.onCustomerEventAvro(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.zenwave360.modulith.events.scs;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest(classes = { TestsConfiguration.class })
@Import({ MessageEventSerializerConfiguration.class })
@Transactional
public class SCSJsonEventExternalizerTest {

@Autowired
TestsConfiguration.CustomerEventsProducer customerEventsProducer;

@Autowired
JdbcTemplate jdbcTemplate;

@Test
void testExternalizeJsonEvent() {
var event = new io.zenwave360.modulith.events.scs.dtos.json.CustomerEvent()
.withName("John Doe");
customerEventsProducer.onCustomerEventJson(event);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.zenwave360.modulith.events.scs;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.EmbeddedKafkaZKBroker;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableAutoConfiguration
@Import({ SpringCloudStreamEventExternalizerConfiguration.class })
@EmbeddedKafka(partitions = 1, topics = { "customers-json-topic" })
@EnableTransactionManagement
public class TestsConfiguration {

@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper(); // Customize if needed
}

@Bean
EmbeddedKafkaBroker embeddedKafkaBroker() {
return new EmbeddedKafkaZKBroker(1, true, 1, "customers-json-topic");
}

@Bean
CustomerEventsProducer customerEventsProducer(ApplicationEventPublisher applicationEventPublisher) {
return new CustomerEventsProducer(applicationEventPublisher);
}

static class CustomerEventsProducer {

private final ApplicationEventPublisher applicationEventPublisher;

public CustomerEventsProducer(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}

public void onCustomerEventJson(io.zenwave360.modulith.events.scs.dtos.json.CustomerEvent event) {
Message<io.zenwave360.modulith.events.scs.dtos.json.CustomerEvent> message = MessageBuilder.withPayload(event)
.setHeader(
SpringCloudStreamEventExternalizer.SPRING_CLOUD_STREAM_SENDTO_DESTINATION_HEADER,
"customers-json-out-0") // <- target binding name
.build();
applicationEventPublisher.publishEvent(message);
}

public void onCustomerEventAvro(io.zenwave360.modulith.events.scs.dtos.avro.CustomerEvent event) {
Message<io.zenwave360.modulith.events.scs.dtos.avro.CustomerEvent> message = MessageBuilder.withPayload(event)
.setHeader(
SpringCloudStreamEventExternalizer.SPRING_CLOUD_STREAM_SENDTO_DESTINATION_HEADER,
"customers-avro-out-0") // <- target binding name
.build();
applicationEventPublisher.publishEvent(message);
}
}
}
Loading

0 comments on commit 174d812

Please sign in to comment.