Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Schilling committed Jun 20, 2024
1 parent 4477c2a commit 9372059
Show file tree
Hide file tree
Showing 70 changed files with 2,777 additions and 2,295 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@ env:
JAVA_VERSION: 21

jobs:
test:
unit-test:
runs-on: [ ubuntu-latest ]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.version }}
- uses: actions/setup-java@v4
with:
cache: gradle
distribution: ${{ env.JAVA_DISTRIBUTION }}
java-version: ${{ env.JAVA_VERSION }}
- run: ./gradlew clean test spotlessCheck --exclude-task :chucknorris-integration-test:test

integration-test:
runs-on: [ ubuntu-latest ]
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.version }}
- run: ./gradlew test spotlessCheck
- uses: actions/setup-java@v4
with:
cache: gradle
distribution: ${{ env.JAVA_DISTRIBUTION }}
java-version: ${{ env.JAVA_VERSION }}
- name: Run Integration Test
shell: bash
run: ./chucknorris-integration-test/script/run-integration-test.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ out/
hs_err_pid*

# Gradle
bin/
.gradle
build/
gradle-app.setting
Expand Down
1 change: 1 addition & 0 deletions .sdkmanrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java=17.0.11-zulu
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ buildscript {
}
}

version = Git.commitHash()

subprojects { Project project ->
apply plugin: 'com.diffplug.spotless'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'

sourceCompatibility = 17
targetCompatibility = 17
version Git.commitHash()

dependencyManagement {
imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${Version.SPRING_BOOT}") }
Expand All @@ -39,7 +36,7 @@ subprojects { Project project ->

spotless {
java {
googleJavaFormat()
eclipse().configFile('../format.xml')
}
}
}
5 changes: 4 additions & 1 deletion buildSrc/src/main/groovy/Version.groovy
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
interface Version {
public static final String SPRING_BOOT = '2.7.18'
public static final String LOMBOK = '1.18.30'
public static final String JUNIT = '5.10.2'
public static final String REST_ASSURED = '5.4.0'
public static final String SPOTLESS = '7.0.0.BETA1'
public static final String SPRING_BOOT = '2.7.18'
}
12 changes: 12 additions & 0 deletions chucknorris-integration-test/build.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
dependencies {
testImplementation 'commons-logging:commons-logging:1.3.2'

testImplementation "org.junit.jupiter:junit-jupiter-api:${Version.JUNIT}"
testImplementation "org.junit.jupiter:junit-jupiter:${Version.JUNIT}"
testImplementation("io.rest-assured:json-path:${Version.REST_ASSURED}") {
exclude group: "org.apache.groovy", module: "groovy"
exclude group: "org.apache.groovy", module: "groovy-xml"
}
testImplementation("io.rest-assured:rest-assured:${Version.REST_ASSURED}") {
exclude group: "org.apache.groovy", module: "groovy"
exclude group: "org.apache.groovy", module: "groovy-xml"
}
}
26 changes: 23 additions & 3 deletions chucknorris-integration-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ services:
image: chucknorrisio/postgres
container_name: chucknorris-database
healthcheck:
interval: 30s
interval: 5s
retries: 5
start_period: 80s
start_period: 20s
test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ]
timeout: 60s
ports:
- '5432:5432'

service:
test-subject:
environment:
- APPLICATION_EVENT_SNS_TOPIC_ARN=my-application-event-sns-topic-arn
- AWS_ACCESS_KEY_ID=my-aws-access-key-id
Expand All @@ -34,5 +34,25 @@ services:
depends_on:
database:
condition: service_healthy
healthcheck:
interval: 5s
retries: 5
start_period: 20s
test: wget --no-verbose --tries=1 --spider http://localhost:8080 || exit 1
timeout: 60s
ports:
- '8080:8080'

integration-test:
command: "./gradlew :chucknorris-integration-test:test --stacktrace"
container_name: "integration-test"
depends_on:
test-subject:
condition: service_healthy
environment:
- TEST_SUBJECT_BASE_URI=http://test-subject
- TEST_SUBJECT_PORT=8080
image: "azul/zulu-openjdk-alpine:21-jre-headless-latest"
volumes:
- "../:/home/gradle/project"
working_dir: /home/gradle/project
14 changes: 5 additions & 9 deletions chucknorris-integration-test/script/run-integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ readonly SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
readonly MODULE_DIR=$(cd "${SCRIPT_DIR}/.." && pwd)
readonly PROJECT_DIR=$(cd "${SCRIPT_DIR}/../.." && pwd)

./gradlew --build-file "$PROJECT_DIR/build.gradle" clean dockerTagCurrent
./gradlew --build-file "$PROJECT_DIR/chucknorris-web/build.gradle" clean docker

# docker-compose --file "$MODULE_DIR/docker-compose.yml" up \
# --exit-code-from "integration-test" \
# --quiet-pull \
# --remove-orphans

docker-compose --file "$MODULE_DIR/docker-compose.yml" up \
--quiet-pull \
--remove-orphans
docker-compose --file "$MODULE_DIR/docker-compose.yml" up \
--exit-code-from "integration-test" \
--quiet-pull \
--remove-orphans
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.chucknorris.integration_test;

import io.restassured.RestAssured;
import org.junit.jupiter.api.BeforeAll;

import java.util.Optional;

public abstract class AbstractEndpointTest {

@BeforeAll
public static void beforeAll() {
RestAssured.baseURI = Optional
.ofNullable(System.getenv("TEST_SUBJECT_BASE_URI"))
.orElse("http://localhost");

RestAssured.port = Optional
.ofNullable(System.getenv("TEST_SUBJECT_PORT"))
.map(Integer::parseInt)
.orElse(8080);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.chucknorris.integration_test.health;

import io.chucknorris.integration_test.AbstractEndpointTest;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

class HealthEndpointTest extends AbstractEndpointTest {

@DisplayName("Should return joke")
@Test
void shouldReturnJoke() {
var specification = given()
.log().all()
.accept("application/json");

var response = specification.when().get("/health");

response.then()
.log().all()
.statusCode(200)
.assertThat()
.header("Content-Type", Matchers.equalTo("application/json"))
.body("status", Matchers.is("UP"))
.body("start_time", Matchers.isA(String.class))
.body("uptime", Matchers.isA(String.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.chucknorris.integration_test.joke;

import io.chucknorris.integration_test.AbstractEndpointTest;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static io.restassured.RestAssured.given;

class JokeRandomEndpointTest extends AbstractEndpointTest {

@DisplayName("Should return joke as json")
@Test
void shouldReturnJokeAsJson() {
// given:
var specification = given()
.log().all()
.accept("application/json");

// when:
var response = specification.when().get("/jokes/random");

// then:
response.then()
.log().all()
.statusCode(200)
.assertThat()
.header("Content-Type", Matchers.equalTo("application/json"))
.body("categories", Matchers.isA(List.class))
.body("created_at", Matchers.isA(String.class))
.body("icon_url", Matchers.isA(String.class))
.body("id", Matchers.isA(String.class))
.body("updated_at", Matchers.isA(String.class))
.body("url", Matchers.isA(String.class))
.body("value", Matchers.isA(String.class));
}

@DisplayName("Should include CORS headers")
@Test
void shouldIncludeCORSHeaders() {
// given:
var specification = given()
.log().all()
.accept("application/json")
.header("Origin", "http://localhost:3000");

// when:
var response = specification.when().get("/jokes/random");

// then:
response.then()
.log().all()
.statusCode(200)
.assertThat()
.header("Access-Control-Allow-Origin", Matchers.equalTo("*"))
.header("Access-Control-Expose-Headers", Matchers.equalTo("Access-Control-Allow-Origin Access-Control-Allow-Credentials"))
.header("Content-Type", Matchers.equalTo("application/json"));
}
}
29 changes: 4 additions & 25 deletions chucknorris-web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ plugins {
id 'org.springframework.boot'
}

repositories {
mavenCentral()
}

// versions
def lombokVersion = "1.18.30"

dependencies {
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${Version.LOMBOK}"

implementation "com.amazonaws:aws-java-sdk:1.11.561"
implementation "com.fasterxml.jackson.core:jackson-core:2.17.1"
Expand All @@ -20,7 +13,7 @@ dependencies {
implementation "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.3.0"
implementation "org.hibernate:hibernate-validator:6.0.16.Final"
implementation "org.postgresql:postgresql:42.2.9"
implementation "org.projectlombok:lombok:${lombokVersion}"
implementation "org.projectlombok:lombok:${Version.LOMBOK}"
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
implementation "org.springframework.boot:spring-boot-starter-web"
Expand All @@ -35,7 +28,6 @@ tasks {
manifest.attributes("Multi-Release": "true")

archiveBaseName.set(project.name)
archiveVersion.set(project.version)

if (project.hasProperty("archiveName")) {
archiveFileName.set(project.properties["archiveName"] as String)
Expand All @@ -45,25 +37,12 @@ tasks {
docker {
dependsOn bootJar

def imageName = "${project.properties.group}/${project.name}"
name = "${imageName}:latest"
name = "${project.properties.group}/${project.name}"

tag("current", "${imageName}:${project.version}")
tag("latest", "${imageName}:latest")
tag("herokuProduction", "registry.heroku.com/chucky/web")

dockerfile file("${projectDir}/src/main/docker/Dockerfile")
files tasks.bootJar.outputs
buildArgs([JAR_FILE: bootJar.getArchiveFileName().get()])
}

springBoot {
buildInfo {
properties {
artifact = "${project.name}-${project.version}.jar"
version = project.version
name = project.name
}
}
buildArgs([JAR_FILE: bootJar.getArchiveFileName().get()] as Map<String, String>)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@ComponentScan(basePackages = {"io.chucknorris"})
@ComponentScan(basePackages = { "io.chucknorris" })
@EnableJpaAuditing
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Loading

0 comments on commit 9372059

Please sign in to comment.