From 1daca829ec0781eb6df51288b94fa14ceeed8264 Mon Sep 17 00:00:00 2001 From: G_Ivo Date: Mon, 17 Jul 2023 03:17:58 -0400 Subject: [PATCH] fix(rest.command.service): separated async and non async api's to aline with output (#4764) * fix: seperated async and non async api's to aline with output * tests: added coverage for new rest endpoint --- .../rest/command/CommandRestService.java | 35 +++++++++++++-- .../rest/command/api/RestCommandRequest.java | 9 ---- .../provider/test/CommandRestServiceTest.java | 45 ++++++++++++++++--- 3 files changed, 71 insertions(+), 18 deletions(-) diff --git a/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/internal/rest/command/CommandRestService.java b/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/internal/rest/command/CommandRestService.java index 42a8e3338a0..1ab16084c0b 100644 --- a/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/internal/rest/command/CommandRestService.java +++ b/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/internal/rest/command/CommandRestService.java @@ -24,6 +24,7 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.eclipse.kura.KuraErrorCode; import org.eclipse.kura.KuraException; @@ -79,11 +80,39 @@ public RestCommandResponse execCommand(final RestCommandRequest restCommandPaylo } } + /** + * POST method. + * + * Run command with Async Executor service. + * + */ + @POST + @RolesAllowed("command") + @Path("/command/async") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + public Response execAsyncCommand(final RestCommandRequest restCommandPayload) { + try { + return doExecAsyncCommand(restCommandPayload); + } catch (Exception e) { + throw DefaultExceptionHandler.toWebApplicationException(e); + } + } + private RestCommandResponse doExecCommand(RestCommandRequest restCommandPayload) throws KuraException { - return buildRestCommandResponse(this.commandCloudApp.doExec(null, buildKuraMessage(restCommandPayload))); + return buildRestCommandResponse(this.commandCloudApp.doExec(null, buildKuraMessage(restCommandPayload, false))); + } + + private Response doExecAsyncCommand(RestCommandRequest restCommandPayload) throws KuraException { + try { + this.commandCloudApp.doExec(null, buildKuraMessage(restCommandPayload, true)); + return Response.accepted().build(); + } catch (Exception e) { + throw DefaultExceptionHandler.toWebApplicationException(e); + } } - private KuraMessage buildKuraMessage(RestCommandRequest restCommandPayload) { + private KuraMessage buildKuraMessage(RestCommandRequest restCommandPayload, boolean isAsync) { KuraCommandRequestPayload kuraCommandRequestPayload = new KuraCommandRequestPayload( restCommandPayload.getCommand()); @@ -95,7 +124,7 @@ private KuraMessage buildKuraMessage(RestCommandRequest restCommandPayload) { kuraCommandRequestPayload.setArguments(restCommandPayload.getArguments()); kuraCommandRequestPayload.setEnvironmentPairs(restCommandPayload.getEnvironmentPairsAsStringArray()); - kuraCommandRequestPayload.setRunAsync(restCommandPayload.getIsRunAsync()); + kuraCommandRequestPayload.setRunAsync(isAsync); Map payloadProperties = new HashMap<>(); payloadProperties.put(ARGS_KEY.value(), Arrays.asList(RESOURCE_COMMAND)); diff --git a/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/rest/command/api/RestCommandRequest.java b/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/rest/command/api/RestCommandRequest.java index 9a3ab5a5d69..a498ce54108 100644 --- a/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/rest/command/api/RestCommandRequest.java +++ b/kura/org.eclipse.kura.rest.command.provider/src/main/java/org/eclipse/kura/rest/command/api/RestCommandRequest.java @@ -23,7 +23,6 @@ public class RestCommandRequest { private String[] arguments; private Map environmentPairs; private String workingDirectory; - private boolean isRunAsync; public void setCommand(String command) { this.command = command; @@ -45,10 +44,6 @@ public void setPassword(String password) { this.password = password; } - public void setIsRunAsync(boolean isRunAsync) { - this.isRunAsync = isRunAsync; - } - public void setZipBytes(String zipBytes) { this.zipBytes = zipBytes; } @@ -81,10 +76,6 @@ public String getPassword() { return this.password; } - public boolean getIsRunAsync() { - return this.isRunAsync; - } - public String getZipBytes() { return this.zipBytes; } diff --git a/kura/test/org.eclipse.kura.rest.command.provider.test/src/main/java/org/eclipse/kura/rest/command/provider/test/CommandRestServiceTest.java b/kura/test/org.eclipse.kura.rest.command.provider.test/src/main/java/org/eclipse/kura/rest/command/provider/test/CommandRestServiceTest.java index d04bde76129..5d64e5ecac6 100644 --- a/kura/test/org.eclipse.kura.rest.command.provider.test/src/main/java/org/eclipse/kura/rest/command/provider/test/CommandRestServiceTest.java +++ b/kura/test/org.eclipse.kura.rest.command.provider.test/src/main/java/org/eclipse/kura/rest/command/provider/test/CommandRestServiceTest.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.Map; +import javax.ws.rs.core.Response; + import org.eclipse.kura.KuraException; import org.eclipse.kura.cloud.app.command.CommandCloudApp; import org.eclipse.kura.cloud.app.command.KuraCommandRequestPayload; @@ -55,6 +57,8 @@ public class CommandRestServiceTest { KuraMessage capturedKuraMessage; KuraCommandRequestPayload captureKuraCommandRequestPayload; + Response asyncResponse; + @Test public void commandExecNull() throws KuraException { givenCommandCloudApp(); @@ -104,7 +108,6 @@ public void commandExecWithFullParamsTest() throws KuraException { givenEnvironmentPairs(Collections.singletonMap("testvar", "testValue")); givenWorkingDirectory("/tmp"); givenPassword("password"); - givenIsRunAsync(true); givenZipBytes("emlwQnl0ZXM="); givenMockedResponseStdout("example stdout 3"); @@ -121,7 +124,7 @@ public void commandExecWithFullParamsTest() throws KuraException { thenEnvironmentPairsIs(Collections.singletonMap("testvar", "testValue")); thenWorkingDirectoryIs("/tmp"); thenPasswordIs("password"); - thenIsRunAsyncIs(true); + thenIsRunAsyncIs(false); thenZipBytesIs(new byte[] { 'z', 'i', 'p', 'B', 'y', 't', 'e', 's' }); thenVerifyKuraResponseStdoutIs("example stdout 3"); @@ -130,6 +133,36 @@ public void commandExecWithFullParamsTest() throws KuraException { thenVerifyKuraResponseTimedoutIs(false); } + @Test + public void commandExecAsycWithFullParamsTest() throws KuraException { + givenCommandCloudApp(); + givenCommandRestService(); + + givenCommand("ls"); + givenArguments(new String[] { "arg1", "arg2" }); + givenEnvironmentPairs(Collections.singletonMap("testvar", "testValue")); + givenWorkingDirectory("/tmp"); + givenPassword("password"); + givenZipBytes("emlwQnl0ZXM="); + + givenMockedResponseStdout("example stdout 3"); + givenMockedResponseStderr("example stderr 3"); + givenMockedResponseExitCode(0); + givenMockedResponseTimedout(false); + + whenExecAsyncCommand(); + + thenVerifyDoExecIsRun(); + thenCommandRequestIs(Arrays.asList("command")); + thenCommandIs("ls"); + thenArgumentsIs(new String[] { "arg1", "arg2" }); + thenEnvironmentPairsIs(Collections.singletonMap("testvar", "testValue")); + thenWorkingDirectoryIs("/tmp"); + thenPasswordIs("password"); + thenIsRunAsyncIs(true); + thenZipBytesIs(new byte[] { 'z', 'i', 'p', 'B', 'y', 't', 'e', 's' }); + } + private void givenCommandCloudApp() throws KuraException { commandCloudApp = mock(CommandCloudApp.class); @@ -180,10 +213,6 @@ private void givenPassword(String password) { this.restCommandRequest.setPassword(password); } - public void givenIsRunAsync(boolean isRunAsync) { - this.restCommandRequest.setIsRunAsync(isRunAsync); - } - private void givenZipBytes(String zipBytes) { this.restCommandRequest.setZipBytes(zipBytes); } @@ -192,6 +221,10 @@ private void whenExecCommand() { restCommandResponse = commandRestService.execCommand(this.restCommandRequest); } + private void whenExecAsyncCommand() { + asyncResponse = commandRestService.execAsyncCommand(this.restCommandRequest); + } + private void thenVerifyDoExecIsRun() throws KuraException { verify(commandCloudApp).doExec(any(), kuraPayloadArgumentCaptor.capture()); }