This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from AlexanderPraegla/hm-edu-testing
[JENKINS-50074] Added Mockito Test for ByIdResultSelector
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
src/test/java/io/jenkins/plugins/analysis/core/model/ByIdResultSelectorTest.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,116 @@ | ||
package io.jenkins.plugins.analysis.core.model; | ||
|
||
import hudson.model.Run; | ||
import io.jenkins.plugins.analysis.core.views.ResultAction; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Tests the class {@link ByIdResultSelector} | ||
* | ||
* @author Alexander Praegla | ||
*/ | ||
class ByIdResultSelectorTest { | ||
|
||
/** | ||
* Verifies the returned optional is empty if the provided list of {@link ResultAction} is empty | ||
*/ | ||
@Test | ||
void shouldBeEmptyIfActionsAreEmpty() { | ||
ByIdResultSelector cut = new ByIdResultSelector("1"); | ||
|
||
Run<?, ?> run = createRunStub(new ArrayList<>()); | ||
|
||
assertThat(cut.get(run)).isEmpty(); | ||
} | ||
|
||
/** | ||
* Verifies the returned optional is empty if the provided list of {@link ResultAction} | ||
* does not contains the id of the instance of {@link ByIdResultSelector} | ||
*/ | ||
@Test | ||
void shouldBeEmptyIfActionsHasDifferentId() { | ||
ByIdResultSelector cut = new ByIdResultSelector("1"); | ||
|
||
List<ResultAction> resultActions = new ArrayList<>(); | ||
resultActions.add(createResultActionStub("2")); | ||
|
||
Run<?, ?> run = createRunStub(resultActions); | ||
|
||
assertThat(cut.get(run)).isEmpty(); | ||
} | ||
|
||
/** | ||
* Verifies the returned optional is not empty if the provided list of {@link ResultAction} contains one element | ||
*/ | ||
@Test | ||
void shouldFindActionWithSameId() { | ||
ByIdResultSelector cut = new ByIdResultSelector("1"); | ||
|
||
List<ResultAction> resultActions = new ArrayList<>(); | ||
resultActions.add(createResultActionStub("1")); | ||
|
||
Run<?, ?> run = createRunStub(resultActions); | ||
|
||
assertThat(cut.get(run)).isPresent(); | ||
} | ||
|
||
/** | ||
* Verifies the returned optional is empty if the provided list of {@link ResultAction} contains two elements with different id | ||
*/ | ||
@Test | ||
void shouldFindEmptyIfListContainsTwoDifferentIds() { | ||
ByIdResultSelector cut = new ByIdResultSelector("1"); | ||
|
||
List<ResultAction> resultActions = new ArrayList<>(); | ||
resultActions.add(createResultActionStub("2")); | ||
resultActions.add(createResultActionStub("3")); | ||
|
||
Run<?, ?> run = createRunStub(resultActions); | ||
|
||
assertThat(cut.get(run)).isEmpty(); | ||
} | ||
|
||
/** | ||
* Verifies the returned optional is not empty if the provided list of {@link ResultAction} contains two elements | ||
*/ | ||
@Test | ||
void shouldFindActionWithSameIdIfListContainsTwoDifferentIds() { | ||
ByIdResultSelector cut = new ByIdResultSelector("1"); | ||
|
||
List<ResultAction> resultActions = new ArrayList<>(); | ||
resultActions.add(createResultActionStub("2")); | ||
resultActions.add(createResultActionStub("1")); | ||
|
||
Run<?, ?> run = createRunStub(resultActions); | ||
|
||
assertThat(cut.get(run)).isPresent(); | ||
} | ||
|
||
/** | ||
* Verifies the toString method | ||
*/ | ||
@Test | ||
void testToString() { | ||
ByIdResultSelector cut = new ByIdResultSelector("1"); | ||
assertThat(cut.toString()).isEqualTo("io.jenkins.plugins.analysis.core.views.ResultAction with ID 1"); | ||
} | ||
|
||
private Run<?, ?> createRunStub(List<ResultAction> resultActions) { | ||
Run<?, ?> run = mock(Run.class); | ||
when(run.getActions(ResultAction.class)).thenReturn(resultActions); | ||
return run; | ||
} | ||
|
||
private ResultAction createResultActionStub(String mockedIdValue) { | ||
ResultAction resultAction = mock(ResultAction.class); | ||
when(resultAction.getId()).thenReturn(mockedIdValue); | ||
return resultAction; | ||
} | ||
} |