generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tests for PO-926 * tests for PO-926 wip * tests for PO-926 wip * checkstyle * small test update * adding tests for PO-926 * adding more tests for PO-926 - inactive offences, no results * checkstyle * checkstyle * checkstyle * removing not required tag
- Loading branch information
1 parent
25a574a
commit 2008328
Showing
4 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/functionalTest/java/uk/gov/hmcts/opal/steps/offences/SearchOffencesRequestStepDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package uk.gov.hmcts.opal.steps.offences; | ||
|
||
import io.cucumber.datatable.DataTable; | ||
import io.cucumber.java.en.When; | ||
import net.serenitybdd.rest.SerenityRest; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import uk.gov.hmcts.opal.steps.BaseStepDef; | ||
|
||
import java.util.Map; | ||
|
||
import static uk.gov.hmcts.opal.config.Constants.OFFENCES_SEARCH_URI; | ||
import static uk.gov.hmcts.opal.steps.BearerTokenStepDef.getToken; | ||
|
||
public class SearchOffencesRequestStepDef extends BaseStepDef { | ||
@When("I make a request to the offence search api filtering by") | ||
public void postOffencesSearchRequest(DataTable filters) throws JSONException { | ||
Map<String, String> dataToPost = filters.asMap(String.class, String.class); | ||
JSONObject requestBody = new JSONObject(); | ||
requestBody.put("cjs_code", dataToPost.get("cjs_code") != null ? dataToPost.get("cjs_code") : ""); | ||
requestBody.put("title", dataToPost.get("title") != null ? dataToPost.get("title") : ""); | ||
requestBody.put("act_section", dataToPost.get("act_section") != null ? dataToPost.get("act_section") : ""); | ||
requestBody.put("active_date", dataToPost.get("active_date") != null ? dataToPost.get("active_date") : ""); | ||
requestBody.put("max_results", dataToPost.get("max_results") != null ? dataToPost.get("max_results") : "100"); | ||
|
||
SerenityRest | ||
.given() | ||
.header("Authorization", "Bearer " + getToken()) | ||
.accept("*/*") | ||
.contentType("application/json") | ||
.body(requestBody.toString()) | ||
.when() | ||
.post(getTestUrl() + OFFENCES_SEARCH_URI); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/functionalTest/java/uk/gov/hmcts/opal/steps/offences/SearchOffencesResponseStepDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package uk.gov.hmcts.opal.steps.offences; | ||
|
||
import io.cucumber.datatable.DataTable; | ||
import io.cucumber.java.en.Then; | ||
import net.serenitybdd.rest.SerenityRest; | ||
import uk.gov.hmcts.opal.steps.BaseStepDef; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static net.serenitybdd.rest.SerenityRest.then; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class SearchOffencesResponseStepDef extends BaseStepDef { | ||
//static Logger log = LoggerFactory.getLogger(SearchOffencesResponseStepDef.class.getName()); | ||
|
||
@Then("The offence search response returns {int}") | ||
public void draftAccountResponse(int statusCode) { | ||
then().assertThat() | ||
.statusCode(statusCode); | ||
} | ||
|
||
@Then("the response contains results with a cjs code starting with {string}") | ||
public void offenceResponseCjsStartsWith(String cjsCode) { | ||
|
||
List<String> cjsCodes = SerenityRest.then().extract().jsonPath().getList("searchData.cjs_code"); | ||
//log.info("CJS Codes: {}", cjsCodes); | ||
|
||
cjsCodes.forEach(code -> { | ||
//log.info("Checking CJS Code: {}", code); | ||
//log.info("CJS Code starts with: {}", cjsCode); | ||
assertTrue(code.startsWith(cjsCode)); | ||
}); | ||
} | ||
|
||
@Then("the offences in the response contain the following data") | ||
public void offenceResponseContainsData(DataTable expectedData) { | ||
Map<String, String> expectedDataMap = expectedData.asMap(String.class, String.class); | ||
int count = SerenityRest.then().extract().jsonPath().getInt("count"); | ||
|
||
for (int i = 0; i < count; i++) { | ||
for (String key : expectedDataMap.keySet()) { | ||
String actual = SerenityRest.then().extract().path("searchData[" + i + "]." + key); | ||
assertTrue(actual.contains(expectedDataMap.get(key) | ||
), "Values are not equal: " + key + " - " + actual + " - " + expectedDataMap.get(key)); | ||
} | ||
} | ||
} | ||
|
||
@Then("the offences in the response are before {string} only") | ||
public void offenceResponseBeforeDate(String activeDate) { | ||
// Format the active date string to a LocalDateTime object | ||
DateTimeFormatter activeDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
LocalDateTime parsedActiveDate = LocalDateTime.parse(activeDate, activeDateFormatter); | ||
|
||
// Extract the list of dates from the response | ||
List<String> usedFromDates = SerenityRest.then().extract().jsonPath().getList("searchData.date_used_from"); | ||
List<String> usedToDates = SerenityRest.then().extract().jsonPath().getList("searchData.date_used_to"); | ||
|
||
// Iterate through each date in the response | ||
for (String dateFromResponse : usedFromDates) { | ||
// Parse the date from the response to a LocalDateTime object | ||
LocalDateTime parsedDateFromResponse = LocalDateTime.parse(dateFromResponse); | ||
// Assert that the date from the response is before the active date | ||
assertTrue(parsedDateFromResponse.isBefore(parsedActiveDate), | ||
"Response date is not before Active date: " | ||
+ "\n Date from response: " + parsedDateFromResponse | ||
+ "\n Active Date: " + parsedActiveDate); | ||
} | ||
for (String dateFromResponse : usedToDates) { | ||
// Parse the date from the response to a LocalDateTime object | ||
LocalDateTime parsedDateFromResponse = LocalDateTime.parse(dateFromResponse); | ||
// Assert that the active date is before the used to date from the response | ||
assertTrue(parsedActiveDate.isBefore(parsedDateFromResponse), | ||
"Active date is not before Response date: " | ||
+ "\n Date from response: " + parsedDateFromResponse | ||
+ "\n Active Date: " + parsedActiveDate); | ||
} | ||
} | ||
|
||
@Then("there are {int} offences in the response") | ||
public void offenceResponseContainsCount(int count) { | ||
int responseCount = then().extract().jsonPath().getInt("count"); | ||
int cjsCodes = SerenityRest.then().extract().jsonPath().getList("searchData.cjs_code").size(); | ||
assertEquals(responseCount, count, "Expected count: " + count + " Actual count: " + responseCount); | ||
assertEquals(cjsCodes, count, "Expected count: " + count + " Actual count: " + cjsCodes); | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...sources/features/opalMode/manualAccountCreation/offences/PO-926_offencesSearchApi.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
@Opal @PO-926 | ||
Feature: PO-926 Offences Search API | ||
|
||
Scenario: Offence Search API - Search by CJS code | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | TH68002 | | ||
| title | | | ||
| act_section | | | ||
| active_date | | | ||
| max_results | | | ||
|
||
Then The offence search response returns 200 | ||
And the response contains results with a cjs code starting with "TH68002" | ||
And the offences in the response contain the following data | ||
| cjs_code | TH68002 | | ||
| offence_title | in dwelling | | ||
|
||
Scenario: Offence Search API - Search Title | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | | | ||
| title | in dwelling other | | ||
| act_section | | | ||
| active_date | | | ||
| max_results | | | ||
|
||
Then The offence search response returns 200 | ||
And the offences in the response contain the following data | ||
| offence_title | in dwelling other | | ||
|
||
Scenario: Offence Search API - Search by Act and Section | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | | | ||
| title | | | ||
| act_section | Contrary to section 1(1) and (5) of the Aviation and Maritime Security Act 1990. | | ||
| active_date | | | ||
| max_results | | | ||
|
||
Then The offence search response returns 200 | ||
And the offences in the response contain the following data | ||
| offence_oas | Contrary to section 1(1) and (5) of the Aviation and Maritime Security Act 1990. | | ||
|
||
Scenario: Offence Search API - Search by all fields | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | AV9 | | ||
| title | PERSONAL INJURY and endangering safe operation | | ||
| act_section | Contrary to section 1(1) and (5) of the Aviation and Maritime Security Act 1990. | | ||
| active_date | | | ||
| max_results | | | ||
|
||
Then The offence search response returns 200 | ||
And the offences in the response contain the following data | ||
| cjs_code | AV9 | | ||
| offence_title | personal injury and endangering safe operation | | ||
| offence_oas | Contrary to section 1(1) and (5) of the Aviation and Maritime Security Act 1990. | | ||
|
||
Scenario: Offence Search API - Max results | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | | | ||
| title | | | ||
| act_section | | | ||
| active_date | | | ||
| max_results | 2 | | ||
|
||
Then The offence search response returns 200 | ||
And there are 2 offences in the response | ||
|
||
When I make a request to the offence search api filtering by | ||
| cjs_code | | | ||
| title | | | ||
| act_section | | | ||
| active_date | | | ||
| max_results | 20 | | ||
|
||
Then The offence search response returns 200 | ||
And there are 20 offences in the response | ||
|
||
Scenario: Offence Search API - Search by Active Date | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | PA1101 | | ||
| title | | | ||
| act_section | | | ||
| active_date | 1920-03-12 00:00:00 | | ||
| max_results | 100 | | ||
|
||
Then The offence search response returns 200 | ||
Then the offences in the response are before "1920-03-12 00:00:00" only | ||
|
||
Scenario: Offence Search API - Inactive Offences - Active Date Null - Inactive offences returned | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | PA1101 | | ||
| title | | | ||
| act_section | | | ||
| active_date | | | ||
| max_results | 100 | | ||
Then The offence search response returns 200 | ||
And there are 3 offences in the response | ||
|
||
Scenario: Offence Search API - Inactive Offences - Active Date populated - Inactive offences not returned | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | PA1101 | | ||
| title | | | ||
| act_section | | | ||
| active_date | 2024-03-12 00:00:00 | | ||
| max_results | 100 | | ||
Then The offence search response returns 200 | ||
And there are 0 offences in the response | ||
|
||
Scenario: Offence Search API - No Results | ||
### need to test no results are returned when no offences match the search criteria and the status is 200 | ||
Given I am testing as the "[email protected]" user | ||
When I make a request to the offence search api filtering by | ||
| cjs_code | AB12345 | | ||
| title | Offence not real | | ||
| act_section | | | ||
| active_date | | | ||
| max_results | 10 | | ||
Then The offence search response returns 200 | ||
And there are 0 offences in the response |