Пример #1
0
  private Command<?> toCommand(String commandLine) throws ArgumentValidationException {
    String[] commandLineArray = CommandLines.parseArguments(commandLine);
    String commandName = commandLineArray[0];
    String[] commandArgs = Arrays.copyOfRange(commandLineArray, 1, commandLineArray.length);

    Class<?> optionsClass = commandRegistry.getOptionsClass(commandName);
    Command<Object> command = commandRegistry.newCommand(commandName);
    command.setOptions(CliFactory.parseArguments(optionsClass, commandArgs));
    return command;
  }
Пример #2
0
  @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);
  }