Skip to content

Commit 16a3240

Browse files
authored
CFTS-6684 :: Added swagger end point tests (#624)
* bumped test-definitions and befta-fw to latest * addition of swagger FTs and fix for git workflow action * Update swagger.yml * fix typo * fix param * updated swagger json * updated to java 17 swagger tests * updated Swagger api details * updated spec * updated spec with actual results * empty commit to trigger build * empty commit to trigger build * empty commit to trigger build * Update S-125.2.td.json Fixed typo * Update S-125.1.td.json Fix body null issue * Update S-125.1.td.json revert * Update UserProfileTestAutomationAdapter.java Modified to calculate scenarioContext * Update F-125_Test_Swagger_UI_Base_Data.td.json Test * Update build.gradle bumped befta * Added missing code for contains * Update swagger.yml
1 parent f9ef9b0 commit 16a3240

File tree

10 files changed

+165
-10
lines changed

10 files changed

+165
-10
lines changed

.github/workflows/swagger.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ jobs:
2323
java-version: 21
2424
distribution: 'temurin'
2525
- name: Run Swagger Publisher
26-
run: ./gradlew test --tests uk.gov.hmcts.ccd.swagger.SwaggerPublisher
26+
run: ./gradlew integrationTest --tests uk.gov.hmcts.ccd.swagger.OpenApiPublisher
2727
- name: Commit to repository
2828
run: |
2929
mkdir swagger-staging
3030
cd swagger-staging
3131
git init
3232
git config user.email "[email protected]"
3333
git config user.name "GitHub action"
34-
git remote add upstream https://${{ secrets.GH_TOKEN }}@github.com/hmcts/cnp-api-docs.git
34+
git remote add upstream "https://jenkins-reform-hmcts:${{ secrets.SWAGGER_PUBLISHER_API_TOKEN }}@github.com/hmcts/cnp-api-docs.git"
3535
git pull upstream master
3636
repo=`echo "$GITHUB_REPOSITORY" | cut -f2- -d/`
3737
echo "$(cat /tmp/swagger-specs.json)" > "docs/specs/$repo.json"

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ dependencies {
191191
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
192192

193193
testImplementation group: 'com.github.hmcts', name: 'ccd-test-definitions', version: '7.24.2'
194-
testImplementation group: 'com.github.hmcts', name: 'befta-fw', version: '9.2.0'
194+
testImplementation group: 'com.github.hmcts', name: 'befta-fw', version: '9.2.3'
195195
testImplementation 'com.github.hmcts:fortify-client:1.4.8:all'
196196
testImplementation 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.9.11'
197197
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
11
package uk.gov.hmcts.ccd.userprofile.befta;
22

33
import uk.gov.hmcts.befta.DefaultTestAutomationAdapter;
4+
import uk.gov.hmcts.befta.exception.FunctionalTestException;
5+
import uk.gov.hmcts.befta.player.BackEndFunctionalTestScenarioContext;
6+
import uk.gov.hmcts.befta.util.ReflectionUtils;
47

58
public class UserProfileTestAutomationAdapter extends DefaultTestAutomationAdapter {
69

10+
@Override
11+
public synchronized Object calculateCustomValue(BackEndFunctionalTestScenarioContext scenarioContext, Object key) {
12+
if (key.toString().startsWith("no_dynamic_injection_")) {
13+
return key.toString().replace("no_dynamic_injection_","");
14+
} else if (key.toString().startsWith("approximately ")) {
15+
try {
16+
String actualSizeFromHeaderStr = (String) ReflectionUtils.deepGetFieldInObject(scenarioContext,
17+
"testData.actualResponse.headers.Content-Length");
18+
String expectedSizeStr = key.toString().replace("approximately ", "");
19+
20+
int actualSize = Integer.parseInt(actualSizeFromHeaderStr);
21+
int expectedSize = Integer.parseInt(expectedSizeStr);
22+
23+
if (Math.abs(actualSize - expectedSize) < (actualSize * 10 / 100)) {
24+
return actualSizeFromHeaderStr;
25+
}
26+
return expectedSize;
27+
} catch (Exception e) {
28+
throw new FunctionalTestException("Problem checking acceptable response payload: ", e);
29+
}
30+
} else if (key.toString().startsWith("contains ")) {
31+
try {
32+
String actualValueStr = (String) ReflectionUtils.deepGetFieldInObject(scenarioContext,
33+
"testData.actualResponse.body.__plainTextValue__");
34+
String expectedValueStr = key.toString().replace("contains ", "");
35+
36+
if (actualValueStr.contains(expectedValueStr)) {
37+
return actualValueStr;
38+
}
39+
return "expectedValueStr " + expectedValueStr + " not present in response ";
40+
} catch (Exception e) {
41+
throw new FunctionalTestException("Problem checking acceptable response payload: ", e);
42+
}
43+
}
44+
return super.calculateCustomValue(scenarioContext, key);
45+
}
746
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#===============================================
2+
@F-125
3+
Feature: F-125: Swagger Pages and Open API Specs
4+
#===============================================
5+
6+
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7+
@S-125.1
8+
Scenario: must show Swagger UI page APIs
9+
10+
Given an appropriate test context as detailed in the test data source,
11+
12+
When a request is prepared with appropriate values,
13+
And it is submitted to call the [Get Swagger UI Page] operation of [CCD User Profile],
14+
15+
Then a positive response is received,
16+
And the response has all the details as expected
17+
And a call [to observe the swagger UI content] will get the expected response as in [S-125.1].
18+
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
19+
@S-125.2
20+
Scenario: must show Swagger Json APIs
21+
22+
Given an appropriate test context as detailed in the test data source,
23+
24+
When a request is prepared with appropriate values,
25+
And it is submitted to call the [Get Swagger Json API] operation of [CCD User Profile],
26+
27+
Then a positive response is received,
28+
And the response has all the details as expected
29+
And a call [to observe the swagger json content] will get the expected response as in [S-125.2].
30+
31+
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"_guid_": "F-125_Test_Swagger_JSON_Base_Data",
3+
"title": "Verify swagger JSON Content",
4+
5+
"productName": "CCD User Profile",
6+
"operationName": "Get Swagger Json API",
7+
8+
"method": "GET",
9+
"uri": "https://hmcts.github.io/cnp-api-docs/specs/{ccd-user-profile-api-version}",
10+
11+
"expectedResponse": {
12+
"headers" : {
13+
"Content-Encoding" : "gzip",
14+
"Content-Length" : "${[scenarioContext][customValues][approximately 5838]}",
15+
"Content-Type" : "application/json; charset=utf-8",
16+
"Date" : "[[ANYTHING_PRESENT]]",
17+
"Expires" : "[[ANYTHING_PRESENT]]",
18+
"Last-Modified" : "[[ANYTHING_PRESENT]]",
19+
"Server" : "GitHub.com"
20+
}
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"_guid_": "F-125_Test_Swagger_UI_Base_Data",
3+
"title": "Verify swagger UI Content",
4+
5+
"productName": "CCD User Profile",
6+
"operationName": "Get Swagger UI Page",
7+
8+
"method": "GET",
9+
"uri": "/swagger-ui/index.html",
10+
11+
"expectedResponse": {
12+
"responseCode" : 200,
13+
"responseMessage" : "OK",
14+
"headers" : {
15+
"Accept-Ranges" : "bytes",
16+
"Content-Encoding" : "gzip",
17+
"Content-Length" : "${[scenarioContext][customValues][approximately 1190]}",
18+
"Content-Type" : "text/html"
19+
},
20+
"body" : {
21+
"__plainTextValue__": "${[scenarioContext][customValues][contains Swagger UI]}"
22+
}
23+
}
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"_guid_": "S-125.1",
3+
"title": "must show Swagger UI page for APIs",
4+
"_extends_": "F-125_Test_Swagger_UI_Base_Data",
5+
6+
"specs": [
7+
"to observe the swagger UI content"
8+
],
9+
10+
"request" :{
11+
}
12+
}
13+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"_guid_": "S-125.2",
3+
"title": "Must return a positive appropriate response when Swagger JSON URL is accessed",
4+
"_extends_": "F-125_Test_Swagger_JSON_Base_Data",
5+
6+
"specs": [
7+
"to observe the swagger json content"
8+
],
9+
10+
"request" :{
11+
"pathVariables" : {
12+
"ccd-user-profile-api-version": "ccd-user-profile-api.json"
13+
}
14+
},
15+
16+
"expectedResponse": {
17+
"responseCode" : 200,
18+
"responseMessage" : "OK",
19+
"body" : {
20+
"openapi" : "[[ANYTHING_PRESENT]]",
21+
"servers" : "[[ANYTHING_PRESENT]]",
22+
"components" : "[[ANYTHING_PRESENT]]",
23+
"paths": "[[ANYTHING_PRESENT]]",
24+
"info": {
25+
"description": "API to store and retrieve user profile data.",
26+
"title": "User Profile API",
27+
"version": "[[ANYTHING_PRESENT]]"
28+
}
29+
}
30+
}
31+
}

src/integrationTest/java/uk/gov/hmcts/ccd/BaseTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import org.junit.BeforeClass;
65
import org.junit.jupiter.api.BeforeAll;
76
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -47,10 +46,6 @@ static void setProperties(DynamicPropertyRegistry registry) {
4746
@BeforeAll
4847
public static void beforeAll() {
4948
postgresqlContainer.start();
50-
}
51-
52-
@BeforeClass
53-
public static void init() {
5449
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
5550
}
5651

src/main/java/uk/gov/hmcts/ccd/OpenApiConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class OpenApiConfig {
1111
@Bean
1212
public OpenAPI api() {
1313
return new OpenAPI()
14-
.info(new Info().title("SpringShop API")
15-
.description("Spring shop sample application")
14+
.info(new Info().title("User Profile API")
15+
.description("API to store and retrieve user profile data.")
1616
.version("v0.0.1")
1717
);
1818
}

0 commit comments

Comments
 (0)