@After public void tearDown() throws Exception { SCMMetadataStore.getInstance().clear(); } }
@Test public void shouldPopulateDataCorrectly() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); SCMView scmView = createSCMView("display-value", "template"); SCMMetadataStore.getInstance().addMetadataFor("plugin-id", scmConfigurations, scmView); assertThat(SCMMetadataStore.getInstance().getConfigurationMetadata("plugin-id"), is(scmConfigurations)); assertThat(SCMMetadataStore.getInstance().getViewMetadata("plugin-id"), is(scmView)); assertThat(SCMMetadataStore.getInstance().displayValue("plugin-id"), is("display-value")); assertThat(SCMMetadataStore.getInstance().template("plugin-id"), is("template")); assertThat(SCMMetadataStore.getInstance().getConfigurationMetadata("some-plugin-which-does-not-exist"), is(nullValue())); assertThat(SCMMetadataStore.getInstance().getViewMetadata("some-plugin-which-does-not-exist"), is(nullValue())); assertThat(SCMMetadataStore.getInstance().displayValue("some-plugin-which-does-not-exist"), is(nullValue())); assertThat(SCMMetadataStore.getInstance().template("some-plugin-which-does-not-exist"), is(nullValue())); }
@PostConstruct public void applyPluginMetadata() { String pluginId = getPluginId(); for (ConfigurationProperty configurationProperty : configuration) { SCMMetadataStore scmMetadataStore = SCMMetadataStore.getInstance(); if (scmMetadataStore.getConfigurationMetadata(pluginId) != null) { boolean isSecureProperty = scmMetadataStore.hasOption(pluginId, configurationProperty.getConfigurationKey().getName(), SCMConfiguration.SECURE); configurationProperty.handleSecureValueConfiguration(isSecureProperty); } } }
public Boolean doesPluginExist(){ return SCMMetadataStore.getInstance().hasPlugin(getPluginId()); }
@Override public String getTypeForDisplay() { String type = scmConfig == null ? null : SCMMetadataStore.getInstance().displayValue(scmConfig.getPluginConfiguration().getId()); return type == null ? "SCM" : type; }
private void handleSCMProperties(List<String> list) { SCMConfigurations metadata = SCMMetadataStore.getInstance().getConfigurationMetadata(getPluginId()); for (ConfigurationProperty configurationProperty : configuration) { handleProperty(list, metadata, configurationProperty); } }
@Test public void shouldCorrectlyGetTypeDisplay() { assertThat(pluggableSCMMaterialConfig.getTypeForDisplay(), is("SCM")); pluggableSCMMaterialConfig.setSCMConfig(SCMMother.create("scm-id")); assertThat(pluggableSCMMaterialConfig.getTypeForDisplay(), is("SCM")); SCMMetadataStore.getInstance().addMetadataFor("plugin", null, null); assertThat(pluggableSCMMaterialConfig.getTypeForDisplay(), is("SCM")); SCMView scmView = mock(SCMView.class); when(scmView.displayValue()).thenReturn("scm-name"); SCMMetadataStore.getInstance().addMetadataFor("plugin", null, scmView); assertThat(pluggableSCMMaterialConfig.getTypeForDisplay(), is("scm-name")); }
@Test public void shouldRemoveMetadataOnPluginUnLoadedCallback() throws Exception { SCMMetadataStore.getInstance().addMetadataFor(pluginDescriptor.id(), new SCMConfigurations(), createSCMView(null, null)); when(scmExtension.canHandlePlugin(pluginDescriptor.id())).thenReturn(true); metadataLoader.pluginUnLoaded(pluginDescriptor); assertThat(SCMMetadataStore.getInstance().getConfigurationMetadata(pluginDescriptor.id()), is(nullValue())); assertThat(SCMMetadataStore.getInstance().getViewMetadata(pluginDescriptor.id()), is(nullValue())); }
@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())); }
public void validate(final SCM scmConfig) { final String pluginId = scmConfig.getPluginConfiguration().getId(); final SCMPropertyConfiguration configuration = getScmPropertyConfiguration(scmConfig); ValidationResult validationResult = scmExtension.isSCMConfigurationValid(pluginId, configuration); if (SCMMetadataStore.getInstance().hasPreferenceFor(pluginId)) { SCMConfigurations configurationMetadata = SCMMetadataStore.getInstance().getConfigurationMetadata(pluginId); for (SCMConfiguration scmConfiguration : configurationMetadata.list()) { String key = scmConfiguration.getKey(); boolean isRequired = SCMMetadataStore.getInstance().hasOption(pluginId, key, Property.REQUIRED); ConfigurationProperty property = scmConfig.getConfiguration().getProperty(key); String configValue = property == null ? null : property.getValue(); if (isRequired && StringUtils.isBlank(configValue)) { validationResult.addError(new ValidationError(key, "This field is required")); } } } for (ValidationError validationError : validationResult.getErrors()) { ConfigurationProperty property = scmConfig.getConfiguration().getProperty(validationError.getKey()); if (property != null) { property.addError(validationError.getKey(), validationError.getMessage()); } } }
@Test public void shouldBeAbleToCheckIfPluginExists() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); SCMView scmView = createSCMView(null, null); SCMMetadataStore.getInstance().addMetadataFor("plugin-id", scmConfigurations, scmView); assertThat(SCMMetadataStore.getInstance().hasPlugin("plugin-id"), is(true)); assertThat(SCMMetadataStore.getInstance().hasPlugin("some-plugin-which-does-not-exist"), is(false)); }
@Test public void shouldFetchSCMMetadataForPluginsWhichImplementSCMExtensionPoint() { SCMPropertyConfiguration scmPropertyConfiguration = new SCMPropertyConfiguration(); scmPropertyConfiguration.add(new SCMProperty("k1").with(SCMProperty.REQUIRED, true).with(SCMProperty.PART_OF_IDENTITY, false)); when(scmExtension.getSCMConfiguration(pluginDescriptor.id())).thenReturn(scmPropertyConfiguration); when(scmExtension.getSCMView(pluginDescriptor.id())).thenReturn(createSCMView("display-value", "template")); metadataLoader.fetchSCMMetaData(pluginDescriptor); SCMConfigurations configurationMetadata = SCMMetadataStore.getInstance().getConfigurationMetadata(pluginDescriptor.id()); assertThat(configurationMetadata.size(), is(1)); SCMConfiguration scmConfiguration = configurationMetadata.get("k1"); assertThat(scmConfiguration.getKey(), is("k1")); assertThat(scmConfiguration.getOption(SCMProperty.REQUIRED), is(true)); assertThat(scmConfiguration.getOption(SCMProperty.PART_OF_IDENTITY), is(false)); SCMView viewMetadata = SCMMetadataStore.getInstance().getViewMetadata(pluginDescriptor.id()); assertThat(viewMetadata.displayValue(), is("display-value")); assertThat(viewMetadata.template(), is("template")); }
@Before public void setUp() throws Exception { pluginDescriptor = new GoPluginDescriptor("plugin-id", "1.0", null, null, null, true); pluginManager = mock(PluginManager.class); scmExtension = mock(SCMExtension.class); metadataLoader = new SCMMetadataLoader(scmExtension, pluginManager); SCMMetadataStore.getInstance().removeMetadata(pluginDescriptor.id()); }
void fetchSCMMetaData(GoPluginDescriptor pluginDescriptor) { try { SCMPropertyConfiguration scmConfiguration = scmExtension.getSCMConfiguration(pluginDescriptor.id()); if (scmConfiguration == null) { throw new RuntimeException(format("Plugin[%s] returned null SCM configuration", pluginDescriptor.id())); } SCMView scmView = scmExtension.getSCMView(pluginDescriptor.id()); if (scmView == null) { throw new RuntimeException(format("Plugin[%s] returned null SCM view", pluginDescriptor.id())); } scmMetadataStore.addMetadataFor(pluginDescriptor.id(), new SCMConfigurations(scmConfiguration), scmView); } catch (GoPluginFrameworkException e) { LOGGER.error("Failed to fetch SCM metadata for plugin : {}", pluginDescriptor.id(), e); } }
public boolean hasOption(String pluginId, String key, Option<Boolean> option) { SCMConfigurations configurations = getConfigurationMetadata(pluginId); if (configurations != null) { SCMConfiguration scmConfiguration = configurations.get(key); if (scmConfiguration != null) { return scmConfiguration.hasOption(option); } } return option.getValue(); }
@Test public void shouldCorrectlyGetTypeDisplay() { PluggableSCMMaterial pluggableSCMMaterial = new PluggableSCMMaterial("scm-id"); assertThat(pluggableSCMMaterial.getTypeForDisplay(), is("SCM")); pluggableSCMMaterial.setSCMConfig(SCMMother.create("scm-id")); assertThat(pluggableSCMMaterial.getTypeForDisplay(), is("SCM")); SCMMetadataStore.getInstance().addMetadataFor("plugin", null, null); assertThat(pluggableSCMMaterial.getTypeForDisplay(), is("SCM")); SCMView scmView = mock(SCMView.class); when(scmView.displayValue()).thenReturn("scm-name"); SCMMetadataStore.getInstance().addMetadataFor("plugin", null, scmView); assertThat(pluggableSCMMaterial.getTypeForDisplay(), is("scm-name")); }
@Test public void shouldThrowExceptionWhenNullSCMConfigurationReturned() { when(scmExtension.getSCMConfiguration(pluginDescriptor.id())).thenReturn(null); try { metadataLoader.fetchSCMMetaData(pluginDescriptor); } catch (Exception e) { assertThat(e.getMessage(), is("Plugin[plugin-id] returned null SCM configuration")); } assertThat(SCMMetadataStore.getInstance().getConfigurationMetadata(pluginDescriptor.id()), nullValue()); }
@Test public void shouldNotTryRemoveMetadataOnPluginUnLoadedCallback() throws Exception { SCMConfigurations scmConfigurations = new SCMConfigurations(); SCMView scmView = createSCMView(null, null); SCMMetadataStore.getInstance().addMetadataFor(pluginDescriptor.id(), scmConfigurations, scmView); when(scmExtension.canHandlePlugin(pluginDescriptor.id())).thenReturn(false); metadataLoader.pluginUnLoaded(pluginDescriptor); assertThat(SCMMetadataStore.getInstance().getConfigurationMetadata(pluginDescriptor.id()), is(scmConfigurations)); assertThat(SCMMetadataStore.getInstance().getViewMetadata(pluginDescriptor.id()), is(scmView)); }
@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")); }
@Override public String getTypeForDisplay() { String type = scmConfig == null ? null : SCMMetadataStore.getInstance().displayValue(scmConfig.getPluginConfiguration().getId()); return type == null ? "SCM" : type; }