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 #497 from amihaiemil/496
#496 TaskResignations implemented and tested
- Loading branch information
Showing
3 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
self-core-impl/src/main/java/com/selfxdsd/core/tasks/TaskResignations.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,91 @@ | ||
/** | ||
* Copyright (c) 2020, Self XDSD Contributors | ||
* All rights reserved. | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), | ||
* to read the Software only. Permission is hereby NOT GRANTED to use, copy, | ||
* modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software. | ||
* <p> | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | ||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | ||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.selfxdsd.core.tasks; | ||
|
||
import com.selfxdsd.api.Resignation; | ||
import com.selfxdsd.api.Resignations; | ||
import com.selfxdsd.api.Task; | ||
import com.selfxdsd.api.storage.Storage; | ||
|
||
import java.util.Iterator; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Resignations registered in a Task. | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $Id$ | ||
* @since 0.0.21 | ||
*/ | ||
public final class TaskResignations implements Resignations { | ||
|
||
/** | ||
* The task in question. | ||
*/ | ||
private final Task task; | ||
|
||
/** | ||
* The task's resignations. | ||
*/ | ||
private final Supplier<Stream<Resignation>> resignations; | ||
|
||
/** | ||
* Self storage. | ||
*/ | ||
private final Storage storage; | ||
|
||
/** | ||
* Ctor. | ||
* @param task The task. | ||
* @param resignations The task's resignations. | ||
* @param storage Self storage. | ||
*/ | ||
public TaskResignations( | ||
final Task task, | ||
final Supplier<Stream<Resignation>> resignations, | ||
final Storage storage | ||
) { | ||
this.task = task; | ||
this.resignations = resignations; | ||
this.storage = storage; | ||
} | ||
|
||
@Override | ||
public Resignations ofTask(final Task task) { | ||
if(this.task.equals(task)) { | ||
return this; | ||
} else { | ||
throw new IllegalStateException( | ||
"Already seeing the resignations of Task #" | ||
+ this.task.issueId() + " from project " | ||
+ this.task.project().repoFullName() + " at " | ||
+ this.task.project().provider() + "." | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<Resignation> iterator() { | ||
return this.resignations.get().iterator(); | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
self-core-impl/src/test/java/com/selfxdsd/core/tasks/TaskResignationsTestCase.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,117 @@ | ||
/** | ||
* Copyright (c) 2020, Self XDSD Contributors | ||
* All rights reserved. | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), | ||
* to read the Software only. Permission is hereby NOT GRANTED to use, copy, | ||
* modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software. | ||
* <p> | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | ||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | ||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.selfxdsd.core.tasks; | ||
|
||
import com.selfxdsd.api.Project; | ||
import com.selfxdsd.api.Resignation; | ||
import com.selfxdsd.api.Resignations; | ||
import com.selfxdsd.api.Task; | ||
import com.selfxdsd.api.storage.Storage; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Unit tests for {@link TaskResignations}. | ||
* @author Mihai Andronache (amihaiemil@gmail.com) | ||
* @version $Id$ | ||
* @since 0.0.21 | ||
*/ | ||
public final class TaskResignationsTestCase { | ||
|
||
/** | ||
* TaskResignations can be iterated. | ||
*/ | ||
@Test | ||
public void canBeIterated() { | ||
final Resignations resignations = new TaskResignations( | ||
Mockito.mock(Task.class), | ||
() -> { | ||
final List<Resignation> resigs = new ArrayList<>(); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
return resigs.stream(); | ||
}, | ||
Mockito.mock(Storage.class) | ||
); | ||
MatcherAssert.assertThat( | ||
resignations, | ||
Matchers.iterableWithSize(3) | ||
); | ||
} | ||
|
||
/** | ||
* TaskResignations.ofTask returns self if the specified Task | ||
* is the same. | ||
*/ | ||
@Test | ||
public void ofTaskReturnsSelf() { | ||
final Task task = Mockito.mock(Task.class); | ||
final Resignations resignations = new TaskResignations( | ||
task, | ||
() -> { | ||
final List<Resignation> resigs = new ArrayList<>(); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
return resigs.stream(); | ||
}, | ||
Mockito.mock(Storage.class) | ||
); | ||
MatcherAssert.assertThat( | ||
resignations.ofTask(task), | ||
Matchers.is(resignations) | ||
); | ||
} | ||
|
||
/** | ||
* TaskResignations.ofTask(...) throws ISE if the specified | ||
* Task is a different one. | ||
*/ | ||
@Test(expected = IllegalStateException.class) | ||
public void ofTaskComplainsOnDifferentTask() { | ||
final Task task = Mockito.mock(Task.class); | ||
Mockito.when(task.project()).thenReturn(Mockito.mock(Project.class)); | ||
final Task other = Mockito.mock(Task.class); | ||
final Resignations resignations = new TaskResignations( | ||
task, | ||
() -> { | ||
final List<Resignation> resigs = new ArrayList<>(); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
resigs.add(Mockito.mock(Resignation.class)); | ||
return resigs.stream(); | ||
}, | ||
Mockito.mock(Storage.class) | ||
); | ||
MatcherAssert.assertThat( | ||
resignations.ofTask(other), | ||
Matchers.nullValue() | ||
); | ||
} | ||
} |