From a258d260e27ee5c524f9395748782501d0b533c9 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 23 Aug 2024 14:03:21 +0530 Subject: [PATCH] avniproject/avni-webapp#1306 - test scenarios for edit location --- .../BulkLocationEditorIntegrationTest.java | 123 +++++++++++++++--- .../src/test/resources/application.properties | 2 +- 2 files changed, 107 insertions(+), 18 deletions(-) diff --git a/avni-server-api/src/test/java/org/avni/server/importer/batch/csv/writer/BulkLocationEditorIntegrationTest.java b/avni-server-api/src/test/java/org/avni/server/importer/batch/csv/writer/BulkLocationEditorIntegrationTest.java index 8b747aa55..909614d38 100644 --- a/avni-server-api/src/test/java/org/avni/server/importer/batch/csv/writer/BulkLocationEditorIntegrationTest.java +++ b/avni-server-api/src/test/java/org/avni/server/importer/batch/csv/writer/BulkLocationEditorIntegrationTest.java @@ -1,6 +1,7 @@ package org.avni.server.importer.batch.csv.writer; import org.avni.server.application.*; +import org.avni.server.dao.AddressLevelTypeRepository; import org.avni.server.dao.LocationRepository; import org.avni.server.dao.application.FormRepository; import org.avni.server.domain.AddressLevel; @@ -15,17 +16,14 @@ import org.avni.server.service.builder.TestConceptService; import org.avni.server.service.builder.TestDataSetupService; import org.avni.server.service.builder.TestLocationService; -import org.junit.Ignore; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.jdbc.Sql; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Map; -import java.util.UUID; +import java.util.*; +import java.util.stream.IntStream; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; @Sql(value = {"/tear-down.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(value = {"/tear-down.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) @@ -72,10 +70,21 @@ public void setUp() throws Exception { ).build(); formRepository.save(locationForm); - AddressLevel bihar = testLocationService.save(new AddressLevelBuilder().title("Bihar").type(state).withUuid(UUID.randomUUID()).withDefaultValuesForNewEntity().build()); - AddressLevel vaishali = testLocationService.save(new AddressLevelBuilder().title("Vaishali").parent(bihar).type(district).withUuid(UUID.randomUUID()).withDefaultValuesForNewEntity().build()); - testLocationService.save(new AddressLevelBuilder().title("Mahua").parent(vaishali).type(block).withUuid(UUID.randomUUID()).withDefaultValuesForNewEntity().build()); - testLocationService.save(new AddressLevelBuilder().title("Gaya").parent(bihar).type(district).withUuid(UUID.randomUUID()).withDefaultValuesForNewEntity().build()); + AddressLevel bihar = testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Bihar").type(state).build()); + AddressLevel district1 = testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("District1").parent(bihar).type(district).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block11").parent(district1).type(block).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block12").parent(district1).type(block).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block13").parent(district1).type(block).build()); + + AddressLevel district2 = testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("District2").parent(bihar).type(district).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block21").parent(district2).type(block).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block22").parent(district2).type(block).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block23").parent(district2).type(block).build()); + + AddressLevel district3 = testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("District3").parent(bihar).type(district).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block31").parent(district3).type(block).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block32").parent(district3).type(block).build()); + testLocationService.save(new AddressLevelBuilder().withDefaultValuesForNewEntity().title("Block33").parent(district3).type(block).build()); setUser(organisationData.getUser().getUsername()); Map hierarchies = locationHierarchyService.determineAddressHierarchiesForAllAddressLevelTypesInOrg(); @@ -83,19 +92,99 @@ public void setUp() throws Exception { } private void lineageExists(String... lineage) { - AddressLevel address = this.locationRepository.findByTitleLineageIgnoreCase(String.join(", ", lineage)).get(); - assertNotNull(address); + Optional address = this.locationRepository.findByTitleLineageIgnoreCase(String.join(", ", lineage)); + assertTrue(address.isPresent()); + } + + private void lineageNotExists(String... lineage) { + Optional address = this.locationRepository.findByTitleLineageIgnoreCase(String.join(", ", lineage)); + assertFalse(address.isPresent()); + } + + private String[] verifyExists(String... strings) { + return strings; + } + + private String[] verifyNotExists(String... strings) { + return strings; } @Test - @Ignore public void shouldEdit() { - success(header("Location with full hierarchy","New location name","Parent location with full hierarchy","GPS coordinates"), - dataRow("Bihar, Vaishali, Mahua", "Mahnar", "Bihar, Vaishali", "23.45,43.85")); - lineageExists("Bihar", "Vaishali", "Mahnar"); + // no change + success(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"), + dataRow("Bihar, District1, Block11", "Block11", "Bihar, District1", "23.45,43.85"), + verifyExists("Bihar", "District1", "Block11")); + + // no change with spaces + success(header(" Location with full hierarchy ", " New location name", " Parent location with full hierarchy", " GPS coordinates"), + dataRow("Bihar, District1, Block11", "Block11", "Bihar, District1", "23.45,43.85"), + verifyExists("Bihar", "District1", "Block11")); + + // change name + success(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"), + dataRow("Bihar, District1, Block11", "Block11toNew", "Bihar, District1", "23.45,43.85"), + verifyExists("Bihar", "District1", "Block11toNew"), + verifyNotExists("Bihar, District1, Block11")); + + // change parent + success(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"), + dataRow("Bihar, District1, Block12", "Block24", "Bihar, District2", "23.45,43.85"), + verifyExists("Bihar", "District2", "Block24"), + verifyNotExists("Bihar, District1, Block12")); + + // existing location in different case + success(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"), + dataRow("Bihar, District1, Block13", "Block13new", "bihar, district1", "23.45,43.85"), + verifyExists("Bihar", "District1", "Block13new"), + verifyNotExists("Bihar, District1, Block13")); + + // lineage with spaces + failure(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"), + dataRow("Bihar, District3, Block31", " Block31New", "Bihar, District3", "23.45,43.85"), + error("Provided Location does not exist in Avni. Please add it or check for spelling mistakes 'Bihar, District3, Block31'"), + verifyExists("Bihar", "District3", "Block31"), + verifyNotExists("Bihar, District3, Block31New")); + + // change to non existing parent + failure(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"), + dataRow("Bihar, District2, Block21", " Block21Town", "Bihar, DistrictN", "23.45,43.85"), + error("Provided new Location parent does not exist in Avni. Please add it or check for spelling mistakes 'Bihar, DistrictN'"), + verifyExists("Bihar", "District2", "Block21"), + verifyNotExists("Bihar, District2, Block21Town")); + + treatAsDescriptor(header("Location with full hierarchy", "New location name", "Parent location with full hierarchy", "GPS coordinates"), + dataRow("Can be found from Admin -> Locations -> Click Export. Used to specify which location's fields need to be updated. mandatory field", + "Enter new name here ONLY if it needs to be updated", + "Hierarchy of parent location that should contain the child location", + "Ex: 23.45,43.85")); } - private void success(String[] headers, String[] dataRow) { + private void treatAsDescriptor(String[] headers, String... descriptorCells) { + long before = locationRepository.count(); + bulkLocationEditor.write(Collections.singletonList(new Row(headers, descriptorCells))); + long after = locationRepository.count(); + assertEquals(before, after); + } + + private void success(String[] headers, String[] dataRow, String[] exists, String[] ... notExists) { bulkLocationEditor.editLocation(new Row(headers, dataRow), new ArrayList<>()); + lineageExists(exists); + for (String[] notExist : notExists) { + lineageNotExists(notExist); + } + } + + private void failure(String[] headers, String[] dataRow, String errorMessage, String[] exists, String[] ... notExists) { + try { + bulkLocationEditor.editLocation(new Row(headers, dataRow), new ArrayList<>()); + fail(); + } catch (Exception exception) { + assertEquals(errorMessage, exception.getMessage()); + } + lineageExists(exists); + for (String[] notExist : notExists) { + lineageNotExists(notExist); + } } } diff --git a/avni-server-api/src/test/resources/application.properties b/avni-server-api/src/test/resources/application.properties index 14b8e97d4..b08725666 100644 --- a/avni-server-api/src/test/resources/application.properties +++ b/avni-server-api/src/test/resources/application.properties @@ -19,7 +19,7 @@ spring.flyway.schemas=public spring.flyway.baseline-on-migrate=false # JPA, Hibernate and Spring Data -spring.jpa.show-sql=true +spring.jpa.show-sql=false spring.jpa.hibernate.ddl-auto=none spring.jpa.properties.jadira.usertype.databaseZone=jvm spring.jpa.properties.jadira.usertype.javaZone=jvm