Skip to content

Support reusing Conditions for Helper in Spring Boot 3 Upgrade Report YAML #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

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

protected final ObjectMapper yamlObjectMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.engine.recipe;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.sbm.java.migration.conditions.HasMemberAnnotation;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Fabian Krüger
*/
class ConditionDeserializerTest {
@Test
void deserializeCondition() throws JsonProcessingException {
String yaml =
"""
type: org.springframework.sbm.java.migration.conditions.HasMemberAnnotation
annotation: "some.Annotation"
""";

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
SimpleModule moule = new SimpleModule("ConditionModule");
AutowireCapableBeanFactory beanFactory = Mockito.mock(AutowireCapableBeanFactory.class);
moule.addDeserializer(Condition.class, new ConditionDeserializer(objectMapper, beanFactory));
objectMapper.registerModule(moule);

Condition condition = objectMapper.readValue(yaml, Condition.class);

assertThat(condition).isInstanceOf(HasMemberAnnotation.class);
HasMemberAnnotation typedCondition = (HasMemberAnnotation) condition;
assertThat(typedCondition.getDescription()).isEqualTo("If there are any fields annotated with some.Annotation");
assertThat(typedCondition).hasFieldOrPropertyWithValue("annotation", "some.Annotation");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,16 @@
@Setter
public class SpringBootUpgradeReportSection {

private static final String ls = System.lineSeparator();

/**
* Helper acting as {@link Condition} and data provide for a {@link SpringBootUpgradeReportSection}.
* @deprecated Use {@link AbstractHelper} instead
*/
@Deprecated(forRemoval = true)
public interface Helper<T> extends Condition {
/**
* @return {@code Map<String, T>} the model data for the template.
*/
Map<String, T> getData();
}

public static abstract class AbstractHelper<T> implements Helper<T> {

@Override
public String getDescription() {
return "";
}
}

public static final String CHANGE_HEADER = "What Changed";
public static final String AFFECTED = "Why is the application affected";
public static final String REMEDIATION = "Remediation";

public boolean shouldRender(ProjectContext context) {
return helper.evaluate(context);
}

private static final String ls = System.lineSeparator();
/**
* The spring project(s)/modules this change comes from.
*
* e.g. {@code spring-boot} and {@code actuator}
*/
private List<String> projects;
/**
* Section title
*/
Expand Down Expand Up @@ -108,15 +88,22 @@ public boolean shouldRender(ProjectContext context) {
@NotNull
private Set<String> contributors;

@JsonIgnore
private Helper<Object> helper;
@NotNull
private SpringBootUpgradeReportSectionHelper<?> helper;

@JsonIgnore
@Autowired
private SpringBootUpgradeReportFreemarkerSupport freemarkerSupport;

public boolean shouldRender(ProjectContext context) {
return helper.evaluate(context);
}



public String render(ProjectContext context) {
if (getHelper().evaluate(context)) {
Map<String, Object> params = getHelper().getData();
Map<String, ?> params = getHelper().getData();

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

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

private void renderGitHubInfo(StringBuilder sb) {
void renderGitHubInfo(StringBuilder sb) {
if(gitHubIssue != null) {
sb.append("Issue: https://github.com/spring-projects-experimental/spring-boot-migrator/issues/").append(gitHubIssue).append("[#").append(gitHubIssue).append("^, role=\"ext-link\"]");
}
if(contributors != null && gitHubIssue != null) {
sb.append(", ");
} else {
sb.append(ls);
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);
}
if(contributors != null) {
List<Author> authors = getAuthors();
sb.append("Contributors: ");
sb.append("**Contributors:** ");
String authorsString = authors.stream().map(a -> "https://github.com/" + a.getHandle() + "[@" + a.getHandle() + "^, role=\"ext-link\"]").collect(Collectors.joining(", "));
sb.append(authorsString).append(ls);
sb.append(authorsString).append(" + ").append(ls);
}
if(projects != null){
String projectsList = projects.stream().collect(Collectors.joining(", "));
sb.append("**Projects:** ").append(projectsList).append(ls);
}
}

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

public List<Author> getAuthors() {
if(contributors == null) {
return List.of();
}

return contributors.stream()
.map(c -> {
Matcher matcher = Pattern.compile("(.*)\\[(.*)\\]").matcher(c);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.boot.upgrade_27_30.report;

import org.springframework.sbm.engine.recipe.Condition;

import java.util.Map;

/**
* Helper base class which is {@link Condition} and data provider for a {@link SpringBootUpgradeReportSection}.
*
* @author Fabian Krüger
*/
public abstract class SpringBootUpgradeReportSectionHelper<T> implements Condition {
/**
* @return {@code Map<String, T>} the model data for the template.
*/
public abstract Map<String, T> getData();

@Override
public String getDescription() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.sbm.boot.upgrade_27_30.report.helper;
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.Module;
import org.springframework.sbm.engine.context.ProjectContext;
Expand All @@ -29,7 +30,7 @@
/**
* @author Fabian Krüger
*/
public class ActuatorEndpointsSanitizationHelper extends SpringBootUpgradeReportSection.AbstractHelper<List<BuildFile>> {
public class ActuatorEndpointsSanitizationHelper extends SpringBootUpgradeReportSectionHelper<List<BuildFile>> {

private static final String ACTUATOR_GROUP_ID = "org.springframework.boot";
private static final String ACTUATOR_ARTIFACT_ID = "spring-boot-actuator";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@
package org.springframework.sbm.boot.upgrade_27_30.report.helper;

import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
import org.springframework.sbm.build.migration.conditions.NoPluginRepositoryExistsCondition;
import org.springframework.sbm.build.migration.conditions.NoRepositoryExistsCondition;
import org.springframework.sbm.engine.context.ProjectContext;

import java.util.Map;

public class AddSpringBootRepositoriesHelper implements SpringBootUpgradeReportSection.Helper<String>{
@Override
public String getDescription() {
return null;
}

public class AddSpringBootRepositoriesHelper extends SpringBootUpgradeReportSectionHelper<String> {
@Override
public boolean evaluate(ProjectContext context) {
return new NoRepositoryExistsCondition().evaluate(context) && new NoPluginRepositoryExistsCondition().evaluate(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;

Expand All @@ -26,7 +27,7 @@
import java.util.Map;
import java.util.stream.Collectors;

public class BannerSupportHelper implements SpringBootUpgradeReportSection.Helper<List<String>> {
public class BannerSupportHelper extends SpringBootUpgradeReportSectionHelper<List<String>> {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.sbm.boot.properties.api.SpringBootApplicationProperties;
import org.springframework.sbm.boot.properties.search.SpringBootApplicationPropertiesResourceListFilter;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSectionHelper;
import org.springframework.sbm.build.migration.conditions.NoDependencyExistMatchingRegex;
import org.springframework.sbm.engine.context.ProjectContext;

Expand All @@ -34,7 +35,7 @@
/**
* @author Fabian Krüger
*/
public class ChangesToDataPropertiesHelper implements SpringBootUpgradeReportSection.Helper<List<ChangesToDataPropertiesHelper.Match>> {
public class ChangesToDataPropertiesHelper extends SpringBootUpgradeReportSectionHelper<List<ChangesToDataPropertiesHelper.Match>> {

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

Expand Down
Loading