diff --git a/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/SpringBootKafkaAvroApplication.java b/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/SpringBootKafkaAvroConsumerApplication.java similarity index 63% rename from kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/SpringBootKafkaAvroApplication.java rename to kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/SpringBootKafkaAvroConsumerApplication.java index 8111ec73..3ef4a304 100644 --- a/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/SpringBootKafkaAvroApplication.java +++ b/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/SpringBootKafkaAvroConsumerApplication.java @@ -4,9 +4,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class SpringBootKafkaAvroApplication { +public class SpringBootKafkaAvroConsumerApplication { public static void main(String[] args) { - SpringApplication.run(SpringBootKafkaAvroApplication.class, args); + SpringApplication.run(SpringBootKafkaAvroConsumerApplication.class, args); } } diff --git a/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/controller/KafkaController.java b/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/controller/KafkaController.java deleted file mode 100644 index de6e059e..00000000 --- a/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/controller/KafkaController.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.springbootkafkaavro.controller; - -import com.example.springbootkafkaavro.model.Person; -import com.example.springbootkafkaavro.service.KafkaProducer; - -import lombok.RequiredArgsConstructor; - -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping(value = "/person") -@RequiredArgsConstructor -public class KafkaController { - - private final KafkaProducer producer; - - @PostMapping(value = "/publish") - public void sendMessageToKafkaTopic( - @RequestParam("name") String name, @RequestParam("age") Integer age) { - Person person = new Person(); - person.setAge(age); - person.setName(name); - this.producer.sendMessage(person); - } -} diff --git a/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/service/KafkaProducer.java b/kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/KafkaProducer.java similarity index 81% rename from kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/service/KafkaProducer.java rename to kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/KafkaProducer.java index ed79ab3d..9f27e937 100644 --- a/kafka-avro/spring-boot-kafka-avro-consumer/src/main/java/com/example/springbootkafkaavro/service/KafkaProducer.java +++ b/kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/KafkaProducer.java @@ -1,14 +1,14 @@ -package com.example.springbootkafkaavro.service; +package com.example.springbootkafkaavro; import com.example.springbootkafkaavro.model.Person; import com.example.springbootkafkaavro.util.ApplicationConstants; import lombok.RequiredArgsConstructor; +import org.springframework.boot.test.context.TestComponent; import org.springframework.kafka.core.KafkaTemplate; -import org.springframework.stereotype.Component; -@Component +@TestComponent @RequiredArgsConstructor public class KafkaProducer { diff --git a/kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/SpringBootKafkaAvroApplicationTests.java b/kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/SpringBootKafkaAvroConsumerApplicationTests.java similarity index 91% rename from kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/SpringBootKafkaAvroApplicationTests.java rename to kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/SpringBootKafkaAvroConsumerApplicationTests.java index 57d6dea0..ffafa83a 100644 --- a/kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/SpringBootKafkaAvroApplicationTests.java +++ b/kafka-avro/spring-boot-kafka-avro-consumer/src/test/java/com/example/springbootkafkaavro/SpringBootKafkaAvroConsumerApplicationTests.java @@ -2,17 +2,17 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static java.util.concurrent.TimeUnit.SECONDS; +import com.example.springbootkafkaavro.model.Person; import com.example.springbootkafkaavro.repository.PersonRepository; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; @@ -26,10 +26,12 @@ @SpringBootTest @AutoConfigureMockMvc -class SpringBootKafkaAvroApplicationTests { +@Import(KafkaProducer.class) +class SpringBootKafkaAvroConsumerApplicationTests { @Autowired MockMvc mockMvc; @Autowired PersonRepository personRepository; + @Autowired KafkaProducer kafkaProducer; private static final Network KAFKA_NETWORK = Network.newNetwork(); private static final String CONFLUENT_PLATFORM_VERSION = "7.4.0"; @@ -99,9 +101,10 @@ public String getSchemaUrl() { @Test void contextLoads() throws Exception { - this.mockMvc - .perform(post("/person/publish").param("name", "junit").param("age", "33")) - .andExpect(status().isOk()); + Person person = new Person(); + person.setAge(33); + person.setName("junit"); + this.kafkaProducer.sendMessage(person); await().atMost(10, SECONDS) .untilAsserted(() -> assertThat(personRepository.count()).isEqualTo(1)); }