public <T> T fromXmlPartial(String partial, Class<T> o) throws Exception { return fromXmlPartial(toInputStream(partial, UTF_8), o); }
private CruiseConfig getOldConfigAndMutateWithChanges(NoOverwriteUpdateConfigCommand noOverwriteCommand, String oldMd5) throws Exception { LOGGER.debug("[Config Save] --- Mutating old config"); String configXmlAtOldMd5 = configRepository.getRevision(oldMd5).getContent(); CruiseConfig cruiseConfigAtOldMd5 = magicalGoConfigXmlLoader.fromXmlPartial(configXmlAtOldMd5, BasicCruiseConfig.class); CruiseConfig config = noOverwriteCommand.update(cruiseConfigAtOldMd5); LOGGER.debug("[Config Save] --- Done mutating old config"); return config; }
@Test public void shouldLoadMaterialWithAutoUpdate() throws Exception { MaterialConfig material = xmlLoader.fromXmlPartial("<hg url=\"file:///tmp/testSvnRepo/project1/trunk\" autoUpdate=\"false\"/>", HgMaterialConfig.class); assertThat(material.isAutoUpdate(), is(false)); material = xmlLoader.fromXmlPartial("<git url=\"file:///tmp/testSvnRepo/project1/trunk\" autoUpdate=\"false\"/>", GitMaterialConfig.class); assertThat(material.isAutoUpdate(), is(false)); material = xmlLoader.fromXmlPartial("<svn url=\"file:///tmp/testSvnRepo/project1/trunk\" autoUpdate=\"false\"/>", SvnMaterialConfig.class); assertThat(material.isAutoUpdate(), is(false)); material = xmlLoader.fromXmlPartial("<p4 port='localhost:1666' autoUpdate='false' ><view/></p4>", P4MaterialConfig.class); assertThat(material.isAutoUpdate(), is(false)); }
@Test public void shouldLoadFromSvnPartial() throws Exception { String buildXmlPartial = "<svn url=\"http://foo.bar\" username=\"cruise\" password=\"password\" materialName=\"http___foo.bar\"/>"; MaterialConfig materialConfig = xmlLoader.fromXmlPartial(buildXmlPartial, SvnMaterialConfig.class); MaterialConfig svnMaterial = MaterialConfigsMother.svnMaterialConfig("http://foo.bar", null, "cruise", "password", false, null); assertThat(materialConfig, is(svnMaterial)); }
@Test public void shouldLoadBranchFromGitPartial() throws Exception { String gitPartial = "<git url='file:///tmp/testGitRepo/project1' branch='foo'/>"; GitMaterialConfig gitMaterial = xmlLoader.fromXmlPartial(gitPartial, GitMaterialConfig.class); assertThat(gitMaterial.getBranch(), is("foo")); }
@Test public void shouldWriteEmptyOnCancelTaskWhenDefined() throws Exception { String partial = "<job name=\"functional\">\n" + " <tasks>\n" + " <exec command=\"echo\">\n" + " <oncancel />\n" + " </exec>\n" + " </tasks>\n" + "</job>"; JobConfig jobConfig = xmlLoader.fromXmlPartial(partial, JobConfig.class); assertThat(xmlWriter.toXmlPartial(jobConfig), is(partial)); }
@Test public void autoUpdateShouldBeTrueByDefault() throws Exception { MaterialConfig hgMaterial = xmlLoader.fromXmlPartial("<hg url=\"file:///tmp/testSvnRepo/project1/trunk\"/>", HgMaterialConfig.class); assertThat(hgMaterial.isAutoUpdate(), is(true)); }
@Test public void autoUpdateShouldUnderstandTrue() throws Exception { MaterialConfig hgMaterial = xmlLoader.fromXmlPartial("<hg url=\"file:///tmp/testSvnRepo/project1/trunk\" autoUpdate=\"true\"/>", HgMaterialConfig.class); assertThat(hgMaterial.isAutoUpdate(), is(true)); }
@Test public void shouldLoadShallowFlagFromGitPartial() throws Exception { String gitPartial = "<git url='file:///tmp/testGitRepo/project1' shallowClone=\"true\" />"; GitMaterialConfig gitMaterial = xmlLoader.fromXmlPartial(gitPartial, GitMaterialConfig.class); assertTrue(gitMaterial.isShallowClone()); }
@Test public void shouldLoadBuildPlanFromXmlPartial() throws Exception { String buildXmlPartial = "<job name=\"functional\">\n" + " <artifacts>\n" + " <artifact type=\"build\" src=\"artifact1.xml\" dest=\"cruise-output\" />\n" + " </artifacts>\n" + "</job>"; JobConfig build = xmlLoader.fromXmlPartial(buildXmlPartial, JobConfig.class); assertThat(build.name(), is(new CaseInsensitiveString("functional"))); assertThat(build.artifactConfigs().size(), is(1)); }
@Test public void shouldNotKillAllChildrenWhenEmptyOnCancel() throws Exception { RakeTask rakeTask = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()).fromXmlPartial( "<rake>" + " <oncancel />" + "</rake>", RakeTask.class ); assertThat(rakeTask.cancelTask(), is(instanceOf(NullTask.class))); } }
@Test public void shouldDefaultToUsingAKillAllChildrenCancelTask() throws Exception { RakeTask rakeTask = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()).fromXmlPartial("<rake/>",RakeTask.class); assertThat(rakeTask.cancelTask(), is(instanceOf(KillAllChildProcessTask.class))); }
@Test public void shouldBeAbleToLoadADependencyMaterialFromConfig() throws Exception { String xml = "<pipeline pipelineName=\"pipeline-name\" stageName=\"stage-name\" />"; DependencyMaterialConfig material = loader.fromXmlPartial(xml, DependencyMaterialConfig.class); assertThat(material.getPipelineName(), is(new CaseInsensitiveString("pipeline-name"))); assertThat(material.getStageName(), is(new CaseInsensitiveString("stage-name"))); assertThat(writer.toXmlPartial(material), is(xml)); }
@Test public void shouldLoadIgnoresFromHgPartial() throws Exception { String buildXmlPartial = "<hg url=\"file:///tmp/testSvnRepo/project1/trunk\" >\n" + " <filter>\n" + " <ignore pattern=\"x\"/>\n" + " </filter>\n" + " </hg>"; MaterialConfig hgMaterial = xmlLoader.fromXmlPartial(buildXmlPartial, HgMaterialConfig.class); Filter parsedFilter = hgMaterial.filter(); Filter expectedFilter = new Filter(); expectedFilter.add(new IgnoredFiles("x")); assertThat(parsedFilter, is(expectedFilter)); }
@Test public void shouldLoadIgnoresFromSvnPartial() throws Exception { String buildXmlPartial = "<svn url=\"file:///tmp/testSvnRepo/project1/trunk\" >\n" + " <filter>\n" + " <ignore pattern=\"x\"/>\n" + " </filter>\n" + " </svn>"; MaterialConfig svnMaterial = xmlLoader.fromXmlPartial(buildXmlPartial, SvnMaterialConfig.class); Filter parsedFilter = svnMaterial.filter(); Filter expectedFilter = new Filter(); expectedFilter.add(new IgnoredFiles("x")); assertThat(parsedFilter, is(expectedFilter)); }
@Test public void shouldBeAbleToParseNewAuthorization() throws Exception { AuthConfig config = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()).fromXmlPartial(NEW_AUTHORIZATION, AuthConfig.class); assertThat(config.size(), is(3)); }
@Test public void shouldLoadIgnoresFromP4Partial() throws Exception { String gitPartial = "<p4 port=\"localhost:8080\">\n" + " <filter>\n" + " <ignore pattern=\"x\"/>\n" + " </filter>\n" + " <view></view>\n" + "</p4>"; MaterialConfig p4Material = xmlLoader.fromXmlPartial(gitPartial, P4MaterialConfig.class); Filter parsedFilter = p4Material.filter(); Filter expectedFilter = new Filter(); expectedFilter.add(new IgnoredFiles("x")); assertThat(parsedFilter, is(expectedFilter)); }
@Test public void shouldLoadIgnoresFromGitPartial() throws Exception { String gitPartial = "<git url='file:///tmp/testGitRepo/project1' >\n" + " <filter>\n" + " <ignore pattern='x'/>\n" + " </filter>\n" + " </git>"; GitMaterialConfig gitMaterial = xmlLoader.fromXmlPartial(gitPartial, GitMaterialConfig.class); assertThat(gitMaterial.getBranch(), is(GitMaterialConfig.DEFAULT_BRANCH)); Filter parsedFilter = gitMaterial.filter(); Filter expectedFilter = new Filter(); expectedFilter.add(new IgnoredFiles("x")); assertThat(parsedFilter, is(expectedFilter)); }
public GoConfigOperationalResponse<PipelineConfigs> updateXml(String groupName, String xmlPartial, final String md5, Username username, HttpLocalizedOperationResult result) throws Exception { if (!userHasPermissions(username, groupName, result)) { return new GoConfigOperationalResponse<>(GoConfigValidity.valid(), null); } GoConfigValidity goConfigValidity = goConfigService.groupSaver(groupName).saveXml(xmlPartial, md5); if (!goConfigValidity.isValid()) { handleError(groupName, goConfigValidity, result); return new GoConfigOperationalResponse<>(goConfigValidity, null); } String savedSuccessMessage = "Saved configuration successfully."; String localizableMessage = goConfigValidity.wasMerged() ? LocalizedMessage.composite(savedSuccessMessage, "The configuration was modified by someone else, but your changes were merged successfully.") : savedSuccessMessage; result.setMessage(localizableMessage); PipelineConfigs pipelineConfigs = magicalGoConfigXmlLoader.fromXmlPartial(xmlPartial, BasicPipelineConfigs.class); return new GoConfigOperationalResponse<>(goConfigValidity, pipelineConfigs); }
@Test public void shouldSupportMultipleArgs() throws Exception { String xml = "<exec command='ls'>\n" + " <arg>arg1</arg>\n" + " <arg>arg2</arg>\n" + "</exec>"; ExecTask execTask = new MagicalGoConfigXmlLoader(new ConfigCache(), ConfigElementImplementationRegistryMother.withNoPlugins()).fromXmlPartial(xml, ExecTask.class); assertThat(execTask.getArgList(), is(new Arguments(new Argument("arg1"), new Argument("arg2")))); }