Skip to content

Commit

Permalink
fix(rest.command.service): separated async and non async api's to ali…
Browse files Browse the repository at this point in the history
…ne with output (#4764)

* fix: seperated async and non async api's to aline with output

* tests: added coverage for new rest endpoint
  • Loading branch information
GregoryIvo authored Jul 17, 2023
1 parent 05d6688 commit 1daca82
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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<String, Object> payloadProperties = new HashMap<>();
payloadProperties.put(ARGS_KEY.value(), Arrays.asList(RESOURCE_COMMAND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class RestCommandRequest {
private String[] arguments;
private Map<String, String> environmentPairs;
private String workingDirectory;
private boolean isRunAsync;

public void setCommand(String command) {
this.command = command;
Expand All @@ -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;
}
Expand Down Expand Up @@ -81,10 +76,6 @@ public String getPassword() {
return this.password;
}

public boolean getIsRunAsync() {
return this.isRunAsync;
}

public String getZipBytes() {
return this.zipBytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +57,8 @@ public class CommandRestServiceTest {
KuraMessage capturedKuraMessage;
KuraCommandRequestPayload captureKuraCommandRequestPayload;

Response asyncResponse;

@Test
public void commandExecNull() throws KuraException {
givenCommandCloudApp();
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
Expand All @@ -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());
}
Expand Down

0 comments on commit 1daca82

Please sign in to comment.