@Override public Set<Material> prune(Set<Material> materials, Map params) { HashSet<Material> prunedCollection = new HashSet<>(); String paramSCMName = (String) params.get(SCM_NAME); if (StringUtils.isNotBlank(paramSCMName)) { for (Material material : materials) { if (material instanceof PluggableSCMMaterial && paramSCMName.equalsIgnoreCase(((PluggableSCMMaterial) material).getScmConfig().getName())) { prunedCollection.add(material); } } } return prunedCollection; } }
@Test public void shouldNotCalculateFingerprintWhenAvailable() { String fingerprint = "fingerprint"; SCM scmConfig = mock(SCM.class); PluggableSCMMaterial pluggableSCMMaterial = new PluggableSCMMaterial(); pluggableSCMMaterial.setSCMConfig(scmConfig); pluggableSCMMaterial.setFingerprint(fingerprint); assertThat(pluggableSCMMaterial.getFingerprint(), is(fingerprint)); verify(scmConfig, never()).getFingerprint(); }
@Override public void toJson(Map jsonMap, Revision revision) { jsonMap.put("scmType", getTypeForDisplay()); jsonMap.put("materialName", getDisplayName()); jsonMap.put("location", getUriForDisplay()); jsonMap.put("folder", getFolder()); jsonMap.put("action", "Modified"); }
@Override public Material toOldMaterial(String name, String folder, String password) { PluggableSCMMaterial pluggableSCMMaterial = JsonHelper.fromJson(configuration, PluggableSCMMaterial.class); pluggableSCMMaterial.setName(new CaseInsensitiveString(name)); pluggableSCMMaterial.setId(id); pluggableSCMMaterial.setFolder(folder); pluggableSCMMaterial.setFingerprint(getFingerprint()); return pluggableSCMMaterial; }
@Override public String getDisplayName() { CaseInsensitiveString name = getName(); return (name == null || name.isBlank()) ? getUriForDisplay() : name.toString(); }
@Test public void shouldNotThrowUpWhenAdditionalDataIsRandomJunkAndNotJSON() { ConfigurationProperty k1 = ConfigurationPropertyMother.create("k1", false, "v1"); SCM scmConfig = SCMMother.create("scm-id", "tw-dev", "pluginid", "version", new Configuration(k1)); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); material.setName(new CaseInsensitiveString("tw-dev:go-agent")); Modifications modifications = new Modifications(new Modification("loser", "comment", "email", new Date(), "revision-123", "salkdfjdsa-jjgkj!!!vcxknbvkjk")); EnvironmentVariableContext environmentVariableContext = new EnvironmentVariableContext(); material.populateEnvironmentContext(environmentVariableContext, new MaterialRevision(material, modifications), null); assertThat(environmentVariableContext.getProperty("GO_SCM_TW_DEV_GO_AGENT_LABEL"), is("revision-123")); assertThat(environmentVariableContext.getProperty("GO_SCM_TW_DEV_GO_AGENT_K1"), is("v1")); }
@Override public void checkout(PluggableSCMMaterial material, File baseDir, Revision revision, SubprocessExecutionContext execCtx) { SCMPropertyConfiguration scmPropertyConfiguration = buildSCMPropertyConfigurations(material.getScmConfig()); MaterialInstance materialInstance = materialRepository.findMaterialInstance(material); PluggableSCMMaterialRevision pluggableSCMMaterialRevision = (PluggableSCMMaterialRevision) revision; SCMRevision scmRevision = new SCMRevision( pluggableSCMMaterialRevision.getRevision(), pluggableSCMMaterialRevision.getTimestamp(), null, null, pluggableSCMMaterialRevision.getData(), null); Result result = scmExtension.checkout(material.getPluginId(), scmPropertyConfiguration, baseDir.getAbsolutePath(), scmRevision); if(!result.isSuccessful()) throw new RuntimeException("Failed to perform checkout on pluggable SCM"); }
@Test public void shouldGetJsonRepresentationForPluggableSCMMaterial() { ConfigurationProperty k1 = create("k1", false, "v1"); SCM scmConfig = SCMMother.create("scm-id", "scm-name", "pluginid", "version", new Configuration(k1)); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); material.setFolder("folder"); Map<String, String> jsonMap = new LinkedHashMap<>(); material.toJson(jsonMap, new PluggableSCMMaterialRevision("rev123", new Date())); assertThat(jsonMap.get("scmType"), is("SCM")); assertThat(jsonMap.get("materialName"), is("scm-name")); assertThat(jsonMap.get("location"), is(material.getUriForDisplay())); assertThat(jsonMap.get("folder"), is("folder")); assertThat(jsonMap.get("action"), is("Modified")); }
@Test public void shouldGetUriForDisplayNameIfNameIsNull() { ConfigurationProperty k1 = ConfigurationPropertyMother.create("k1", false, "scm-v1"); ConfigurationProperty k2 = ConfigurationPropertyMother.create("k2", false, "scm-v2"); SCM scmConfig = SCMMother.create("scm-id", null, "pluginid", "version", new Configuration(k1, k2)); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); assertThat(material.getDisplayName(), is(material.getUriForDisplay())); }
@Test public void shouldGetDifferentFingerprintWhenPluginIdChanges() { SCM scmConfig = SCMMother.create("scm-id", "scm-name", "plugin-1", "version", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"))); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); SCM anotherSCMConfig = SCMMother.create("scm-id", "scm-name", "plugin-2", "version", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"))); PluggableSCMMaterial anotherMaterial = new PluggableSCMMaterial(); anotherMaterial.setSCMConfig(anotherSCMConfig); assertThat(material.getFingerprint().equals(anotherMaterial.getFingerprint()), is(false)); }
@Test public void shouldGetLongDescription() { ConfigurationProperty k1 = ConfigurationPropertyMother.create("k1", false, "scm-v1"); ConfigurationProperty k2 = ConfigurationPropertyMother.create("k2", false, "scm-v2"); Configuration configuration = new Configuration(k1, k2); SCM scmConfig = SCMMother.create("scm-id", "scm-name", "pluginid", "version", configuration); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); assertThat(material.getLongDescription(), is(material.getUriForDisplay())); }
@Test public void shouldReturnWorkingDirectoryCorrectly() { PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setFolder("dest"); String baseFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); String workingFolder = new File(baseFolder, "dest").getAbsolutePath(); assertThat(material.workingDirectory(new File(baseFolder)).getAbsolutePath(), is(workingFolder)); material.setFolder(null); assertThat(material.workingDirectory(new File(baseFolder)).getAbsolutePath(), is(baseFolder)); }
@Test public void shouldGetSqlCriteria() { SCM scmConfig = SCMMother.create("scm-id", "scm-name", "pluginid", "version", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"))); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); Map<String, Object> criteria = material.getSqlCriteria(); assertThat(criteria.get("type"), is(PluggableSCMMaterial.class.getSimpleName())); assertThat(criteria.get("fingerprint"), is(material.getFingerprint())); }
@Test public void shouldGetDisplayName() { ConfigurationProperty k1 = ConfigurationPropertyMother.create("k1", false, "v1"); SCM scmConfig = SCMMother.create("scm-id", "scm-name", "pluginid", "version", new Configuration(k1)); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); assertThat(material.getDisplayName(), is("scm-name")); }
@Test(expected = RuntimeException.class) public void shouldLogToErrorOutputWhenPluginSendsErrorResponse() { SCM scmConfig = SCMMother.create("scm-id", "scm-name", "pluginid", "version", new Configuration()); PluggableSCMMaterial pluggableSCMMaterial = MaterialsMother.pluggableSCMMaterial(); pluggableSCMMaterial.setFolder("destination-folder"); pluggableSCMMaterial.setSCMConfig(scmConfig); 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"), any(), eq(destinationFolder), any())) .thenThrow(new RuntimeException("some message from plugin")); pluggableSCMMaterialAgent.prepare(); verify(consumer, times(1)).errOutput("Material scm-name checkout failed: some message from plugin"); } }
@Test public void shouldTestEqualsBasedOnConfiguration() { PluggableSCMMaterial material = MaterialsMother.pluggableSCMMaterial("scm-id", "scm-name", ConfigurationPropertyMother.create("key1", false, "value1")); MaterialInstance materialInstance = material.createMaterialInstance(); MaterialInstance materialInstanceCopy = material.createMaterialInstance(); material.getScmConfig().getConfiguration().add(ConfigurationPropertyMother.create("key2", false, "value2")); MaterialInstance newMaterialInstance = material.createMaterialInstance(); assertThat(materialInstance, is(materialInstanceCopy)); assertThat(materialInstance, is(not(newMaterialInstance))); }
public static PluggableSCMMaterial pluggableSCMMaterial(String scmId, String scmName, final String pluginid, final String version, List<ConfigurationProperty> properties) { PluggableSCMMaterial material = new PluggableSCMMaterial(scmId); material.setId(1); SCM scmConfig = SCMMother.create(scmId, scmName, pluginid, version, new Configuration(properties)); material.setSCMConfig(scmConfig); return material; }
@Override public void prepare() { try { PluggableSCMMaterial material = (PluggableSCMMaterial) revision.getMaterial(); Modification latestModification = revision.getLatestModification(); SCMRevision scmRevision = new SCMRevision(latestModification.getRevision(), latestModification.getModifiedTime(), null, null, latestModification.getAdditionalDataMap(), null); File destinationFolder = material.workingDirectory(workingDirectory); Result result = scmExtension.checkout(material.getScmConfig().getPluginConfiguration().getId(), buildSCMPropertyConfigurations(material.getScmConfig()), destinationFolder.getAbsolutePath(), scmRevision); handleCheckoutResult(material, result); } catch (Exception e) { consumer.taggedErrOutput(PREP_ERR, String.format("Material %s checkout failed: %s", revision.getMaterial().getDisplayName(), e.getMessage())); throw e; } }
@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 shouldGetNameFromSCMName() { ConfigurationProperty k1 = ConfigurationPropertyMother.create("k1", false, "v1"); SCM scmConfig = SCMMother.create("scm-id", "scm-name", "pluginid", "version", new Configuration(k1)); PluggableSCMMaterial material = new PluggableSCMMaterial(); material.setSCMConfig(scmConfig); assertThat(material.getName().toString(), is("scm-name")); }