public SCMConfigurations(SCMPropertyConfiguration configuration) { for (Property property : configuration.list()) { configurations.add(new SCMConfiguration(property)); } }
@Test public void shouldSortByDisplayOrder() throws Exception { SCMConfiguration p1 = new SCMConfiguration("k1").with(SCMConfiguration.DISPLAY_ORDER, 1); SCMConfiguration p2 = new SCMConfiguration("k2").with(SCMConfiguration.DISPLAY_ORDER, 3); assertThat(p2.compareTo(p1), is(2)); } }
@Test public void shouldGetAllSCMsSortedByDisplayOrder() throws Exception { SCMConfiguration c1 = new SCMConfiguration("k1").with(SCMConfiguration.DISPLAY_ORDER, 2); SCMConfiguration c2 = new SCMConfiguration("k2").with(SCMConfiguration.DISPLAY_ORDER, 0); SCMConfiguration c3 = new SCMConfiguration("k3").with(SCMConfiguration.DISPLAY_ORDER, 1); SCMConfigurations scmConfigurations = new SCMConfigurations(); scmConfigurations.add(c1); scmConfigurations.add(c2); scmConfigurations.add(c3); List<SCMConfiguration> scmConfigurationList = scmConfigurations.list(); assertThat(scmConfigurationList.get(0), is(c2)); assertThat(scmConfigurationList.get(1), is(c3)); assertThat(scmConfigurationList.get(2), is(c1)); }
@Test public void shouldGetOptionIfAvailable() { SCMConfiguration scmConfiguration = new SCMConfiguration("key"); scmConfiguration.with(SCMConfiguration.REQUIRED, true); assertThat(scmConfiguration.hasOption(SCMConfiguration.REQUIRED), is(true)); assertThat(scmConfiguration.hasOption(SCMConfiguration.SECURE), is(false)); }
@Test public void shouldNotOverwriteValuesIfTheyAreNotAvailableInConfigAttributesMap() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); scmConfigurations.add(new SCMConfiguration("KEY1")); scmConfigurations.add(new SCMConfiguration("Key2")); SCMPreference scmPreference = mock(SCMPreference.class); when(scmPreference.getScmConfigurations()).thenReturn(scmConfigurations); SCMMetadataStore.getInstance().setPreferenceFor("plugin-id", scmPreference); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"), ConfigurationPropertyMother.create("Key2")); SCM scm = new SCM("scm-id", new PluginConfiguration("plugin-id", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1"); scm.setConfigAttributes(attributeMap); assertThat(scm.getConfigAsMap().get("KEY1").get(SCM.VALUE_KEY), is("value1")); assertThat(scm.getConfigAsMap().get("Key2").get(SCM.VALUE_KEY), is(nullValue())); }
@Test public void shouldGetOptionValue() { SCMConfiguration scmConfiguration = new SCMConfiguration("key"); scmConfiguration.with(SCMConfiguration.DISPLAY_NAME, "some display name"); scmConfiguration.with(SCMConfiguration.DISPLAY_ORDER, 3); assertThat(scmConfiguration.getOption(SCMConfiguration.DISPLAY_NAME), is("some display name")); assertThat(scmConfiguration.getOption(SCMConfiguration.DISPLAY_ORDER), is(3)); }
@Test public void shouldAddPropertyComingFromAttributesMapIfPresentInConfigStoreEvenIfItISNotPresentInCurrentConfiguration() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); scmConfigurations.add(new SCMConfiguration("KEY1")); scmConfigurations.add(new SCMConfiguration("Key2")); SCMPreference scmPreference = mock(SCMPreference.class); when(scmPreference.getScmConfigurations()).thenReturn(scmConfigurations); SCMMetadataStore.getInstance().setPreferenceFor("plugin-id", scmPreference); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); SCM scm = new SCM("scm-id", new PluginConfiguration("plugin-id", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1", "Key2", "value2"); scm.setConfigAttributes(attributeMap); assertThat(scm.getConfigAsMap().get("KEY1").get(SCM.VALUE_KEY), is("value1")); assertThat(scm.getConfigAsMap().get("Key2").get(SCM.VALUE_KEY), is("value2")); }
@Test public void shouldPopulateItselfFromConfigAttributesMap() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); scmConfigurations.add(new SCMConfiguration("KEY1")); scmConfigurations.add(new SCMConfiguration("Key2")); SCMPreference scmPreference = mock(SCMPreference.class); when(scmPreference.getScmConfigurations()).thenReturn(scmConfigurations); SCMMetadataStore.getInstance().setPreferenceFor("plugin-id", scmPreference); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"), ConfigurationPropertyMother.create("Key2")); SCM scm = new SCM("scm-id", new PluginConfiguration("plugin-id", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1", "Key2", "value2"); scm.setConfigAttributes(attributeMap); assertThat(scm.getConfigAsMap().get("KEY1").get(SCM.VALUE_KEY), is("value1")); assertThat(scm.getConfigAsMap().get("Key2").get(SCM.VALUE_KEY), is("value2")); }
@Test public void shouldIgnoreKeysPresentInConfigAttributesMapButNotPresentInConfigStore() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); scmConfigurations.add(new SCMConfiguration("KEY1")); SCMPreference scmPreference = mock(SCMPreference.class); when(scmPreference.getScmConfigurations()).thenReturn(scmConfigurations); SCMMetadataStore.getInstance().setPreferenceFor("plugin-id", scmPreference); Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1")); SCM scm = new SCM("scm-id", new PluginConfiguration("plugin-id", "1"), configuration); Map<String, String> attributeMap = DataStructureUtils.m("KEY1", "value1", "Key2", "value2"); scm.setConfigAttributes(attributeMap); assertThat(scm.getConfigAsMap().get("KEY1").get(SCM.VALUE_KEY), is("value1")); assertFalse(scm.getConfigAsMap().containsKey("Key2")); }
@Test public void shouldSetConfigAttributesAsAvailable() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); scmConfigurations.add(new SCMConfiguration("url")); scmConfigurations.add(new SCMConfiguration("username")); scmConfigurations.add(new SCMConfiguration("password").with(SECURE, true)); SCMMetadataStore.getInstance().addMetadataFor("plugin-id", scmConfigurations, null); Map<String, String> attributeMap = DataStructureUtils.m(SCM.SCM_ID, "scm-id", SCM.NAME, "scm-name", SCM.AUTO_UPDATE, "false", "url", "http://localhost", "username", "user", "password", "pass"); SCM scm = new SCM(null, new PluginConfiguration("plugin-id", "1"), new Configuration()); scm.setConfigAttributes(attributeMap); assertThat(scm.getId(), is("scm-id")); assertThat(scm.getName(), is("scm-name")); assertThat(scm.isAutoUpdate(), is(false)); assertThat(scm.getPluginConfiguration().getId(), is("plugin-id")); assertThat(scm.getConfigAsMap().get("url").get(SCM.VALUE_KEY), is("http://localhost")); assertThat(scm.getConfigAsMap().get("username").get(SCM.VALUE_KEY), is("user")); assertThat(scm.getConfigAsMap().get("password").get(SCM.VALUE_KEY), is("pass")); assertThat(scm.getConfiguration().getProperty("password").getConfigurationValue(), is(nullValue())); assertThat(scm.getConfiguration().getProperty("password").getEncryptedConfigurationValue(), is(not(nullValue()))); }
@Test public void shouldOnlyDisplayFieldsWhichAreNonSecureAndPartOfIdentityInGetConfigForDisplayWhenPluginExists() throws Exception { SCMConfigurations scmConfiguration = new SCMConfigurations(); scmConfiguration.add(new SCMConfiguration("key1").with(PART_OF_IDENTITY, true).with(SECURE, false)); scmConfiguration.add(new SCMConfiguration("key2").with(PART_OF_IDENTITY, false).with(SECURE, false)); scmConfiguration.add(new SCMConfiguration("key3").with(PART_OF_IDENTITY, true).with(SECURE, true)); scmConfiguration.add(new SCMConfiguration("key4").with(PART_OF_IDENTITY, false).with(SECURE, true)); scmConfiguration.add(new SCMConfiguration("key5").with(PART_OF_IDENTITY, true).with(SECURE, false)); SCMMetadataStore.getInstance().addMetadataFor("plugin-id", scmConfiguration, null); Configuration configuration = new Configuration(create("key1", false, "value1"), create("key2", false, "value2"), create("key3", true, "value3"), create("key4", true, "value4"), create("key5", false, "value5")); SCM scm = SCMMother.create("scm", "scm-name", "plugin-id", "1.0", configuration); assertThat(scm.getConfigForDisplay(), is("[key1=value1, key5=value5]")); }
@Test public void shouldMakeConfigurationSecureBasedOnMetadata() throws Exception { GoCipher goCipher = new GoCipher(); //meta data of SCM SCMConfigurations scmConfiguration = new SCMConfigurations(); scmConfiguration.add(new SCMConfiguration("key1").with(SECURE, true)); scmConfiguration.add(new SCMConfiguration("key2").with(SECURE, false)); SCMMetadataStore.getInstance().addMetadataFor("plugin-id", scmConfiguration, null); /*secure property is set based on metadata*/ ConfigurationProperty secureProperty = new ConfigurationProperty(new ConfigurationKey("key1"), new ConfigurationValue("value1"), null, goCipher); ConfigurationProperty nonSecureProperty = new ConfigurationProperty(new ConfigurationKey("key2"), new ConfigurationValue("value2"), null, goCipher); SCM scm = SCMMother.create("scm-id", "scm-name", "plugin-id", "1.0", new Configuration(secureProperty, nonSecureProperty)); scm.applyPluginMetadata(); //assert SCM properties assertThat(secureProperty.isSecure(), is(true)); assertThat(secureProperty.getEncryptedConfigurationValue(), is(notNullValue())); assertThat(secureProperty.getEncryptedValue(), is(goCipher.encrypt("value1"))); assertThat(nonSecureProperty.isSecure(), is(false)); assertThat(nonSecureProperty.getValue(), is("value2")); }