|
| 1 | +package stashpullrequestbuilder.stashpullrequestbuilder; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.*; |
| 5 | +import static org.mockito.Mockito.doReturn; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import hudson.EnvVars; |
| 10 | +import hudson.model.Build; |
| 11 | +import hudson.model.Job; |
| 12 | +import hudson.model.Run; |
| 13 | +import hudson.model.TaskListener; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Rule; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.jvnet.hudson.test.JenkinsRule; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.junit.MockitoJUnit; |
| 21 | +import org.mockito.junit.MockitoJUnitRunner; |
| 22 | +import org.mockito.junit.MockitoRule; |
| 23 | +import org.mockito.quality.Strictness; |
| 24 | + |
| 25 | +@RunWith(MockitoJUnitRunner.class) |
| 26 | +public class StashBuildEnvironmentContributorTest { |
| 27 | + |
| 28 | + @Rule public JenkinsRule jenkinsRule = new JenkinsRule(); |
| 29 | + @Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); |
| 30 | + |
| 31 | + private StashBuildEnvironmentContributor contributor; |
| 32 | + |
| 33 | + private EnvVars envVars; |
| 34 | + private StashCause cause; |
| 35 | + |
| 36 | + @Mock private TaskListener listener; |
| 37 | + |
| 38 | + @Before |
| 39 | + public void before() { |
| 40 | + contributor = new StashBuildEnvironmentContributor(); |
| 41 | + envVars = new EnvVars(); |
| 42 | + cause = |
| 43 | + new StashCause( |
| 44 | + "StashHost", |
| 45 | + "SourceBranch", |
| 46 | + "TargetBranch", |
| 47 | + "SourceRepositoryOwner", |
| 48 | + "SourceRepositoryName", |
| 49 | + "PullRequestId", |
| 50 | + "DestinationRepositoryOwner", |
| 51 | + "DestinationRepositoryName", |
| 52 | + "PullRequestTitle", |
| 53 | + "SourceCommitHash", |
| 54 | + "DestinationCommitHash", |
| 55 | + "BuildStartCommentId", |
| 56 | + "PullRequestVersion", |
| 57 | + null); |
| 58 | + } |
| 59 | + |
| 60 | + private void checkEnvVars() { |
| 61 | + assertThat(envVars.size(), is(10)); |
| 62 | + |
| 63 | + assertThat(envVars, hasEntry("destinationCommitHash", "DestinationCommitHash")); |
| 64 | + assertThat(envVars, hasEntry("destinationRepositoryName", "DestinationRepositoryName")); |
| 65 | + assertThat(envVars, hasEntry("destinationRepositoryOwner", "DestinationRepositoryOwner")); |
| 66 | + assertThat(envVars, hasEntry("pullRequestId", "PullRequestId")); |
| 67 | + assertThat(envVars, hasEntry("pullRequestTitle", "PullRequestTitle")); |
| 68 | + assertThat(envVars, hasEntry("sourceBranch", "SourceBranch")); |
| 69 | + assertThat(envVars, hasEntry("sourceCommitHash", "SourceCommitHash")); |
| 70 | + assertThat(envVars, hasEntry("sourceRepositoryName", "SourceRepositoryName")); |
| 71 | + assertThat(envVars, hasEntry("sourceRepositoryOwner", "SourceRepositoryOwner")); |
| 72 | + assertThat(envVars, hasEntry("targetBranch", "TargetBranch")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void populatesVariablesForRun() throws Exception { |
| 77 | + Run<?, ?> run = mock(Run.class); |
| 78 | + when(run.getCause(StashCause.class)).thenReturn(cause); |
| 79 | + |
| 80 | + contributor.buildEnvironmentFor(run, envVars, listener); |
| 81 | + checkEnvVars(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void noVariablesForRunWithoutCause() throws Exception { |
| 86 | + Run<?, ?> run = mock(Run.class); |
| 87 | + when(run.getCause(StashCause.class)).thenReturn(null); |
| 88 | + |
| 89 | + contributor.buildEnvironmentFor(run, envVars, listener); |
| 90 | + assertThat(envVars, is(anEmptyMap())); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void populatesVariablesForRootBuild() throws Exception { |
| 95 | + Build<?, ?> rootBuild = mock(Build.class); |
| 96 | + |
| 97 | + doReturn(rootBuild).when(rootBuild).getRootBuild(); |
| 98 | + when(rootBuild.getCause(StashCause.class)).thenReturn(cause); |
| 99 | + |
| 100 | + contributor.buildEnvironmentFor(rootBuild, envVars, listener); |
| 101 | + checkEnvVars(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void populatesVariablesForChildBuild() throws Exception { |
| 106 | + Build<?, ?> childBuild = mock(Build.class); |
| 107 | + Build<?, ?> rootBuild = mock(Build.class); |
| 108 | + |
| 109 | + doReturn(rootBuild).when(childBuild).getRootBuild(); |
| 110 | + when(rootBuild.getCause(StashCause.class)).thenReturn(cause); |
| 111 | + |
| 112 | + contributor.buildEnvironmentFor(childBuild, envVars, listener); |
| 113 | + checkEnvVars(); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void noVariablesForJob() throws Exception { |
| 118 | + Job<?, ?> job = mock(Job.class); |
| 119 | + |
| 120 | + contributor.buildEnvironmentFor(job, envVars, listener); |
| 121 | + assertThat(envVars, is(anEmptyMap())); |
| 122 | + } |
| 123 | +} |
0 commit comments