Skip to content

Commit

Permalink
Po 57 serenity automation for api testing (#85)
Browse files Browse the repository at this point in the history
* implementation of serenity for backend int tests

* Apply smoke/functional tasks for Serenity

* Upgrade jackson

* Checkstyle fixes

* Artifact test reports

* cleaning up unnecessary config and files

---------

Co-authored-by: Dan Lysiak <danlysiak@hotmail.com>
  • Loading branch information
CadeFaulkner and danlysiak authored Dec 13, 2023
1 parent c1a4e1e commit bf6becd
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 140 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
!gradle/wrapper/gradle-wrapper.jar
*.class
bin/main/application.yaml
smoke-test-report
functional-test-report
target

### STS ###
.apt_generated
Expand Down
15 changes: 15 additions & 0 deletions Jenkinsfile_CNP
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,19 @@ def product = "opal"
def component = "fines-service"

withPipeline(type, product, component) {
afterAlways('smokeTest:dev') {
steps.archiveArtifacts allowEmptyArchive: true, artifacts: 'smoke-test-report/**/*'
}

afterAlways('smokeTest:stg') {
steps.archiveArtifacts allowEmptyArchive: true, artifacts: 'smoke-test-report/**/*'
}

afterAlways('functionalTest:dev') {
steps.archiveArtifacts allowEmptyArchive: true, artifacts: 'functional-test-report/**/*'
}

afterAlways('functionalTest:stg') {
steps.archiveArtifacts allowEmptyArchive: true, artifacts: 'functional-test-report/**/*'
}
}
65 changes: 41 additions & 24 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
id 'org.sonarqube' version '4.4.1.3373'
id 'uk.gov.hmcts.java' version '0.12.52'
id 'org.flywaydb.flyway' version '10.2.0'
id 'net.serenity-bdd.serenity-gradle-plugin' version '4.0.27'
}

group = 'uk.gov.hmcts'
Expand Down Expand Up @@ -36,15 +37,6 @@ sourceSets {
}
resources.srcDir file('src/integrationTest/resources')
}

smokeTest {
java {
compileClasspath += main.output
runtimeClasspath += main.output
srcDir file('src/smokeTest/java')
}
resources.srcDir file('src/smokeTest/resources')
}
}

configurations {
Expand Down Expand Up @@ -79,13 +71,6 @@ test {
failFast = true
}

task functional(type: Test) {
description = "Runs functional tests"
group = "Verification"
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
}

task integration(type: Test) {
description = "Runs integration tests"
group = "Verification"
Expand All @@ -94,10 +79,36 @@ task integration(type: Test) {
failFast = true
}

task functional(type: Test) {
description = "Runs functional tests"
group = "Verification"
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
testLogging.showStandardStreams = true
systemProperty "cucumber.filter.tags", "not @Smoke and not @Ignore"
doLast {
tasks.aggregate.aggregate()
delete "${rootDir}/functional-test-report/"
new File("${rootDir}/functional-test-report").mkdirs()
file("${rootDir}/target/site/serenity").renameTo(file("${rootDir}/functional-test-report"))
logger.quiet("Functional Test Report available at - file://${rootDir}/functional-test-report/index.html")
}
}

task smoke(type: Test) {
description = "Runs Smoke Tests"
testClassesDirs = sourceSets.smokeTest.output.classesDirs
classpath = sourceSets.smokeTest.runtimeClasspath
group = "Verification"
testClassesDirs = sourceSets.functionalTest.output.classesDirs
classpath = sourceSets.functionalTest.runtimeClasspath
testLogging.showStandardStreams = true
systemProperty "cucumber.filter.tags", "@Smoke and not @Ignore"
doLast {
tasks.aggregate.aggregate()
delete "${rootDir}/smoke-test-report/"
new File("${rootDir}/smoke-test-report").mkdirs()
file("${rootDir}/target/site/serenity").renameTo(file("${rootDir}/smoke-test-report"))
logger.quiet("Smoke Test Report available at - file://${rootDir}/smoke-test-report/index.html")
}
}

jacocoTestReport {
Expand Down Expand Up @@ -153,10 +164,11 @@ repositories {

ext {
log4JVersion = "2.22.0"
serenityVersion = "4.0.27"
}

ext['snakeyaml.version'] = '2.0'
ext['jackson.version'] = '2.15.3'
ext['jackson.version'] = '2.16.0'
ext['tomcat.version'] = '10.1.16'

dependencies {
Expand Down Expand Up @@ -184,8 +196,8 @@ dependencies {
implementation 'org.flywaydb:flyway-core'
implementation 'org.postgresql:postgresql'

implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.0'

implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.0'
compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'

Expand All @@ -202,6 +214,15 @@ dependencies {
exclude group: 'junit', module: 'junit'
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.junit.platform:junit-platform-suite:1.10.1'
testImplementation 'io.cucumber:cucumber-junit-platform-engine:7.14.0'

testImplementation "net.serenity-bdd:serenity-core:${serenityVersion}"
testImplementation "net.serenity-bdd:serenity-rest-assured:${serenityVersion}"
testImplementation "net.serenity-bdd:serenity-report-resources:${serenityVersion}"

testImplementation "net.serenity-bdd:serenity-cucumber:${serenityVersion}"
testImplementation "net.serenity-bdd:serenity-ensure:${serenityVersion}"
}

application {
Expand All @@ -217,10 +238,6 @@ bootJar {
}

// Gradle 7.x issue, workaround from: https://github.com/gradle/gradle/issues/17236#issuecomment-894768083
rootProject.tasks.named("processSmokeTestResources") {
duplicatesStrategy = 'include'
}

rootProject.tasks.named("processFunctionalTestResources") {
duplicatesStrategy = 'include'
}
Expand Down
18 changes: 0 additions & 18 deletions src/functionalTest/java/uk/gov/hmcts/opal/BaseFunctionalTest.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/functionalTest/java/uk/gov/hmcts/opal/Junit5RunnerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.hmcts.opal;

import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
public class Junit5RunnerTest {

}

This file was deleted.

10 changes: 10 additions & 0 deletions src/functionalTest/java/uk/gov/hmcts/opal/steps/BaseStepDef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.hmcts.opal.steps;

public class BaseStepDef {

private static final String TEST_URL = System.getenv().getOrDefault("TEST_URL", "http://localhost:4550");

protected String getTestUrl() {
return TEST_URL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uk.gov.hmcts.opal.steps;

import io.cucumber.java.PendingException;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import net.serenitybdd.rest.SerenityRest;
import org.hamcrest.Matchers;

public class HealthApiStepDef extends BaseStepDef {

@Then("I check the health of the fines api")
public void checkHealthOfFinesApi() {
System.out.println("Test URL: " + getTestUrl());
SerenityRest.when().get(getTestUrl() + "/health");
SerenityRest.then().assertThat().statusCode(200).and().body("status", Matchers.is("UP"));
}

@And("this test is todo")
public void thisStepIsTodo() {
//TODO step is todo
throw new PendingException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package uk.gov.hmcts.opal.steps;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.When;
import net.serenitybdd.rest.SerenityRest;
import org.hamcrest.Matchers;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import static net.serenitybdd.rest.SerenityRest.then;

public class NotesApiStepDef extends BaseStepDef {

@When("I post the following data to the notes API")
public void postToNotesApi(DataTable notesFields) throws JSONException {
Map<String, String> dataToPost = notesFields.asMap(String.class, String.class);

JSONObject body = new JSONObject();
body.put("associatedRecordId", dataToPost.get("recordId"));
body.put("associatedRecordType", dataToPost.get("recordType"));
body.put("noteText", dataToPost.get("noteText"));
body.put("noteType", dataToPost.get("noteType"));
body.put("postedBy", dataToPost.get("postedBy"));
body.put("postedDate", dataToPost.get("postedDate"));
if (dataToPost.get("noteId") != null) {
body.put("noteId", dataToPost.get("noteId"));
}
SerenityRest.given()
.accept("*/*")
.contentType("application/json")
.body(body.toString())
.when()
.post(getTestUrl() + "/api/notes");
then().assertThat()
.statusCode(201)
.body("associatedRecordId", Matchers.equalTo(dataToPost.get("recordId")))
.body("associatedRecordType", Matchers.equalTo(dataToPost.get("recordType")))
.body("noteText", Matchers.equalTo(dataToPost.get("noteText")))
.body("noteType", Matchers.equalTo(dataToPost.get("noteType")))
.body("postedBy", Matchers.equalTo(dataToPost.get("postedBy")));
//.body("postedDate", equalTo(dataToPost.get("postedDate")));
}

}
Empty file.
4 changes: 4 additions & 0 deletions src/functionalTest/resources/features/healthAPI.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@Smoke
Feature: test the application health API
Scenario: I query the status of the health API
Then I check the health of the fines api
12 changes: 12 additions & 0 deletions src/functionalTest/resources/features/notesAPI.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@Smoke
Feature: Notes API tests
Scenario: I post some data to the Notes API and the response contains the correct Data - null noteID
Given I post the following data to the notes API
|recordId |123123 |
|recordType |def_account |
|noteText |Auto Test Note |
|noteType |NT |
|postedBy |Auto Test |
|postedDate |2023-12-07T11:21:48.677Z |
|noteID | |

6 changes: 6 additions & 0 deletions src/functionalTest/resources/junit-platform.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cucumber.execution.parallel.enabled=true
cucumber.execution.parallel.config.strategy=dynamic

cucumber.plugin=io.cucumber.core.plugin.SerenityReporterParallel,pretty,json:target/cucumber.json,timeline:target/test-results/timeline
cucumber.snippet-type=camelcase
cucumber.publish.enabled=false
4 changes: 4 additions & 0 deletions src/functionalTest/resources/serenity.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
serenity{
project.name=Opal Automation
reports.show.step.details=true
}
18 changes: 0 additions & 18 deletions src/smokeTest/java/uk/gov/hmcts/opal/BaseSmokeTest.java

This file was deleted.

Loading

0 comments on commit bf6becd

Please sign in to comment.