package com.maxdemarzi.tags;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.test.server.HTTP;

import java.util.ArrayList;
import java.util.HashMap;

public class GetTagTest {
    @Rule
    public Neo4jRule neo4j = new Neo4jRule()
            .withFixture(FIXTURE)
            .withExtension("/v1", Tags.class);

    @Test
    public void shouldGetTag() {
        HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString());

        HTTP.Response response = HTTP.GET(neo4j.httpURI().resolve("/v1/tags/neo4j").toString());
        ArrayList<HashMap> actual  = response.content();
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void shouldGetTagLimited() {
        HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString());

        HTTP.Response response = HTTP.GET(neo4j.httpURI().resolve("/v1/tags/neo4j?limit=1").toString());
        ArrayList<HashMap> actual  = response.content();
        Assert.assertTrue(actual.size() == 1);
        Assert.assertEquals(expected.get(0), actual.get(0));
    }

    @Test
    public void shouldGetTagSince() {
        HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString());

        HTTP.Response response = HTTP.GET(neo4j.httpURI().resolve("/v1/tags/neo4j?since=1490208700").toString());
        ArrayList<HashMap> actual  = response.content();
        Assert.assertTrue(actual.size() == 1);
        Assert.assertEquals(expected.get(1), actual.get(0));
    }

    private static final String FIXTURE =
            "CREATE (max:User {username:'maxdemarzi', " +
                    "email: 'max@neo4j.com', " +
                    "name: 'Max De Marzi'," +
                    "password: 'swordfish'})" +
                    "CREATE (jexp:User {username:'jexp', " +
                    "email: 'michael@neo4j.com', " +
                    "name: 'Michael Hunger'," +
                    "password: 'tunafish'," +
                    "hash: '0bd90aeb51d5982062f4f303a62df935'})" +
                    "CREATE (laeg:User {username:'laexample', " +
                    "email: 'luke@neo4j.com', " +
                    "name: 'Luke Gannon'," +
                    "password: 'cuddlefish'," +
                    "hash: '0bd90aeb51d5982062f4f303a62df935'})" +
                    "CREATE (post1:Post {status:'Hello World! #neo4j', " +
                    "time: 1490140299})" +
                    "CREATE (post2:Post {status:'How are you! #neo4j', " +
                    "time: 1490208700})" +
                    "CREATE (neo4j:Tag {name:'neo4j', time: 1490054400})" +
                    "CREATE (jexp)-[:POSTED_ON_2017_03_21 {time: 1490140299}]->(post1)" +
                    "CREATE (laeg)-[:POSTED_ON_2017_03_22 {time: 1490208700}]->(post2)" +
                    "CREATE (laeg)-[:REPOSTED_ON_2017_03_22 {time: 1490208800}]->(post1)" +
                    "CREATE (max)-[:LIKES {time: 1490209300 }]->(post1)" +
                    "CREATE (max)-[:LIKES {time: 1490209400 }]->(post2)" +
                    "CREATE (post1)-[:TAGGED_ON_2017_03_21 {time: 1490140299 }]->(neo4j)" +
                    "CREATE (post2)-[:TAGGED_ON_2017_03_21 {time: 1490208700 }]->(neo4j)" ;

    private static final ArrayList<HashMap<String, Object>> expected = new ArrayList<HashMap<String, Object>>() {{
        add(new HashMap<String, Object>() {{
            put("username", "laexample");
            put("name", "Luke Gannon");
            put("hash", "0bd90aeb51d5982062f4f303a62df935");
            put("status", "How are you! #neo4j");
            put("time", 1490208700);
            put("likes", 1);
            put("reposts", 0);
        }});
        add(new HashMap<String, Object>() {{
            put("username", "jexp");
            put("name", "Michael Hunger");
            put("hash", "0bd90aeb51d5982062f4f303a62df935");
            put("status", "Hello World! #neo4j");
            put("time", 1490140299);
            put("likes", 1);
            put("reposts", 1);
        }});
    }};
}

@@ -227,16 +227,60 @@
 shMap;%0A%0A
+import static com.maxdemarzi.Properties.*;%0A%0A
 public c
@@ -1573,24 +1573,651 @@
 0));%0A    %7D%0A%0A
+    @Test%0A    public void shouldNotGetTagNotFound() %7B%0A        HTTP.POST(neo4j.httpURI().resolve(%22/v1/schema/create%22).toString());%0A%0A        HTTP.Response response = HTTP.GET(neo4j.httpURI().resolve(%22/v1/tags/notneo4j%22).toString());%0A        HashMap actual  = response.content();%0A        Assert.assertEquals(400, response.status());%0A        Assert.assertEquals(%22Tag Not Found.%22, actual.get(%22error%22));%0A        Assert.assertFalse(actual.containsKey(USERNAME));%0A        Assert.assertFalse(actual.containsKey(EMAIL));%0A        Assert.assertFalse(actual.containsKey(NAME));%0A        Assert.assertFalse(actual.containsKey(STATUS));%0A    %7D%0A
     private 

add tag not found test