From ef6eed69e230e2cd54e7f7995281bfd10ecf1e7c Mon Sep 17 00:00:00 2001 From: Joy A Date: Fri, 25 Oct 2024 18:09:07 +0530 Subject: [PATCH] #785 | Fix bug (code review comment) and add tests --- .../server/service/AddressLevelService.java | 6 ++- .../AddressLevelServiceIntegrationTest.java | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java b/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java index d9a4625d9..fbfd59d40 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/AddressLevelService.java @@ -155,10 +155,14 @@ public Optional findByAddressMap(Map addressMap) { } String typeHierarchyForAddressMap = addressLevels.stream().map(al -> String.valueOf(al.getTypeId())).collect(Collectors.joining(".")); + String lineageForAddressMap = addressLevels.stream().map(al -> String.valueOf(al.getId())).collect(Collectors.joining(".")); TreeSet addressLevelTypeHierarchies = locationHierarchyService.fetchAndFilterHierarchies(); if (addressLevelTypeHierarchies.stream().anyMatch(hierarchy -> hierarchy.contains(typeHierarchyForAddressMap))) { - return Optional.of(addressLevels.get(addressLevels.size() - 1)); + AddressLevel matchedAddressLevel = addressLevels.get(addressLevels.size() - 1); + if (matchedAddressLevel.getLineage().equals(lineageForAddressMap)) { + return Optional.of(matchedAddressLevel); + } } return Optional.empty(); } diff --git a/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java b/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java index ea565f2fa..0c4d34db7 100644 --- a/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java +++ b/avni-server-api/src/test/java/org/avni/server/service/AddressLevelServiceIntegrationTest.java @@ -13,7 +13,9 @@ import org.springframework.test.context.jdbc.Sql; import java.util.Arrays; +import java.util.HashMap; import java.util.Map; +import java.util.Optional; import static org.junit.Assert.assertEquals; @@ -45,4 +47,44 @@ public void getTitleLineages() { assertEquals("karnataka, bangalore", titleLineages.get(bangalore.getId())); assertEquals("kerala, kochi", titleLineages.get(kochi.getId())); } + + @Test + public void findByAddressMap() { + testDataSetupService.setupOrganisation(); + AddressLevelType grandParent = addressLevelTypeRepository.save(new AddressLevelTypeBuilder().withUuid("GP").name("GP").level(3d).build()); + AddressLevelType parent = addressLevelTypeRepository.save(new AddressLevelTypeBuilder().withUuid("Parent").name("Parent").parent(grandParent).level(2d).build()); + AddressLevelType child = addressLevelTypeRepository.save(new AddressLevelTypeBuilder().withUuid("Child").name("Child").parent(parent).level(1d).build()); + + AddressLevel gp1AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("gp1").title("gp1").type(grandParent).build()); + AddressLevel parent1AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("parent1").title("parent1").type(parent).parent(gp1AddressLevel).build()); + AddressLevel child1AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("child1").title("child1").type(child).parent(parent1AddressLevel).build()); + AddressLevel gp2AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("gp2").title("gp2").type(grandParent).build()); + AddressLevel parent2AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("parent2").title("parent2").type(parent).parent(gp2AddressLevel).build()); + AddressLevel child2AddressLevel = testLocationService.save(new AddressLevelBuilder().withUuid("child2").title("child2").type(child).parent(parent2AddressLevel).build()); + Map addressLevelMap = new HashMap<>(); + + //valid hierarchy + addressLevelMap.put(parent.getName(), parent1AddressLevel.getTitle()); + addressLevelMap.put(child.getName(), child1AddressLevel.getTitle()); + addressLevelMap.put(grandParent.getName(), gp1AddressLevel.getTitle()); + assertEquals(child1AddressLevel, addressLevelService.findByAddressMap(addressLevelMap).get()); + + //valid locations in valid hierarchy but not related + addressLevelMap.put(grandParent.getName(), gp2AddressLevel.getTitle()); + assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); + addressLevelMap.put(child.getName(), child2AddressLevel.getTitle()); + assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); + + addressLevelMap.put(parent.getName(), parent2AddressLevel.getTitle()); + assertEquals(child2AddressLevel, addressLevelService.findByAddressMap(addressLevelMap).get()); + + //case insensitive + addressLevelMap.remove(parent.getName()); + addressLevelMap.put(parent.getName().toUpperCase(), parent2AddressLevel.getTitle().toUpperCase()); + assertEquals(child2AddressLevel, addressLevelService.findByAddressMap(addressLevelMap).get()); + + //partial hierarchy + addressLevelMap.remove(grandParent.getName()); + assertEquals(Optional.empty(), addressLevelService.findByAddressMap(addressLevelMap)); + } }