@Test
  public void shouldBeAbleToResolveSecureConfigPropertiesForSCMs() throws Exception {
    String encryptedValue = new GoCipher().encrypt("secure-two");
    String xml =
        "<cruise schemaVersion='"
            + GoConstants.CONFIG_SCHEMA_VERSION
            + "'>\n"
            + "<scms>\n"
            + "    <scm id='scm-id' name='name'>\n"
            + "		<pluginConfiguration id='plugin-id' version='1.0'/>\n"
            + "      <configuration>\n"
            + "        <property>\n"
            + "          <key>plain</key>\n"
            + "          <value>value</value>\n"
            + "        </property>\n"
            + "        <property>\n"
            + "          <key>secure-one</key>\n"
            + "          <value>secure-value</value>\n"
            + "        </property>\n"
            + "        <property>\n"
            + "          <key>secure-two</key>\n"
            + "          <encryptedValue>"
            + encryptedValue
            + "</encryptedValue>\n"
            + "        </property>\n"
            + "      </configuration>\n"
            + "    </scm>\n"
            + "  </scms>"
            + "<pipelines group=\"group_name\">\n"
            + "  <pipeline name=\"new_name\">\n"
            + "    <materials>\n"
            + "      <scm ref='scm-id' />\n"
            + "    </materials>\n"
            + "    <stage name=\"stage_name\">\n"
            + "      <jobs>\n"
            + "        <job name=\"job_name\" />\n"
            + "      </jobs>\n"
            + "    </stage>\n"
            + "  </pipeline>\n"
            + "</pipelines></cruise>";

    // meta data of scm
    SCMPropertyConfiguration scmConfiguration = new SCMPropertyConfiguration();
    scmConfiguration.add(new SCMProperty("plain"));
    scmConfiguration.add(new SCMProperty("secure-one").with(SCMConfiguration.SECURE, true));
    scmConfiguration.add(new SCMProperty("secure-two").with(SCMConfiguration.SECURE, true));
    SCMMetadataStore.getInstance()
        .addMetadataFor("plugin-id", new SCMConfigurations(scmConfiguration), null);

    GoConfigHolder goConfigHolder = xmlLoader.loadConfigHolder(xml);
    SCM scmConfig = goConfigHolder.config.getSCMs().first();
    PipelineConfig pipelineConfig =
        goConfigHolder.config.pipelineConfigByName(new CaseInsensitiveString("new_name"));
    PluggableSCMMaterialConfig pluggableSCMMaterialConfig =
        (PluggableSCMMaterialConfig) pipelineConfig.materialConfigs().get(0);
    assertThat(pluggableSCMMaterialConfig.getSCMConfig(), is(scmConfig));
    Configuration configuration = pluggableSCMMaterialConfig.getSCMConfig().getConfiguration();
    assertThat(configuration.get(0).getConfigurationValue().getValue(), is("value"));
    assertThat(
        configuration.get(1).getEncryptedValue().getValue(),
        is(new GoCipher().encrypt("secure-value")));
    assertThat(configuration.get(2).getEncryptedValue().getValue(), is(encryptedValue));
  }
  @Test
  public void shouldFailValidationIfSCMWithDuplicateFingerprintExists() throws Exception {
    SCMPropertyConfiguration scmConfiguration = new SCMPropertyConfiguration();
    scmConfiguration.add(new SCMProperty("SCM-KEY1"));
    scmConfiguration.add(
        new SCMProperty("SCM-KEY2").with(REQUIRED, false).with(PART_OF_IDENTITY, false));
    scmConfiguration.add(
        new SCMProperty("SCM-KEY3")
            .with(REQUIRED, false)
            .with(PART_OF_IDENTITY, false)
            .with(SECURE, true));
    SCMMetadataStore.getInstance()
        .addMetadataFor("plugin-1", new SCMConfigurations(scmConfiguration), null);

    String xml =
        "<cruise schemaVersion='"
            + GoConstants.CONFIG_SCHEMA_VERSION
            + "'>\n"
            + "<scms>\n"
            + "    <scm id='scm-id-1' name='name-1'>\n"
            + "		<pluginConfiguration id='plugin-1' version='1.0'/>\n"
            + "      <configuration>\n"
            + "        <property>\n"
            + "          <key>SCM-KEY1</key>\n"
            + "          <value>scm-key1</value>\n"
            + "        </property>\n"
            + "        <property>\n"
            + "          <key>SCM-KEY2</key>\n"
            + "          <value>scm-key2</value>\n"
            + "        </property>\n"
            + "        <property>\n"
            + "          <key>SCM-KEY3</key>\n"
            + "          <value>scm-key3</value>\n"
            + "        </property>\n"
            + "      </configuration>\n"
            + "    </scm>\n"
            + "    <scm id='scm-id-2' name='name-2'>\n"
            + "		<pluginConfiguration id='plugin-1' version='1.0'/>\n"
            + "      <configuration>\n"
            + "        <property>\n"
            + "          <key>SCM-KEY1</key>\n"
            + "          <value>scm-key1</value>\n"
            + "        </property>\n"
            + "        <property>\n"
            + "          <key>SCM-KEY2</key>\n"
            + "          <value>another-scm-key2</value>\n"
            + "        </property>\n"
            + "        <property>\n"
            + "          <key>SCM-KEY3</key>\n"
            + "          <value>another-scm-key3</value>\n"
            + "        </property>\n"
            + "      </configuration>\n"
            + "    </scm>\n"
            + "  </scms>"
            + "</cruise>";

    try {
      xmlLoader.loadConfigHolder(xml);
      fail("should have thrown duplicate fingerprint exception");
    } catch (GoConfigInvalidException e) {
      assertThat(e.getMessage(), is("Cannot save SCM, found duplicate SCMs. name-1, name-2"));
    }
  }