Skip to content

Commit 26ec07e

Browse files
authored
Support reusing Conditions for Helper in Spring Boot 3 Upgrade Report YAML (#592)
* Deprecate outdated class * Support to reuse existing condition for Helper * Remove linebreak in pom.xml * Remove GitHub info from rendered asciidoc in tests * Test for ConditionDeserializer * Refactor internal Helper class into top level class * Change section numeration in report * Add Helper that is always true and returns no data * Add description to Condition
1 parent 270ad9b commit 26ec07e

File tree

35 files changed

+782
-174
lines changed

35 files changed

+782
-174
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/MultiModuleAwareActionDeserializer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@
2525
import java.lang.reflect.Constructor;
2626
import java.lang.reflect.InvocationTargetException;
2727

28+
/***
29+
*
30+
* @deprecated Use "One Action implementation per use case" instead, see https://github.com/spring-projects-experimental/spring-boot-migrator/discussions/345
31+
*/
2832
@Component
2933
@RequiredArgsConstructor
34+
@Deprecated(forRemoval = true)
3035
public class MultiModuleAwareActionDeserializer implements ActionDeserializer {
3136

3237
protected final ObjectMapper yamlObjectMapper;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.engine.recipe;
17+
18+
import com.fasterxml.jackson.core.JsonProcessingException;
19+
import com.fasterxml.jackson.databind.DeserializationFeature;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.fasterxml.jackson.databind.module.SimpleModule;
22+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
23+
import org.junit.jupiter.api.Test;
24+
import org.mockito.Mockito;
25+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
26+
import org.springframework.sbm.java.migration.conditions.HasMemberAnnotation;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* @author Fabian Krüger
32+
*/
33+
class ConditionDeserializerTest {
34+
@Test
35+
void deserializeCondition() throws JsonProcessingException {
36+
String yaml =
37+
"""
38+
type: org.springframework.sbm.java.migration.conditions.HasMemberAnnotation
39+
annotation: "some.Annotation"
40+
""";
41+
42+
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
43+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
44+
SimpleModule moule = new SimpleModule("ConditionModule");
45+
AutowireCapableBeanFactory beanFactory = Mockito.mock(AutowireCapableBeanFactory.class);
46+
moule.addDeserializer(Condition.class, new ConditionDeserializer(objectMapper, beanFactory));
47+
objectMapper.registerModule(moule);
48+
49+
Condition condition = objectMapper.readValue(yaml, Condition.class);
50+
51+
assertThat(condition).isInstanceOf(HasMemberAnnotation.class);
52+
HasMemberAnnotation typedCondition = (HasMemberAnnotation) condition;
53+
assertThat(typedCondition.getDescription()).isEqualTo("If there are any fields annotated with some.Annotation");
54+
assertThat(typedCondition).hasFieldOrPropertyWithValue("annotation", "some.Annotation");
55+
}
56+
}

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/SpringBootUpgradeReportActionDeserializer.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/SpringBootUpgradeReportSection.java

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,16 @@
4747
@Setter
4848
public class SpringBootUpgradeReportSection {
4949

50-
private static final String ls = System.lineSeparator();
51-
52-
/**
53-
* Helper acting as {@link Condition} and data provide for a {@link SpringBootUpgradeReportSection}.
54-
* @deprecated Use {@link AbstractHelper} instead
55-
*/
56-
@Deprecated(forRemoval = true)
57-
public interface Helper<T> extends Condition {
58-
/**
59-
* @return {@code Map<String, T>} the model data for the template.
60-
*/
61-
Map<String, T> getData();
62-
}
63-
64-
public static abstract class AbstractHelper<T> implements Helper<T> {
65-
66-
@Override
67-
public String getDescription() {
68-
return "";
69-
}
70-
}
71-
7250
public static final String CHANGE_HEADER = "What Changed";
7351
public static final String AFFECTED = "Why is the application affected";
7452
public static final String REMEDIATION = "Remediation";
75-
76-
public boolean shouldRender(ProjectContext context) {
77-
return helper.evaluate(context);
78-
}
79-
53+
private static final String ls = System.lineSeparator();
54+
/**
55+
* The spring project(s)/modules this change comes from.
56+
*
57+
* e.g. {@code spring-boot} and {@code actuator}
58+
*/
59+
private List<String> projects;
8060
/**
8161
* Section title
8262
*/
@@ -108,15 +88,22 @@ public boolean shouldRender(ProjectContext context) {
10888
@NotNull
10989
private Set<String> contributors;
11090

111-
@JsonIgnore
112-
private Helper<Object> helper;
91+
@NotNull
92+
private SpringBootUpgradeReportSectionHelper<?> helper;
93+
11394
@JsonIgnore
11495
@Autowired
11596
private SpringBootUpgradeReportFreemarkerSupport freemarkerSupport;
11697

98+
public boolean shouldRender(ProjectContext context) {
99+
return helper.evaluate(context);
100+
}
101+
102+
103+
117104
public String render(ProjectContext context) {
118105
if (getHelper().evaluate(context)) {
119-
Map<String, Object> params = getHelper().getData();
106+
Map<String, ?> params = getHelper().getData();
120107

121108
try (StringWriter writer = new StringWriter()) {
122109
String templateContent = buildTemplate();
@@ -137,7 +124,7 @@ public String render(ProjectContext context) {
137124
throw new IllegalArgumentException("Could not render Section '"+ getTitle()+"', evaluating the context returned false");
138125
}
139126

140-
private void renderTemplate(Map<String, Object> params, StringWriter writer, String templateContent) throws IOException, TemplateException {
127+
private void renderTemplate(Map<String, ?> params, StringWriter writer, String templateContent) throws IOException, TemplateException {
141128
String templateName = getTitle().replace(" ", "") + UUID.randomUUID();
142129
freemarkerSupport.getStringLoader().putTemplate(templateName, templateContent);
143130
Template t = freemarkerSupport.getConfiguration().getTemplate(templateName);
@@ -207,20 +194,19 @@ private void renderLineBreak(StringBuilder sb) {
207194
sb.append(ls);
208195
}
209196

210-
private void renderGitHubInfo(StringBuilder sb) {
197+
void renderGitHubInfo(StringBuilder sb) {
211198
if(gitHubIssue != null) {
212-
sb.append("Issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/").append(gitHubIssue).append("[#").append(gitHubIssue).append("^, role=\"ext-link\"]");
213-
}
214-
if(contributors != null && gitHubIssue != null) {
215-
sb.append(", ");
216-
} else {
217-
sb.append(ls);
199+
sb.append("**Issue:** https://github.com/spring-projects-experimental/spring-boot-migrator/issues/").append(gitHubIssue).append("[#").append(gitHubIssue).append("^, role=\"ext-link\"] ").append(" + ").append(ls);
218200
}
219201
if(contributors != null) {
220202
List<Author> authors = getAuthors();
221-
sb.append("Contributors: ");
203+
sb.append("**Contributors:** ");
222204
String authorsString = authors.stream().map(a -> "https://github.com/" + a.getHandle() + "[@" + a.getHandle() + "^, role=\"ext-link\"]").collect(Collectors.joining(", "));
223-
sb.append(authorsString).append(ls);
205+
sb.append(authorsString).append(" + ").append(ls);
206+
}
207+
if(projects != null){
208+
String projectsList = projects.stream().collect(Collectors.joining(", "));
209+
sb.append("**Projects:** ").append(projectsList).append(ls);
224210
}
225211
}
226212

@@ -229,6 +215,10 @@ private void renderSectionTitle(StringBuilder sb) {
229215
}
230216

231217
public List<Author> getAuthors() {
218+
if(contributors == null) {
219+
return List.of();
220+
}
221+
232222
return contributors.stream()
233223
.map(c -> {
234224
Matcher matcher = Pattern.compile("(.*)\\[(.*)\\]").matcher(c);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.boot.upgrade_27_30.report;
17+
18+
import org.springframework.sbm.engine.recipe.Condition;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* Helper base class which is {@link Condition} and data provider for a {@link SpringBootUpgradeReportSection}.
24+
*
25+
* @author Fabian Krüger
26+
*/
27+
public abstract class SpringBootUpgradeReportSectionHelper<T> implements Condition {
28+
/**
29+
* @return {@code Map<String, T>} the model data for the template.
30+
*/
31+
public abstract Map<String, T> getData();
32+
33+
@Override
34+
public String getDescription() {
35+
return "";
36+
}
37+
}

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/ActuatorEndpointsSanitizationHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.sbm.boot.upgrade_27_30.report.helper;
1717
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
1818
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
19+
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
1920
import org.springframework.sbm.build.api.BuildFile;
2021
import org.springframework.sbm.build.api.Module;
2122
import org.springframework.sbm.engine.context.ProjectContext;
@@ -29,7 +30,7 @@
2930
/**
3031
* @author Fabian Krüger
3132
*/
32-
public class ActuatorEndpointsSanitizationHelper extends SpringBootUpgradeReportSection.AbstractHelper<List<BuildFile>> {
33+
public class ActuatorEndpointsSanitizationHelper extends SpringBootUpgradeReportSectionHelper<List<BuildFile>> {
3334

3435
private static final String ACTUATOR_GROUP_ID = "org.springframework.boot";
3536
private static final String ACTUATOR_ARTIFACT_ID = "spring-boot-actuator";

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/AddSpringBootRepositoriesHelper.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@
1717
package org.springframework.sbm.boot.upgrade_27_30.report.helper;
1818

1919
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
20+
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
2021
import org.springframework.sbm.build.migration.conditions.NoPluginRepositoryExistsCondition;
2122
import org.springframework.sbm.build.migration.conditions.NoRepositoryExistsCondition;
2223
import org.springframework.sbm.engine.context.ProjectContext;
2324

2425
import java.util.Map;
2526

26-
public class AddSpringBootRepositoriesHelper implements SpringBootUpgradeReportSection.Helper<String>{
27-
@Override
28-
public String getDescription() {
29-
return null;
30-
}
31-
27+
public class AddSpringBootRepositoriesHelper extends SpringBootUpgradeReportSectionHelper<String> {
3228
@Override
3329
public boolean evaluate(ProjectContext context) {
3430
return new NoRepositoryExistsCondition().evaluate(context) && new NoPluginRepositoryExistsCondition().evaluate(context);

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/BannerSupportHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
2020
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
21+
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
2122
import org.springframework.sbm.engine.context.ProjectContext;
2223
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
2324

@@ -26,7 +27,7 @@
2627
import java.util.Map;
2728
import java.util.stream.Collectors;
2829

29-
public class BannerSupportHelper implements SpringBootUpgradeReportSection.Helper<List<String>> {
30+
public class BannerSupportHelper extends SpringBootUpgradeReportSectionHelper<List<String>> {
3031

3132
public static final String VERSION_PATTERN = "(2\\.7\\..*)|(3\\.0\\..*)";
3233

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/ChangesToDataPropertiesHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.sbm.boot.properties.api.SpringBootApplicationProperties;
2222
import org.springframework.sbm.boot.properties.search.SpringBootApplicationPropertiesResourceListFilter;
2323
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
24+
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
2425
import org.springframework.sbm.build.migration.conditions.NoDependencyExistMatchingRegex;
2526
import org.springframework.sbm.engine.context.ProjectContext;
2627

@@ -34,7 +35,7 @@
3435
/**
3536
* @author Fabian Krüger
3637
*/
37-
public class ChangesToDataPropertiesHelper implements SpringBootUpgradeReportSection.Helper<List<ChangesToDataPropertiesHelper.Match>> {
38+
public class ChangesToDataPropertiesHelper extends SpringBootUpgradeReportSectionHelper<List<ChangesToDataPropertiesHelper.Match>> {
3839

3940
public static final String VERSION_PATTERN = "(2\\.7\\..*)|(3\\.0\\..*)";
4041

0 commit comments

Comments
 (0)