From 8cdd64981346345a89f55104d64fe0fa12b28195 Mon Sep 17 00:00:00 2001 From: Andrus Adamchik Date: Wed, 17 Aug 2022 12:52:31 +0200 Subject: [PATCH] Ag. response eats exceptions thrown while calculating attributes #576 test ... works correctly --- .../GET_EntityOverlay_PerRequestIT.java | 28 ++++++++++++++++++ .../io/agrest/meta/AgEntityOverlayTest.java | 29 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/agrest-cayenne/src/test/java/io/agrest/cayenne/GET_EntityOverlay_PerRequestIT.java b/agrest-cayenne/src/test/java/io/agrest/cayenne/GET_EntityOverlay_PerRequestIT.java index 62a4f7b34..1a1dd09ae 100644 --- a/agrest-cayenne/src/test/java/io/agrest/cayenne/GET_EntityOverlay_PerRequestIT.java +++ b/agrest-cayenne/src/test/java/io/agrest/cayenne/GET_EntityOverlay_PerRequestIT.java @@ -23,6 +23,7 @@ import javax.ws.rs.core.Configuration; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; +import java.util.function.Function; public class GET_EntityOverlay_PerRequestIT extends DbTest { @@ -187,6 +188,16 @@ public void test_OverlaidExclude() { .bodyEquals(1, "{\"id\":1,\"name\":\"N\"}"); } + @Test + public void test_OverlaidDataReaderException() { + + tester.e4().insertColumns("id", "c_varchar").values(2, "a").values(4, "b").exec(); + + tester.target("/e4_with_exception") + .get() + .wasServerError(); + } + @Path("") public static class Resource { @@ -264,5 +275,22 @@ public DataResponse getE2_With_exclude(@Context UriInfo uriInfo) { .clientParams(uriInfo.getQueryParameters()) .get(); } + + @GET + @Path("e4_with_exception") + public DataResponse getE4_withException(@Context UriInfo uriInfo) { + + Function throwing = e4 -> { + throw new RuntimeException("testing this exception"); + }; + + AgEntityOverlay overlay = AgEntity.overlay(E4.class).attribute("ax", String.class, throwing); + + return AgJaxrs + .select(E4.class, config) + .entityOverlay(overlay) + .clientParams(uriInfo.getQueryParameters()) + .get(); + } } } diff --git a/agrest-engine/src/test/java/io/agrest/meta/AgEntityOverlayTest.java b/agrest-engine/src/test/java/io/agrest/meta/AgEntityOverlayTest.java index f8992fdb7..7daa0d9ea 100644 --- a/agrest-engine/src/test/java/io/agrest/meta/AgEntityOverlayTest.java +++ b/agrest-engine/src/test/java/io/agrest/meta/AgEntityOverlayTest.java @@ -8,6 +8,7 @@ import io.agrest.access.ReadFilter; import io.agrest.access.UpdateAuthorizer; import io.agrest.pojo.model.P1; +import io.agrest.reader.DataReader; import io.agrest.resolver.RootDataResolver; import io.agrest.runtime.processor.select.SelectContext; import org.apache.cayenne.di.Injector; @@ -16,6 +17,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.function.Function; import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; @@ -47,6 +49,33 @@ public void testResolve_attribute() { assertEquals(1, eo.getAttributes().size()); } + @Test + public void testResolve_attribute_throwingFunction() { + RootDataResolver r1 = mock(RootDataResolver.class); + + AgEntity e = new DefaultEntity<>( + "p1", P1.class, + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), + r1, + ReadFilter.allowsAllFilter(), + CreateAuthorizer.allowsAllFilter(), + UpdateAuthorizer.allowsAllFilter(), + DeleteAuthorizer.allowsAllFilter() + ); + + Function throwing = p1 -> { + throw new RuntimeException("testing this exception"); + }; + + AgEntityOverlay attributeOverlay = AgEntity + .overlay(P1.class) + .attribute("a1", String.class, throwing); + + AgEntity eo = attributeOverlay.resolve(mock(AgSchema.class), e); + DataReader reader = eo.getAttribute("a1").getDataReader(); + assertThrows(RuntimeException.class, () -> reader.read(new P1())); + } + @Test public void testResolve_RootResolver() { RootDataResolver r1 = mock(RootDataResolver.class);