Skip to content

Commit

Permalink
reorganize apply test cases into nested classes
Browse files Browse the repository at this point in the history
  • Loading branch information
codezninja committed Sep 23, 2020
1 parent c5baeb1 commit 6ec359f
Showing 1 changed file with 69 additions and 66 deletions.
135 changes: 69 additions & 66 deletions test/ParameterStoreBuildWrapperPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -58,81 +58,84 @@ class ParameterStoreBuildWrapperPluginTest {
when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:])
}

@Test
void doesNotDecorateTheTerraformValidateStageIfGlobalParametersNotSet() {
def expectedClosure = { -> }
TerraformValidateStage stage = mock(TerraformValidateStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

plugin.apply(stage)

verify(stage, never()).decorate(expectedClosure)
}

@Test
void decorateTheTerraformValidateStageIfGlobalParametersSet() {
String path = '/somePath/'
def expectedClosure = { -> }
Map gp = [path: path]
TerraformValidateStage stage = mock(TerraformValidateStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(gp)
class WithTerraformValidateStage {
@Test
void doesNotDecorateTheTerraformValidateStageIfGlobalParametersNotSet() {
def expectedClosure = { -> }
TerraformValidateStage stage = mock(TerraformValidateStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

plugin.withGlobalParameter(path)
plugin.apply(stage)
plugin.apply(stage)

verify(stage).decorate(TerraformValidateStage.ALL, expectedClosure)
}

@Test
void decorateTheTerraformEnvironmentStageIfGlobalParametersNotSet() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "MyEnv"
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
def expectedClosure = { -> }
configureJenkins(repoName: repoName, organization: organization)
verify(stage, never()).decorate(expectedClosure)
}

TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
@Test
void decorateTheTerraformValidateStageIfGlobalParametersSet() {
String path = '/somePath/'
def expectedClosure = { -> }
Map gp = [path: path]
TerraformValidateStage stage = mock(TerraformValidateStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

doReturn(environment).when(stage).getEnvironment()
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(apo)
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(gp)

plugin.apply(stage)
plugin.withGlobalParameter(path)
plugin.apply(stage)

verify(stage, times(2)).decorate(anyString(), eq(expectedClosure))
verify(stage).decorate(TerraformValidateStage.ALL, expectedClosure)
}
}

@Test
void decorateTheTerraformEnvironmentStageWhenGlobalParametersSet() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "MyEnv"
String path = '/someOtherPath/'
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
Map gp = [path: path]
def firstClosure = { -> }
def secondClosure = { -> }
configureJenkins(repoName: repoName, organization: organization)

TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

doReturn(environment).when(stage).getEnvironment()
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
doReturn(firstClosure).when(plugin).addParameterStoreBuildWrapper(gp)
doReturn(secondClosure).when(plugin).addParameterStoreBuildWrapper(apo)

plugin.withGlobalParameter(path)
plugin.apply(stage)

verify(stage, times(2)).decorate(anyString(), eq(firstClosure))
verify(stage, times(2)).decorate(anyString(), eq(secondClosure))
class WithTerraformEnvironmentStage {
@Test
void decorateTheTerraformEnvironmentStageIfGlobalParametersNotSet() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "MyEnv"
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
def expectedClosure = { -> }
configureJenkins(repoName: repoName, organization: organization)

TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

doReturn(environment).when(stage).getEnvironment()
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(apo)

plugin.apply(stage)

verify(stage, times(2)).decorate(anyString(), eq(expectedClosure))
}

@Test
void decorateTheTerraformEnvironmentStageWhenGlobalParametersSet() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "MyEnv"
String path = '/someOtherPath/'
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
Map gp = [path: path]
def firstClosure = { -> }
def secondClosure = { -> }
configureJenkins(repoName: repoName, organization: organization)

TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

doReturn(environment).when(stage).getEnvironment()
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
doReturn(firstClosure).when(plugin).addParameterStoreBuildWrapper(gp)
doReturn(secondClosure).when(plugin).addParameterStoreBuildWrapper(apo)

plugin.withGlobalParameter(path)
plugin.apply(stage)

verify(stage, times(2)).decorate(anyString(), eq(firstClosure))
verify(stage, times(2)).decorate(anyString(), eq(secondClosure))
}
}

}

public class GetEnvironmentParameterOptions {
Expand Down

0 comments on commit 6ec359f

Please sign in to comment.