@@ -2,8 +2,17 @@ import static org.hamcrest.Matchers.hasItem
22import static org.hamcrest.Matchers.instanceOf
33import static org.junit.Assert.assertEquals
44import static org.junit.Assert.assertThat
5+ import static org.junit.Assert.assertTrue
56import static org.mockito.Mockito.mock
67import static org.mockito.Mockito.when
8+ import static org.mockito.Mockito.spy;
9+ import static org.mockito.Mockito.verify;
10+ import static org.mockito.Mockito.anyString
11+ import static org.mockito.Mockito.eq
12+ import static org.mockito.Mockito.doReturn
13+ import static org.mockito.Mockito.any
14+ import static org.mockito.Mockito.never
15+ import static org.mockito.Mockito.times
716
817import org.junit.After
918import org.junit.Test
@@ -28,43 +37,151 @@ class ParameterStoreBuildWrapperPluginTest {
2837 }
2938
3039 @Test
31- void modifiesTerraformEnvironmentStageCommandWithGlobalParameter () {
32- ParameterStoreBuildWrapperPlugin . withGlobalParameter( ' /somePath/ ' ) . init()
40+ void modifiesTerraformValidateStageCommand () {
41+ ParameterStoreBuildWrapperPlugin . init()
3342
34- Collection actualPlugins = TerraformEnvironmentStage . getPlugins()
43+ Collection actualPlugins = TerraformValidateStage . getPlugins()
3544 assertThat (actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin . class)))
3645 }
46+ }
47+
48+ public class Apply {
49+ @After
50+ public void reset () {
51+ Jenkinsfile . instance = null
52+ ParameterStoreBuildWrapperPlugin . reset()
53+ }
54+
55+ private configureJenkins (Map config = [:]) {
56+ Jenkinsfile . instance = mock(Jenkinsfile . class)
57+ when(Jenkinsfile . instance. getStandardizedRepoSlug()). thenReturn(config. repoSlug)
58+ when(Jenkinsfile . instance. getRepoName()). thenReturn(config. repoName ?: ' repo' )
59+ when(Jenkinsfile . instance. getOrganization()). thenReturn(config. organization ?: ' org' )
60+ when(Jenkinsfile . instance. getEnv()). thenReturn(config. env ?: [:])
61+ }
3762
3863 @Test
39- void modifiesTerraformEnvironmentStageCommandWithGlobalParameterAndOptions () {
40- ParameterStoreBuildWrapperPlugin . withGlobalParameter(' /somePath/' , [someKey : true , anotherKey : ' someValue' ]). init()
64+ void doesNotDecorateTheTerraformValidateStageIfGlobalParametersNotSet () {
65+ def expectedClosure = { -> }
66+ TerraformValidateStage stage = mock(TerraformValidateStage . class)
67+ ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin ())
4168
42- Collection actualPlugins = TerraformEnvironmentStage . getPlugins()
43- assertThat (actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin . class)))
69+ plugin. apply(stage)
70+
71+ verify(stage, never()). decorate(expectedClosure)
4472 }
4573
4674 @Test
47- void modifiesTerraformValidateStageCommand () {
48- ParameterStoreBuildWrapperPlugin . init()
75+ void decorateTheTerraformValidateStageIfGlobalParametersSet () {
76+ String path = ' /somePath/'
77+ def expectedClosure = { -> }
78+ Map gp = [path : path]
79+ TerraformValidateStage stage = mock(TerraformValidateStage . class)
80+ ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin ())
4981
50- Collection actualPlugins = TerraformValidateStage . getPlugins()
51- assertThat (actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin . class)))
82+ doReturn(expectedClosure). when(plugin). addParameterStoreBuildWrapper(gp)
83+
84+ plugin. withGlobalParameter(path)
85+ plugin. apply(stage)
86+
87+ verify(stage). decorate(TerraformValidateStage . ALL , expectedClosure)
5288 }
5389
5490 @Test
55- void modifiesTerraformValidateStageCommandWithGlobalParameter () {
56- ParameterStoreBuildWrapperPlugin . withGlobalParameter(' /somePath/' ). init()
91+ void decorateTheTerraformEnvironmentStageIfGlobalParametersNotSet () {
92+ String organization = " MyOrg"
93+ String repoName = " MyRepo"
94+ String environment = " MyEnv"
95+ Map apo = [path : " /${ organization} /${ repoName} /${ environment} /" , credentialsId : " ${ environment.toUpperCase()} _PARAMETER_STORE_ACCESS" ]
96+ def expectedClosure = { -> }
97+ configureJenkins(repoName : repoName, organization : organization)
5798
58- Collection actualPlugins = TerraformValidateStage . getPlugins()
59- assertThat (actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin . class)))
99+ TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage . class)
100+ ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin ())
101+
102+ doReturn(environment). when(stage). getEnvironment()
103+ doReturn(apo). when(plugin). getEnvironmentParameterOptions(environment)
104+ doReturn(expectedClosure). when(plugin). addParameterStoreBuildWrapper(apo)
105+
106+ plugin. apply(stage)
107+
108+ verify(stage, times(2 )). decorate(anyString(), eq(expectedClosure))
60109 }
61110
62111 @Test
63- void modifiesTerraformValidateStageCommandWithGlobalParameterAndOptions () {
64- ParameterStoreBuildWrapperPlugin . withGlobalParameter(' /somePath/' , [someKey : true , anotherKey : ' someValue' ]). init()
112+ void decorateTheTerraformEnvironmentStageWhenGlobalParametersSet () {
113+ String organization = " MyOrg"
114+ String repoName = " MyRepo"
115+ String environment = " MyEnv"
116+ String path = ' /someOtherPath/'
117+ Map apo = [path : " /${ organization} /${ repoName} /${ environment} /" , credentialsId : " ${ environment.toUpperCase()} _PARAMETER_STORE_ACCESS" ]
118+ Map gp = [path : path]
119+ def firstClosure = { -> }
120+ def secondClosure = { -> }
121+ configureJenkins(repoName : repoName, organization : organization)
65122
66- Collection actualPlugins = TerraformValidateStage . getPlugins()
67- assertThat (actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin . class)))
123+ TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage . class)
124+ ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin ())
125+
126+ doReturn(environment). when(stage). getEnvironment()
127+ doReturn(apo). when(plugin). getEnvironmentParameterOptions(environment)
128+ doReturn(firstClosure). when(plugin). addParameterStoreBuildWrapper(gp)
129+ doReturn(secondClosure). when(plugin). addParameterStoreBuildWrapper(apo)
130+
131+ plugin. withGlobalParameter(path)
132+ plugin. apply(stage)
133+
134+ verify(stage, times(2 )). decorate(anyString(), eq(firstClosure))
135+ verify(stage, times(2 )). decorate(anyString(), eq(secondClosure))
136+ }
137+
138+ }
139+
140+ public class GetEnvironmentParameterOptions {
141+ @After
142+ public void reset () {
143+ Jenkinsfile . instance = null
144+ ParameterStoreBuildWrapperPlugin . reset()
145+ }
146+
147+ private configureJenkins (Map config = [:]) {
148+ Jenkinsfile . instance = mock(Jenkinsfile . class)
149+ when(Jenkinsfile . instance. getStandardizedRepoSlug()). thenReturn(config. repoSlug)
150+ when(Jenkinsfile . instance. getRepoName()). thenReturn(config. repoName ?: ' repo' )
151+ when(Jenkinsfile . instance. getOrganization()). thenReturn(config. organization ?: ' org' )
152+ when(Jenkinsfile . instance. getEnv()). thenReturn(config. env ?: [:])
153+ }
154+
155+ @Test
156+ void validPathBasedOnEnvironment () {
157+ String organization = " MyOrg"
158+ String repoName = " MyRepo"
159+ String environment = " qa"
160+ Map expected = [path : " /${ organization} /${ repoName} /${ environment} /" . toString()]
161+
162+ configureJenkins(repoName : repoName, organization : organization)
163+ ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin ())
164+ doReturn(expected. path. toString()). when(plugin). pathForEnvironment(environment)
165+
166+ Map actual = plugin. getEnvironmentParameterOptions(environment)
167+
168+ assertEquals (expected. path, actual. path)
169+ }
170+
171+ @Test
172+ void validCredentialsBasedOnEnvironment () {
173+ String organization = " MyOrg"
174+ String repoName = " MyRepo"
175+ String environment = " qa"
176+ Map expected = [credentialsId : " ${ environment.toUpperCase()} _PARAMETER_STORE_ACCESS" ]
177+
178+ configureJenkins(repoName : repoName, organization : organization)
179+ ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin ())
180+ doReturn(expected. path. toString()). when(plugin). pathForEnvironment(environment)
181+
182+ Map actual = plugin. getEnvironmentParameterOptions(environment)
183+
184+ assertEquals (expected. credentialsId, actual. credentialsId)
68185 }
69186 }
70187
@@ -133,28 +250,48 @@ class ParameterStoreBuildWrapperPluginTest {
133250 }
134251
135252 @Test
136- void addGlobalParameter () {
137- def result = ParameterStoreBuildWrapperPlugin . withGlobalParameter(' /path/' , [])
138-
253+ void addGlobalParameterWithNoOptions () {
254+ String path = ' /path/'
255+ def result = ParameterStoreBuildWrapperPlugin . withGlobalParameter(path)
256+ println (result)
139257 assertEquals ([[path : ' /path/' ]], result. globalParameterOptions)
140258 }
141259
260+ @Test
261+ void addGlobalParameterWithEmptyOptions () {
262+ Map options = [:]
263+ String path = ' /path/'
264+ ArrayList<Map > expected = []
265+ expected << [path : path] + options
266+
267+ def result = ParameterStoreBuildWrapperPlugin . withGlobalParameter(path, options)
268+
269+ assertEquals (expected, result. globalParameterOptions)
270+ }
271+
142272 @Test
143273 void addGlobalParameterWithOptions () {
144- def result = ParameterStoreBuildWrapperPlugin . withGlobalParameter(' /path/' , [recursive : true , basename : ' relative' ])
274+ Map options = [recursive : true , basename : ' relative' ]
275+ String path = ' /path/'
276+ ArrayList<Map > expected = []
277+ expected << [path : path] + options
145278
146- assertEquals ([[path : ' /path/' , recursive : true , basename : ' relative' ]], result. globalParameterOptions)
279+ def result = ParameterStoreBuildWrapperPlugin . withGlobalParameter(path, options)
280+
281+ assertEquals (expected, result. globalParameterOptions)
147282 }
148283
149284 @Test
150285 void addMulitpleGlobalParameters () {
151- ArrayList expected = []
152- def result = ParameterStoreBuildWrapperPlugin . withGlobalParameter(' /path/' , [])
153- .withGlobalParameter(' /path2/' , [recursive : true ])
154- .withGlobalParameter(' /path3/' , [basename : ' something' ])
286+ ArrayList<Map > expected = []
287+ def result = ParameterStoreBuildWrapperPlugin . withGlobalParameter(' /path/' )
288+ .withGlobalParameter(' /path2/' , [:])
289+ .withGlobalParameter(' /path3/' , [recursive : true ])
290+ .withGlobalParameter(' /path4/' , [basename : ' something' ])
155291 expected << [path :' /path/' ]
156- expected << [path : ' /path2/' , recursive : true ]
157- expected << [path : ' /path3/' , basename : ' something' ]
292+ expected << [path :' /path2/' ]
293+ expected << [path : ' /path3/' , recursive : true ]
294+ expected << [path : ' /path4/' , basename : ' something' ]
158295 assertEquals (expected, result. globalParameterOptions)
159296 }
160297 }
0 commit comments