-
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
Following the general 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
0c8117e
commit 4a4de0e
Showing
9 changed files
with
158 additions
and
63 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
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) { | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
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,23 @@ | ||
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.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ServiceNamesDeserializer extends JsonDeserializer<Map<SupportedLanguage, String>> { | ||
@Override | ||
public Map<SupportedLanguage, String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { | ||
Map<SupportedLanguage, String> supportedLanguageToServiceName = new HashMap<>(); | ||
jsonParser.getCodec().readValue(jsonParser, new TypeReference<Map<String, String>>() {}) | ||
.forEach((key, value) -> | ||
supportedLanguageToServiceName.put(SupportedLanguage.fromIso639AlphaTwoCode(key), value)); | ||
return Collections.unmodifiableMap(supportedLanguageToServiceName); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,7 +158,7 @@ void shouldHandleDisputeCreatedEvent() throws QueueException { | |
|
||
assertThat(emails.size(), is(2)); | ||
assertThat(emails, hasItems("[email protected]", "[email protected]")); | ||
assertThat(personalisation.get("serviceName"), is(service.getName())); | ||
assertThat(personalisation.get("serviceNames"), is(service.getName())); | ||
assertThat(personalisation.get("paymentExternalId"), is("456")); | ||
assertThat(personalisation.get("serviceReference"), is("tx ref")); | ||
assertThat(personalisation.get("sendEvidenceToPayDueDate"), is("28 February 2022")); | ||
|
@@ -207,7 +207,7 @@ void shouldHandleDisputeLostEvent() throws QueueException { | |
|
||
assertThat(emails.size(), is(2)); | ||
assertThat(emails, hasItems("[email protected]", "[email protected]")); | ||
assertThat(personalisation.get("serviceName"), is(service.getName())); | ||
assertThat(personalisation.get("serviceNames"), is(service.getName())); | ||
assertThat(personalisation.get("serviceReference"), is("tx ref")); | ||
assertThat(personalisation.get("organisationName"), is(service.getMerchantDetails().getName())); | ||
|
||
|
@@ -244,7 +244,7 @@ void shouldHandleDisputeWonEvent() throws QueueException { | |
|
||
assertThat(emails.size(), is(2)); | ||
assertThat(emails, hasItems("[email protected]", "[email protected]")); | ||
assertThat(personalisation.get("serviceName"), is(service.getName())); | ||
assertThat(personalisation.get("serviceNames"), is(service.getName())); | ||
assertThat(personalisation.get("serviceReference"), is("tx ref")); | ||
assertThat(personalisation.get("organisationName"), is(service.getMerchantDetails().getName())); | ||
|
||
|
@@ -281,7 +281,7 @@ void shouldHandleDisputeEvidenceSubmittedEvent() throws QueueException { | |
|
||
assertThat(emails.size(), is(2)); | ||
assertThat(emails, hasItems("[email protected]", "[email protected]")); | ||
assertThat(personalisation.get("serviceName"), is(service.getName())); | ||
assertThat(personalisation.get("serviceNames"), is(service.getName())); | ||
assertThat(personalisation.get("serviceReference"), is("tx ref")); | ||
assertThat(personalisation.get("organisationName"), is(service.getMerchantDetails().getName())); | ||
|
||
|
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); | ||
} | ||
|
||
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 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); | ||
} | ||
|
||
@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.