Skip to content

Commit

Permalink
Actuator: Add Actuator
Browse files Browse the repository at this point in the history
  • Loading branch information
swarajsaaj committed Dec 18, 2021
1 parent e15ffc5 commit d2edba8
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ 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 @@ -16,6 +16,10 @@ public static Dependency springBootStarterWebDependency() {
return Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-web").build();
}

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

public static Dependency tomcatDependency() {
return Dependency.builder().groupId("org.springframework.boot").artifactId("spring-boot-starter-tomcat").build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ public void addSpringBootUndertow(Project project) {
addLoggerInConfiguration(project, "io.undertow", Level.WARN);
}

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

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

@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,5 +7,6 @@ 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 @@ -38,4 +38,12 @@ public void addSpringBootUndertow(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
springBootMvcApplicationService.addSpringBootUndertow(project);
}

@Operation(summary = "Add Spring Boot Actuator")
@ApiResponses({ @ApiResponse(responseCode = "500", description = "An error occurred while adding Spring Boot Actuator") })
@PostMapping("/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
Expand Up @@ -147,6 +147,36 @@ void shouldAddSpringBootUndertowWithServerPort() {
assertExceptionHandler(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 @@ -22,6 +22,15 @@ public static List<String> springBootStarterUndertowDependency() {
);
}

public static List<String> springBootStarterActuatorDependency() {
return List.of(
"<dependency>",
"<groupId>org.springframework.boot</groupId>",
"<artifactId>spring-boot-starter-actuator</artifactId>",
"</dependency>"
);
}

public static List<String> springBootStarterWebWithoutTomcat() {
return List.of(
"<dependency>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ 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 shouldSpringfoxDependency() {
Dependency dependency = SpringBootMvc.springfoxDependency();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,43 @@ 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/mvc/web/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"
);
}
}

0 comments on commit d2edba8

Please sign in to comment.