Do not use WebApplicationException to report 204 13/107913/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 18 Sep 2023 19:13:57 +0000 (21:13 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 19 Sep 2023 12:26:20 +0000 (12:26 +0000)
Use plain Responses to complete the AsyncResponse here, bypassing the
need to create an exception.

Change-Id: I73457c0a2147035445dd5e5252038c8d6021088a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImpl.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImplTest.java

index 660a5b8a69fc073672fa8e6853f9138f2f11d8c1..cbd309685573490555203d39dd176db939bad968 100644 (file)
@@ -23,12 +23,11 @@ import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
-import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.container.AsyncResponse;
 import javax.ws.rs.container.Suspended;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
@@ -184,9 +183,9 @@ public final class RestconfInvokeOperationsServiceImpl {
 
                 final ContainerNode resultData = response.value();
                 if (resultData == null || resultData.isEmpty()) {
-                    ar.resume(new WebApplicationException(Status.NO_CONTENT));
+                    ar.resume(Response.noContent().build());
                 } else {
-                    ar.resume(new NormalizedNodePayload(context.inference(), resultData));
+                    ar.resume(Response.ok().entity(new NormalizedNodePayload(context.inference(), resultData)).build());
                 }
             }
 
index 63a56a10c8e963b295bf7577d87d74032167371c..ea8b545a5398c119419b6790de82e0dd9558e5e7 100644 (file)
@@ -7,8 +7,6 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
@@ -28,9 +26,8 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
-import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.container.AsyncResponse;
-import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -100,13 +97,16 @@ public class RestconfInvokeOperationsServiceImplTest {
 
         prepNNC(result);
         final var ar = mock(AsyncResponse.class);
-        final var response = ArgumentCaptor.forClass(NormalizedNodePayload.class);
+        final var captor = ArgumentCaptor.forClass(Response.class);
         invokeOperationsService.invokeRpcXML("invoke-rpc-module:rpc-test", new ByteArrayInputStream("""
             <input xmlns="invoke:rpc:module"/>
             """.getBytes(StandardCharsets.UTF_8)), mock(UriInfo.class), ar);
-        verify(ar).resume(response.capture());
+        verify(ar).resume(captor.capture());
 
-        assertSame(result, response.getValue().data());
+        final var response = captor.getValue();
+        assertEquals(200, response.getStatus());
+        final var entity = (NormalizedNodePayload) response.getEntity();
+        assertSame(result, entity.data());
     }
 
     @Test
@@ -116,18 +116,17 @@ public class RestconfInvokeOperationsServiceImplTest {
 
         prepNNC(result);
         final var ar = mock(AsyncResponse.class);
-        final var response = ArgumentCaptor.forClass(Throwable.class);
+        final var captor = ArgumentCaptor.forClass(Response.class);
         invokeOperationsService.invokeRpcJSON("invoke-rpc-module:rpc-test", new ByteArrayInputStream("""
             {
               "invoke-rpc-module:input" : {
               }
             }
             """.getBytes(StandardCharsets.UTF_8)), mock(UriInfo.class), ar);
-        verify(ar).resume(response.capture());
+        verify(ar).resume(captor.capture());
 
-        final Throwable failure = response.getValue();
-        assertThat(failure, instanceOf(WebApplicationException.class));
-        assertEquals(Status.NO_CONTENT.getStatusCode(), ((WebApplicationException) failure).getResponse().getStatus());
+        final var response = captor.getValue();
+        assertEquals(204, response.getStatus());
     }
 
     @Test