-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-13030: Use CreateServiceRequest pojo instead of JsonNode (#2653)
* PP-13030: Use CreateServiceRequest pojo instead of JsonNode Following the general pattern in most of our Dropwizard codebases, we want to avoid JsonNode as the POST request body where possible so we can advantage of using javax Validation constraints. It is also generally agreed that using hard coded URL strings makes for easier-to-read code. Added a few more test cases in ServiceResourceCreateIT to reflect the pact tests between selfservice and adminusers: * [a valid create service request with empty object](https://github.com/alphagov/pay-selfservice/blob/b2053a1049bc28813e8e7363245ad546ef19b675/test/pact/adminusers-client/service/create-service.pact.test.js#L50) * [a valid create service request with gateway account ids](https://github.com/alphagov/pay-selfservice/blob/b2053a1049bc28813e8e7363245ad546ef19b675/test/pact/adminusers-client/service/create-service.pact.test.js#L85C31-L85C86) * [a valid create service request with service name](https://github.com/alphagov/pay-selfservice/blob/b2053a1049bc28813e8e7363245ad546ef19b675/test/pact/adminusers-client/service/create-service.pact.test.js#L124)
- Loading branch information
1 parent
f8bfb9f
commit 00a0f8a
Showing
9 changed files
with
167 additions
and
68 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
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 |
---|---|---|
|
@@ -343,6 +343,8 @@ paths: | |
schema: | ||
$ref: '#/components/schemas/Service' | ||
description: Created | ||
"400": | ||
description: Service name keys must be one of 'en' (English) or 'cy' (Welsh) | ||
"409": | ||
description: Gateway account IDs provided has already been assigned to another | ||
service | ||
|
@@ -1184,6 +1186,17 @@ components: | |
example: [email protected] | ||
required: | ||
CreateServiceRequest: | ||
type: object | ||
properties: | ||
gateway_account_ids: | ||
type: array | ||
items: | ||
type: string | ||
service_name: | ||
type: object | ||
additionalProperties: | ||
type: string | ||
CreateUserRequest: | ||
type: object | ||
properties: | ||
|
16 changes: 16 additions & 0 deletions
16
src/main/java/uk/gov/pay/adminusers/model/CreateServiceRequest.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,16 @@ | ||
package uk.gov.pay.adminusers.model; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import uk.gov.service.payments.commons.model.SupportedLanguage; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public record CreateServiceRequest( | ||
List<String> gatewayAccountIds, | ||
@JsonDeserialize(using = ServiceNamesDeserializer.class) Map<SupportedLanguage, String> serviceName) { | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
src/main/java/uk/gov/pay/adminusers/model/ServiceNamesDeserializer.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,21 @@ | ||
package uk.gov.pay.adminusers.model; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import uk.gov.service.payments.commons.model.SupportedLanguage; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static java.util.stream.Collectors.toUnmodifiableMap; | ||
|
||
public class ServiceNamesDeserializer extends JsonDeserializer<Map<SupportedLanguage, String>> { | ||
@Override | ||
public Map<SupportedLanguage, String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { | ||
return jsonParser.getCodec().readValue(jsonParser, new TypeReference<Map<String, String>>() {}) | ||
.entrySet().stream() | ||
.collect(toUnmodifiableMap(entry -> SupportedLanguage.fromIso639AlphaTwoCode(entry.getKey()), Map.Entry::getValue)); | ||
} | ||
} |
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
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
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
102 changes: 91 additions & 11 deletions
102
src/test/java/uk/gov/pay/adminusers/resources/ServiceResourceCreateIT.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 |
---|---|---|
@@ -1,37 +1,117 @@ | ||
package uk.gov.pay.adminusers.resources; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.restassured.response.ValidatableResponse; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
import uk.gov.service.payments.commons.model.SupportedLanguage; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static io.restassured.http.ContentType.JSON; | ||
import static org.hamcrest.Matchers.emptyIterable; | ||
import static org.hamcrest.Matchers.hasEntry; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static uk.gov.service.payments.commons.model.SupportedLanguage.ENGLISH; | ||
import static uk.gov.service.payments.commons.model.SupportedLanguage.WELSH; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class ServiceResourceCreateIT extends IntegrationTest { | ||
|
||
@Test | ||
void shouldCreateService() { | ||
JsonNode payload = mapper | ||
.valueToTree(Map.of( | ||
"service_name", Map.of(SupportedLanguage.ENGLISH.toString(), "Service name"), | ||
"gateway_account_ids", List.of("1") | ||
)); | ||
|
||
givenSetup() | ||
void create_service_with_all_parameters_successfully() { | ||
var validatableResponse = givenSetup() | ||
.when() | ||
.contentType(JSON) | ||
.body(payload) | ||
.body(Map.of("service_name", Map.of(ENGLISH.toString(), "Service name"), | ||
"gateway_account_ids", List.of("1"))) | ||
.post("v1/api/services") | ||
.then() | ||
.statusCode(201) | ||
.body("created_date", is(not(nullValue()))) | ||
.body("gateway_account_ids", is(List.of("1"))) | ||
.body("service_name", hasEntry("en", "Service name")); | ||
|
||
assertStandardFields(validatableResponse); | ||
} | ||
|
||
@Test | ||
void can_create_default_service_with_empty_request_body() { | ||
var validatableResponse = givenSetup() | ||
.when() | ||
.contentType(JSON) | ||
.post("v1/api/services") | ||
.then() | ||
.statusCode(201) | ||
.body("created_date", is(not(nullValue()))) | ||
.body("gateway_account_ids", is(emptyIterable())) | ||
.body("service_name", hasEntry("en", "System Generated")) | ||
.body("name", is("System Generated")); | ||
|
||
assertStandardFields(validatableResponse); | ||
} | ||
|
||
@Test | ||
void can_create_default_service_with_gateway_account_ids_only() { | ||
var validatableResponse = givenSetup() | ||
.when() | ||
.contentType(JSON) | ||
.body(Map.of("gateway_account_ids", List.of("1", "2"))) | ||
.post("v1/api/services") | ||
.then() | ||
.statusCode(201) | ||
.body("created_date", is(not(nullValue()))) | ||
.body("gateway_account_ids", is(List.of("1", "2"))) | ||
.body("service_name", hasEntry("en", "System Generated")) | ||
.body("name", is("System Generated")); | ||
|
||
assertStandardFields(validatableResponse); | ||
} | ||
|
||
@Test | ||
void can_create_default_service_with_service_name_only() { | ||
var validatableResponse = givenSetup() | ||
.when() | ||
.contentType(JSON) | ||
.body(Map.of("service_name", Map.of(ENGLISH.toString(), "Service name", WELSH.toString(), "Welsh name"))) | ||
.post("v1/api/services") | ||
.then() | ||
.statusCode(201) | ||
.body("created_date", is(not(nullValue()))) | ||
.body("gateway_account_ids", is(emptyIterable())) | ||
.body("service_name", hasEntry("en", "Service name")) | ||
.body("service_name", hasEntry("cy", "Welsh name")) | ||
.body("name", is("Service name")); | ||
|
||
assertStandardFields(validatableResponse); | ||
} | ||
|
||
private void assertStandardFields(ValidatableResponse validatableResponse) { | ||
validatableResponse | ||
.body("current_psp_test_account_stage", is("NOT_STARTED")) | ||
.body("current_go_live_stage", is("NOT_STARTED")) | ||
.body("default_billing_address_country", is("GB")) | ||
.body("agent_initiated_moto_enabled", is(false)) | ||
.body("takes_payments_over_phone", is(false)) | ||
.body("experimental_features_enabled", is(false)) | ||
.body("internal", is(false)) | ||
.body("archived", is(false)) | ||
.body("redirect_to_service_immediately_on_terminal_state", is(false)) | ||
.body("collect_billing_address", is(true)); | ||
} | ||
|
||
@Test | ||
void return_bad_request_when_invalid_supported_language_provided() { | ||
givenSetup() | ||
.when() | ||
.contentType(JSON) | ||
.body(Map.of("service_name", Map.of("fr", "Service name"))) | ||
.post("v1/api/services") | ||
.then() | ||
.statusCode(400) | ||
.body("message", is("Unable to process JSON")) | ||
.body("details", is("fr is not a supported ISO 639-1 code")); | ||
} | ||
} |
Oops, something went wrong.