/**
 * Copyright (c) 2009-2016, rultor.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met: 1) Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the following
 * disclaimer. 2) Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution. 3) Neither the name of the rultor.com nor
 * the names of its contributors may be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * 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.rultor.agents.github;

import com.jcabi.github.Issue;
import com.jcabi.github.Repo;
import com.jcabi.github.mock.MkGithub;
import com.jcabi.matchers.XhtmlMatchers;
import com.rultor.spi.Agent;
import com.rultor.spi.Talk;
import java.io.IOException;
import java.util.ResourceBundle;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.xembly.Directives;

/**
 * Tests for ${@link Reports}.
 *
 * @author Yegor Bugayenko (yegor@teamed.io)
 * @version $Id$
 * @since 1.3
 */
public final class ReportsTest {
    /**
     * Message bundle.
     */
    private static final ResourceBundle PHRASES = ResourceBundle.getBundle(
        "phrases"
    );

    /**
     * Xpath to check that talk was executed correctly.
     */
    private static final String XPATH = "/talk[not(request)]";

    /**
     * Reports can report a result of a request.
     * @throws Exception In case of error
     */
    @Test
    public void reportsRequestResult() throws Exception {
        final Repo repo = new MkGithub().randomRepo();
        final Talk talk = ReportsTest.example(
            repo, repo.issues().create("", "")
        );
        final Agent agent = new Reports(repo.github());
        agent.execute(talk);
        MatcherAssert.assertThat(
            talk.read(),
            XhtmlMatchers.hasXPath(XPATH)
        );
    }

    /**
     * Reports can report a result of a request, when stop command fails.
     * @throws Exception In case of error
     */
    @Test
    public void reportsRequestResultWhenStopFails() throws Exception {
        final Repo repo = new MkGithub().randomRepo();
        final Talk talk = ReportsTest.example(
            repo, repo.issues().create("Bug", "stop it please")
        );
        final Agent agent = new Reports(repo.github());
        agent.execute(talk);
        MatcherAssert.assertThat(
            talk.read(),
            XhtmlMatchers.hasXPath(XPATH)
        );
        MatcherAssert.assertThat(
            "Comment contains warning about stop request",
            repo.issues().get(1).comments().get(1).toString().contains(
                ReportsTest.PHRASES.getString("Reports.stop-fails")
            )
        );
    }

    /**
     * Create Talk, that will be used to test Reports.
     * @param repo Repository
     * @param issue Issue
     * @return Example of Talk
     * @throws IOException In case of error
     */
    private static Talk example(final Repo repo, final Issue issue)
        throws IOException {
        final Talk result = new Talk.InFile();
        result.modify(
            new Directives().xpath("/talk").add("wire")
                .add("href").set("http://test").up()
                .add("github-repo").set(repo.coordinates().toString()).up()
                .add("github-issue").set(Integer.toString(issue.number())).up()
                .up()
                .add("request").attr("id", "1")
                .add("msec").set("1234567").up()
                .add("success").set("true").up()
                .add("type").set("something").up()
                .add("args")
        );
        return result;
    }
}

@@ -1692,16 +1692,45 @@
 Github;%0A
+import com.jcabi.log.Logger;%0A
 import c
@@ -2261,16 +2261,121 @@
     );%0A%0A
+    /**%0A     * Duration of command execution.%0A     */%0A    private static final long DURATION = 1234567;%0A%0A
     /**%0A
@@ -3246,32 +3246,114 @@
 ows Exception %7B%0A
+        final String user = %22john%22;%0A        final String stop = %22stop it please%22;%0A
         final Re
@@ -3367,32 +3367,36 @@
  = new MkGithub(
+user
 ).randomRepo();%0A
@@ -3488,32 +3488,20 @@
 (%22Bug%22, 
-%22
 stop
- it please%22
 )%0A      
@@ -3850,95 +3850,503 @@
 (1).
-toString().contains(%0A                ReportsTest.PHRASES.getString(%22Reports.stop-fails%22
+json().getString(%0A                %22body%22%0A            ).equals(%0A                String.format(%0A                    %22%3E %25s%5Cn%5Cn@%25s %25s %25s%22,%0A                    stop,%0A                    user,%0A                    ReportsTest.PHRASES.getString(%22Reports.stop-fails%22),%0A                    Logger.format(%0A                        ReportsTest.PHRASES.getString(%22Reports.success%22),%0A                        %22http://www.rultor.com/t/1-1%22,%0A                        DURATION%0A                    )%0A                
 )%0A  
@@ -5116,17 +5116,16 @@
 set(
-%221234567%22
+DURATION
 ).up

full equality