From 09ebe8b1ce511615f72194ec6c27b1b2bed224ca Mon Sep 17 00:00:00 2001 From: JAndrewCH Date: Mon, 11 Mar 2024 11:19:06 +0000 Subject: [PATCH] use generic read/write converters --- .../pscdataapi/config/ApplicationConfig.java | 5 ++- .../converter/CompanyPscReadConverter.java | 31 +++++--------- .../converter/CompanyPscWriteConverter.java | 26 ++++-------- .../CompanyPscReadConverterTest.java | 38 ------------------ .../CompanyPscWriteConverterTest.java | 40 ------------------- 5 files changed, 21 insertions(+), 119 deletions(-) delete mode 100644 src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java delete mode 100644 src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java diff --git a/src/main/java/uk/gov/companieshouse/pscdataapi/config/ApplicationConfig.java b/src/main/java/uk/gov/companieshouse/pscdataapi/config/ApplicationConfig.java index 51d816a7..d4d548d9 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/config/ApplicationConfig.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/config/ApplicationConfig.java @@ -15,8 +15,10 @@ import org.springframework.data.mongodb.core.convert.MongoCustomConversions; import uk.gov.companieshouse.api.InternalApiClient; import uk.gov.companieshouse.api.api.CompanyMetricsApiService; +import uk.gov.companieshouse.api.converter.EnumWriteConverter; import uk.gov.companieshouse.pscdataapi.converter.CompanyPscReadConverter; import uk.gov.companieshouse.pscdataapi.converter.CompanyPscWriteConverter; +import uk.gov.companieshouse.pscdataapi.models.PscData; import uk.gov.companieshouse.pscdataapi.serialization.LocalDateDeSerializer; import uk.gov.companieshouse.pscdataapi.serialization.LocalDateSerializer; import uk.gov.companieshouse.sdk.manager.ApiSdkManager; @@ -33,7 +35,8 @@ public class ApplicationConfig { public MongoCustomConversions mongoCustomConversions() { ObjectMapper objectMapper = mongoDbObjectMapper(); return new MongoCustomConversions(List.of(new CompanyPscWriteConverter(objectMapper), - new CompanyPscReadConverter(objectMapper))); + new CompanyPscReadConverter(objectMapper, PscData.class), + new EnumWriteConverter())); } @Bean diff --git a/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverter.java b/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverter.java index 591c43ee..d70b5f92 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverter.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverter.java @@ -3,28 +3,17 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.bson.Document; import org.springframework.core.convert.converter.Converter; +import org.springframework.data.convert.ReadingConverter; +import uk.gov.companieshouse.api.company.Data; +import uk.gov.companieshouse.api.converter.ReadConverter; +import uk.gov.companieshouse.api.delta.Psc; import uk.gov.companieshouse.api.psc.FullRecordCompanyPSCApi; import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException; +import uk.gov.companieshouse.pscdataapi.models.PscData; -public class CompanyPscReadConverter implements Converter { - - private final ObjectMapper objectMapper; - - public CompanyPscReadConverter(ObjectMapper objectMapper) { - this.objectMapper = objectMapper; - } - - /** - * Read convertor. - * @param source source Document. - * @return psc API object. - */ - @Override - public FullRecordCompanyPSCApi convert(Document source) { - try { - return objectMapper.readValue(source.toJson(), FullRecordCompanyPSCApi.class); - } catch (Exception ex) { - throw new FailedToConvertException(ex.getMessage()); - } +@ReadingConverter +public class CompanyPscReadConverter extends ReadConverter { + public CompanyPscReadConverter(ObjectMapper objectMapper, Class objectClass) { + super(objectMapper, objectClass); } -} +} \ No newline at end of file diff --git a/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverter.java b/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverter.java index 8722512b..3604fb24 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverter.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverter.java @@ -3,28 +3,16 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.mongodb.BasicDBObject; import org.springframework.core.convert.converter.Converter; +import org.springframework.data.convert.WritingConverter; +import uk.gov.companieshouse.api.company.Data; +import uk.gov.companieshouse.api.converter.WriteConverter; import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException; +import uk.gov.companieshouse.pscdataapi.models.PscData; import uk.gov.companieshouse.pscdataapi.models.PscDocument; -public class CompanyPscWriteConverter implements Converter { - - private final ObjectMapper objectMapper; - +@WritingConverter +public class CompanyPscWriteConverter extends WriteConverter { public CompanyPscWriteConverter(ObjectMapper objectMapper) { - this.objectMapper = objectMapper; - } - - /** - * Write convertor. - * @param source source Document. - * @return psc BSON object. - */ - @Override - public BasicDBObject convert(PscDocument source) { - try { - return BasicDBObject.parse(objectMapper.writeValueAsString(source)); - } catch (Exception ex) { - throw new FailedToConvertException(ex.getMessage()); - } + super(objectMapper); } } \ No newline at end of file diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java deleted file mode 100644 index 3d4a2aaa..00000000 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscReadConverterTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package uk.gov.companieshouse.pscdataapi.converter; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.bson.Document; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import uk.gov.companieshouse.api.psc.FullRecordCompanyPSCApi; -import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException; -import uk.gov.companieshouse.pscdataapi.util.TestHelper; - -import static org.junit.Assert.assertThrows; - -class CompanyPscReadConverterTest { - - private CompanyPscReadConverter converter; - - @BeforeEach - void setUp() { - converter = new CompanyPscReadConverter(new ObjectMapper()); - } - - @Test - void canConvertDocument() { - Document notificationIdNode = new Document("notification_id", TestHelper.NOTIFICATION_ID); - Document document = new Document("external_data", notificationIdNode); - FullRecordCompanyPSCApi pscApi = converter.convert(document); - - Assertions.assertNotNull(pscApi); - Assertions.assertEquals(TestHelper.NOTIFICATION_ID, pscApi.getExternalData().getNotificationId()); - } - - @Test - void conversionFails() { - Document brokenDocument = new Document("unknown_property", "value"); - assertThrows(FailedToConvertException.class, () -> converter.convert(brokenDocument)); - } -} diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java deleted file mode 100644 index 4ebf238d..00000000 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/converter/CompanyPscWriteConverterTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package uk.gov.companieshouse.pscdataapi.converter; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.mongodb.BasicDBObject; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import uk.gov.companieshouse.pscdataapi.models.PscDocument; - -import uk.gov.companieshouse.pscdataapi.exceptions.FailedToConvertException; -import uk.gov.companieshouse.pscdataapi.util.TestHelper; - -import static org.junit.Assert.assertThrows; - -class CompanyPscWriteConverterTest { - - private CompanyPscWriteConverter converter; - - @BeforeEach - void setUp() { - converter = new CompanyPscWriteConverter(new ObjectMapper()); - } - - @Test - void canConvertDocument() { - PscDocument document = new PscDocument(); - document.setNotificationId(TestHelper.PSC_ID); - - BasicDBObject object = converter.convert(document); - - Assertions.assertNotNull(object); - String json = object.toJson(); - Assertions.assertTrue(json.contains(TestHelper.PSC_ID)); - } - - @Test - void conversionFails() { - assertThrows(FailedToConvertException.class, () -> converter.convert(null)); - } -}