Skip to content

Commit

Permalink
Merge pull request #917 from alphagov/bau-reduce-ObjectMapper-instant…
Browse files Browse the repository at this point in the history
…iation

bau: Reduce ObjectMapper instantiation
  • Loading branch information
oswaldquek authored Dec 23, 2020
2 parents 705b935 + 9a81173 commit c972790
Show file tree
Hide file tree
Showing 24 changed files with 277 additions and 272 deletions.
6 changes: 3 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": null,
"lines": null
},
"generated_at": "2020-08-12T14:42:40Z",
"generated_at": "2020-12-22T18:07:34Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -167,7 +167,7 @@
"hashed_secret": "c3a8fcf85a56dee95cff4ce7adf0bb9a4c18f747",
"is_secret": false,
"is_verified": false,
"line_number": 60,
"line_number": 59,
"type": "Hex High Entropy String"
}
],
Expand All @@ -194,7 +194,7 @@
"hashed_secret": "614770647df3ab100b871bfc0d20e72c8625a5c4",
"is_secret": false,
"is_verified": false,
"line_number": 42,
"line_number": 44,
"type": "Hex High Entropy String"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ServiceUpdateRequest {
public static final String FIELD_PATH = "path";
public static final String FIELD_VALUE = "value";

private static ObjectMapper objectMapper = new ObjectMapper();

private String op;
private String path;
private JsonNode value;
Expand Down Expand Up @@ -51,7 +53,7 @@ public Map<String, Object> valueAsObject() {
if (value != null) {
if ((value.isTextual() && !isEmpty(value.asText())) || value.isObject()) {
try {
return new ObjectMapper().readValue(value.traverse(), new TypeReference<Map<String, Object>>() {});
return objectMapper.readValue(value.traverse(), new TypeReference<Map<String, Object>>() {});
} catch (IOException e) {
throw new RuntimeException("Malformed JSON object in ServiceUpdateRequest.value", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

@Converter
public class CustomBrandingConverter implements AttributeConverter<Map<String, Object>, PGobject> {

private static ObjectMapper objectMapper = new ObjectMapper();

@Override
public PGobject convertToDatabaseColumn(Map<String, Object> customBranding) {
PGobject dbCustomBranding = new PGobject();
dbCustomBranding.setType("json");
try {
dbCustomBranding.setValue(new ObjectMapper().writeValueAsString(customBranding));
dbCustomBranding.setValue(objectMapper.writeValueAsString(customBranding));
} catch (SQLException | JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand All @@ -33,7 +36,7 @@ public Map<String, Object> convertToEntityAttribute(PGobject dbCustomBranding) {
if (dbCustomBranding == null || isEmpty(dbCustomBranding.toString())) {
return null;
} else {
return new ObjectMapper().readValue(dbCustomBranding.toString(), new TypeReference<>() {});
return objectMapper.readValue(dbCustomBranding.toString(), new TypeReference<>() {});
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;

public class CreateUserRequestTest {
class CreateUserRequestTest {

private static ObjectMapper objectMapper = new ObjectMapper();

@Test
public void shouldConstructAUser_fromMinimalValidUserJson() throws Exception {
void shouldConstructAUser_fromMinimalValidUserJson() throws Exception {
String minimumUserJson = "{" +
"\"username\": \"a-username\"," +
"\"telephone_number\": \"2123524\"," +
"\"gateway_account_ids\": [\"1\", \"2\"]," +
"\"email\": \"[email protected]\"" +
"}";

JsonNode jsonNode = new ObjectMapper().readTree(minimumUserJson);
JsonNode jsonNode = objectMapper.readTree(minimumUserJson);
CreateUserRequest createUserRequest = CreateUserRequest.from(jsonNode);

assertThat(createUserRequest.getUsername(), is("a-username"));
Expand All @@ -33,7 +35,7 @@ public void shouldConstructAUser_fromMinimalValidUserJson() throws Exception {
}

@Test
public void shouldConstructAUser_fromCompleteValidUserJson() throws Exception {
void shouldConstructAUser_fromCompleteValidUserJson() throws Exception {
String minimunUserJson = "{" +
"\"username\": \"a-username\"," +
"\"password\": \"a-password\"," +
Expand All @@ -43,7 +45,7 @@ public void shouldConstructAUser_fromCompleteValidUserJson() throws Exception {
"\"email\": \"[email protected]\"" +
"}";

JsonNode jsonNode = new ObjectMapper().readTree(minimunUserJson);
JsonNode jsonNode = objectMapper.readTree(minimunUserJson);
CreateUserRequest createUserRequest = CreateUserRequest.from(jsonNode);

assertThat(createUserRequest.getUsername(), is("a-username"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class ServiceUpdateRequestTest {
class ServiceUpdateRequestTest {

private ObjectMapper objectMapper = new ObjectMapper();
private static ObjectMapper objectMapper = new ObjectMapper();

@BeforeEach
public void before() {
void before() {
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, true);
}

@Test
public void shouldTransformToObjectCorrectly() throws IOException {
void shouldTransformToObjectCorrectly() throws IOException {
Map<String, Object> payload = Map.of("path", "custom_branding", "op", "replace", "value", Map.of("image_url", "image url", "css_url", "css url"));
String content = objectMapper.writeValueAsString(payload);
JsonNode jsonNode = objectMapper.readTree(content);
Expand All @@ -37,10 +37,10 @@ public void shouldTransformToObjectCorrectly() throws IOException {
}

@Test
public void shouldReturnAList_whenJsonIsArray() throws IOException {
void shouldReturnAList_whenJsonIsArray() throws IOException {
String jsonPayload = fixture("fixtures/resource/service/patch/array-replace-service-name-en-replace-service-name-cy.json");

final List<ServiceUpdateRequest> requests = ServiceUpdateRequest.getUpdateRequests(new ObjectMapper().readTree(jsonPayload));
final List<ServiceUpdateRequest> requests = ServiceUpdateRequest.getUpdateRequests(objectMapper.readTree(jsonPayload));

assertThat(requests.size(), is(2));
requests.sort(Comparator.comparing(ServiceUpdateRequest::getPath));
Expand All @@ -52,7 +52,7 @@ public void shouldReturnAList_whenJsonIsArray() throws IOException {
}

@Test
public void shouldReturnAList_whenJsonIsSingleObject() throws IOException {
void shouldReturnAList_whenJsonIsSingleObject() throws IOException {
//language=JSON
String jsonPayload =
"{\n" +
Expand All @@ -61,7 +61,7 @@ public void shouldReturnAList_whenJsonIsSingleObject() throws IOException {
" \"value\": \"new-en-name\"\n" +
"}\n";

final List<ServiceUpdateRequest> requests = ServiceUpdateRequest.getUpdateRequests(new ObjectMapper().readTree(jsonPayload));
final List<ServiceUpdateRequest> requests = ServiceUpdateRequest.getUpdateRequests(objectMapper.readTree(jsonPayload));

assertThat(requests.size(), is(1));
assertThat(requests.get(0).getPath(), is("name"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;

public class UpdateMerchantDetailsRequestTest {
class UpdateMerchantDetailsRequestTest {

private static ObjectMapper objectMapper = new ObjectMapper();

private final String name = "name";
private final String telephoneNumber = "03069990000";
Expand All @@ -22,14 +24,14 @@ public class UpdateMerchantDetailsRequestTest {
private final String email = "[email protected]";

@Test
public void shouldConstructMerchantDetails_fromMinimalValidJson() {
void shouldConstructMerchantDetails_fromMinimalValidJson() {
Map<String, Object> payload = Map.of(
"name", name,
"address_line1", addressLine1,
"address_city", addressCity,
"address_country", addressCountry,
"address_postcode", addressPostcode);
JsonNode jsonNode = new ObjectMapper().valueToTree(payload);
JsonNode jsonNode = objectMapper.valueToTree(payload);
UpdateMerchantDetailsRequest updateMerchantDetailsRequest = UpdateMerchantDetailsRequest.from(jsonNode);
assertThat(updateMerchantDetailsRequest.getName(), is(name));
assertThat(updateMerchantDetailsRequest.getAddressLine1(), is(addressLine1));
Expand All @@ -40,7 +42,7 @@ public void shouldConstructMerchantDetails_fromMinimalValidJson() {
}

@Test
public void shouldConstructMerchantDetails_fromCompleteValidJson() {
void shouldConstructMerchantDetails_fromCompleteValidJson() {
Map<String, Object> payload = Map.of(
"name", name,
"telephone_number", telephoneNumber,
Expand All @@ -50,7 +52,7 @@ public void shouldConstructMerchantDetails_fromCompleteValidJson() {
"address_country", addressCountry,
"address_postcode", addressPostcode,
"email", email);
JsonNode jsonNode = new ObjectMapper().valueToTree(payload);
JsonNode jsonNode = objectMapper.valueToTree(payload);
UpdateMerchantDetailsRequest updateMerchantDetailsRequest = UpdateMerchantDetailsRequest.from(jsonNode);
assertThat(updateMerchantDetailsRequest.getName(), is(name));
assertThat(updateMerchantDetailsRequest.getTelephoneNumber(), is(telephoneNumber));
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/uk/gov/pay/adminusers/model/UserEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
import static uk.gov.pay.adminusers.model.Permission.permission;
import static uk.gov.pay.adminusers.model.Role.role;

public class UserEntityTest {
class UserEntityTest {

private static ObjectMapper objectMapper = new ObjectMapper();

@Test
public void shouldConstructAUser_fromMinimalValidUserJson() throws Exception {
void shouldConstructAUser_fromMinimalValidUserJson() throws Exception {
String minimumUserJson = "{" +
"\"username\": \"a-username\"," +
"\"telephone_number\": \"+441134960000\"," +
"\"gateway_account_ids\": [\"1\", \"2\"]," +
"\"email\": \"[email protected]\"" +
"}";

JsonNode jsonNode = new ObjectMapper().readTree(minimumUserJson);
JsonNode jsonNode = objectMapper.readTree(minimumUserJson);
CreateUserRequest createUserRequest = CreateUserRequest.from(jsonNode);

UserEntity userEntity = UserEntity.from(createUserRequest);
Expand All @@ -46,7 +48,7 @@ public void shouldConstructAUser_fromMinimalValidUserJson() throws Exception {
}

@Test
public void creatingAUser_shouldSetGatewayAccountAndRole_whenServiceRoleIsSet() {
void creatingAUser_shouldSetGatewayAccountAndRole_whenServiceRoleIsSet() {
UserEntity userEntity = new UserEntity();
String gatewayAccountId = "1";
ServiceEntity service = new ServiceEntity(List.of(gatewayAccountId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@
import static uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid;
import static uk.gov.pay.adminusers.model.Role.role;

public class ServiceDaoIT extends DaoTestBase {
class ServiceDaoIT extends DaoTestBase {

private ServiceDao serviceDao;
private ObjectMapper objectMapper = new ObjectMapper();
private static final String EN_NAME = "en-test-name";
private static final String CY_NAME = "gwasanaeth prawf";

@BeforeEach
public void before() {
void before() {
serviceDao = env.getInstance(ServiceDao.class);
}

@Test
public void shouldSaveAService_withCustomisations() throws Exception {
void shouldSaveAService_withCustomisations() throws Exception {
ServiceEntity insertedServiceEntity = ServiceEntityBuilder.aServiceEntity()
.withExperimentalFeaturesEnabled(true)
.build();
Expand All @@ -78,7 +78,7 @@ public void shouldSaveAService_withCustomisations() throws Exception {
}

@Test
public void shouldSaveAService_withMultipleServiceNames() {
void shouldSaveAService_withMultipleServiceNames() {
ServiceEntity insertedServiceEntity = ServiceEntityBuilder.aServiceEntity()
.withCustomBranding(null)
.withServiceNameEntity(SupportedLanguage.WELSH, CY_NAME)
Expand All @@ -104,7 +104,7 @@ public void shouldSaveAService_withMultipleServiceNames() {
}

@Test
public void shouldSaveAService_withoutCustomisations_andServiceName() {
void shouldSaveAService_withoutCustomisations_andServiceName() {
ServiceEntity insertedServiceEntity = ServiceEntityBuilder.aServiceEntity()
.withCustomBranding(null)
.withServiceNameEntity(SupportedLanguage.ENGLISH, EN_NAME)
Expand All @@ -131,7 +131,7 @@ public void shouldSaveAService_withoutCustomisations_andServiceName() {
}

@Test
public void shouldSaveAService_withMerchantDetails() {
void shouldSaveAService_withMerchantDetails() {
MerchantDetailsEntity merchantDetails = MerchantDetailsEntityBuilder.aMerchantDetailsEntity().build();
ServiceEntity insertedServiceEntity = ServiceEntityBuilder.aServiceEntity()
.withMerchantDetailsEntity(merchantDetails)
Expand All @@ -154,7 +154,7 @@ public void shouldSaveAService_withMerchantDetails() {
}

@Test
public void shouldFindByServiceExternalId() {
void shouldFindByServiceExternalId() {
ZonedDateTime now = ZonedDateTime.now(UTC);
ServiceEntity insertedServiceEntity = ServiceEntityBuilder.aServiceEntity()
.withExperimentalFeaturesEnabled(true)
Expand All @@ -177,7 +177,7 @@ public void shouldFindByServiceExternalId() {
}

@Test
public void shouldReturnServiceValuesFromDatabase() {
void shouldReturnServiceValuesFromDatabase() {
ServiceEntity insertedServiceEntity = ServiceEntityBuilder.aServiceEntity()
.withRedirectToServiceImmediatelyOnTerminalState(true)
.withCreatedDate(ZonedDateTime.parse("2020-11-01T00:00:00Z"))
Expand All @@ -193,7 +193,7 @@ public void shouldReturnServiceValuesFromDatabase() {
}

@Test
public void shouldFindServiceWithMultipleLanguage_byServiceExternalId() {
void shouldFindServiceWithMultipleLanguage_byServiceExternalId() {
Set<ServiceNameEntity> serviceNames = new HashSet<>(List.of(
createServiceName(SupportedLanguage.ENGLISH, EN_NAME),
createServiceName(SupportedLanguage.WELSH, CY_NAME)
Expand All @@ -219,7 +219,7 @@ public void shouldFindServiceWithMultipleLanguage_byServiceExternalId() {
}

@Test
public void shouldFindByGatewayAccountId() {
void shouldFindByGatewayAccountId() {
GatewayAccountIdEntity gatewayAccountIdEntity = new GatewayAccountIdEntity();
String gatewayAccountId = randomUuid();
gatewayAccountIdEntity.setGatewayAccountId(gatewayAccountId);
Expand All @@ -237,7 +237,7 @@ public void shouldFindByGatewayAccountId() {
}

@Test
public void shouldGetRoleCountForAService() {
void shouldGetRoleCountForAService() {
String serviceExternalId = randomUuid();
Integer roleId = randomInt();
setupUsersForServiceAndRole(serviceExternalId, roleId, 3);
Expand All @@ -248,7 +248,7 @@ public void shouldGetRoleCountForAService() {
}

@Test
public void shouldMergeGoLiveStage() {
void shouldMergeGoLiveStage() {
GatewayAccountIdEntity gatewayAccountIdEntity = new GatewayAccountIdEntity();
String gatewayAccountId = randomUuid();
gatewayAccountIdEntity.setGatewayAccountId(gatewayAccountId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.pay.adminusers.resources;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import uk.gov.pay.adminusers.fixtures.ServiceDbFixture;
import uk.gov.pay.adminusers.model.MerchantDetails;
Expand All @@ -11,7 +10,6 @@

public class EmailResourceIT extends IntegrationTest {

private ObjectMapper objectMapper = new ObjectMapper();
private static final String GATEWAY_ACCOUNT_ID = "DIRECT_DEBIT:mdshfsehdtfsdtjg";
private Map<String, Object> validEmailRequest = Map.of(
"address", "[email protected]",
Expand All @@ -32,7 +30,7 @@ public void shouldReceiveAPayloadAndSendEmail() {
"postcode", "country", "[email protected]"
))
.insertService();
String body = objectMapper.valueToTree(validEmailRequest).toString();
String body = mapper.valueToTree(validEmailRequest).toString();
givenSetup()
.when()
.accept(JSON)
Expand Down
Loading

0 comments on commit c972790

Please sign in to comment.