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 integration tests against a generated project #2

Merged
merged 5 commits into from
Jan 17, 2022
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
40 changes: 38 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,47 @@ jobs:
java-version: 11
- name: Build with Gradle
run: ./gradlew build

integration-test:
name: "integration-test (Spring Boot version ${{ matrix.spring-boot-version }})"
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
include:
- spring-boot-version: 2.5.+
experimental: false
- spring-boot-version: 2.6.+
experimental: false
- spring-boot-version: 2.+
experimental: true
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-java@v1
with:
java-version: 11
- name: Run integration test
run: ./gradlew check -p integration-tests -PspringBootVersion=${{ matrix.spring-boot-version }}

publish:
needs:
- build
- integration-test
if: ${{ github.ref == 'refs/heads/main' || startswith(github.ref, 'refs/tags/') }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-java@v1
with:
java-version: 11
- name: Publish
if: ${{ github.ref == 'refs/heads/main' || startswith(github.ref, 'refs/tags/') }}
env:
SIGNING_PRIVATE_KEY: ${{ secrets.MAVEN_CENTRAL_GPG_KEY }}
SIGNING_PASSWORD: ${{ secrets.MAVEN_CENTRAL_GPG_PASSWORD }}
ORG_GRADLE_PROJECT_sonatype_username: ${{ secrets.MAVEN_CENTRAL_USERNAME }}
ORG_GRADLE_PROJECT_sonatype_password: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}
run: ./gradlew publish
run: ./gradlew publish
24 changes: 21 additions & 3 deletions contentcloud-spring-boot-autoconfigure/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
plugins {
id 'java-library'
id 'maven-publish'
}

// apply from: "${rootDir}/gradle/publish.gradle"

repositories {
mavenCentral()
}

configurations {
compileOnly {
extendsFrom(annotationProcessor)
}

testAnnotationProcessor {
extendsFrom(annotationProcessor)
}
}

dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
annotationProcessor platform(project(':contentcloud-spring-boot-platform'))
annotationProcessor 'org.projectlombok:lombok'

implementation platform(project(':contentcloud-spring-boot-platform'))
implementation 'org.springframework.boot:spring-boot-autoconfigure'

compileOnly "com.github.paulcwarren:spring-content-autoconfigure"
compileOnly "com.github.paulcwarren:spring-content-s3"

testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.github.paulcwarren:spring-content-s3-boot-starter'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package eu.xenit.contentcloud.spring.autoconfigure.s3;

import internal.org.springframework.content.s3.boot.autoconfigure.S3ContentAutoConfiguration;
import internal.org.springframework.content.s3.boot.autoconfigure.S3ContentAutoConfiguration.S3Properties;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import software.amazon.awssdk.services.s3.S3Client;

@Configuration
// @AutoConfigureAfter uses a string because the class may not be present on the classpath,
// and we don't want an exception in that case
@AutoConfigureBefore(name="internal.org.springframework.content.s3.boot.autoconfigure.S3ContentAutoConfiguration")
@ConditionalOnClass({S3Client.class, S3ContentAutoConfiguration.class})
@ConditionalOnProperty(
prefix="spring.content.storage.type",
name = "default",
havingValue = "s3",
matchIfMissing=true)
public class S3RegionAutoConfiguration {
@Component
@ConfigurationProperties(prefix = "spring.content.s3")
@Data
public static class S3AdditionalProperties {
private String region;
}

/**
* This component will be registered (and created) before S3Client,
* and is thus able to set a property that is later picked up by the S3Client.
*/
@Component
@RequiredArgsConstructor
static class S3RegionConfigurer implements InitializingBean, DisposableBean {
private final S3Properties s3Properties;
private final S3AdditionalProperties s3AdditionalProperties;

private final static Object AWS_REGION_SENTINEL = new Object();
private final static Object AWS_REGION_NON_EXISTENT = new Object();

private Object previousAwsRegionProperty = AWS_REGION_SENTINEL;

private void replaceRegion(String newRegion) {
if(previousAwsRegionProperty == AWS_REGION_SENTINEL) {
if(System.getProperties().contains("aws.region")) {
previousAwsRegionProperty = System.getProperty("aws.region");
} else {
previousAwsRegionProperty = AWS_REGION_NON_EXISTENT;
}

}
System.setProperty("aws.region", newRegion);
}

@Override
public void afterPropertiesSet() throws Exception {
if(StringUtils.hasText(s3AdditionalProperties.getRegion())) {
replaceRegion(s3AdditionalProperties.getRegion());
} else if(StringUtils.hasText(s3Properties.endpoint)) {
replaceRegion("none");
}
}

@Override
public void destroy() throws Exception {
if (AWS_REGION_NON_EXISTENT.equals(previousAwsRegionProperty)) {
System.getProperties().remove("aws.region");
} else if(previousAwsRegionProperty != AWS_REGION_SENTINEL) {
System.setProperty("aws.region", (String)previousAwsRegionProperty);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
eu.xenit.contentcloud.spring.autoconfigure.s3.S3RegionAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package eu.xenit.contentcloud.spring.autoconfigure.s3;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.parallel.Resources.SYSTEM_PROPERTIES;

import java.util.Arrays;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

class S3RegionAutoConfigurationTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(S3RegionAutoConfiguration.class))
.withUserConfiguration(TestConfig.class);

@SneakyThrows
private static SdkClientConfiguration extractConfiguration(S3Client s3Client) {
Class<? extends S3Client> s3clazz = s3Client.getClass();
var sdkClientConfigField = Arrays.stream(s3clazz.getDeclaredFields())
.filter(field -> field.getType() == SdkClientConfiguration.class)
.findAny()
.get();

sdkClientConfigField.setAccessible(true);
return (SdkClientConfiguration) sdkClientConfigField.get(s3Client);
}

@Test
@ResourceLock(SYSTEM_PROPERTIES)
void setsRegionWhenConfiguredExplicitly() {
contextRunner
.withPropertyValues("spring.content.s3.region=my-region")
.run(context -> {
S3Client client = context.getBean(S3Client.class);

SdkClientConfiguration configuration = extractConfiguration(client);

assertEquals(Region.of("my-region"), configuration.option(AwsClientOption.AWS_REGION));
});
}

@Test
@ResourceLock(SYSTEM_PROPERTIES)
void setsFakeRegionWhenConfiguredWithEndpoint() {
contextRunner
.withPropertyValues("spring.content.s3.endpoint=http://some-endpoint:1234/")
.run(context -> {
S3Client client = context.getBean(S3Client.class);

SdkClientConfiguration configuration = extractConfiguration(client);

assertEquals(Region.of("none"), configuration.option(AwsClientOption.AWS_REGION));
});
}


@Test
@ResourceLock(SYSTEM_PROPERTIES)
void setsNoRegionWhenNotConfigured() {
contextRunner
.run(context -> {
assertThat(context).getFailure().hasRootCauseInstanceOf(SdkClientException.class);
});
}

@Test
@ResourceLock(SYSTEM_PROPERTIES)
void setsNoRegionWhenS3NotOnClasspath() {
contextRunner
.withClassLoader(new FilteredClassLoader(S3Client.class))
.run(context -> {
assertThat(context).hasNotFailed();
assertThat(context).doesNotHaveBean(S3Client.class);
});
}

@Test
@ResourceLock(SYSTEM_PROPERTIES)
void setsNoRegionWhenS3NotEnabled() {
contextRunner
.withPropertyValues("spring.content.storage.type.default=fs")
.run(context -> {
assertThat(context).hasNotFailed();
assertThat(context).doesNotHaveBean(S3Client.class);
});
}


@Configuration
@EnableAutoConfiguration
@AutoConfigurationPackage
public static class TestConfig {

}

}
2 changes: 2 additions & 0 deletions contentcloud-spring-boot-starter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies {

api 'com.querydsl:querydsl-jpa'

implementation project(':contentcloud-spring-boot-autoconfigure')

runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.postgresql:postgresql'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
Expand Down
20 changes: 20 additions & 0 deletions integration-tests/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id 'base'
}

import java.util.stream.Collectors;

def integTestTasks(taskName) {
return gradle.includedBuilds.stream()
.filter(build -> build.name.startsWith('it-'))
.map(build -> build.task(taskName))
.collect(Collectors.toList())
}

check {
dependsOn(integTestTasks(':check'))
}

clean {
dependsOn(integTestTasks(':clean'))
}
36 changes: 36 additions & 0 deletions integration-tests/integration-tests-spring/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
31 changes: 31 additions & 0 deletions integration-tests/integration-tests-spring/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
plugins {
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'eu.xenit.contentcloud.userapps.holmes'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
implementation 'eu.xenit.contentcloud.starter:contentcloud-spring-boot-starter:0.1.0-SNAPSHOT'
annotationProcessor 'eu.xenit.contentcloud.starter:contentcloud-spring-boot-starter-annotations:0.1.0-SNAPSHOT'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
useJUnitPlatform()
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading