Skip to content

Commit 091c890

Browse files
ishu-thakurfabapp2
authored andcommitted
Adding TestProjectContext for the JmxEndpointExposure.
1 parent 9c98510 commit 091c890

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.springframework.sbm.boot.upgrade_27_30.actions;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
5+
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
6+
import org.springframework.sbm.boot.properties.api.SpringBootApplicationProperties;
7+
import org.springframework.sbm.boot.properties.search.SpringBootApplicationPropertiesResourceListFilter;
8+
import org.springframework.sbm.engine.context.ProjectContext;
9+
import org.springframework.sbm.project.resource.TestProjectContext;
10+
11+
import java.util.List;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class Boot_27_30_JmxEndpointExposureActionTest {
16+
private final String DUMMY_PROPERTY_FILE = "foo=bar\n" +
17+
"defaultBasePackage=org.springframework.sbm";
18+
19+
@Test
20+
public void givenAProjectWithoutJmxEndpointExposureOverride_andSpringBootProperties_applyAction_expectPropertyAdded() {
21+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
22+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
23+
.addProjectResource("src/main/resources/application.properties", DUMMY_PROPERTY_FILE)
24+
.build();
25+
26+
Boot_27_30_JmxEndpointExposureAction boot_27_30_jmxEndpointExposureAction = new Boot_27_30_JmxEndpointExposureAction();
27+
boot_27_30_jmxEndpointExposureAction.apply(projectContext);
28+
29+
List<SpringBootApplicationProperties> bootApplicationProperties = new SpringBootApplicationPropertiesResourceListFilter().apply(projectContext.getProjectResources());
30+
assertThat(bootApplicationProperties.size()).isEqualTo(1);
31+
assertThat(bootApplicationProperties.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
32+
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.springframework.sbm.boot.upgrade_27_30.conditions;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
5+
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
6+
import org.springframework.sbm.engine.context.ProjectContext;
7+
import org.springframework.sbm.project.resource.TestProjectContext;
8+
9+
import java.nio.file.Path;
10+
11+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
12+
13+
public class JmxEndpointExposureConditionTest {
14+
15+
private static final String APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
16+
"migrate=true\n" +
17+
"management.endpoints.jmx.exposure.include=*\n";
18+
19+
private static final String APPLICATION_PROPERTIES_WITHOUT_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
20+
"migrate=true\n";
21+
22+
@Test
23+
public void givenProjectWithJmxEndpointExposureCustomization_evaluateCondition_expectFalse() {
24+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
25+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
26+
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
27+
.build();
28+
29+
JmxEndpointExposureCondition jmxEndpointExposureCondition = new JmxEndpointExposureCondition();
30+
assertThat(jmxEndpointExposureCondition.evaluate(projectContext)).isFalse();
31+
}
32+
33+
@Test
34+
public void givenProjectWithJmxEndpointExposureCustomization_evaluateCondition_expectTrue() {
35+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
36+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
37+
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITHOUT_JMX_ENDPOINT_EXPOSED)
38+
.build();
39+
40+
JmxEndpointExposureCondition jmxEndpointExposureCondition = new JmxEndpointExposureCondition();
41+
assertThat(jmxEndpointExposureCondition.evaluate(projectContext)).isTrue();
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.springframework.sbm.boot.upgrade_27_30.filter;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
5+
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
6+
import org.springframework.sbm.engine.context.ProjectContext;
7+
import org.springframework.sbm.project.resource.TestProjectContext;
8+
import org.springframework.sbm.properties.api.PropertiesSource;
9+
10+
import java.nio.file.Path;
11+
import java.util.List;
12+
13+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
14+
15+
public class JmxEndpointExposureFinderTest {
16+
private static final String APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED = "foo=bar\n" +
17+
"migrate=true\n" +
18+
"management.endpoints.jmx.exposure.include=*\n";
19+
20+
private static final String MULTI_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
21+
"xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
22+
" <modelVersion>4.0.0</modelVersion>\n" +
23+
"\n" +
24+
" <groupId>org.springframework.sbm</groupId>\n" +
25+
" <artifactId>spring-boot-migrator</artifactId>\n" +
26+
" <version>0.11.2-SNAPSHOT</version>\n" +
27+
" <packaging>pom</packaging>\n" +
28+
" <modules>\n" +
29+
" <module>module1</module>\n" +
30+
" <module>module2</module>\n" +
31+
" </modules>\n" +
32+
"</project>";
33+
34+
private static final String SUB_MODULE_POM_XML = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
35+
"xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
36+
" <parent>\n" +
37+
" <artifactId>spring-boot-migrator</artifactId>\n" +
38+
" <groupId>org.springframework.sbm</groupId>\n" +
39+
" <version>0.11.2-SNAPSHOT</version>\n" +
40+
" <relativePath>../../pom.xml</relativePath>\n" +
41+
" </parent>\n" +
42+
" <modelVersion>4.0.0</modelVersion>\n" +
43+
"\n" +
44+
" <artifactId>{{module}}</artifactId>\n" +
45+
"</project>";
46+
@Test
47+
public void givenProjectWithJmxEndpointExposureCustomization_findResources_returnResource(){
48+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
49+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
50+
.addProjectResource(Path.of("src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
51+
.build();
52+
53+
JmxEndpointExposureFinder jmxEndpointExposureFinder = new JmxEndpointExposureFinder();
54+
List<? extends PropertiesSource> propertiesSources = jmxEndpointExposureFinder.apply(projectContext.getProjectResources());
55+
56+
assertThat(propertiesSources.size()).isEqualTo(1);
57+
assertThat(propertiesSources.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
58+
}
59+
60+
@Test
61+
public void givenMultiModuleProjectWithJmxEndpointExposureCustomization_findResources_returnResource(){
62+
ProjectContext projectContext = TestProjectContext.buildProjectContext()
63+
.withMavenRootBuildFileSource(MULTI_MODULE_POM_XML)
64+
.addProjectResource(Path.of("module1","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module1"))
65+
.addProjectResource(Path.of("module2","pom.xml"),SUB_MODULE_POM_XML.replace("{{module}}", "module2"))
66+
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
67+
.addProjectResource(Path.of("module1","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
68+
.addProjectResource(Path.of("module2","src", "main", "resources", "application.properties"), APPLICATION_PROPERTIES_WITH_JMX_ENDPOINT_EXPOSED)
69+
.build();
70+
71+
JmxEndpointExposureFinder jmxEndpointExposureFinder = new JmxEndpointExposureFinder();
72+
List<? extends PropertiesSource> propertiesSources = jmxEndpointExposureFinder.apply(projectContext.getProjectResources());
73+
74+
assertThat(propertiesSources.size()).isEqualTo(2);
75+
assertThat(propertiesSources.get(0).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
76+
assertThat(propertiesSources.get(1).getProperty("management.endpoints.jmx.exposure.include").isPresent()).isTrue();
77+
}
78+
}

0 commit comments

Comments
 (0)