Skip to content

Commit a0a3f98

Browse files
committed
more refactor
1 parent a04335b commit a0a3f98

2 files changed

Lines changed: 50 additions & 58 deletions

File tree

src/main/java/org/folio/fqm/utils/EntityTypeUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class EntityTypeUtils {
4949

5050
public static final org.jooq.Field<String[]> RESULT_ID_FIELD = field("result_id", String[].class);
5151

52+
private static final String COUNTRIES_SOURCE = "countries";
53+
5254
/**
5355
* Returns a list of strings corresponding to the names of the id columns of an entity type.
5456
*
@@ -352,7 +354,7 @@ public static List<String> getCountryLocalizationFieldPaths(EntityType entityTyp
352354
EntityTypeUtils.runOnEveryField(entityType, (field, parentPath) -> {
353355
if (field.getSource() == null
354356
|| field.getSource().getType() != SourceColumn.TypeEnum.FQM
355-
|| !"countries".equals(field.getSource().getName())) {
357+
|| !COUNTRIES_SOURCE.equals(field.getSource().getName())) {
356358
return;
357359
}
358360

src/test/java/org/folio/fqm/service/ResultSetServiceTest.java

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ void localizeTopLevelCountryField_shouldReturnEarlyWhenCodeIsBlank() {
348348

349349
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
350350
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
351-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
352351
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
353352
.thenReturn(List.of(Map.of("id", contentId.toString(), "country", " ")));
354353

@@ -385,7 +384,6 @@ void localizeTopLevelCountryField_shouldReturnEarlyWhenValueIsNotString() {
385384

386385
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
387386
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
388-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
389387
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
390388
.thenReturn(List.of(Map.of("id", contentId.toString(), "country", 123)));
391389

@@ -435,7 +433,6 @@ void getResultSet_shouldNotLocalizeNestedCountryField_whenRootFieldIsBlank() {
435433

436434
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
437435
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
438-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
439436
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
440437
.thenReturn(List.of(Map.of("id", contentId.toString(), "", addressesJson)));
441438

@@ -484,7 +481,6 @@ void getResultSet_shouldNotLocalizeNestedCountryField_whenLeafFieldIsBlank() {
484481

485482
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
486483
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
487-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
488484
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
489485
.thenReturn(List.of(Map.of("id", contentId.toString(), "addresses", addressesJson)));
490486

@@ -533,7 +529,6 @@ void getResultSet_shouldNotLocalizeNestedCountryField_whenRootFieldValueIsNotStr
533529

534530
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
535531
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
536-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
537532
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
538533
.thenReturn(List.of(Map.of("id", contentId.toString(), "addresses", notAString)));
539534

@@ -592,7 +587,6 @@ void getResultSet_shouldNotLocalizeNestedCountryField_whenRootFieldValueIsNotArr
592587

593588
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
594589
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
595-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
596590
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
597591
.thenReturn(List.of(Map.of("id", contentId.toString(), "addresses", notAnArrayJson)));
598592

@@ -641,7 +635,6 @@ void getResultSet_shouldNotLocalizeNestedCountryField_whenRootFieldValueIsInvali
641635

642636
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
643637
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
644-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
645638
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
646639
.thenReturn(List.of(Map.of("id", contentId.toString(), "addresses", invalidJson)));
647640

@@ -658,7 +651,6 @@ void getResultSet_shouldNotLocalizeNestedCountryField_whenRootFieldValueIsInvali
658651
void getResultSet_shouldNotUpdateRootField_whenNoCountryCodeIsTranslated() {
659652
UUID entityTypeId = UUID.randomUUID();
660653
UUID contentId = UUID.randomUUID();
661-
// The countryId is not translatable (translationService returns empty)
662654
String addressesJson = "[{\"city\":\"Auburn\",\"countryId\":\"ZZZ\"}]";
663655

664656
var addressesColumn = new EntityTypeColumn()
@@ -691,21 +683,55 @@ void getResultSet_shouldNotUpdateRootField_whenNoCountryCodeIsTranslated() {
691683

692684
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
693685
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
694-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
695686
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
696687
.thenReturn(List.of(Map.of("id", contentId.toString(), "addresses", addressesJson)));
697688
// Simulate no translation for ZZZ
698689
when(translationService.format("mod-fqm-manager.countries.ZZZ")).thenReturn(null);
699690

700691
List<Map<String, Object>> actual = service.getResultSet(entityTypeId, fields, listIds, tenantIds, true);
701692

702-
assertEquals(1, actual.size());
703-
assertEquals(contentId.toString(), actual.getFirst().get("id"));
704-
// The addresses field should be unchanged
705693
assertEquals(addressesJson, actual.getFirst().get("addresses"));
706694
verify(translationService).format("mod-fqm-manager.countries.ZZZ");
707695
}
708696

697+
@Test
698+
void getResultSet_shouldNotLocalizeCountryField_whenFieldPathIsEmpty() {
699+
UUID entityTypeId = UUID.randomUUID();
700+
UUID contentId = UUID.randomUUID();
701+
// Simulate a top-level country field with an empty field path
702+
EntityType entityType = new EntityType()
703+
.name("test_entity")
704+
.id(entityTypeId.toString())
705+
.columns(List.of(
706+
new EntityTypeColumn().name("id").isIdColumn(true),
707+
new EntityTypeColumn()
708+
.name("")
709+
.source(new SourceColumn(entityTypeId, "countryId")
710+
.type(SourceColumn.TypeEnum.FQM)
711+
.name("countries"))
712+
))
713+
.sources(List.of(new EntityTypeSourceDatabase().type("db").alias("source1").target("target1")));
714+
715+
List<String> fields = List.of("id", "");
716+
List<String> tenantIds = List.of("tenant_01");
717+
List<List<String>> listIds = List.of(List.of(contentId.toString()));
718+
String countryCode = "US";
719+
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
720+
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
721+
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
722+
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
723+
.thenReturn(List.of(Map.of("id", contentId.toString(), "", countryCode)));
724+
725+
List<Map<String, Object>> actual = service.getResultSet(entityTypeId, fields, listIds, tenantIds, true);
726+
727+
assertEquals(1, actual.size());
728+
assertEquals(contentId.toString(), actual.getFirst().get("id"));
729+
assertEquals(countryCode, actual.getFirst().get(""));
730+
verify(translationService, never()).format(anyString());
731+
}
732+
733+
//////////////////////
734+
709735
@ParameterizedTest
710736
@MethodSource("arrayShouldNotChangeCases")
711737
void getResultSet_shouldNotChangeArray_whenElementIsNotExpectedType(String addressesJson) {
@@ -745,51 +771,6 @@ void getResultSet_shouldNotChangeArray_whenElementIsNotExpectedType(String addre
745771
verify(translationService, never()).format(anyString());
746772
}
747773

748-
private static List<Arguments> arrayShouldNotChangeCases() {
749-
return List.of(
750-
// Element is not an object
751-
Arguments.of("[\"notAnObject\"]"),
752-
// Value node is null or not textual
753-
Arguments.of("[{\"city\":\"Auburn\"},{\"city\":\"Auburn\",\"countryId\":123}]")
754-
);
755-
}
756-
757-
@Test
758-
void getResultSet_shouldNotLocalizeCountryField_whenFieldPathIsEmpty() {
759-
UUID entityTypeId = UUID.randomUUID();
760-
UUID contentId = UUID.randomUUID();
761-
// Simulate a top-level country field with an empty field path
762-
EntityType entityType = new EntityType()
763-
.name("test_entity")
764-
.id(entityTypeId.toString())
765-
.columns(List.of(
766-
new EntityTypeColumn().name("id").isIdColumn(true),
767-
new EntityTypeColumn()
768-
.name("")
769-
.source(new SourceColumn(entityTypeId, "countryId")
770-
.type(SourceColumn.TypeEnum.FQM)
771-
.name("countries"))
772-
))
773-
.sources(List.of(new EntityTypeSourceDatabase().type("db").alias("source1").target("target1")));
774-
775-
List<String> fields = List.of("id", "");
776-
List<String> tenantIds = List.of("tenant_01");
777-
List<List<String>> listIds = List.of(List.of(contentId.toString()));
778-
String countryCode = "US";
779-
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, null, true)).thenReturn(entityType);
780-
when(entityTypeFlatteningService.getFlattenedEntityType(entityTypeId, "tenant_01", true)).thenReturn(entityType);
781-
when(settingsClient.getTenantTimezone()).thenReturn(ZoneId.of("UTC"));
782-
when(resultSetRepository.getResultSet(entityTypeId, fields, listIds, tenantIds))
783-
.thenReturn(List.of(Map.of("id", contentId.toString(), "", countryCode)));
784-
785-
List<Map<String, Object>> actual = service.getResultSet(entityTypeId, fields, listIds, tenantIds, true);
786-
787-
assertEquals(1, actual.size());
788-
assertEquals(contentId.toString(), actual.getFirst().get("id"));
789-
assertEquals(countryCode, actual.getFirst().get(""));
790-
verify(translationService, never()).format(anyString());
791-
}
792-
793774
@ParameterizedTest
794775
@MethodSource("missingCountryTranslationCases")
795776
void shouldNotLocalizeTopLevelCountryWhenTranslationIsMissing(String translationResult) {
@@ -834,4 +815,13 @@ static java.util.stream.Stream<Arguments> missingCountryTranslationCases() {
834815
Arguments.of("mod-fqm-manager.countries.US")
835816
);
836817
}
818+
819+
private static List<Arguments> arrayShouldNotChangeCases() {
820+
return List.of(
821+
// Element is not an object
822+
Arguments.of("[\"notAnObject\"]"),
823+
// Value node is null or not textual
824+
Arguments.of("[{\"city\":\"Auburn\"},{\"city\":\"Auburn\",\"countryId\":123}]")
825+
);
826+
}
837827
}

0 commit comments

Comments
 (0)