From c7272ef4f462cbeca340b3ae2317cbf0ccffafe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Grob?= Date: Mon, 16 Nov 2020 11:32:27 +0100 Subject: [PATCH] Implement missing JpaRepository method deleteAllInBatch() --- .../generate/StockMethodsAdder.java | 24 +++++++++++++++++++ .../it/spring/data/jpa/CountryResource.java | 7 ++++++ .../spring/data/jpa/CountryResourceTest.java | 21 ++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/StockMethodsAdder.java b/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/StockMethodsAdder.java index 546c2cde145ba..1efc88a7831f6 100644 --- a/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/StockMethodsAdder.java +++ b/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/StockMethodsAdder.java @@ -51,6 +51,7 @@ import io.quarkus.spring.data.runtime.RepositorySupport; import io.quarkus.spring.data.runtime.TypesConverter; + public class StockMethodsAdder { private static Set ALL_SPRING_DATA_REPOSITORY_METHODS = null; @@ -110,6 +111,8 @@ public void add(ClassCreator classCreator, FieldDescriptor entityClassFieldDescr generateDelete(classCreator, generatedClassName, entityTypeStr, allMethodsToBeImplementedToResult); generateDeleteAllWithIterable(classCreator, generatedClassName, entityTypeStr, allMethodsToBeImplementedToResult); generateDeleteAll(classCreator, entityClassFieldDescriptor, generatedClassName, allMethodsToBeImplementedToResult); + generateDeleteAllInBatch(classCreator, entityClassFieldDescriptor, generatedClassName, + allMethodsToBeImplementedToResult); handleUnimplementedMethods(classCreator, allMethodsToBeImplementedToResult); } @@ -922,6 +925,27 @@ private void generateDeleteAll(ClassCreator classCreator, FieldDescriptor entity } } + private void generateDeleteAllInBatch(ClassCreator classCreator, FieldDescriptor entityClassFieldDescriptor, + String generatedClassName, Map allMethodsToBeImplementedToResult) { + + MethodDescriptor deleteAllInBatchDescriptor = MethodDescriptor.ofMethod(generatedClassName, "deleteAllInBatch", + void.class); + + if (allMethodsToBeImplementedToResult.containsKey(deleteAllInBatchDescriptor)) { + if (!classCreator.getExistingMethods().contains(deleteAllInBatchDescriptor)) { + try (MethodCreator deleteAll = classCreator.getMethodCreator(deleteAllInBatchDescriptor)) { + deleteAll.addAnnotation(Transactional.class); + ResultHandle result = deleteAll.invokeVirtualMethod( + MethodDescriptor.ofMethod(AbstractJpaOperations.class, "deleteAll", long.class, Class.class), + deleteAll.readStaticField(operationsField), + deleteAll.readInstanceField(entityClassFieldDescriptor, deleteAll.getThis())); + deleteAll.returnValue(result); + } + } + allMethodsToBeImplementedToResult.put(deleteAllInBatchDescriptor, true); + } + } + private void handleUnimplementedMethods(ClassCreator classCreator, Map allMethodsToBeImplementedToResult) { for (Map.Entry entry : allMethodsToBeImplementedToResult.entrySet()) { diff --git a/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/CountryResource.java b/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/CountryResource.java index 4a7ef5cde549e..23ab25f8491b9 100644 --- a/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/CountryResource.java +++ b/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/CountryResource.java @@ -5,6 +5,7 @@ import java.util.stream.Collectors; import javax.persistence.NoResultException; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -73,4 +74,10 @@ public Country editIso3(@PathParam("id") Long id, @PathParam("iso3") String iso3 public Country getOne(@PathParam("id") Long id) { return countryRepository.getOne(id); } + + @DELETE + @Path("/") + public void deleteAllInBatch() { + this.countryRepository.deleteAllInBatch(); + } } diff --git a/integration-tests/spring-data-jpa/src/test/java/io/quarkus/it/spring/data/jpa/CountryResourceTest.java b/integration-tests/spring-data-jpa/src/test/java/io/quarkus/it/spring/data/jpa/CountryResourceTest.java index d73258505604a..c3e5c3dafb9bf 100644 --- a/integration-tests/spring-data-jpa/src/test/java/io/quarkus/it/spring/data/jpa/CountryResourceTest.java +++ b/integration-tests/spring-data-jpa/src/test/java/io/quarkus/it/spring/data/jpa/CountryResourceTest.java @@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; @@ -15,17 +16,22 @@ import java.util.Set; import java.util.stream.Collectors; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import io.quarkus.test.junit.QuarkusTest; @QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class CountryResourceTest { private static final Set NOT_ADDED_OR_REMOVED = new HashSet<>( Arrays.asList("Greece", "France", "Czechia")); @Test + @Order(1) void testAll() { List countries = when().get("/country/all").then() .statusCode(200) @@ -37,6 +43,7 @@ void testAll() { } @Test + @Order(2) void testPage() { when().get("/country/page/1/0").then() .statusCode(200) @@ -56,6 +63,7 @@ void testPage() { } @Test + @Order(3) void testPageSorted() { String response = when().get("/country/page-sorted/2/0").then() .statusCode(200) @@ -65,6 +73,7 @@ void testPageSorted() { } @Test + @Order(4) void testGetOne() { when().get("/country/getOne/1").then() .statusCode(200) @@ -75,6 +84,7 @@ void testGetOne() { } @Test + @Order(5) void testNewAndEditIso() { when().get("/country/all").then() .statusCode(200) @@ -102,4 +112,15 @@ void testNewAndEditIso() { when().get("/country/editIso3/100/ZZZ").then() .statusCode(500); } + + @Test + @Order(6) + void testDeleteAllInBatch() { + when().delete("/country").then() + .statusCode(204); + + when().get("/country/all").then() + .statusCode(200) + .body("size()", equalTo(0)); + } }