Skip to content

Commit f457f3b

Browse files
committed
wip reworking TestProjectContext and simplify Conditions for Boot Upgrade recipe and report
1 parent 4404643 commit f457f3b

File tree

6 files changed

+87
-59
lines changed

6 files changed

+87
-59
lines changed

applications/rest-service/src/main/resources/static/js/recipe.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,19 @@ function applyRecipes(btn) {
7777
beforeSend: function() {
7878
state.startedRunningRecipe();
7979
},
80-
error: function() {
80+
error: function(e) {
8181
// mark red flashlights / play alarm sound
82+
console.log("Error while applying recipe: " + e)
8283
}
8384
})
8485
.done(function () {
85-
$(`.run-a-recipe[recipe='${recipeName}']`)
86-
.closest(".sect2")
87-
.remove();
86+
// var section = $(`.run-a-recipe[recipe='${recipeName}']`)
87+
$(`.run-a-recipe[recipe='${recipeName}']`).fadeOut( 1200, "linear", function() {
88+
$(`.run-a-recipe[recipe='${recipeName}']`)
89+
.closest(".sect2")
90+
.remove();
91+
});
92+
8893
})
8994
.always(function () {
9095
state.completedRunningRecipe();

components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ public static class Builder {
249249
private OpenRewriteMavenBuildFile mockedBuildFile;
250250
private DependencyHelper dependencyHelper = new DependencyHelper();
251251
private SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties();
252+
253+
private Optional<String> springVersion = Optional.empty();
254+
252255
private JavaParser javaParser;
253256

254257
public Builder(Path projectRoot) {
@@ -456,10 +459,20 @@ public ProjectContext serializeProjectContext(Path targetDir) {
456459
public ProjectContext build() {
457460
verifyValidBuildFileSetup();
458461

459-
if (dependencies != null && !dependencies.isEmpty()) {
460-
String generatedPomXml = renderPomXmlWithGivenDependencies();
461-
resourcesWithRelativePaths.put(Path.of("pom.xml"), generatedPomXml);
462-
}
462+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
463+
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
464+
" <modelVersion>4.0.0</modelVersion>\n" +
465+
" <groupId>com.example</groupId>\n" +
466+
" <artifactId>dummy-root</artifactId>\n" +
467+
" <version>0.1.0-SNAPSHOT</version>\n" +
468+
" <packaging>jar</packaging>\n" +
469+
"{{}}\n" +
470+
"{{dependencies}}\n" +
471+
"</project>\n";
472+
473+
xml = xml.replace("{{dependencies}}", getDependenciesSection());
474+
475+
resourcesWithRelativePaths.put(Path.of("pom.xml"), xml);
463476

464477
if (!containsAnyPomXml()) {
465478
withDummyRootBuildFile();
@@ -588,22 +601,9 @@ private Parser.Input createParserInput(Path path, String value) {
588601

589602

590603
@NotNull
591-
private String renderPomXmlWithGivenDependencies() {
592-
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
593-
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
594-
" <modelVersion>4.0.0</modelVersion>\n" +
595-
" <groupId>com.example</groupId>\n" +
596-
" <artifactId>dummy-root</artifactId>\n" +
597-
" <version>0.1.0-SNAPSHOT</version>\n" +
598-
" <packaging>jar</packaging>\n" +
599-
"{{dependencies}}\n" +
600-
"</project>\n";
601-
602-
String dependenciesText = null;
603-
if(dependencies.isEmpty()) {
604-
dependenciesText = "";
605-
} else {
606-
StringBuilder dependenciesSection = new StringBuilder();
604+
private String getDependenciesSection() {
605+
StringBuilder dependenciesSection = new StringBuilder();
606+
if(!dependencies.isEmpty()) {
607607
dependenciesSection.append(" ").append("<dependencies>").append("\n");
608608
dependencyHelper.mapCoordinatesToDependencies(dependencies).stream().forEach(dependency -> {
609609
dependenciesSection.append(" ").append(" ").append("<dependency>").append("\n");
@@ -613,11 +613,15 @@ private String renderPomXmlWithGivenDependencies() {
613613
dependenciesSection.append(" ").append(" ").append("</dependency>").append("\n");
614614
});
615615
dependenciesSection.append(" ").append("</dependencies>").append("\n");
616-
dependenciesText = dependenciesSection.toString();
617616
}
618617

619-
String buildFileSource = xml.replace("{{dependencies}}", dependenciesText);
620-
return buildFileSource;
618+
return dependenciesSection.toString();
619+
}
620+
621+
public Builder withSpringBootParentOf(String springVersion) {
622+
623+
this.springVersion = Optional.of(springVersion);
624+
return this;
621625
}
622626
}
623627

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openrewrite.ExecutionContext;
2121
import org.openrewrite.java.JavaIsoVisitor;
2222
import org.openrewrite.java.tree.J;
23+
import org.springframework.sbm.boot.common.conditions.IsSpringBootProject;
2324
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportSection;
2425
import org.springframework.sbm.engine.context.ProjectContext;
2526
import org.springframework.sbm.project.resource.RewriteSourceFileHolder;
@@ -42,6 +43,13 @@ public String getDescription() {
4243

4344
@Override
4445
public boolean evaluate(ProjectContext context) {
46+
IsSpringBootProject isSpringBootProject = new IsSpringBootProject();
47+
isSpringBootProject.setVersionPattern("2\\.7\\..*|3\\.0\\..*");
48+
boolean isSpringBootApplication = isSpringBootProject.evaluate(context);
49+
if(!isSpringBootApplication) {
50+
return false;
51+
}
52+
4553
//CrudRepositoryExtension
4654
List<RewriteSourceFileHolder<J.CompilationUnit>> pagingAndSortingFileHolders =
4755
context.getProjectJavaSources().find(pagingAndSortingFinders("org.springframework.data.repository.PagingAndSortingRepository"));

components/sbm-recipes-boot-upgrade/src/main/resources/recipes/27_30/report/sbu30-paging-and-sorting-repository.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
- type: org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter
88
condition:
9-
type: org.springframework.sbm.boot.common.conditions.IsSpringBootProject
10-
versionPattern: "2\\.7\\..*|3\\.0\\..*"
9+
type: org.springframework.sbm.boot.upgrade_27_30.report.helper.PagingAndSortingHelper
1110
description: Add CrudRepository interface extension additionally to PagingAndSortingRepository
1211
openRewriteRecipe: |-
1312
type: specs.openrewrite.org/v1beta/recipe

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/report/SpringBootUpgradeReportTestSupport.java

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import lombok.Setter;
2020
import org.springframework.sbm.engine.context.ProjectContext;
2121
import org.springframework.sbm.engine.context.ProjectContextHolder;
22-
import org.springframework.sbm.engine.recipe.Action;
2322
import org.springframework.sbm.engine.recipe.Recipe;
2423
import org.springframework.sbm.engine.recipe.Recipes;
2524
import org.springframework.sbm.project.resource.TestProjectContext;
@@ -29,6 +28,7 @@
2928
import org.stringtemplate.v4.ST;
3029

3130
import java.nio.file.Path;
31+
import java.util.ArrayList;
3232
import java.util.List;
3333
import java.util.Map;
3434
import java.util.function.Consumer;
@@ -115,7 +115,7 @@ private void verifyDoesNotRender() {
115115
if(SectionBuilderData.class.isInstance(builderData)) {
116116
SectionBuilderData sectionBuilderData = SectionBuilderData.class.cast(builderData);
117117
withRecipes(recipes -> {
118-
Recipe recipe = recipes.getRecipeByName("boot-2.7-3.0-upgrade-report2").get();
118+
Recipe recipe = recipes.getRecipeByName("sbu30-report").get();
119119
SpringBootUpgradeReportAction action = (SpringBootUpgradeReportAction) recipe.getActions().get(0);
120120
List<SpringBootUpgradeReportSection> sections = (List<SpringBootUpgradeReportSection>) ReflectionTestUtils.getField(recipe.getActions().get(0), "sections");
121121
List<SpringBootUpgradeReportSection> matchingSections = sections
@@ -134,7 +134,7 @@ private void verifyDoesNotRender() {
134134
} else if(ReportBuilderData.class.isInstance(builderData)) {
135135
ReportBuilderData reportBuilderData = ReportBuilderData.class.cast(builderData);
136136
withRecipes(recipes -> {
137-
Recipe recipe = recipes.getRecipeByName("boot-2.7-3.0-upgrade-report2").get();
137+
Recipe recipe = recipes.getRecipeByName("sbu30-report").get();
138138
SpringBootUpgradeReportAction action = (SpringBootUpgradeReportAction) recipe.apply(reportBuilderData.getContext()).get(0);
139139
bruteForceProjectContextIntoProjectContextHolder(reportBuilderData.getContext(), action);
140140
List<SpringBootUpgradeReportSection> sections = (List<SpringBootUpgradeReportSection>) ReflectionTestUtils.getField(recipe.getActions().get(0), "sections");
@@ -159,15 +159,15 @@ private void verify(Consumer<String> assertion) {
159159
if(ReportBuilderData.class.isInstance(builderData)) {
160160
ReportBuilderData reportBuilderData = ReportBuilderData.class.cast(builderData);
161161
withRecipes(recipes -> {
162-
Recipe recipe = recipes.getRecipeByName("boot-2.7-3.0-upgrade-report2").get();
162+
Recipe recipe = recipes.getRecipeByName("sbu30-report").get();
163163
SpringBootUpgradeReportAction action = (SpringBootUpgradeReportAction) recipe.getActions().get(0);
164164
bruteForceProjectContextIntoProjectContextHolder(builderData.getContext(), action);
165165
// ReflectionTestUtils.setField(action, "upgradeReportProcessor", (SpringBootUpgradeReportFileSystemRenderer) s -> assertion.accept(s));
166166
action.apply(reportBuilderData.getContext());
167167
});
168168
} else if(SectionBuilderData.class.isInstance(builderData)) {
169169
withRecipes(recipes -> {
170-
Recipe recipe = recipes.getRecipeByName("boot-2.7-3.0-upgrade-report2").get();
170+
Recipe recipe = recipes.getRecipeByName("sbu30-report").get();
171171
SpringBootUpgradeReportAction action = (SpringBootUpgradeReportAction) recipe.getActions().get(0);
172172
bruteForceProjectContextIntoProjectContextHolder(builderData.getContext(), action);
173173
List<SpringBootUpgradeReportSection> sections = (List<SpringBootUpgradeReportSection>) ReflectionTestUtils.getField(recipe.getActions().get(0), "sections");
@@ -183,7 +183,7 @@ private void verify(Consumer<String> assertion) {
183183
SpringBootUpgradeReportSection sectionUnderTest = matchingSections.get(0);
184184
action.apply(builderData.getContext());
185185
String renderedSection = sectionUnderTest.render(builderData.getContext());
186-
String renderedSectionWithoutButtonCode = replaceRe4cipeButtonCodeFromExpectedOutput(sectionUnderTest, renderedSection);
186+
String renderedSectionWithoutButtonCode = replaceRecipeButtonCodeFromExpectedOutput(sectionUnderTest, renderedSection);
187187

188188
assertion.accept(renderedSectionWithoutButtonCode);
189189
});
@@ -194,32 +194,43 @@ private void verify(Consumer<String> assertion) {
194194
* Another hack, removing the expected button code added to the Asciidoc to free tests from asserting invisible
195195
* code of buttons to apply a recipe.
196196
*/
197-
private String replaceRe4cipeButtonCodeFromExpectedOutput(SpringBootUpgradeReportSection sectionUnderTest, String renderedSection) {
197+
private String replaceRecipeButtonCodeFromExpectedOutput(SpringBootUpgradeReportSection sectionUnderTest, String renderedSection) {
198198
StringBuilder sb = new StringBuilder();
199-
List<String> buttonCodes = sectionUnderTest
200-
.getRemediation()
201-
.getPossibilities()
202-
.stream()
203-
.filter(p -> p.getRecipe() != null)
204-
.map(RemediationPossibility::getRecipe)
205-
.map(recipe -> {
206-
String target = """
199+
List<String> buttonCodes = new ArrayList<>();
200+
if(sectionUnderTest.getRemediation().getPossibilities().isEmpty()) {
201+
String recipe = sectionUnderTest.getRemediation().getRecipe();
202+
if(recipe != null) {
203+
String target = """
207204
208-
++++
209-
<form name="recipe-1" action="http://localhost:8080/spring-boot-upgrade" method="post">
210-
<input type="hidden" name="recipeName" value="<RECIPE>" />
211-
<button name="<RECIPE>" type="submit" style="background-color:#71ea5b;border:width: 120px;
212-
text-align: center;
213-
font-size: 15px;
214-
padding: 20px;
215-
border-radius: 15px;">Run Recipe</button>
216-
</form>
217-
++++
205+
++++
206+
<div class="run-a-recipe" recipe="<RECIPE>">
207+
</div>
208+
++++
218209
219-
""";
220-
return target.replace("<RECIPE>", recipe);
221-
})
222-
.collect(Collectors.toList());
210+
""";
211+
buttonCodes.add(target.replace("<RECIPE>", recipe));
212+
}
213+
} else {
214+
buttonCodes = sectionUnderTest
215+
.getRemediation()
216+
.getPossibilities()
217+
.stream()
218+
.filter(p -> p.getRecipe() != null)
219+
.map(RemediationPossibility::getRecipe)
220+
.map(recipe -> {
221+
String target = """
222+
223+
++++
224+
<div class="run-a-recipe" recipe="<RECIPE>">
225+
</div>
226+
++++
227+
228+
""";
229+
return target.replace("<RECIPE>", recipe);
230+
})
231+
.collect(Collectors.toList());
232+
}
233+
223234
for(String buttonCode : buttonCodes) {
224235
renderedSection = renderedSection.replace(buttonCode, "");
225236
}
@@ -228,7 +239,7 @@ private String replaceRe4cipeButtonCodeFromExpectedOutput(SpringBootUpgradeRepor
228239

229240
private void withRecipes(Consumer<Recipes> recipesConsumer) {
230241
RecipeTestSupport.testRecipe(
231-
Path.of("recipes/boot-new-report.yaml"), recipesConsumer,
242+
Path.of("recipes/27_30/report/sbu30-report.yaml"), recipesConsumer,
232243
SpringBootUpgradeReportActionDeserializer.class,
233244
SpringBootUpgradeReportFreemarkerSupport.class,
234245
SpringBootUpgradeReportFileSystemRenderer.class,

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/report/helper/PagingAndSortingHelperTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public class A {}
165165
""";
166166

167167
ProjectContext context = TestProjectContext.buildProjectContext()
168+
.withSpringBootParentOf("2.7,1")
168169
.addJavaSource("src/main/java", javaClassWithReactiveSortingRepo)
169170
.addJavaSource("src/main/java",javaClassWithoutReactiveSortingRepo)
170171
.withBuildFileHavingDependencies("org.springframework.data:spring-data-commons:2.7.1")

0 commit comments

Comments
 (0)