package com.asana;

import com.asana.models.Task;
import com.google.common.io.Resources;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class ResourcesTest extends AsanaTest {
    @Test
    public void testAttachmentsFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/attachments/1").code(200).content("{ \"data\": { \"name\": \"name\" }}");
        assertEquals(client.attachments.findById("1").execute().name, "name");
    }

    @Test
    public void testProjectsFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/projects/1").code(200).content("{ \"data\": { \"id\": \"1\" }}");
        assertEquals(client.projects.findById("1").execute().id, "1");
    }

    @Test
    public void testStoriesFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/stories/1").code(200).content("{ \"data\": { \"id\": \"1\" }}");
        assertEquals(client.stories.findById("1").execute().id, "1");
    }

    @Test
    public void testTagsFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/tags/1").code(200).content("{ \"data\": { \"id\": \"1\" }}");
        assertEquals(client.tags.findById("1").execute().id, "1");
    }

    @Test
    public void testTasksFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/tasks/1").code(200).content("{ \"data\": { \"id\": \"1\" }}");
        assertEquals(client.tasks.findById("1").execute().id, "1");
    }

    @Test
    public void testTeamsFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/teams/1").code(200).content("{ \"data\": { \"id\": \"1\" }}");
        assertEquals(client.teams.findById("1").execute().id, "1");
    }

    @Test
    public void testUsersFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/users/1").code(200).content("{ \"data\": { \"id\": \"1\" }}");
        assertEquals(client.users.findById("1").execute().id, "1");
    }

    @Test
    public void testWorkspacesFindById() throws IOException {
        dispatcher.registerResponse("GET", "http://app/workspaces/1").code(200).content("{ \"data\": { \"id\": \"1\" }}");
        assertEquals(client.workspaces.findById("1").execute().id, "1");
    }

    @Test
    public void testFindTaskWithDueOn() throws IOException {
        String taskContent = IOUtils.toString(new FileInputStream(Resources.getResource("taskWithDueAt.json").getFile()));
        dispatcher.registerResponse("GET", "http://app/tasks/1").code(200).content(taskContent);
        Task task = client.tasks.findById("1").execute();
        assertEquals("task.dueOn", "2015-10-05", task.dueOn.toString());
    }
}

@@ -43,16 +43,60 @@
 s.Task;%0A
+import com.google.api.client.util.DateTime;%0A
 import c
@@ -247,16 +247,66 @@
 ception;
+%0Aimport java.util.Calendar;%0Aimport java.util.Date;
 %0A%0Aimport
@@ -2557,20 +2557,21 @@
 oid test
-Find
+Parse
 TaskWith
@@ -2571,20 +2571,25 @@
 askWithD
-ueOn
+ateFields
 () throw
@@ -2882,16 +2882,17 @@
 cute();%0A
+%0A
         
@@ -2952,20 +2952,202 @@
 oString());%0A
+        assertEquals(%22task.dueAt%22, %222015-10-05T16:30:00.000Z%22, task.dueAt.toString());%0A        assertEquals(%22task.createdAt%22, %222015-09-11T22:51:12.643Z%22, task.createdAt.toString());%0A
     %7D%0A%7D%0A

test more date fields