Skip to content

#159 Added action for Johnzon upgrade for Spring boot migration 2.7 t… #265

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
merged 5 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,26 @@
package org.springframework.sbm.boot.upgrade_27_30.actions;

import org.springframework.sbm.boot.upgrade_27_30.filters.JohnzonDependencyCondition;
import org.springframework.sbm.build.api.BuildFile;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.AbstractAction;

public class Boot_27_30_UpgradeReplaceJohnzonDependencies extends AbstractAction {

private static final String JHONZON_DEPENDENCY_PATTERN = "org.apache.johnzon:johnzon-core";
private static final String JHONZON_DEPENDENCY = "org.apache.johnzon:johnzon-core:1.2.18-jakarta";

@Override
public void apply(ProjectContext context) {
BuildFile buildFile = context.getBuildFile();
buildFile.removeDependenciesMatchingRegex(JHONZON_DEPENDENCY_PATTERN);
buildFile.addDependency(Dependency.fromCoordinates(JHONZON_DEPENDENCY));
}

@Override
public boolean isApplicable(ProjectContext context) {
return new JohnzonDependencyCondition().evaluate(context);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.springframework.sbm.boot.upgrade_27_30.filters;

import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.Condition;
import org.springframework.sbm.java.impl.OpenRewriteJavaSource;

/**
* This filter finds out if any of the resource uses Johnzon
*/
public class JohnzonDependencyCondition implements Condition {

@Override
public String getDescription() {
return "Checks if any of the class in the project imports Apache Jhonzon classes";
}

@Override
public boolean evaluate(ProjectContext context) {
return context.getProjectJavaSources()
.asStream()
.anyMatch(js -> js.hasImportStartingWith("org.apache.johnzon.core"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.springframework.sbm.boot.upgrade_27_30.actions;

import org.junit.jupiter.api.Test;
import org.springframework.sbm.boot.properties.SpringApplicationPropertiesPathMatcher;
import org.springframework.sbm.boot.properties.SpringBootApplicationPropertiesRegistrar;
import org.springframework.sbm.build.api.Dependency;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Boot_27_30_UpgradeReplaceJohnzonDependenciesTest {

private static final String JHONZON_UNSUPPORTED_DEPENDENCY = "org.apache.johnzon:johnzon-core:1.2.11";
private static final String JHONZON_UPDATED_DEPENDENCY = "org.apache.johnzon:johnzon-core:1.2.18-jakarta";
private static final String JAVA_CLASS_WITH_JHONZON_IMPORT = "package org.spring.boot.migration;"
+ "import org.apache.johnzon.core.JsonStringImpl;"
+ "public class BootReplaceJohnzonDependencyDummyClass{"
+ "}";


private static final String JAVA_CLASS_WITHOUT_JHONZON_IMPORT = "package org.spring.boot.migration;"
+ "public class BootReplaceJohnzonDependencyDummyClass{"
+ "}";


@Test
public void givenProjectWithSelfManagedJohnzonDependency_migrate_expectJohnzonDependencyUpdated(){
ProjectContext projectContext = getProjectContextWithJohnzonDependency(JAVA_CLASS_WITH_JHONZON_IMPORT);
Boot_27_30_UpgradeReplaceJohnzonDependencies upgradeReplaceJohnzonDependencies = new Boot_27_30_UpgradeReplaceJohnzonDependencies();
upgradeReplaceJohnzonDependencies.apply(projectContext);

assertTrue(projectContext
.getBuildFile()
.hasExactDeclaredDependency(Dependency.fromCoordinates(JHONZON_UPDATED_DEPENDENCY))
);
}


@Test
public void givenProjectWithSpringManagedJohnzonDependency_migrate_expectJohnzonDependencyUpdated(){
ProjectContext projectContext = getProjectContextWithoutJohnzonDependency(JAVA_CLASS_WITH_JHONZON_IMPORT);
Boot_27_30_UpgradeReplaceJohnzonDependencies upgradeReplaceJohnzonDependencies = new Boot_27_30_UpgradeReplaceJohnzonDependencies();
upgradeReplaceJohnzonDependencies.apply(projectContext);

assertTrue(projectContext
.getBuildFile()
.hasExactDeclaredDependency(Dependency.fromCoordinates(JHONZON_UPDATED_DEPENDENCY))
);
}

@Test
public void givenProjectWithoutJohnzonImport_checkActionApplicability_expectFalse(){
ProjectContext projectContext = getProjectContextWithoutJohnzonDependency(JAVA_CLASS_WITHOUT_JHONZON_IMPORT);
Boot_27_30_UpgradeReplaceJohnzonDependencies upgradeReplaceJohnzonDependencies = new Boot_27_30_UpgradeReplaceJohnzonDependencies();

assertFalse(upgradeReplaceJohnzonDependencies.isApplicable(projectContext));
}

private ProjectContext getProjectContextWithJohnzonDependency(String... javaSources){
return TestProjectContext.buildProjectContext()
.withBuildFileHavingDependencies(JHONZON_UNSUPPORTED_DEPENDENCY)
.withJavaSources(javaSources)
.build();
}

private ProjectContext getProjectContextWithoutJohnzonDependency(String... javaSources){
return TestProjectContext.buildProjectContext()
.withDummyRootBuildFile()
.withJavaSources(javaSources)
.addRegistrar(new SpringBootApplicationPropertiesRegistrar(new SpringApplicationPropertiesPathMatcher()))
.build();
}
}