Skip to content

Commit

Permalink
Merge pull request #1857 from Bolo89/move-common-springboot-actuator
Browse files Browse the repository at this point in the history
Move common springboot actuator
  • Loading branch information
pascalgrimaud authored May 27, 2022
2 parents 982be13 + 16e7b79 commit a891583
Show file tree
Hide file tree
Showing 23 changed files with 357 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public void addSpringBootUndertow(Project project) {
springBootMvcService.addSpringBootUndertow(project);
}

public void addSpringBootActuator(Project project) {
springBootMvcService.addSpringBootActuator(project);
}

public void addExceptionHandler(Project project) {
springBootMvcService.addExceptionHandler(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public static Dependency springBootStarterWebDependency() {
return Dependency.builder().groupId(SPRINGBOOT_PACKAGE).artifactId("spring-boot-starter-web").build();
}

public static Dependency springBootActuatorDependency() {
return Dependency.builder().groupId(SPRINGBOOT_PACKAGE).artifactId("spring-boot-starter-actuator").build();
}

public static Dependency tomcatDependency() {
return Dependency.builder().groupId(SPRINGBOOT_PACKAGE).artifactId("spring-boot-starter-tomcat").build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.corsFiles;
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.corsProperties;
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.problemSpringDependency;
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.springBootActuatorDependency;
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.springBootStarterValidation;
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.springBootStarterWebDependency;
import static tech.jhipster.lite.generator.server.springboot.mvc.web.domain.SpringBootMvc.tomcatDependency;
Expand Down Expand Up @@ -75,23 +74,6 @@ public void addSpringBootUndertow(Project project) {
addCorsProperties(project);
}

@Override
public void addSpringBootActuator(Project project) {
buildToolService.addDependency(project, springBootActuatorDependency());

springBootCommonService.addPropertiesComment(project, "Spring Boot Actuator");
springBootCommonService.addProperties(project, "management.endpoints.web.base-path", "/management");
springBootCommonService.addProperties(
project,
"management.endpoints.web.exposure.include",
"configprops, env, health, info, logfile, loggers, threaddump"
);
springBootCommonService.addProperties(project, "management.endpoint.health.probes.enabled", "true");
springBootCommonService.addProperties(project, "management.endpoint.health.group.liveness.include", "livenessState");
springBootCommonService.addProperties(project, "management.endpoint.health.group.readiness.include", "readinessState");
springBootCommonService.addPropertiesNewLine(project);
}

@Override
public void addExceptionHandler(Project project) {
project.addDefaultConfig(PACKAGE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ public interface SpringBootMvcService {

void addSpringBootMvc(Project project);
void addSpringBootUndertow(Project project);
void addSpringBootActuator(Project project);
void addExceptionHandler(Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,4 @@ public void addSpringBootUndertow(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
springBootMvcApplicationService.addSpringBootUndertow(project);
}

@Operation(summary = "Add Spring Boot Actuator")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Spring Boot Actuator")
@PostMapping("/technical-tools/actuator")
@GeneratorStep(id = GeneratorAction.SPRINGBOOT_ACTUATOR)
public void addSpringBootActuator(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
springBootMvcApplicationService.addSpringBootActuator(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Technical tools - Actuator

## Description

This context is used to add actuator, in existing project.

## Maintainers

- [Bolo](https://github.com/bolo89)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.domain.SpringBootActuatorService;

@Service
public class SpringBootActuatorApplicationService {

private final SpringBootActuatorService springBootActuatorService;

public SpringBootActuatorApplicationService(SpringBootActuatorService springBootActuatorService) {
this.springBootActuatorService = springBootActuatorService;
}

public void addActuator(Project project) {
springBootActuatorService.addActuator(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.domain;

import tech.jhipster.lite.generator.buildtool.generic.domain.Dependency;

public class SpringBootActuator {

private SpringBootActuator() {}

public static Dependency springBootActuatorDependency() {
return Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-actuator").build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.domain;

import static tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.domain.SpringBootActuator.springBootActuatorDependency;

import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;

public class SpringBootActuatorDomainService implements SpringBootActuatorService {

private final BuildToolService buildToolService;
private final SpringBootCommonService springBootCommonService;

public SpringBootActuatorDomainService(BuildToolService buildToolService, SpringBootCommonService springBootCommonService) {
this.buildToolService = buildToolService;
this.springBootCommonService = springBootCommonService;
}

@Override
public void addActuator(Project project) {
buildToolService.addDependency(project, springBootActuatorDependency());

springBootCommonService.addPropertiesComment(project, "Spring Boot Actuator");
springBootCommonService.addProperties(project, "management.endpoints.web.base-path", "/management");
springBootCommonService.addProperties(
project,
"management.endpoints.web.exposure.include",
"configprops, env, health, info, logfile, loggers, threaddump"
);
springBootCommonService.addProperties(project, "management.endpoint.health.probes.enabled", "true");
springBootCommonService.addPropertiesNewLine(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.domain;

import tech.jhipster.lite.generator.project.domain.Project;

public interface SpringBootActuatorService {
void addActuator(Project project);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.infrastructure.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;
import tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.domain.SpringBootActuatorDomainService;
import tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.domain.SpringBootActuatorService;

@Configuration
public class SpringBootActuatorBeanConfiguration {

private final BuildToolService buildToolService;
private final SpringBootCommonService springBootCommonService;

public SpringBootActuatorBeanConfiguration(BuildToolService buildToolService, SpringBootCommonService springBootCommonService) {
this.buildToolService = buildToolService;
this.springBootCommonService = springBootCommonService;
}

@Bean
public SpringBootActuatorService springBootActuatorService() {
return new SpringBootActuatorDomainService(buildToolService, springBootCommonService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.infrastructure.primary.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.jhipster.lite.generator.project.domain.GeneratorAction;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.application.SpringBootActuatorApplicationService;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/servers/spring-boot/technical-tools")
@Tag(name = "Spring Boot - Technical tools")
class SpringBootActuatorResource {

private final SpringBootActuatorApplicationService springBootActuatorApplicationService;

public SpringBootActuatorResource(SpringBootActuatorApplicationService springBootActuatorApplicationService) {
this.springBootActuatorApplicationService = springBootActuatorApplicationService;
}

@Operation(summary = "Add Spring Boot Actuator")
@ApiResponse(responseCode = "500", description = "An error occurred while adding Spring Boot Actuator")
@PostMapping("/actuator")
@GeneratorStep(id = GeneratorAction.SPRINGBOOT_ACTUATOR)
public void addActuator(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
springBootActuatorApplicationService.addActuator(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator;
Original file line number Diff line number Diff line change
Expand Up @@ -168,36 +168,6 @@ void shouldAddSpringBootUndertowWithServerPort() {
assertCors(project);
}

@Test
void shouldAddSpringBootActuator() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);
springBootApplicationService.init(project);

springBootMvcApplicationService.addSpringBootActuator(project);

assertFileContent(project, POM_XML, springBootStarterActuatorDependency());

assertFileContent(project, getPath(MAIN_RESOURCES, "config", APPLICATION_PROPERTIES), "management.endpoints.web.base-path=/management");
assertFileContent(
project,
getPath(MAIN_RESOURCES, "config", APPLICATION_PROPERTIES),
"management.endpoints.web.exposure.include=configprops, env, health, info, logfile, loggers, threaddump"
);
assertFileContent(project, getPath(MAIN_RESOURCES, "config", APPLICATION_PROPERTIES), "management.endpoint.health.probes.enabled=true");
assertFileContent(
project,
getPath(MAIN_RESOURCES, "config", APPLICATION_PROPERTIES),
"management.endpoint.health.group.liveness.include=livenessState"
);
assertFileContent(
project,
getPath(MAIN_RESOURCES, "config", APPLICATION_PROPERTIES),
"management.endpoint.health.group.readiness.include=readinessState"
);
}

@Test
void shouldAddExceptionHandler() {
Project project = tmpProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,6 @@ void shouldAddSpringBootUndertow() {
verify(projectRepository).template(ProjectFilesAsserter.filesCountArgument(2));
}

@Test
void shouldAddSpringBootActuator() {
Project project = tmpProjectWithPomXml();

springBootMvcDomainService.addSpringBootActuator(project);

verify(buildToolService).addDependency(any(Project.class), any(Dependency.class));

verify(springBootCommonService).addPropertiesComment(any(Project.class), anyString());
verify(springBootCommonService, times(5)).addProperties(any(Project.class), anyString(), any());
verify(springBootCommonService).addPropertiesNewLine(any(Project.class));
}

@Test
void shouldNotAddExceptionHandler() {
Project project = tmpProjectWithPomXml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ void shouldUndertowDependency() {
assertThat(dependency.getScope()).isEmpty();
}

@Test
void shouldActuatorDependency() {
Dependency dependency = SpringBootMvc.springBootActuatorDependency();

assertThat(dependency.getGroupId()).isEqualTo("org.springframework.boot");
assertThat(dependency.getArtifactId()).isEqualTo("spring-boot-starter-actuator");
assertThat(dependency.getVersion()).isEmpty();
assertThat(dependency.getScope()).isEmpty();
}

@Test
void shouldProblemSpringDependency() {
Dependency dependency = SpringBootMvc.problemSpringDependency();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,43 +92,4 @@ void shouldAddSpringBootUndertow() throws Exception {

assertFileContent(projectPath, "src/main/resources/config/application.properties", "server.port=8080");
}

@Test
void shouldAddSpringBootActuator() throws Exception {
ProjectDTO projectDTO = TestUtils.readFileToObject("json/chips.json", ProjectDTO.class).folder(tmpDirForTest());
Project project = ProjectDTO.toProject(projectDTO);
initApplicationService.init(project);
mavenApplicationService.init(project);
springBootApplicationService.init(project);

mockMvc
.perform(
post("/api/servers/spring-boot/technical-tools/actuator")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.convertObjectToJsonBytes(projectDTO))
)
.andExpect(status().isOk());

String projectPath = projectDTO.getFolder();
assertFileExist(projectPath, POM_XML);
assertFileContent(project, POM_XML, springBootStarterActuatorDependency());

assertFileContent(projectPath, "src/main/resources/config/application.properties", "management.endpoints.web.base-path=/management");
assertFileContent(
projectPath,
"src/main/resources/config/application.properties",
"management.endpoints.web.exposure.include=configprops, env, health, info, logfile, loggers, threaddump"
);
assertFileContent(projectPath, "src/main/resources/config/application.properties", "management.endpoint.health.probes.enabled=true");
assertFileContent(
projectPath,
"src/main/resources/config/application.properties",
"management.endpoint.health.group.liveness.include=livenessState"
);
assertFileContent(
projectPath,
"src/main/resources/config/application.properties",
"management.endpoint.health.group.readiness.include=readinessState"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tech.jhipster.lite.generator.server.springboot.technicaltools.actuator.application;

import static tech.jhipster.lite.TestUtils.tmpProject;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import tech.jhipster.lite.IntegrationTest;
import tech.jhipster.lite.generator.buildtool.maven.application.MavenApplicationService;
import tech.jhipster.lite.generator.init.application.InitApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.server.springboot.core.application.SpringBootApplicationService;

@IntegrationTest
class SpringBootActuatorApplicationServiceIT {

@Autowired
InitApplicationService initApplicationService;

@Autowired
MavenApplicationService mavenApplicationService;

@Autowired
SpringBootApplicationService springBootApplicationService;

@Autowired
SpringBootActuatorApplicationService springBootActuatorApplicationService;

@Test
void shouldAddSpringBootActuator() {
Project project = tmpProject();
initApplicationService.init(project);
mavenApplicationService.addPomXml(project);
springBootApplicationService.init(project);

springBootActuatorApplicationService.addActuator(project);

SpringbootActuatorAssert.assertDependencies(project);
SpringbootActuatorAssert.assertProperties(project);
}
}
Loading

0 comments on commit a891583

Please sign in to comment.