Skip to content

Commit

Permalink
For yegor256#76: Exec register error tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulodamaso committed Oct 27, 2018
1 parent 220aed2 commit 61291e3
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/main/java/io/wring/agents/Exec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,14 +53,20 @@
* One execution.
*
* @author Yegor Bugayenko ([email protected])
* @author Paulo Lobo ([email protected])
* @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 {

/**
Expand All @@ -77,16 +84,34 @@ final class Exec {
*/
private final transient Pipe pipe;

/**
* Errors.
*/
private final Errors errors;

/**
* Ctor.
* @param agt Agent
* @param evt Events
* @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;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/wring/model/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Directive> asXembly() throws IOException {
throw new UnsupportedOperationException("not implemented");
}

@Override
public void delete() throws IOException {
throw new UnsupportedOperationException("delete() not implemented");
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/io/wring/model/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
package io.wring.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

/**
* Errors.
Expand Down Expand Up @@ -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<Error> errors;

/**
* Constructor.
*/
public Simple() {
this.errors = new ArrayList<>(0);
}

@Override
public Iterable<Error> iterate() throws IOException {
return this.errors;
}

@Override
public void register(final String title, final String description) {
this.errors.add(new Error.Simple(title, description));
}
}
}
27 changes: 27 additions & 0 deletions src/test/java/io/wring/agents/ExecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@
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;

/**
* Test case for {@link Exec}.
* @author Yegor Bugayenko ([email protected])
* @author Paulo Lobo ([email protected])
* @version $Id$
* @since 0.13
*/
Expand Down Expand Up @@ -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("<error>")).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)
)
);
}

}

0 comments on commit 61291e3

Please sign in to comment.