From c53d6c33289e6c4916ecc19adab9ae3d6b0dbecd Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Sat, 29 Aug 2020 17:37:30 +0300 Subject: [PATCH 1/2] #496 TaskResignations implemented and tested --- .../selfxdsd/core/tasks/TaskResignations.java | 91 ++++++++++++++ .../core/tasks/TaskResignationsTestCase.java | 117 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 self-core-impl/src/main/java/com/selfxdsd/core/tasks/TaskResignations.java create mode 100644 self-core-impl/src/test/java/com/selfxdsd/core/tasks/TaskResignationsTestCase.java diff --git a/self-core-impl/src/main/java/com/selfxdsd/core/tasks/TaskResignations.java b/self-core-impl/src/main/java/com/selfxdsd/core/tasks/TaskResignations.java new file mode 100644 index 00000000..51596574 --- /dev/null +++ b/self-core-impl/src/main/java/com/selfxdsd/core/tasks/TaskResignations.java @@ -0,0 +1,91 @@ +/** + * Copyright (c) 2020, Self XDSD Contributors + * All rights reserved. + *

+ * 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. + *

+ * 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> 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> 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 iterator() { + return this.resignations.get().iterator(); + } +} diff --git a/self-core-impl/src/test/java/com/selfxdsd/core/tasks/TaskResignationsTestCase.java b/self-core-impl/src/test/java/com/selfxdsd/core/tasks/TaskResignationsTestCase.java new file mode 100644 index 00000000..b96031a1 --- /dev/null +++ b/self-core-impl/src/test/java/com/selfxdsd/core/tasks/TaskResignationsTestCase.java @@ -0,0 +1,117 @@ +/** + * Copyright (c) 2020, Self XDSD Contributors + * All rights reserved. + *

+ * 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. + *

+ * 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 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 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 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() + ); + } +} From 8e185426ce882e7986b9a923ebcc015663556ec6 Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Sat, 29 Aug 2020 17:50:16 +0300 Subject: [PATCH 2/2] #496 InMemoryResignations.ofTask(Task) implemented --- .../selfxdsd/core/mock/InMemoryResignations.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/self-core-impl/src/test/java/com/selfxdsd/core/mock/InMemoryResignations.java b/self-core-impl/src/test/java/com/selfxdsd/core/mock/InMemoryResignations.java index b8df3dcb..0170907f 100644 --- a/self-core-impl/src/test/java/com/selfxdsd/core/mock/InMemoryResignations.java +++ b/self-core-impl/src/test/java/com/selfxdsd/core/mock/InMemoryResignations.java @@ -26,20 +26,20 @@ import com.selfxdsd.api.Resignations; import com.selfxdsd.api.Task; import com.selfxdsd.api.storage.Storage; +import com.selfxdsd.core.tasks.TaskResignations; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Objects; +import java.util.function.Supplier; +import java.util.stream.Stream; /** * In-memory Resignations. * @author Mihai Andronache (amihaiemil@gmail.com) * @version $Id$ * @since 0.0.21 - * @todo #462:30min Implement and write tests for a Task's resignations. - * It will be a class named TaskResignations implementing interface - * Resignations. */ public final class InMemoryResignations implements Resignations { @@ -65,7 +65,15 @@ public InMemoryResignations(final Storage storage) { @Override public Resignations ofTask(final Task task) { - return null; + final Supplier> ofTask = () -> this.resignations + .values() + .stream() + .filter(r -> r.task().equals(task)); + return new TaskResignations( + task, + ofTask, + this.storage + ); } @Override