Skip to content

Commit

Permalink
Merge pull request #497 from amihaiemil/496
Browse files Browse the repository at this point in the history
#496 TaskResignations implemented and tested
  • Loading branch information
amihaiemil committed Aug 29, 2020
2 parents 83f47d0 + 8e18542 commit 4686ad8
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 4 deletions.
@@ -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();
}
}
Expand Up @@ -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 {

Expand All @@ -65,7 +65,15 @@ public InMemoryResignations(final Storage storage) {

@Override
public Resignations ofTask(final Task task) {
return null;
final Supplier<Stream<Resignation>> ofTask = () -> this.resignations
.values()
.stream()
.filter(r -> r.task().equals(task));
return new TaskResignations(
task,
ofTask,
this.storage
);
}

@Override
Expand Down
@@ -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()
);
}
}

0 comments on commit 4686ad8

Please sign in to comment.