Skip to content

Commit

Permalink
Implement missing JpaRepository method deleteAllInBatch()
Browse files Browse the repository at this point in the history
  • Loading branch information
renegrob committed Dec 2, 2020
1 parent c0ac247 commit c7272ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import io.quarkus.spring.data.runtime.RepositorySupport;
import io.quarkus.spring.data.runtime.TypesConverter;


public class StockMethodsAdder {

private static Set<MethodInfo> ALL_SPRING_DATA_REPOSITORY_METHODS = null;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -922,6 +925,27 @@ private void generateDeleteAll(ClassCreator classCreator, FieldDescriptor entity
}
}

private void generateDeleteAllInBatch(ClassCreator classCreator, FieldDescriptor entityClassFieldDescriptor,
String generatedClassName, Map<MethodDescriptor, Boolean> 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<MethodDescriptor, Boolean> allMethodsToBeImplementedToResult) {
for (Map.Entry<MethodDescriptor, Boolean> entry : allMethodsToBeImplementedToResult.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> NOT_ADDED_OR_REMOVED = new HashSet<>(
Arrays.asList("Greece", "France", "Czechia"));

@Test
@Order(1)
void testAll() {
List<Country> countries = when().get("/country/all").then()
.statusCode(200)
Expand All @@ -37,6 +43,7 @@ void testAll() {
}

@Test
@Order(2)
void testPage() {
when().get("/country/page/1/0").then()
.statusCode(200)
Expand All @@ -56,6 +63,7 @@ void testPage() {
}

@Test
@Order(3)
void testPageSorted() {
String response = when().get("/country/page-sorted/2/0").then()
.statusCode(200)
Expand All @@ -65,6 +73,7 @@ void testPageSorted() {
}

@Test
@Order(4)
void testGetOne() {
when().get("/country/getOne/1").then()
.statusCode(200)
Expand All @@ -75,6 +84,7 @@ void testGetOne() {
}

@Test
@Order(5)
void testNewAndEditIso() {
when().get("/country/all").then()
.statusCode(200)
Expand Down Expand Up @@ -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));
}
}

0 comments on commit c7272ef

Please sign in to comment.