@Test
  public void testGetCommand_ReturnsResponseContainingCommandStateDtoOfSpecifiedJob() {
    // Setup
    Integer jobId = 1;
    CommandJob job = createCommandJob(jobId, createImportCommand(), new Date(1l));
    CommandJobService mockCommandJobService = createMock(CommandJobService.class);
    expect(mockCommandJobService.getCommand(jobId)).andReturn(job).atLeastOnce();

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);

    replay(mockCommandJobService);

    // Exercise
    Response response = sut.getCommand(jobId);

    // Verify mocks
    verify(mockCommandJobService);

    // Verify that the HTTP response code was OK (200) and that the body contains
    // the CommandStateDto of the specified task.
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
    assertThat(response.getEntity()).isNotNull();
    assertThat(containsDtoForJob(response, job)).isTrue();
  }
  private void testGetCommands(List<CommandJob> commandJobList) {
    // Setup
    CommandJobService mockCommandJobService = createMockCommandJobService();
    expect(mockCommandJobService.getHistory()).andReturn(commandJobList).atLeastOnce();

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);

    replay(mockCommandJobService);

    // Exercise
    List<CommandStateDto> commandStateDtoList = sut.getCommands();

    // Verify behaviour
    verify(mockCommandJobService);

    // Verify state
    assertThat(commandStateDtoList).isNotNull();
    assertDtoListMatchesJobList(commandStateDtoList, commandJobList);
  }
Beispiel #3
0
  @Override
  @SuppressWarnings("OverlyStrongTypeCast")
  public void execute(JobExecutionContext context) throws JobExecutionException {
    autowireSelf(context);

    Command<?> command = getCommand(context);
    CommandJob commandJob = new CommandJob(command);
    if (command instanceof ReportCommand) {
      commandJob.setProject(((ReportCommand) command).getOptions().getProject());
    }
    commandJobService.launchCommand(commandJob, getSubject(context));
  }
  @Test
  public void testGetCommand_ReturnsNotFoundResponseIfJobDoesNotExist() {
    // Setup
    Integer bogusJobId = 1;
    CommandJobService mockCommandJobService = createMockCommandJobService();
    expect(mockCommandJobService.getCommand(bogusJobId)).andReturn(null).atLeastOnce();

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);

    replay(mockCommandJobService);

    // Exercise
    Response response = sut.getCommand(bogusJobId);

    // Verify mocks
    verify(mockCommandJobService);

    // Verify that the HTTP response code was NOT FOUND (404).
    assertThat(response.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
  }
  @Test
  public void testDeleteCompletedCommands() {
    // Setup
    CommandJobService mockCommandJobService = createMockCommandJobService();
    mockCommandJobService.deleteCompletedCommands();
    expectLastCall().once();

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);

    replay(mockCommandJobService);

    // Exercise
    Response response = sut.deleteCompletedCommands();

    // Verify mocks
    verify(mockCommandJobService);

    // Verify that the HTTP response code was OK (200).
    assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
  }
  @Test
  public void testCreateReport() {
    // Setup
    Integer jobId = 1;
    CommandRegistry mockCommandRegistry = createMockCommandRegistry();
    ReportCommand reportCommand = createReportCommand();
    expect(mockCommandRegistry.<ReportCommandOptions>newCommand(reportCommand.getName()))
        .andReturn(reportCommand)
        .atLeastOnce();

    CommandJobService mockCommandJobService = createMockCommandJobService();
    expect(
            mockCommandJobService.launchCommand(
                eqCommandJob(createCommandJob(jobId, reportCommand, null))))
        .andReturn(jobId)
        .atLeastOnce();

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);
    sut.setCommandRegistry(mockCommandRegistry);

    replay(mockCommandRegistry, mockCommandJobService);

    // Exercise
    ReportCommandOptionsDto optionsDto = createReportCommandOptionsDto("test report", "project1");
    Response response = sut.createReport(optionsDto);

    // Verify mocks
    verify(mockCommandRegistry, mockCommandJobService);

    // Verify that the options in the dto were applied to the launched command
    ReportCommandOptions importOptions = reportCommand.getOptions();
    assertThat(optionsDto.getName()).isEqualTo(importOptions.getName());

    // Verify that the HTTP response code was CREATED (201) and that the "Location"
    // header was set to '/shell/command/{jobId}'.
    assertThat(response.getStatus()).isEqualTo(Response.Status.CREATED.getStatusCode());
    assertThat(response.getMetadata().getFirst("Location").toString())
        .isEqualTo("/shell/command/" + jobId);
  }
  @Test
  public void testDeleteCommand_ReturnsBadRequestResponseIfJobIsNotDeletable() {
    // Setup
    Integer jobId = 1;
    CommandJobService mockCommandJobService = createMockCommandJobService();
    mockCommandJobService.deleteCommand(jobId);
    expectLastCall().andThrow(new IllegalStateException());

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);

    replay(mockCommandJobService);

    // Exercise
    Response response = sut.deleteCommand(jobId);

    // Verify mocks
    verify(mockCommandJobService);

    // Verify that the HTTP response code was BAD REQUEST (400).
    assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode());
  }
  @Test
  public void testSetCommandStatus_ReturnsNotFoundResponseIfJobDoesNotExist() {
    // Setup
    Integer bogusJobId = 1;
    CommandJobService mockCommandJobService = createMockCommandJobService();
    mockCommandJobService.cancelCommand(bogusJobId);
    expectLastCall().andThrow(new NoSuchCommandJobException(bogusJobId));

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);

    replay(mockCommandJobService);

    // Exercise
    Response response = sut.setCommandStatus(bogusJobId, Status.CANCELED.toString());

    // Verify mocks
    verify(mockCommandJobService);

    // Verify that the HTTP response code was NOT FOUND (404).
    assertThat(response.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
  }
  @Test
  public void testSetCommandStatus() {
    // Setup
    Integer jobId = 1;
    CommandJobService mockCommandJobService = createMock(CommandJobService.class);
    mockCommandJobService.cancelCommand(jobId);
    expectLastCall().atLeastOnce();

    WebShellResource sut = new WebShellResource();
    sut.setCommandJobService(mockCommandJobService);

    replay(mockCommandJobService);

    // Exercise
    Response response = sut.setCommandStatus(jobId, Status.CANCELED.toString());

    // Verify mocks
    verify(mockCommandJobService);

    // Verify that the HTTP response code was OK (200).
    assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
  }