From 61291e3c15a3a23b598a401d96750b6d7c913556 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Fri, 26 Oct 2018 23:56:55 -0300 Subject: [PATCH 1/4] For #76: Exec register error tests. --- src/main/java/io/wring/agents/Exec.java | 29 +++++++++++++++-- src/main/java/io/wring/model/Error.java | 36 +++++++++++++++++++++ src/main/java/io/wring/model/Errors.java | 30 +++++++++++++++++ src/test/java/io/wring/agents/ExecTest.java | 27 ++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/wring/agents/Exec.java b/src/main/java/io/wring/agents/Exec.java index 8e9babc..a54c3b6 100644 --- a/src/main/java/io/wring/agents/Exec.java +++ b/src/main/java/io/wring/agents/Exec.java @@ -33,6 +33,7 @@ import com.jcabi.log.Logger; import com.jcabi.manifests.Manifests; import io.sentry.Sentry; +import io.wring.model.Errors; import io.wring.model.Events; import io.wring.model.Pipe; import java.io.ByteArrayOutputStream; @@ -52,14 +53,20 @@ * One execution. * * @author Yegor Bugayenko (yegor256@gmail.com) + * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.13 - * @todo #72:30min Errors occurred during execution must be saved and later + * @todo #76:30min Errors occurred during execution must be saved and later * listed to user in a page. User should be able to mark errors as `read` * in this page. Wire Error and Errors implementations in Exec flow so - * these errors are properly saved once they happen. Cover this with tests. + * these errors are properly saved once they happen and remove + * Exec(final Agent agt, final Events evt, final Pipe ppe) constructor so + * errors is injected in Exec every time. Then remove Singularfield and + * UnusedPrivateField check ignores and uncomment tests for error + * registering in ExecTest.registerErrors. * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ +@SuppressWarnings({"PMD.SingularField", "PMD.UnusedPrivateField"}) final class Exec { /** @@ -77,6 +84,11 @@ final class Exec { */ private final transient Pipe pipe; + /** + * Errors. + */ + private final Errors errors; + /** * Ctor. * @param agt Agent @@ -84,9 +96,22 @@ final class Exec { * @param ppe Pipe */ Exec(final Agent agt, final Events evt, final Pipe ppe) { + this(agt, evt, ppe, new Errors.Simple()); + } + + /** + * Ctor. + * @param agt Agent + * @param evt Events + * @param ppe Pipe + * @param err Errors + * @checkstyle ParameterNumberCheck (2 lines) + */ + Exec(final Agent agt, final Events evt, final Pipe ppe, final Errors err) { this.agent = agt; this.events = evt; this.pipe = ppe; + this.errors = err; } /** diff --git a/src/main/java/io/wring/model/Error.java b/src/main/java/io/wring/model/Error.java index 76dfbe9..574a6ee 100644 --- a/src/main/java/io/wring/model/Error.java +++ b/src/main/java/io/wring/model/Error.java @@ -53,4 +53,40 @@ public interface Error { * @throws IOException If fails */ void delete() throws IOException; + + /** + * Simple Error implementation. + */ + final class Simple implements Error { + + /** + * Error title. + */ + private final String title; + + /** + * Error description. + */ + private final String description; + + /** + * Constructor. + * @param title Title + * @param description Description + */ + public Simple(final String title, final String description) { + this.title = title; + this.description = description; + } + + @Override + public Iterable asXembly() throws IOException { + throw new UnsupportedOperationException("not implemented"); + } + + @Override + public void delete() throws IOException { + throw new UnsupportedOperationException("delete() not implemented"); + } + } } diff --git a/src/main/java/io/wring/model/Errors.java b/src/main/java/io/wring/model/Errors.java index a13ade4..2112cd3 100644 --- a/src/main/java/io/wring/model/Errors.java +++ b/src/main/java/io/wring/model/Errors.java @@ -30,6 +30,8 @@ package io.wring.model; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; /** * Errors. @@ -57,4 +59,32 @@ public interface Errors { * @throws IOException If fails */ void register(String title, String description); + + /** + * Simple implementation of Error repo. + */ + final class Simple implements Errors { + + /** + * Errors. + */ + private final Collection errors; + + /** + * Constructor. + */ + public Simple() { + this.errors = new ArrayList<>(0); + } + + @Override + public Iterable iterate() throws IOException { + return this.errors; + } + + @Override + public void register(final String title, final String description) { + this.errors.add(new Error.Simple(title, description)); + } + } } diff --git a/src/test/java/io/wring/agents/ExecTest.java b/src/test/java/io/wring/agents/ExecTest.java index 1f44aca..08af519 100644 --- a/src/test/java/io/wring/agents/ExecTest.java +++ b/src/test/java/io/wring/agents/ExecTest.java @@ -30,9 +30,14 @@ package io.wring.agents; import io.wring.fake.FkPipe; +import io.wring.model.Errors; import io.wring.model.Events; import java.io.IOException; +import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.hamcrest.collection.IsIterableWithSize; +import org.hamcrest.core.IsEqual; +import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import org.mockito.hamcrest.MockitoHamcrest; @@ -40,6 +45,7 @@ /** * Test case for {@link Exec}. * @author Yegor Bugayenko (yegor256@gmail.com) + * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.13 */ @@ -69,4 +75,25 @@ public void catchesExceptions() throws Exception { ); } + /** + * Exec can register an error. + * @throws Exception If some problem inside + */ + @Test + @Ignore + public void registerErrors() throws Exception { + final Agent agent = Mockito.mock(Agent.class); + final Events events = Mockito.mock(Events.class); + final Errors errors = new Errors.Simple(); + Mockito.doThrow(new IOException("")).when(agent).push(events); + new Exec(agent, events, new FkPipe(), errors).run(); + MatcherAssert.assertThat( + "Could not register error", + errors.iterate(), + new IsIterableWithSize<>( + new IsEqual<>(1) + ) + ); + } + } From 4c46002e752f788e90c687aca67fed862cd24f94 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Sat, 27 Oct 2018 02:01:40 -0300 Subject: [PATCH 2/4] For #76: Todo text correction. --- src/main/java/io/wring/agents/Exec.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/wring/agents/Exec.java b/src/main/java/io/wring/agents/Exec.java index a54c3b6..20c4aba 100644 --- a/src/main/java/io/wring/agents/Exec.java +++ b/src/main/java/io/wring/agents/Exec.java @@ -62,7 +62,7 @@ * these errors are properly saved once they happen and remove * Exec(final Agent agt, final Events evt, final Pipe ppe) constructor so * errors is injected in Exec every time. Then remove Singularfield and - * UnusedPrivateField check ignores and uncomment tests for error + * UnusedPrivateField check ignores below and uncomment tests for error * registering in ExecTest.registerErrors. * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ From 4580642db412c22fea1277df1c8771847dd66e09 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Mon, 29 Oct 2018 23:16:25 -0300 Subject: [PATCH 3/4] For #76: Added todo for as xembly and delete implementation. --- src/main/java/io/wring/model/Error.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/io/wring/model/Error.java b/src/main/java/io/wring/model/Error.java index 574a6ee..b8023ae 100644 --- a/src/main/java/io/wring/model/Error.java +++ b/src/main/java/io/wring/model/Error.java @@ -56,6 +56,10 @@ public interface Error { /** * Simple Error implementation. + * @todo #76:30min Implement Error.Simple asXembly and delete method. + * asXembly must return a xembly directive with element 'error' and + * child elements 'title' + * and 'description'. */ final class Simple implements Error { From f2ce7b9afb5d7ace3b7d5ec68bb99b4f2e674d31 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 30 Oct 2018 21:23:34 -0300 Subject: [PATCH 4/4] For #76: Exception removal. --- src/main/java/io/wring/dynamo/DyErrors.java | 3 +-- src/main/java/io/wring/fake/FkErrors.java | 3 +-- src/main/java/io/wring/model/Errors.java | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/wring/dynamo/DyErrors.java b/src/main/java/io/wring/dynamo/DyErrors.java index 83cbce2..6a83a02 100644 --- a/src/main/java/io/wring/dynamo/DyErrors.java +++ b/src/main/java/io/wring/dynamo/DyErrors.java @@ -32,7 +32,6 @@ import com.jcabi.dynamo.Region; import io.wring.model.Error; import io.wring.model.Errors; -import java.io.IOException; /** * Errors stored in Dynamo database. @@ -70,7 +69,7 @@ public DyErrors(final Region reg, final String user) { } @Override - public Iterable iterate() throws IOException { + public Iterable iterate() { throw new UnsupportedOperationException("iterate not implemented"); } diff --git a/src/main/java/io/wring/fake/FkErrors.java b/src/main/java/io/wring/fake/FkErrors.java index 8187fcd..fbc415e 100644 --- a/src/main/java/io/wring/fake/FkErrors.java +++ b/src/main/java/io/wring/fake/FkErrors.java @@ -31,7 +31,6 @@ import io.wring.model.Error; import io.wring.model.Errors; -import java.io.IOException; import java.util.Collection; import org.cactoos.collection.CollectionOf; @@ -65,7 +64,7 @@ public FkErrors() { } @Override - public Iterable iterate() throws IOException { + public Iterable iterate() { return this.errors; } diff --git a/src/main/java/io/wring/model/Errors.java b/src/main/java/io/wring/model/Errors.java index ffc3c74..53b3645 100644 --- a/src/main/java/io/wring/model/Errors.java +++ b/src/main/java/io/wring/model/Errors.java @@ -47,7 +47,7 @@ public interface Errors { * @return Events * @throws IOException If fails */ - Iterable iterate() throws IOException; + Iterable iterate(); /** * Add a new error. @@ -75,7 +75,7 @@ public Simple() { } @Override - public Iterable iterate() throws IOException { + public Iterable iterate() { return this.errors; }