Skip to content

Commit

Permalink
feat(kafka): link module to primary for init part
Browse files Browse the repository at this point in the history
  • Loading branch information
fdelbrayelle committed Jul 11, 2022
1 parent 619dcec commit 4cb46e8
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 578 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package tech.jhipster.lite.generator.server.springboot.broker.kafka.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.docker.domain.DockerImages;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.KafkaModuleFactory;
import tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.KafkaService;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@Service
public class KafkaApplicationService {

private final KafkaService kafkaService;

public KafkaApplicationService(final KafkaService kafkaService) {
private final KafkaModuleFactory kafkaModuleFactory;

public KafkaApplicationService(final KafkaService kafkaService, final DockerImages dockerImages) {
this.kafkaService = kafkaService;
this.kafkaModuleFactory = new KafkaModuleFactory(dockerImages);
}

public void init(final Project project) {
kafkaService.init(project);
public JHipsterModule init(final JHipsterModuleProperties properties) {
return kafkaModuleFactory.buildModule(properties);
}

public void addDummyProducerConsumer(final Project project) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package tech.jhipster.lite.generator.server.springboot.broker.kafka.domain;

import static tech.jhipster.lite.common.domain.FileUtils.*;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.*;
import static tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.Akhq.*;
import static tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.Kafka.*;
import static tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommon.*;

import java.util.TreeMap;
import tech.jhipster.lite.common.domain.WordUtils;
import tech.jhipster.lite.docker.domain.DockerImages;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.buildtool.generic.domain.Dependency;
import tech.jhipster.lite.generator.project.domain.DefaultConfig;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectFile;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.readme.domain.ReadMeService;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;

import java.util.TreeMap;

import static tech.jhipster.lite.common.domain.FileUtils.getPath;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.BASE_NAME;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.PACKAGE_NAME;
import static tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.Akhq.AKHQ_DOCKER_COMPOSE_FILE;
import static tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.Akhq.AKHQ_DOCKER_IMAGE;

public class KafkaDomainService implements KafkaService {

private static final String SOURCE = "server/springboot/broker/kafka";
Expand All @@ -34,55 +32,16 @@ public class KafkaDomainService implements KafkaService {

private final DockerImages dockerImages;

private final ReadMeService readMeService;

public KafkaDomainService(
final BuildToolService buildToolService,
final ProjectRepository projectRepository,
final SpringBootCommonService springBootCommonService,
final DockerImages dockerImages,
final ReadMeService readMeService
final DockerImages dockerImages
) {
this.buildToolService = buildToolService;
this.projectRepository = projectRepository;
this.springBootCommonService = springBootCommonService;
this.dockerImages = dockerImages;
this.readMeService = readMeService;
}

@Override
public void init(final Project project) {
addApacheKafkaClient(project);
addDockerCompose(project);
addProperties(project);
addTestcontainers(project);
addConfiguration(project);
addReadMeSection(project);
}

private void addConfiguration(final Project project) {
final String packageNamePath = project.getPackageNamePath().orElse(getPath(DefaultConfig.PACKAGE_PATH));
final String configKafkaPath = TECHNICAL_INFRASTRUCTURE_CONFIG + "/kafka";

projectRepository.template(
ProjectFile
.forProject(project)
.withSource(SOURCE, "KafkaProperties.java")
.withDestinationFolder(getPath(MAIN_JAVA, packageNamePath, configKafkaPath))
);
projectRepository.template(
ProjectFile
.forProject(project)
.withSource(SOURCE, "KafkaPropertiesTest.java")
.withDestinationFolder(getPath(TEST_JAVA, packageNamePath, configKafkaPath))
);

projectRepository.template(
ProjectFile
.forProject(project)
.withSource(SOURCE, "KafkaConfiguration.java")
.withDestinationFolder(getPath(MAIN_JAVA, packageNamePath, TECHNICAL_INFRASTRUCTURE_CONFIG + "/kafka"))
);
}

@Override
Expand Down Expand Up @@ -152,32 +111,6 @@ public void addAkhq(final Project project) {
);
}

private void addApacheKafkaClient(final Project project) {
final Dependency dependency = Dependency.builder().groupId("org.apache.kafka").artifactId("kafka-clients").build();
buildToolService.addDependency(project, dependency);
}

private void addDockerCompose(final Project project) {
final String zookeeperDockerImage = dockerImages.get(Zookeeper.ZOOKEEPER_DOCKER_IMAGE).fullName();
final String kafkaDockerImage = dockerImages.get(KAFKA_DOCKER_IMAGE).fullName();

project.addDefaultConfig(BASE_NAME);
project.addConfig("zookeeperDockerImage", zookeeperDockerImage);
project.addConfig("kafkaDockerImage", kafkaDockerImage);
projectRepository.template(
ProjectFile.forProject(project).withSource(SOURCE, KAFKA_DOCKER_COMPOSE_FILE).withDestination(MAIN_DOCKER, KAFKA_DOCKER_COMPOSE_FILE)
);
}

private void addProperties(final Project project) {
final String kebabBaseName = WordUtils.kebabCase(project.getBaseName().orElse(DEFAULT_BASE_NAME));
getKafkaCommonProperties(kebabBaseName)
.forEach((k, v) -> {
springBootCommonService.addProperties(project, k, v);
springBootCommonService.addPropertiesTest(project, k, v);
});
}

private TreeMap<String, Object> getKafkaCommonProperties(final String kebabBaseName) {
final TreeMap<String, Object> result = new TreeMap<>();

Expand All @@ -193,26 +126,4 @@ private TreeMap<String, Object> getKafkaCommonProperties(final String kebabBaseN

return result;
}

void addTestcontainers(final Project project) {
final String packageNamePath = project.getPackageNamePath().orElse(getPath(DefaultConfig.PACKAGE_PATH));
projectRepository.template(
ProjectFile
.forProject(project)
.withSource(SOURCE, "KafkaTestContainerExtension.java")
.withDestinationFolder(getPath(TEST_JAVA, packageNamePath))
);
buildToolService.addVersionPropertyAndDependency(project, "testcontainers", testContainersDependency("kafka"));
springBootCommonService.updateIntegrationTestAnnotation(project, "KafkaTestContainerExtension");
}

private void addReadMeSection(final Project project) {
readMeService.addSection(
project,
"## Apache Kafka",
"""
Before to launch `./mvnw spring-boot:run`, please launch the following command in the root directory: `docker-compose -f src/main/docker/kafka.yml up`.
"""
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import tech.jhipster.lite.generator.project.domain.Project;

public interface KafkaService {
void init(Project project);

void addDummyProducerConsumer(Project project);

void addAkhq(Project project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import tech.jhipster.lite.docker.domain.DockerImages;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.readme.domain.ReadMeService;
import tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.KafkaDomainService;
import tech.jhipster.lite.generator.server.springboot.broker.kafka.domain.KafkaService;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;
Expand All @@ -21,24 +20,20 @@ public class KafkaBeanConfiguration {

private final DockerImages dockerImages;

private final ReadMeService readMeService;

public KafkaBeanConfiguration(
final BuildToolService buildToolService,
final ProjectRepository projectRepository,
final SpringBootCommonService springBootCommonService,
final DockerImages dockerImages,
final ReadMeService readMeService
final DockerImages dockerImages
) {
this.buildToolService = buildToolService;
this.projectRepository = projectRepository;
this.springBootCommonService = springBootCommonService;
this.dockerImages = dockerImages;
this.readMeService = readMeService;
}

@Bean
public KafkaService kafkaService() {
return new KafkaDomainService(buildToolService, projectRepository, springBootCommonService, dockerImages, readMeService);
return new KafkaDomainService(buildToolService, projectRepository, springBootCommonService, dockerImages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ public KafkaResource(KafkaApplicationService kafkaApplicationService) {
this.kafkaApplicationService = kafkaApplicationService;
}

@Operation(summary = "Add Kafka dependencies, with testcontainers")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Kafka")
@PostMapping
@GeneratorStep(id = GeneratorAction.SPRINGBOOT_KAFKA)
public void init(final @RequestBody ProjectDTO projectDTO) {
final Project project = ProjectDTO.toProject(projectDTO);
kafkaApplicationService.init(project);
}

@Operation(summary = "Add dummy Kafka producer and consumer")
@ApiResponse(responseCode = "500", description = "An error occurred while adding dummy Kafka producer and consumer")
@PostMapping("/dummy-producer-consumer")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tech.jhipster.lite.generator.server.springboot.broker.kafka.infrastructure.primary.rest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.server.springboot.broker.kafka.application.KafkaApplicationService;
import tech.jhipster.lite.module.infrastructure.primary.JHipsterModuleApiDoc;
import tech.jhipster.lite.module.infrastructure.primary.JHipsterModuleResource;

@Configuration
public class KafkaResourceConfiguration {

private static final String TAG = "Spring Boot - Broker";

@Bean
JHipsterModuleResource kafkaResourceInit(KafkaApplicationService kafkaApplicationService) {
return JHipsterModuleResource
.builder()
.legacyUrl("/api/servers/spring-boot/brokers/kafka")
.slug("springboot-kafka")
.withoutProperties()
.apiDoc(new JHipsterModuleApiDoc(TAG, "Add Kafka dependencies, with testcontainers"))
.factory(kafkaApplicationService::init);
}
}
Loading

0 comments on commit 4cb46e8

Please sign in to comment.