From 32899cd6796a865871a14d4393bcdc12bb331749 Mon Sep 17 00:00:00 2001 From: nayeemshaik-hmcts <95764500+nayeemshaik-hmcts@users.noreply.github.com> Date: Thu, 8 Sep 2022 16:11:55 +0100 Subject: [PATCH] RDCC-5357 Handling 'D' records in pre-processor logic (#158) * RDCC-5357 Handling 'D' records in pre-processor logic * RDCC-5357 Handling 'D' records in pre-processor logic * RDCC-5357 Handling 'D' records in pre-processor logic * RDCC-5357 Handling 'D' records in pre-processor logic * RDCC-5357 Handling 'D' records in pre-processor logic --- .../camel/processor/CategoriesProcessor.java | 39 +++++++--------- .../camel/util/CommonDataLoadConstants.java | 1 + .../processor/CategoriesProcessorTest.java | 45 +++++++++++++++++-- 3 files changed, 59 insertions(+), 26 deletions(-) diff --git a/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessor.java b/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessor.java index 9d70665e..a8f68b87 100644 --- a/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessor.java +++ b/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessor.java @@ -25,6 +25,7 @@ import static uk.gov.hmcts.reform.data.ingestion.camel.util.MappingConstants.FAILURE; import static uk.gov.hmcts.reform.data.ingestion.camel.util.MappingConstants.PARTIAL_SUCCESS; import static uk.gov.hmcts.reform.data.ingestion.camel.util.MappingConstants.ROUTE_DETAILS; +import static uk.gov.hmcts.reform.rd.commondata.camel.util.CommonDataLoadConstants.ACTIVE_Y; @Component @Slf4j @@ -74,9 +75,9 @@ public void process(Exchange exchange) { if (!CollectionUtils.isEmpty(invalidCategories)) { invalidCategories.forEach(categories -> invalidCategoryIds.add(Pair.of( - categories.getKey(), - categories.getRowId() - ))); + categories.getKey(), + categories.getRowId() + ))); lovServiceJsrValidatorInitializer.auditJsrExceptions( invalidCategoryIds, @@ -101,12 +102,9 @@ private List getValidCategories(Multimap multima List finalCategories = new ArrayList<>(); multimap.asMap().forEach((key, collection) -> { - List categoriesList = collection.stream().toList(); + List categoriesList = collection.stream().toList(); if (categoriesList.size() > 1) { - Categories activeCategories = categoriesList.stream() - .filter(categories -> categories.getActive().equalsIgnoreCase("Y")) - .findFirst().orElse(null); - finalCategories.addAll(filterInvalidCategories(activeCategories,categoriesList)); + finalCategories.addAll(filterInvalidCategories(categoriesList)); } else { finalCategories.addAll(categoriesList); } @@ -117,7 +115,7 @@ private List getValidCategories(Multimap multima private Multimap convertToMultiMap(List categoriesList) { Multimap multimap = ArrayListMultimap.create(); categoriesList.forEach(categories -> multimap.put(categories.getCategoryKey() + categories.getServiceId() - + categories.getKey(),categories)); + + categories.getKey(), categories)); return multimap; } @@ -130,23 +128,18 @@ void setFileStatus(Exchange exchange, ApplicationContext applicationContext, Str ); } - private List filterInvalidCategories(Categories activeCategories,List categoriesList) { + private List filterInvalidCategories(List categoriesList) { List validCategories = new ArrayList<>(); - int counter = 0; - for (Categories category: categoriesList) { - if (counter == 0 && category.getActive().equalsIgnoreCase("D") && activeCategories != null) { - counter++; - continue; - } else { - if ((category.getActive().equalsIgnoreCase("Y") - && activeCategories != null) || category.getActive().equalsIgnoreCase("D")) { - validCategories.add(category); - activeCategories = null; - } + + boolean activeProcessed = false; + + for (Categories category : categoriesList) { + if ((ACTIVE_Y.equalsIgnoreCase(category.getActive()) + && !activeProcessed)) { + validCategories.add(category); + activeProcessed = true; } - counter++; } return validCategories; } - } diff --git a/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/util/CommonDataLoadConstants.java b/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/util/CommonDataLoadConstants.java index 1e16260a..b26b5759 100644 --- a/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/util/CommonDataLoadConstants.java +++ b/src/main/java/uk/gov/hmcts/reform/rd/commondata/camel/util/CommonDataLoadConstants.java @@ -18,4 +18,5 @@ private CommonDataLoadConstants() { "SELECT categorykey, key, serviceid FROM list_of_values WHERE TRIM(active) = ?"; public static final String DELETE_CATEGORY_BY_STATUS = "DELETE FROM list_of_values WHERE TRIM(active) = ?"; + public static final String ACTIVE_Y = "Y"; } diff --git a/src/test/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessorTest.java b/src/test/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessorTest.java index a3cf6d77..6319d53e 100644 --- a/src/test/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessorTest.java +++ b/src/test/java/uk/gov/hmcts/reform/rd/commondata/camel/processor/CategoriesProcessorTest.java @@ -85,7 +85,7 @@ void init() { @Test @DisplayName("Test for LOV Duplicate records Case1") - void testListOfValuesCsv_DupRecord_Case1() throws Exception { + void testListOfValuesCsv_DupRecord_Case1() { var lovServiceList = new ArrayList(); lovServiceList.addAll(getLovServicesCase1()); @@ -101,7 +101,7 @@ void testListOfValuesCsv_DupRecord_Case1() throws Exception { @Test @DisplayName("Test for LOV Duplicate records Case2") - void testListOfValuesCsv_DupRecord_Case2() throws Exception { + void testListOfValuesCsv_DupRecord_Case2() { var lovServiceList = new ArrayList(); lovServiceList.addAll(getLovServicesCase2()); @@ -114,7 +114,23 @@ void testListOfValuesCsv_DupRecord_Case2() throws Exception { List actualLovServiceList = (List) exchange.getMessage().getBody(); Assertions.assertEquals(1, actualLovServiceList.size()); + } + @Test + @DisplayName("Test for LOV 'D' records Case3") + void testListOfValuesCsv_DupRecord_Case3() throws Exception { + var lovServiceList = new ArrayList(); + lovServiceList.addAll(getLovServicesCase3()); + + exchange.getIn().setBody(lovServiceList); + when(((ConfigurableApplicationContext) + applicationContext).getBeanFactory()).thenReturn(configurableListableBeanFactory); + + processor.process(exchange); + verify(processor, times(1)).process(exchange); + + List actualLovServiceList = (List) exchange.getMessage().getBody(); + Assertions.assertEquals(1, actualLovServiceList.size()); } private List getLovServicesCase1() { @@ -146,7 +162,7 @@ private List getLovServicesCase2() { .categoryKey("caseSubType") .serviceId("BBA3") .key("BBA3-001AD") - .valueEN("ADVANCE PAYMENT new") + .valueEN("ADVANCE PAYMENT") .parentCategory("caseType") .parentKey("BBA3-001") .active("D") @@ -171,4 +187,27 @@ private List getLovServicesCase2() { .build() ); } + + private List getLovServicesCase3() { + return ImmutableList.of( + Categories.builder() + .categoryKey("caseSubType") + .serviceId("BBA3") + .key("BBA3-001AD") + .valueEN("ADVANCE PAYMENT") + .parentCategory("caseType") + .parentKey("BBA3-001") + .active("Y") + .build(), + Categories.builder() + .categoryKey("caseSubType") + .serviceId("BBA3") + .key("BBA3-001AD") + .valueEN("ADVANCE PAYMENT") + .parentCategory("caseType") + .parentKey("BBA3-001") + .active("D") + .build() + ); + } }