Search in sources :

Example 6 with Events

use of io.wring.model.Events in project wring by yegor256.

the class JsonAgentTest method makesAgent.

/**
 * JsonAgent can make an agent.
 * @throws Exception If some problem inside
 */
@Test
public void makesAgent() throws Exception {
    final Agent agent = new JsonAgent(new FkBase(), "{\"class\":\"io.wring.agents.FkAgent\"}");
    final Events events = Mockito.mock(Events.class);
    agent.push(events);
    Mockito.verify(events).post(Mockito.anyString(), Mockito.anyString());
}
Also used : FkBase(io.wring.fake.FkBase) Events(io.wring.model.Events) Test(org.junit.Test)

Example 7 with Events

use of io.wring.model.Events in project wring by yegor256.

the class Cycle method exec.

@Override
public void exec(final Pipe pipe) throws Exception {
    final XePrint print = new XePrint(pipe.asXembly());
    final Events events = this.base.user(print.text("{/pipe/urn/text()}")).events();
    final String json = print.text("{/pipe/json/text()}");
    new UncheckedFunc<>(new FuncWithFallback<String, JsonObject>(str -> Json.createReader(new ReaderOf(str)).readObject(), new FuncOf<>(error -> events.post(Cycle.class.getCanonicalName(), String.format("Failed to parse JSON:\n%s\n\n%s", json, new TextOf(new BytesOf(error)).asString()))), obj -> {
        if (obj != null) {
            new Exec(new JsonAgent(this.base, obj), new IgnoreEvents(new BoostEvents(events, obj), obj), pipe).run();
        }
        return obj;
    })).apply(json);
}
Also used : JsonObject(javax.json.JsonObject) Events(io.wring.model.Events) FuncOf(org.cactoos.func.FuncOf) UncheckedFunc(org.cactoos.func.UncheckedFunc) Proc(org.cactoos.Proc) Base(io.wring.model.Base) BytesOf(org.cactoos.io.BytesOf) XePrint(io.wring.model.XePrint) Json(javax.json.Json) Pipe(io.wring.model.Pipe) ReaderOf(org.cactoos.io.ReaderOf) TextOf(org.cactoos.text.TextOf) FuncWithFallback(org.cactoos.func.FuncWithFallback) BytesOf(org.cactoos.io.BytesOf) FuncWithFallback(org.cactoos.func.FuncWithFallback) Events(io.wring.model.Events) ReaderOf(org.cactoos.io.ReaderOf) XePrint(io.wring.model.XePrint) TextOf(org.cactoos.text.TextOf)

Example 8 with Events

use of io.wring.model.Events in project wring by yegor256.

the class BoostEventsTest method passesEventsThroughByJsonConfig.

/**
 * IgnoreEvents can filter out events.
 * @throws Exception If some problem inside
 */
@Test
public void passesEventsThroughByJsonConfig() throws Exception {
    final Events events = Mockito.mock(Events.class);
    final Event event = Mockito.mock(Event.class);
    Mockito.doReturn(event).when(events).event(Mockito.anyString());
    new BoostEvents(events, Json.createReader(new InputStreamOf("{}")).readObject()).post("the title", "the body");
    Mockito.verify(event, Mockito.never()).vote(Mockito.anyInt());
}
Also used : Events(io.wring.model.Events) Event(io.wring.model.Event) InputStreamOf(org.cactoos.io.InputStreamOf) Test(org.junit.Test)

Example 9 with Events

use of io.wring.model.Events in project wring by yegor256.

the class BoostEventsTest method boostsEventsByTitle.

/**
 * BoostEvents can boost events by title.
 * @throws Exception If some problem inside
 */
@Test
public void boostsEventsByTitle() throws Exception {
    final Events events = Mockito.mock(Events.class);
    final Event event = Mockito.mock(Event.class);
    Mockito.doReturn(event).when(events).event(Mockito.anyString());
    new BoostEvents(events, "xyz.*").post("xyz", "some body");
    Mockito.verify(event).vote(Tv.FIVE);
}
Also used : Events(io.wring.model.Events) Event(io.wring.model.Event) Test(org.junit.Test)

Example 10 with Events

use of io.wring.model.Events in project wring by yegor256.

the class ExecTest method catchesExceptions.

/**
 * Exec can catch in case of an error.
 * @throws Exception If some problem inside
 */
@Test
public void catchesExceptions() throws Exception {
    final Agent agent = Mockito.mock(Agent.class);
    final Events events = Mockito.mock(Events.class);
    Mockito.doThrow(new IOException("<bug>")).when(agent).push(events);
    new Exec(agent, events, new FkPipe()).run();
    Mockito.verify(events).post(MockitoHamcrest.argThat(Matchers.startsWith("Internal error (java.io.IOException): \"&lt;bug&gt;\"")), MockitoHamcrest.argThat(Matchers.containsString("java.io.IOException: &lt;bug&gt;\n")));
}
Also used : Events(io.wring.model.Events) IOException(java.io.IOException) FkPipe(io.wring.fake.FkPipe) Test(org.junit.Test)

Aggregations

Events (io.wring.model.Events)16 Test (org.junit.Test)15 Event (io.wring.model.Event)5 User (io.wring.model.User)5 InputStreamOf (org.cactoos.io.InputStreamOf)3 Xembler (org.xembly.Xembler)3 FkBase (io.wring.fake.FkBase)1 FkPipe (io.wring.fake.FkPipe)1 Base (io.wring.model.Base)1 Pipe (io.wring.model.Pipe)1 XePrint (io.wring.model.XePrint)1 IOException (java.io.IOException)1 Json (javax.json.Json)1 JsonObject (javax.json.JsonObject)1 Proc (org.cactoos.Proc)1 FuncOf (org.cactoos.func.FuncOf)1 FuncWithFallback (org.cactoos.func.FuncWithFallback)1 UncheckedFunc (org.cactoos.func.UncheckedFunc)1 BytesOf (org.cactoos.io.BytesOf)1 ReaderOf (org.cactoos.io.ReaderOf)1