@Test public void testControlJvm() { final Jvm mockJvm = mock(Jvm.class); Identifier<Jvm> identifier = Identifier.id(1l, Jvm.class); when(jvmControlService.controlJvm(any(ControlJvmRequest.class), any(User.class))).thenReturn(new CommandOutput(new ExecReturnCode(0), "", "")); when(jvmService.getJvm(identifier)).thenReturn(mockJvm); final CommandOutput execData = mock(CommandOutput.class); final ExecReturnCode execDataReturnCode = mock(ExecReturnCode.class); when(execDataReturnCode.wasSuccessful()).thenReturn(true); when(execData.getReturnCode()).thenReturn(execDataReturnCode); final JsonControlJvm jsonControlJvm = new JsonControlJvm("start"); final Response response = jvmServiceRest.controlJvm(identifier, jsonControlJvm, null, null, authenticatedUser); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); final ApplicationResponse applicationResponse = (ApplicationResponse) response.getEntity(); final Object content = applicationResponse.getApplicationResponseContent(); assertTrue(content instanceof CommandOutput); when(execDataReturnCode.wasSuccessful()).thenReturn(false); when(execDataReturnCode.getReturnCode()).thenReturn(123); boolean exceptionThrown = false; when(jvmControlService.controlJvm(any(ControlJvmRequest.class), any(User.class))).thenReturn(new CommandOutput(execDataReturnCode, "", "Jvm Control Failed")); try { jvmServiceRest.controlJvm(Identifier.id(1l, Jvm.class), jsonControlJvm, null, null, authenticatedUser); } catch (Exception e) { exceptionThrown = true; assertEquals(CommandOutputReturnCode.NO_SUCH_SERVICE.getDesc(), e.getMessage()); } assertTrue(exceptionThrown); }
@Override public Response controlJvm(final Identifier<Jvm> aJvmId, final JsonControlJvm aJvmToControl, Boolean wait, Long waitTimeout, final AuthenticatedUser aUser) { LOGGER.debug("Control JVM requested: {} {} by user {} with wait = {} and timeout = {}s", aJvmId, aJvmToControl, aUser.getUser().getId(), wait, waitTimeout); final CommandOutput commandOutput; final ControlJvmRequest controlJvmRequest = ControlJvmRequestFactory.create(aJvmToControl.toControlOperation(), jvmService.getJvm(aJvmId)); if (Boolean.TRUE.equals(wait)) { waitTimeout = waitTimeout == null ? DEFAULT_WAIT_TIMEOUT : waitTimeout * 1000; try { commandOutput = jvmControlService.controlJvmSynchronously(controlJvmRequest, waitTimeout, aUser.getUser()); } catch (final InterruptedException | JvmControlServiceException e) { LOGGER.error("Control a JVM synchronously has failed!", e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, e.getMessage())); } } else { commandOutput = jvmControlService.controlJvm(controlJvmRequest, aUser.getUser()); } if (commandOutput.getReturnCode().wasSuccessful()) { return ResponseBuilder.ok(commandOutput); } else { final String standardError = commandOutput.getStandardError(); final String standardOutput = commandOutput.getStandardOutput(); String errMessage = standardError != null && !standardError.isEmpty() ? standardError : standardOutput; LOGGER.error("Control JVM unsuccessful: " + errMessage); throw new InternalErrorException(FaultType.CONTROL_OPERATION_UNSUCCESSFUL, CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc()); } }
JvmServiceRestImpl implements JvmServiceRest { @Override public Response controlJvm(final Identifier<Jvm> aJvmId, final JsonControlJvm aJvmToControl, Boolean wait, Long waitTimeout, final AuthenticatedUser aUser) { LOGGER.debug("Control JVM requested: {} {} by user {} with wait = {} and timeout = {}s", aJvmId, aJvmToControl, aUser.getUser().getId(), wait, waitTimeout); final CommandOutput commandOutput; final ControlJvmRequest controlJvmRequest = ControlJvmRequestFactory.create(aJvmToControl.toControlOperation(), jvmService.getJvm(aJvmId)); if (Boolean.TRUE.equals(wait)) { waitTimeout = waitTimeout == null ? DEFAULT_WAIT_TIMEOUT : waitTimeout * 1000; try { commandOutput = jvmControlService.controlJvmSynchronously(controlJvmRequest, waitTimeout, aUser.getUser()); } catch (final InterruptedException | JvmControlServiceException e) { LOGGER.error("Control a JVM synchronously has failed!", e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, e.getMessage())); } } else { commandOutput = jvmControlService.controlJvm(controlJvmRequest, aUser.getUser()); } if (commandOutput.getReturnCode().wasSuccessful()) { return ResponseBuilder.ok(commandOutput); } else { final String standardError = commandOutput.getStandardError(); final String standardOutput = commandOutput.getStandardOutput(); String errMessage = standardError != null && !standardError.isEmpty() ? standardError : standardOutput; LOGGER.error("Control JVM unsuccessful: " + errMessage); throw new InternalErrorException(FaultType.CONTROL_OPERATION_UNSUCCESSFUL, CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc()); } } }
JvmServiceRestImpl implements JvmServiceRest { @Override public Response controlJvm(final Identifier<Jvm> aJvmId, final JsonControlJvm aJvmToControl, Boolean wait, Long waitTimeout, final AuthenticatedUser aUser) { LOGGER.debug("Control JVM requested: {} {} by user {} with wait = {} and timeout = {}s", aJvmId, aJvmToControl, aUser.getUser().getId(), wait, waitTimeout); final CommandOutput commandOutput; final ControlJvmRequest controlJvmRequest = ControlJvmRequestFactory.create(aJvmToControl.toControlOperation(), jvmService.getJvm(aJvmId)); if (Boolean.TRUE.equals(wait)) { waitTimeout = waitTimeout == null ? DEFAULT_WAIT_TIMEOUT : waitTimeout * 1000; try { commandOutput = jvmControlService.controlJvmSynchronously(controlJvmRequest, waitTimeout, aUser.getUser()); } catch (final InterruptedException | JvmControlServiceException e) { LOGGER.error("Control a JVM synchronously has failed!", e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, e.getMessage())); } } else { commandOutput = jvmControlService.controlJvm(controlJvmRequest, aUser.getUser()); } if (commandOutput.getReturnCode().wasSuccessful()) { return ResponseBuilder.ok(commandOutput); } else { final String standardError = commandOutput.getStandardError(); final String standardOutput = commandOutput.getStandardOutput(); String errMessage = standardError != null && !standardError.isEmpty() ? standardError : standardOutput; LOGGER.error("Control JVM unsuccessful: " + errMessage); throw new InternalErrorException(FaultType.CONTROL_OPERATION_UNSUCCESSFUL, CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc()); } } JvmServiceRestImpl(final JvmService theJvmService,
                              final JvmControlService theJvmControlService); }
JvmServiceRestImpl implements JvmServiceRest { @Override public Response controlJvm(final Identifier<Jvm> aJvmId, final JsonControlJvm aJvmToControl, Boolean wait, Long waitTimeout, final AuthenticatedUser aUser) { LOGGER.debug("Control JVM requested: {} {} by user {} with wait = {} and timeout = {}s", aJvmId, aJvmToControl, aUser.getUser().getId(), wait, waitTimeout); final CommandOutput commandOutput; final ControlJvmRequest controlJvmRequest = ControlJvmRequestFactory.create(aJvmToControl.toControlOperation(), jvmService.getJvm(aJvmId)); if (Boolean.TRUE.equals(wait)) { waitTimeout = waitTimeout == null ? DEFAULT_WAIT_TIMEOUT : waitTimeout * 1000; try { commandOutput = jvmControlService.controlJvmSynchronously(controlJvmRequest, waitTimeout, aUser.getUser()); } catch (final InterruptedException | JvmControlServiceException e) { LOGGER.error("Control a JVM synchronously has failed!", e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, e.getMessage())); } } else { commandOutput = jvmControlService.controlJvm(controlJvmRequest, aUser.getUser()); } if (commandOutput.getReturnCode().wasSuccessful()) { return ResponseBuilder.ok(commandOutput); } else { final String standardError = commandOutput.getStandardError(); final String standardOutput = commandOutput.getStandardOutput(); String errMessage = standardError != null && !standardError.isEmpty() ? standardError : standardOutput; LOGGER.error("Control JVM unsuccessful: " + errMessage); throw new InternalErrorException(FaultType.CONTROL_OPERATION_UNSUCCESSFUL, CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc()); } } JvmServiceRestImpl(final JvmService theJvmService,
                              final JvmControlService theJvmControlService); @Override Response getJvms(); @Override Response getJvm(final Identifier<Jvm> aJvmId); @Override Response createJvm(final JsonCreateJvm jsonCreateJvm, final AuthenticatedUser aUser); @Override Response updateJvm(final JsonUpdateJvm aJvmToUpdate, final boolean updateJvmPassword, final AuthenticatedUser aUser); @Override Response deleteJvm(final Identifier<Jvm> id, final boolean hardDelete, final AuthenticatedUser user); @Override Response controlJvm(final Identifier<Jvm> aJvmId, final JsonControlJvm aJvmToControl, Boolean wait,
                               Long waitTimeout, final AuthenticatedUser aUser); @Override Response generateAndDeployJvm(final String jvmName, final AuthenticatedUser user); @Override Response upgradeJDK(final String jvmName, final AuthenticatedUser user); @Override Response generateAndDeployFile(final String jvmName, final String fileName, AuthenticatedUser user); @Override Response diagnoseJvm(Identifier<Jvm> aJvmId, final AuthenticatedUser aUser); @Override Response getResourceNames(final String jvmName); @Override Response getResourceTemplate(final String jvmName, final String resourceTemplateName,
                                        final boolean tokensReplaced); @Override Response updateResourceTemplate(final String jvmName, final String resourceTemplateName,
                                           final String content); @Override Response previewResourceTemplate(final String jvmName, final String fileName, final String groupName, final String template); }
JvmServiceRestImpl implements JvmServiceRest { @Override public Response controlJvm(final Identifier<Jvm> aJvmId, final JsonControlJvm aJvmToControl, Boolean wait, Long waitTimeout, final AuthenticatedUser aUser) { LOGGER.debug("Control JVM requested: {} {} by user {} with wait = {} and timeout = {}s", aJvmId, aJvmToControl, aUser.getUser().getId(), wait, waitTimeout); final CommandOutput commandOutput; final ControlJvmRequest controlJvmRequest = ControlJvmRequestFactory.create(aJvmToControl.toControlOperation(), jvmService.getJvm(aJvmId)); if (Boolean.TRUE.equals(wait)) { waitTimeout = waitTimeout == null ? DEFAULT_WAIT_TIMEOUT : waitTimeout * 1000; try { commandOutput = jvmControlService.controlJvmSynchronously(controlJvmRequest, waitTimeout, aUser.getUser()); } catch (final InterruptedException | JvmControlServiceException e) { LOGGER.error("Control a JVM synchronously has failed!", e); return ResponseBuilder.notOk(Response.Status.INTERNAL_SERVER_ERROR, new FaultCodeException(FaultType.SERVICE_EXCEPTION, e.getMessage())); } } else { commandOutput = jvmControlService.controlJvm(controlJvmRequest, aUser.getUser()); } if (commandOutput.getReturnCode().wasSuccessful()) { return ResponseBuilder.ok(commandOutput); } else { final String standardError = commandOutput.getStandardError(); final String standardOutput = commandOutput.getStandardOutput(); String errMessage = standardError != null && !standardError.isEmpty() ? standardError : standardOutput; LOGGER.error("Control JVM unsuccessful: " + errMessage); throw new InternalErrorException(FaultType.CONTROL_OPERATION_UNSUCCESSFUL, CommandOutputReturnCode.fromReturnCode(commandOutput.getReturnCode().getReturnCode()).getDesc()); } } JvmServiceRestImpl(final JvmService theJvmService,
                              final JvmControlService theJvmControlService); @Override Response getJvms(); @Override Response getJvm(final Identifier<Jvm> aJvmId); @Override Response createJvm(final JsonCreateJvm jsonCreateJvm, final AuthenticatedUser aUser); @Override Response updateJvm(final JsonUpdateJvm aJvmToUpdate, final boolean updateJvmPassword, final AuthenticatedUser aUser); @Override Response deleteJvm(final Identifier<Jvm> id, final boolean hardDelete, final AuthenticatedUser user); @Override Response controlJvm(final Identifier<Jvm> aJvmId, final JsonControlJvm aJvmToControl, Boolean wait,
                               Long waitTimeout, final AuthenticatedUser aUser); @Override Response generateAndDeployJvm(final String jvmName, final AuthenticatedUser user); @Override Response upgradeJDK(final String jvmName, final AuthenticatedUser user); @Override Response generateAndDeployFile(final String jvmName, final String fileName, AuthenticatedUser user); @Override Response diagnoseJvm(Identifier<Jvm> aJvmId, final AuthenticatedUser aUser); @Override Response getResourceNames(final String jvmName); @Override Response getResourceTemplate(final String jvmName, final String resourceTemplateName,
                                        final boolean tokensReplaced); @Override Response updateResourceTemplate(final String jvmName, final String resourceTemplateName,
                                           final String content); @Override Response previewResourceTemplate(final String jvmName, final String fileName, final String groupName, final String template);  }