public class BuildParamDialogTest extends UISpecTestCase {

  @Mock private RequestManager requestManager;

  @Mock private BuildParamDialog.RunBuildCallback callbackRun;

  private JenkinsConfiguration configuration;

  private static final Job JOB_WITH_GOOD_PARAMS =
      new JobBuilder()
          .job("myJob", "blue", "http://dummyserver/jobs/myJob", "false", "true")
          .health("health-80plus", "0 tests en échec sur un total de 24 tests")
          .parameter("integrationTest", BooleanParameterDefinition.name(), "true")
          .parameter(
              "environment",
              ChoiceParameterDefinition.name(),
              "development",
              "development",
              "integration",
              "acceptance",
              "production")
          .parameter("message", StringParameterDefinition.name(), "")
          .get();

  private static final Job JOB_WITH_UNSUPPORTED_PARAMS =
      new JobBuilder()
          .job("myJob", "blue", "http://dummyserver/jobs/myJob", "false", "true")
          .health("health-80plus", "0 tests en échec sur un total de 24 tests")
          .parameter("run", RunParameterDefinition.name(), "blah")
          .get();

  private static final Job JOB_WITH_UNKNOWN_PARAMS =
      new JobBuilder()
          .job("myJob", "blue", "http://dummyserver/jobs/myJob", "false", "true")
          .health("health-80plus", "0 tests en échec sur un total de 24 tests")
          .parameter("run", null, "blah")
          .get();

  public void testDisplay() throws Exception {
    Window uispecDialog = createUISpecWindow(JOB_WITH_GOOD_PARAMS);

    assertEquals("This build requires parameters", uispecDialog.getTitle());

    assertTrue(uispecDialog.getCheckBox("integrationTest").isSelected());

    ComboBox envCombo = uispecDialog.getComboBox("environment");
    assertTrue(envCombo.contains("development", "integration", "acceptance", "production"));
    assertTrue(envCombo.selectionEquals("development"));
    assertTrue(StringUtils.isEmpty(uispecDialog.findSwingComponent(JTextField.class).getText()));

    assertTrue(uispecDialog.getButton("OK").isEnabled());
  }

  public void testLaunchBuild() throws Exception {
    Window uispecDialog = createUISpecWindow(JOB_WITH_GOOD_PARAMS);

    uispecDialog.getCheckBox("integrationTest").unselect();
    uispecDialog.getComboBox("environment").select("acceptance");

    uispecDialog.getButton("OK").click();

    ArgumentCaptor<Map> paramMap = ArgumentCaptor.forClass(Map.class);
    verify(requestManager, times(1))
        .runParameterizedBuild(any(Job.class), any(JenkinsConfiguration.class), paramMap.capture());

    Map expectedParamMapValue = paramMap.getValue();
    assertEquals(3, expectedParamMapValue.size());
    assertEquals("false", expectedParamMapValue.get("integrationTest"));
    assertEquals("acceptance", expectedParamMapValue.get("environment"));
    assertEquals("", expectedParamMapValue.get("message"));
  }

  public void testUnsupportedParams() throws Exception {
    Window uispecDialog = createUISpecWindow(JOB_WITH_UNSUPPORTED_PARAMS);

    TextBox runTextBox = uispecDialog.getTextBox(ComponentMatchers.componentLabelFor("run"));
    assertTrue(runTextBox.textEquals("RunParameterDefinition is unsupported."));
    assertTrue(runTextBox.iconEquals(GuiUtil.loadIcon("error.png")));

    assertFalse(uispecDialog.getButton("OK").isEnabled());
  }

  public void testUnknowParams() throws Exception {
    Window uispecDialog = createUISpecWindow(JOB_WITH_UNKNOWN_PARAMS);

    TextBox runTextBox = uispecDialog.getTextBox(ComponentMatchers.componentLabelFor("run"));
    assertTrue(runTextBox.textEquals("Unkown parameter"));
    assertTrue(runTextBox.iconEquals(GuiUtil.loadIcon("error.png")));

    assertFalse(uispecDialog.getButton("OK").isEnabled());
  }

  public void setUp() throws Exception {
    super.setUp();
    MockitoAnnotations.initMocks(this);
    configuration = new JenkinsConfiguration();
  }

  private Window createUISpecWindow(Job job) {
    return new Window(new BuildParamDialog(job, configuration, requestManager, callbackRun));
  }

  public void tearDown() throws Exception {
    super.tearDown();
  }
}