@Override public SCMPropertyConfiguration responseMessageForSCMConfiguration(String responseBody) { try { SCMPropertyConfiguration scmConfiguration = new SCMPropertyConfiguration(); Map<String, Map> configurations; try { configurations = parseResponseToMap(responseBody); } catch (Exception e) { throw new RuntimeException("SCM configuration should be returned as a map"); } if (configurations == null || configurations.isEmpty()) { throw new RuntimeException("Empty response body"); } for (String key : configurations.keySet()) { if (isEmpty(key)) { throw new RuntimeException("SCM configuration key cannot be empty"); } if (!(configurations.get(key) instanceof Map)) { throw new RuntimeException(format("SCM configuration properties for key '%s' should be represented as a Map", key)); } scmConfiguration.add(toSCMProperty(key, configurations.get(key))); } return scmConfiguration; } catch (Exception e) { throw new RuntimeException(format("Unable to de-serialize json response. %s", e.getMessage())); } }
public SCMConfigurations(SCMPropertyConfiguration configuration) { for (Property property : configuration.list()) { configurations.add(new SCMConfiguration(property)); } }
private SCMPropertyConfiguration buildSCMPropertyConfigurations(SCM scmConfig) { SCMPropertyConfiguration scmPropertyConfiguration = new SCMPropertyConfiguration(); populateConfiguration(scmConfig.getConfiguration(), scmPropertyConfiguration); return scmPropertyConfiguration; }
@Test public void shouldTalkToPluginCheckoutForPrepare() { PluggableSCMMaterial pluggableSCMMaterial = MaterialsMother.pluggableSCMMaterial(); pluggableSCMMaterial.setFolder("destination-folder"); Modification modification = ModificationsMother.oneModifiedFile("r1"); Map<String, String> additionalData = new HashMap<>(); additionalData.put("a1", "v1"); additionalData.put("a2", "v2"); modification.setAdditionalData(new Gson().toJson(additionalData)); MaterialRevision revision = new MaterialRevision(pluggableSCMMaterial, modification); String pipelineFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); String destinationFolder = new File(pipelineFolder, "destination-folder").getAbsolutePath(); PluggableSCMMaterialAgent pluggableSCMMaterialAgent = new PluggableSCMMaterialAgent(scmExtension, revision, new File(pipelineFolder), consumer); when(scmExtension.checkout(eq("pluginid"), scmConfiguration.capture(), eq(destinationFolder), scmRevision.capture())).thenReturn(new Result()); pluggableSCMMaterialAgent.prepare(); verify(scmExtension).checkout(any(String.class), any(SCMPropertyConfiguration.class), any(String.class), any(SCMRevision.class)); assertThat(scmConfiguration.getValue().size(), is(2)); assertThat(scmConfiguration.getValue().get("k1").getValue(), is("v1")); assertThat(scmConfiguration.getValue().get("k2").getValue(), is("v2")); assertThat(scmRevision.getValue().getRevision(), is("r1")); assertThat(scmRevision.getValue().getTimestamp(), is(modification.getModifiedTime())); assertThat(scmRevision.getValue().getData().size(), is(2)); assertThat(scmRevision.getValue().getDataFor("a1"), is("v1")); assertThat(scmRevision.getValue().getDataFor("a2"), is("v2")); }
@Test public void shouldBuildSCMConfigurationFromResponseBody() throws Exception { String responseBody = "{" + "\"key-one\":{}," + "\"key-two\":{\"default-value\":\"two\",\"part-of-identity\":true,\"secure\":true,\"required\":true,\"display-name\":\"display-two\",\"display-order\":\"1\"}," + "\"key-three\":{\"default-value\":\"three\",\"part-of-identity\":false,\"secure\":false,\"required\":false,\"display-name\":\"display-three\",\"display-order\":\"2\"}" + "}"; SCMPropertyConfiguration scmConfiguration = messageHandler.responseMessageForSCMConfiguration(responseBody); assertPropertyConfiguration((SCMProperty) scmConfiguration.get("key-one"), "key-one", null, true, true, false, "", 0); assertPropertyConfiguration((SCMProperty) scmConfiguration.get("key-two"), "key-two", "two", true, true, true, "display-two", 1); assertPropertyConfiguration((SCMProperty) scmConfiguration.get("key-three"), "key-three", "three", false, false, false, "display-three", 2); }
private SCMPropertyConfiguration buildSCMPropertyConfigurations(SCM scmConfig) { SCMPropertyConfiguration scmPropertyConfiguration = new SCMPropertyConfiguration(); populateConfiguration(scmConfig.getConfiguration(), scmPropertyConfiguration); return scmPropertyConfiguration; }
private SCMPropertyConfiguration getScmPropertyConfiguration(SCM scmConfig) { final SCMPropertyConfiguration configuration = new SCMPropertyConfiguration(); for (ConfigurationProperty configurationProperty : scmConfig.getConfiguration()) { configuration.add(new SCMProperty(configurationProperty.getConfigurationKey().getName(), configurationProperty.getValue())); } return configuration; } }
@Before public void setUp() throws Exception { initMocks(this); scmExtension = new SCMExtension(pluginManager); scmExtension.getPluginSettingsMessageHandlerMap().put("1.0", pluginSettingsJSONMessageHandler); scmExtension.getMessageHandlerMap().put("1.0", jsonMessageHandler); pluginSettingsConfiguration = new PluginSettingsConfiguration(); scmPropertyConfiguration = new SCMPropertyConfiguration(); materialData = new HashMap<>(); requestArgumentCaptor = ArgumentCaptor.forClass(GoPluginApiRequest.class); when(pluginManager.resolveExtensionVersion(PLUGIN_ID, SCM_EXTENSION, asList("1.0"))).thenReturn("1.0"); when(pluginManager.isPluginOfType(SCM_EXTENSION, PLUGIN_ID)).thenReturn(true); when(pluginManager.submitTo(eq(PLUGIN_ID), eq(SCM_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody)); }
@Before public void setUp() throws Exception { messageHandler = new JsonMessageHandler1_0(); scmPropertyConfiguration = new SCMPropertyConfiguration(); scmPropertyConfiguration.add(new SCMProperty("key-one", "value-one")); scmPropertyConfiguration.add(new SCMProperty("key-two", "value-two")); materialData = new HashMap<>(); materialData.put("key-one", "value-one"); }
@Test public void shouldTalkToPluginToGetSCMConfiguration() throws Exception { SCMPropertyConfiguration deserializedResponse = new SCMPropertyConfiguration(); when(jsonMessageHandler.responseMessageForSCMConfiguration(responseBody)).thenReturn(deserializedResponse); SCMPropertyConfiguration response = scmExtension.getSCMConfiguration(PLUGIN_ID); assertRequest(requestArgumentCaptor.getValue(), SCM_EXTENSION, "1.0", SCMExtension.REQUEST_SCM_CONFIGURATION, null); verify(jsonMessageHandler).responseMessageForSCMConfiguration(responseBody); assertSame(response, deserializedResponse); }
@Test public void shouldConstructSCMConfiguration() throws Exception { SCMPropertyConfiguration scmPropertyConfiguration = new SCMPropertyConfiguration(); scmPropertyConfiguration.add(new SCMProperty("k1", "v1").with(Property.SECURE, Boolean.TRUE)); SCMConfigurations scmConfigurations = new SCMConfigurations(scmPropertyConfiguration); assertThat(scmConfigurations.list().size(), is(1)); SCMConfiguration scmConfiguration = scmConfigurations.list().get(0); assertThat(scmConfiguration.getKey(), is("k1")); assertThat(scmConfiguration.getValue(), is("v1")); assertThat(scmConfiguration.getOption(SCMConfiguration.REQUIRED), is(true)); assertThat(scmConfiguration.getOption(SCMConfiguration.PART_OF_IDENTITY), is(true)); assertThat(scmConfiguration.getOption(SCMConfiguration.SECURE), is(true)); assertThat(scmConfiguration.getOption(SCMConfiguration.DISPLAY_NAME), is("")); assertThat(scmConfiguration.getOption(SCMConfiguration.DISPLAY_ORDER), is(0)); } }
@Test public void shouldThrowExceptionWhenNullSCMViewReturned() { when(scmExtension.getSCMConfiguration(pluginDescriptor.id())).thenReturn(new SCMPropertyConfiguration()); when(scmExtension.getSCMView(pluginDescriptor.id())).thenReturn(null); try { metadataLoader.fetchSCMMetaData(pluginDescriptor); } catch (Exception e) { assertThat(e.getMessage(), is("Plugin[plugin-id] returned null SCM view")); } assertThat(SCMMetadataStore.getInstance().getConfigurationMetadata(pluginDescriptor.id()), nullValue()); }
@Before public void setUp() throws Exception { extension = mock(SCMExtension.class); SCMPropertyConfiguration value = new SCMPropertyConfiguration(); value.add(new SCMProperty("username", null).with(Property.REQUIRED, true).with(Property.SECURE, false).with(Property.PART_OF_IDENTITY, true).with(Property.DISPLAY_ORDER, 1)); value.add(new SCMProperty("password", null).with(Property.REQUIRED, true).with(Property.SECURE, true).with(Property.PART_OF_IDENTITY, false).with(Property.DISPLAY_ORDER, 2)); when(extension.getSCMConfiguration("plugin1")).thenReturn(value); when(extension.getSCMView("plugin1")).thenReturn(new SCMView() { @Override public String displayValue() { return "some scm plugin"; } @Override public String template() { return "some html"; } }); PluginSettingsConfiguration pluginSettingsConfiguration = new PluginSettingsConfiguration(); pluginSettingsConfiguration.add(new PluginSettingsProperty("k1", null).with(Property.REQUIRED, true).with(Property.SECURE, false).with(Property.DISPLAY_ORDER, 3)); when(extension.getPluginSettingsConfiguration("plugin1")).thenReturn(pluginSettingsConfiguration); when(extension.getPluginSettingsView("plugin1")).thenReturn("settings view"); }
@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")); }
@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);
@Test public void shouldFailValidationIfMaterialWithDuplicateFingerprintIsFound() { String pluginId = "plugin-id"; SCMPropertyConfiguration scmConfiguration = new SCMPropertyConfiguration(); scmConfiguration.add(new SCMProperty("k1")); scmConfiguration.add(new SCMProperty("k2").with(REQUIRED, false).with(PART_OF_IDENTITY, false)); SCMMetadataStore.getInstance().addMetadataFor(pluginId, new SCMConfigurations(scmConfiguration), null);
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);