-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a test case for the BuildActionIterator.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/test/java/io/jenkins/plugins/echarts/BuildActionIteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.jenkins.plugins.echarts; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import edu.hm.hafner.echarts.BuildResult; | ||
|
||
import hudson.model.Run; | ||
|
||
import io.jenkins.plugins.util.BuildAction; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Tests the class {@link BuildActionIterator}. | ||
* | ||
* @author Ullrich Hafner | ||
*/ | ||
class BuildActionIteratorTest { | ||
@Test | ||
void shouldCreateEmptyIterator() { | ||
BuildActionIterator<TestAction> iterator = new BuildActionIterator<>(TestAction.class, Optional.empty()); | ||
|
||
assertThat(iterator).isExhausted(); | ||
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(iterator::next); | ||
} | ||
|
||
@Test | ||
void shouldReturnResultOfCurrentBuild() { | ||
TestAction testAction = mock(TestAction.class); | ||
Run<?, ?> baseline = mock(Run.class); | ||
when(testAction.getOwner()).thenAnswer(i -> baseline); | ||
|
||
BuildActionIterator<TestAction> iterator = new BuildActionIterator<>(TestAction.class, Optional.of(testAction)); | ||
|
||
assertThat(iterator).hasNext(); | ||
assertThat(iterator.next()).usingRecursiveComparison().ignoringFields("build") | ||
.isEqualTo(new BuildResult<>(null, testAction)); | ||
assertThat(iterator).isExhausted(); | ||
} | ||
|
||
@Test | ||
void shouldReturnResultOfPreviousBuild() { | ||
TestAction testAction = mock(TestAction.class); | ||
Run<?, ?> baseline = mock(Run.class); | ||
when(testAction.getOwner()).thenAnswer(i -> baseline); | ||
|
||
BuildActionIterator<TestAction> iterator = new BuildActionIterator<>(TestAction.class, Optional.of(testAction)); | ||
|
||
TestAction previousAction = mock(TestAction.class); | ||
Run<?, ?> previousBuild = mock(Run.class); | ||
when(previousAction.getOwner()).thenAnswer(i -> previousBuild); | ||
|
||
when(previousBuild.getAction(TestAction.class)).thenReturn(previousAction); | ||
when(baseline.getPreviousBuild()).thenAnswer(i -> previousBuild); | ||
|
||
assertThat(iterator).hasNext(); | ||
assertThat(iterator.next()).usingRecursiveComparison().ignoringFields("build") | ||
.isEqualTo(new BuildResult<>(null, testAction)); | ||
|
||
assertThat(iterator).hasNext(); | ||
assertThat(iterator.next()).usingRecursiveComparison().ignoringFields("build") | ||
.isEqualTo(new BuildResult<>(null, previousAction)); | ||
} | ||
|
||
private abstract static class TestAction extends BuildAction<String> { | ||
private static final long serialVersionUID = -8129555082892128959L; | ||
|
||
TestAction(final Run<?, ?> owner, final String result) { | ||
super(owner, result); | ||
} | ||
} | ||
} |