Skip to content
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

add block hound validations to reactive projects #353

Merged
merged 2 commits into from
May 4, 2023
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
1 change: 1 addition & 0 deletions src/main/java/co/com/bancolombia/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Constants {
public static final String COBERTURA_VERSION = "4.0.0";
public static final String RCOMMONS_ASYNC_COMMONS_STARTER_VERSION = "2.0.0";
public static final String RCOMMONS_OBJECT_MAPPER_VERSION = "0.1.0";
public static final String BLOCK_HOUND_VERSION = "1.0.8.RELEASE";
public static final String PLUGIN_VERSION = "3.0.3";
public static final String GRADLE_WRAPPER_VERSION = "7.6.1";
public static final String KOTLIN_VERSION = "1.6.10";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private void initialize() {
params.put("commonsJmsVersion", Constants.COMMONS_JMS_VERSION);
params.put("graphqlKickStartVersion", Constants.GRAPHQL_KICKSTART_VERSION);
params.put("secretsVersion", Constants.SECRETS_VERSION);
params.put("blockHoundVersion", Constants.BLOCK_HOUND_VERSION);
loadPackage();
loadLanguage();
loadIsExample();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package co.com.bancolombia.factory.upgrades.actions;

import co.com.bancolombia.Constants;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.upgrades.UpdateUtils;
import co.com.bancolombia.factory.upgrades.UpgradeAction;
import lombok.SneakyThrows;

public class UpgradeY2023M05D04BlockHound implements UpgradeAction {
private static final String CHECK = "org.junit.platform:junit-platform-launcher";
private static final String MATCH = "compileOnly \"org.projectlombok:lombok";
private static final String APPEND =
"testImplementation 'io.projectreactor.tools:blockhound-junit-platform:"
+ Constants.BLOCK_HOUND_VERSION
+ "'\n"
+ " testRuntimeOnly 'org.junit.platform:junit-platform-launcher'\n\n ";

private static final String CHECK_ARG = "AllowRedefinitionToAddDeleteMethods";
private static final String MATCH_ARG = "test.finalizedBy";
private static final String APPEND_ARG =
"tasks.withType(Test).configureEach {\n"
+ " if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_13)) {\n"
+ " jvmArgs += [\n"
+ " \"-XX:+AllowRedefinitionToAddDeleteMethods\"\n"
+ " ]\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " ";

@Override
@SneakyThrows
public boolean up(ModuleBuilder builder) {
if (builder.isReactive()) {
return builder.updateFile(
Constants.MainFiles.MAIN_GRADLE,
content -> {
String partial = UpdateUtils.appendValidate(content, MATCH, CHECK, APPEND);
return UpdateUtils.appendValidate(partial, MATCH_ARG, CHECK_ARG, APPEND_ARG);
});
} else {
return false;
}
}

@Override
public String name() {
return "3.0.3->3.0.4";
}

@Override
public String description() {
return "Add block hound validations";
}
}
13 changes: 13 additions & 0 deletions src/main/resources/structure/root/main.gradle.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ subprojects {
implementation 'io.projectreactor:reactor-core'
implementation 'io.projectreactor.addons:reactor-extra'

testImplementation 'io.projectreactor.tools:blockhound-junit-platform:{{blockHoundVersion}}'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

testImplementation 'io.projectreactor:reactor-test'
{{/reactive}}
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand All @@ -38,6 +41,16 @@ subprojects {
implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
}

{{#reactive}}
tasks.withType(Test).configureEach {
if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_13)) {
jvmArgs += [
"-XX:+AllowRedefinitionToAddDeleteMethods"
]
}
}
{{/reactive}}

{{#jacoco}}
test.finalizedBy(project.tasks.jacocoTestReport)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package co.com.bancolombia.factory.upgrades.actions;

import static org.gradle.internal.impldep.org.testng.Assert.assertNotNull;
import static org.gradle.internal.impldep.org.testng.Assert.assertTrue;
import static org.mockito.Mockito.*;

import co.com.bancolombia.Constants;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.upgrades.UpgradeAction;
import co.com.bancolombia.utils.FileUtils;
import com.github.mustachejava.resolver.DefaultResolver;
import java.io.IOException;
import java.nio.file.Files;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class UpgradeY2023M05D04BlockHoundTest {
@Mock private Project project;
@Mock private Logger logger;
private ModuleBuilder builder;
private UpgradeAction updater;

@Before
public void setup() throws IOException {
when(project.getName()).thenReturn("UtilsTest");
when(project.getLogger()).thenReturn(logger);
when(project.getProjectDir()).thenReturn(Files.createTempDirectory("sample").toFile());
builder = spy(new ModuleBuilder(project));
doReturn(true).when(builder).isReactive();
updater = new UpgradeY2023M05D04BlockHound();
assertNotNull(updater.name());
assertNotNull(updater.description());
}

@Test
public void shouldApplyUpdate() throws IOException {
String file = Constants.MainFiles.MAIN_GRADLE;
// Arrange
DefaultResolver resolver = new DefaultResolver();
String text = FileUtils.getResourceAsString(resolver, "blockhound/before.txt");
String expectedText = FileUtils.getResourceAsString(resolver, "blockhound/after.txt");
builder.addFile(file, text);
// Act
boolean applied = updater.up(builder);
// Assert
assertTrue(applied);
verify(builder, times(1)).addFile(file, expectedText);
}
}
84 changes: 84 additions & 0 deletions src/test/resources/blockhound/after.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
allprojects {
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
}

subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'io.spring.dependency-management'

compileJava.dependsOn validateStructure
sourceCompatibility = JavaVersion.VERSION_17
test {
useJUnitPlatform()
}
dependencies {
implementation 'io.projectreactor:reactor-core'
implementation 'io.projectreactor.addons:reactor-extra'

testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

testImplementation 'io.projectreactor.tools:blockhound-junit-platform:1.0.8.RELEASE'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

compileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"
implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
}

tasks.withType(Test).configureEach {
if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_13)) {
jvmArgs += [
"-XX:+AllowRedefinitionToAddDeleteMethods"
]
}
}

test.finalizedBy(project.tasks.jacocoTestReport)

jacocoTestReport {
dependsOn test
reports {
xml.setRequired true
xml.setOutputLocation file("${buildDir}/reports/jacoco.xml")
csv.setRequired false
html.setOutputLocation file("${buildDir}/reports/jacocoHtml")
}
}

}

jacoco {
toolVersion = "${jacocoVersion}"
reportsDirectory = file("$buildDir/reports")
}

tasks.register('jacocoMergedReport', JacocoReport) {
dependsOn = subprojects.jacocoTestReport
additionalSourceDirs.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.setFrom files(subprojects.sourceSets.main.output)
executionData.setFrom project.fileTree(dir: '.', include: '**/build/jacoco/test.exec')
reports {
xml.setRequired true
csv.setRequired false
html.setRequired true
}
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs = [
'-Amapstruct.suppressGeneratorTimestamp=true'
]
}

tasks.named('wrapper') {
gradleVersion = '7.6.1'
}
73 changes: 73 additions & 0 deletions src/test/resources/blockhound/before.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
allprojects {
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
}

subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'io.spring.dependency-management'

compileJava.dependsOn validateStructure
sourceCompatibility = JavaVersion.VERSION_17
test {
useJUnitPlatform()
}
dependencies {
implementation 'io.projectreactor:reactor-core'
implementation 'io.projectreactor.addons:reactor-extra'

testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

compileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"
implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
}

test.finalizedBy(project.tasks.jacocoTestReport)

jacocoTestReport {
dependsOn test
reports {
xml.setRequired true
xml.setOutputLocation file("${buildDir}/reports/jacoco.xml")
csv.setRequired false
html.setOutputLocation file("${buildDir}/reports/jacocoHtml")
}
}

}

jacoco {
toolVersion = "${jacocoVersion}"
reportsDirectory = file("$buildDir/reports")
}

tasks.register('jacocoMergedReport', JacocoReport) {
dependsOn = subprojects.jacocoTestReport
additionalSourceDirs.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.setFrom files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.setFrom files(subprojects.sourceSets.main.output)
executionData.setFrom project.fileTree(dir: '.', include: '**/build/jacoco/test.exec')
reports {
xml.setRequired true
csv.setRequired false
html.setRequired true
}
}

tasks.withType(JavaCompile).configureEach {
options.compilerArgs = [
'-Amapstruct.suppressGeneratorTimestamp=true'
]
}

tasks.named('wrapper') {
gradleVersion = '7.6.1'
}