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 2 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
@@ -0,0 +1,26 @@
package org.springframework.sbm.boot.upgrade_27_30.actions;

import org.springframework.sbm.boot.upgrade_27_30.conditions.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 JOHNZON_DEPENDENCY_PATTERN = "org\\.apache\\.johnzon\\:johnzon-core\\:.*";
private static final String JOHNZON_DEPENDENCY = "org.apache.johnzon:johnzon-core:1.2.18-jakarta";

@Override
public void apply(ProjectContext context) {
BuildFile buildFile = context.getBuildFile();
buildFile.removeDependenciesMatchingRegex(JOHNZON_DEPENDENCY_PATTERN);
buildFile.addDependency(Dependency.fromCoordinates(JOHNZON_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,33 @@
package org.springframework.sbm.boot.upgrade_27_30.conditions;

import org.springframework.sbm.build.api.ApplicationModule;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.engine.recipe.Condition;

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

private static final String JOHNZON_DEPENDENCY_PATTERN = "org\\.apache\\.johnzon\\:johnzon-core\\:.*";
private static final String JOHNZON_IMPORT = "org.apache.johnzon.core";

@Override
public String getDescription() {
return "Checks if any of the class in the project imports Apache Johnzon classes or the project has declared dependency on Johnzon library";
}

@Override
public boolean evaluate(ProjectContext context) {
boolean importCondition = context.getProjectJavaSources()
.asStream()
.anyMatch(js -> js.hasImportStartingWith(JOHNZON_IMPORT));

boolean dependencyCondition = context.getApplicationModules()
.stream()
.map(ApplicationModule::getBuildFile)
.anyMatch(b -> b.hasDeclaredDependencyMatchingRegex(JOHNZON_DEPENDENCY_PATTERN));

return importCondition || dependencyCondition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.springframework.sbm.boot.upgrade_27_30.actions;

import org.junit.jupiter.api.Test;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.resource.TestProjectContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class Boot_27_30_UpgradeReplaceJohnzonDependenciesTest {

private static final String JOHNZON_UNSUPPORTED_DEPENDENCY = "org.apache.johnzon:johnzon-core:1.2.11";
private static final String JAVA_CLASS_WITH_JOHNZON_IMPORT = "package org.spring.boot.migration;"
+ "import org.apache.johnzon.core.JsonStringImpl;"
+ "public class BootReplaceJohnzonDependencyDummyClass{"
+ "}";

private static final String EXPECTED_POM = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<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" +
" <modelVersion>4.0.0</modelVersion>\n" +
" <groupId>com.example</groupId>\n" +
" <artifactId>dummy-root</artifactId>\n" +
" <version>0.1.0-SNAPSHOT</version>\n" +
" <packaging>jar</packaging>\n" +
" <dependencies>\n" +
" <dependency>\n" +
" <groupId>org.apache.johnzon</groupId>\n" +
" <artifactId>johnzon-core</artifactId>\n" +
" <version>1.2.18-jakarta</version>\n" +
" </dependency>\n" +
" </dependencies>\n" +
"\n" +
"</project>" +
"\n";

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


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

assertThat(projectContext.getBuildFile().print()).isEqualToIgnoringNewLines(EXPECTED_POM);
}


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

assertThat(projectContext.getBuildFile().print()).isEqualToIgnoringNewLines(EXPECTED_POM);
}

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

assertFalse(upgradeReplaceJohnzonDependencies.isApplicable(projectContext));
}

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

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