|
| 1 | +package javaposse.jobdsl.plugin |
| 2 | + |
| 3 | +import javaposse.jobdsl.dsl.ConfigFile |
| 4 | +import javaposse.jobdsl.dsl.ConfigFileType |
| 5 | +import javaposse.jobdsl.dsl.JobManagement |
| 6 | +import javaposse.jobdsl.dsl.MavenSettingsConfigFile |
| 7 | +import javaposse.jobdsl.dsl.ParametrizedConfigFile |
| 8 | +import org.jenkinsci.lib.configprovider.model.Config |
| 9 | +import org.jenkinsci.plugins.configfiles.GlobalConfigFiles |
| 10 | +import org.jenkinsci.plugins.configfiles.custom.CustomConfig |
| 11 | +import org.jenkinsci.plugins.configfiles.maven.GlobalMavenSettingsConfig |
| 12 | +import org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig |
| 13 | +import org.jenkinsci.plugins.managedscripts.ScriptConfig |
| 14 | +import org.junit.ClassRule |
| 15 | +import org.jvnet.hudson.test.JenkinsRule |
| 16 | +import spock.lang.Shared |
| 17 | +import spock.lang.Specification |
| 18 | + |
| 19 | +import static java.util.UUID.randomUUID |
| 20 | +import static javaposse.jobdsl.plugin.ConfigFileProviderHelper.createNewConfig |
| 21 | +import static javaposse.jobdsl.plugin.ConfigFileProviderHelper.findConfig |
| 22 | +import static javaposse.jobdsl.plugin.ConfigFileProviderHelper.findConfigProvider |
| 23 | + |
| 24 | +class ConfigFileProviderHelperSpec extends Specification { |
| 25 | + @Shared |
| 26 | + @ClassRule |
| 27 | + JenkinsRule jenkinsRule = new JenkinsRule() |
| 28 | + |
| 29 | + @Shared |
| 30 | + Config customConfig |
| 31 | + |
| 32 | + @Shared |
| 33 | + Config mavenSettingsConfig |
| 34 | + |
| 35 | + @Shared |
| 36 | + Config globalMavenSettingsConfig |
| 37 | + |
| 38 | + @Shared |
| 39 | + Config scriptConfig |
| 40 | + |
| 41 | + def setupSpec() { |
| 42 | + GlobalConfigFiles globalConfigFiles = |
| 43 | + jenkinsRule.instance.getExtensionList(GlobalConfigFiles).get(GlobalConfigFiles) |
| 44 | + |
| 45 | + customConfig = |
| 46 | + new CustomConfig(randomUUID().toString(), 'custom', 'foo', 'bar') |
| 47 | + globalConfigFiles.save(customConfig) |
| 48 | + mavenSettingsConfig = |
| 49 | + new MavenSettingsConfig(randomUUID().toString(), 'mavenSettings', 'foo', 'bar', false, []) |
| 50 | + globalConfigFiles.save(mavenSettingsConfig) |
| 51 | + globalMavenSettingsConfig = |
| 52 | + new GlobalMavenSettingsConfig(randomUUID().toString(), 'globalMavenSettings', 'foo', 'bar', false, []) |
| 53 | + globalConfigFiles.save(globalMavenSettingsConfig) |
| 54 | + scriptConfig = |
| 55 | + new ScriptConfig(randomUUID().toString(), 'script', 'foo', 'bar', []) |
| 56 | + globalConfigFiles.save(scriptConfig) |
| 57 | + } |
| 58 | + |
| 59 | + def 'find config'() { |
| 60 | + expect: |
| 61 | + findConfig(customConfig.provider, 'custom') == customConfig |
| 62 | + findConfig(mavenSettingsConfig.provider, 'mavenSettings') == mavenSettingsConfig |
| 63 | + findConfig(globalMavenSettingsConfig.provider, 'globalMavenSettings') == globalMavenSettingsConfig |
| 64 | + findConfig(scriptConfig.provider, 'script') == scriptConfig |
| 65 | + |
| 66 | + findConfig(customConfig.provider, 'script') == null |
| 67 | + } |
| 68 | + |
| 69 | + def 'find config provider'() { |
| 70 | + expect: |
| 71 | + findConfigProvider(ConfigFileType.Custom) == customConfig.provider |
| 72 | + findConfigProvider(ConfigFileType.MavenSettings) == mavenSettingsConfig.provider |
| 73 | + findConfigProvider(ConfigFileType.GlobalMavenSettings) == globalMavenSettingsConfig.provider |
| 74 | + findConfigProvider(ConfigFileType.ManagedScript) == scriptConfig.provider |
| 75 | + } |
| 76 | + |
| 77 | + def 'create new custom config'() { |
| 78 | + setup: |
| 79 | + String id = randomUUID() |
| 80 | + ConfigFile configFile = new ConfigFile(ConfigFileType.Custom, Mock(JobManagement)) |
| 81 | + configFile.name = 'newCustom' |
| 82 | + configFile.comment = 'bbb' |
| 83 | + configFile.content = 'aaa' |
| 84 | + |
| 85 | + when: |
| 86 | + Config config = createNewConfig(id, configFile) |
| 87 | + |
| 88 | + then: |
| 89 | + config instanceof CustomConfig |
| 90 | + config.id == id |
| 91 | + config.name == configFile.name |
| 92 | + config.comment == configFile.comment |
| 93 | + config.content == configFile.content |
| 94 | + } |
| 95 | + |
| 96 | + def 'create new Maven settings config'() { |
| 97 | + setup: |
| 98 | + String id = randomUUID() |
| 99 | + ConfigFile configFile = new MavenSettingsConfigFile(ConfigFileType.MavenSettings, Mock(JobManagement)) |
| 100 | + configFile.name = 'newCustom' |
| 101 | + configFile.comment = 'bbb' |
| 102 | + configFile.content = 'aaa' |
| 103 | + configFile.replaceAll = true |
| 104 | + configFile.credentialsMapping['foo'] = 'bar' |
| 105 | + |
| 106 | + when: |
| 107 | + Config config = createNewConfig(id, configFile) |
| 108 | + |
| 109 | + then: |
| 110 | + config instanceof MavenSettingsConfig |
| 111 | + config.id == id |
| 112 | + config.name == configFile.name |
| 113 | + config.comment == configFile.comment |
| 114 | + config.content == configFile.content |
| 115 | + ((MavenSettingsConfig) config).isReplaceAll |
| 116 | + ((MavenSettingsConfig) config).serverCredentialMappings.size() == 1 |
| 117 | + ((MavenSettingsConfig) config).serverCredentialMappings[0].serverId == 'foo' |
| 118 | + ((MavenSettingsConfig) config).serverCredentialMappings[0].credentialsId == 'bar' |
| 119 | + } |
| 120 | + |
| 121 | + def 'create new global Maven settings config'() { |
| 122 | + setup: |
| 123 | + String id = randomUUID() |
| 124 | + ConfigFile configFile = new MavenSettingsConfigFile(ConfigFileType.GlobalMavenSettings, Mock(JobManagement)) |
| 125 | + configFile.name = 'newCustom' |
| 126 | + configFile.comment = 'bbb' |
| 127 | + configFile.content = 'aaa' |
| 128 | + configFile.replaceAll = true |
| 129 | + configFile.credentialsMapping['foo'] = 'bar' |
| 130 | + |
| 131 | + when: |
| 132 | + Config config = createNewConfig(id, configFile) |
| 133 | + |
| 134 | + then: |
| 135 | + config instanceof GlobalMavenSettingsConfig |
| 136 | + config.id == id |
| 137 | + config.name == configFile.name |
| 138 | + config.comment == configFile.comment |
| 139 | + config.content == configFile.content |
| 140 | + ((GlobalMavenSettingsConfig) config).isReplaceAll |
| 141 | + ((GlobalMavenSettingsConfig) config).serverCredentialMappings.size() == 1 |
| 142 | + ((GlobalMavenSettingsConfig) config).serverCredentialMappings[0].serverId == 'foo' |
| 143 | + ((GlobalMavenSettingsConfig) config).serverCredentialMappings[0].credentialsId == 'bar' |
| 144 | + } |
| 145 | + |
| 146 | + def 'create new script config'() { |
| 147 | + setup: |
| 148 | + String id = randomUUID() |
| 149 | + ConfigFile configFile = new ParametrizedConfigFile(ConfigFileType.ManagedScript, Mock(JobManagement)) |
| 150 | + configFile.name = 'newCustom' |
| 151 | + configFile.comment = 'bbb' |
| 152 | + configFile.content = 'aaa' |
| 153 | + configFile.arguments = ['one', 'two'] |
| 154 | + |
| 155 | + when: |
| 156 | + Config config = createNewConfig(id, configFile) |
| 157 | + |
| 158 | + then: |
| 159 | + config instanceof ScriptConfig |
| 160 | + config.id == id |
| 161 | + config.name == configFile.name |
| 162 | + config.comment == configFile.comment |
| 163 | + config.content == configFile.content |
| 164 | + ((ScriptConfig) config).args.size() == 2 |
| 165 | + ((ScriptConfig) config).args[0].name == 'one' |
| 166 | + ((ScriptConfig) config).args[1].name == 'two' |
| 167 | + } |
| 168 | +} |
0 commit comments