package jp.gr.java_conf.hhayakawa_jp.beehive_client;

import static org.junit.Assert.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import jp.gr.java_conf.hhayakawa_jp.beehive_client.exception.Beehive4jException;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.exception.BeehiveApiFaultException;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.BeeId;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.ChangeStatus;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.MeetingCreator;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.MeetingParticipantUpdater;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.MeetingParticipantUpdaterOperation;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.MeetingUpdater;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.OccurrenceParticipantStatus;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.OccurrenceStatus;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.OccurrenceType;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.Priority;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.TimedTrigger;
import jp.gr.java_conf.hhayakawa_jp.beehive_client.model.Transparency;

public class InvtCreateInvokerTest {

    private static final String calendar_id =
            "334B:3BF0:clnd:38893C00F42F38A1E0404498C8A6612B000B1A7E0450";

    private BeehiveContext context = null;

    @Before
    public void setUp() throws Exception {
        String host = System.getProperty("beehive4j.test.host");
        String user = System.getProperty("beehive4j.test.user");
        String password = System.getProperty("beehive4j.test.password");
        try {
            context = BeehiveContext.getBeehiveContext(
                    new URL(host), user, password);
        } catch (MalformedURLException | BeehiveApiFaultException e) {
            fail(e.getMessage());
        }
    }

    @Test
    public void test() {
        // BeeId
        BeeId calendar = new BeeId(calendar_id, null);

        // MeetingUpdater
        ZonedDateTime start = ZonedDateTime.of(
                2016, 6, 5, 12, 0, 0, 0, ZoneId.of("Asia/Tokyo"));
        ZonedDateTime end = ZonedDateTime.of(
                2016, 6, 5, 13, 0, 0, 0, ZoneId.of("Asia/Tokyo"));
        String name = "Test String of name.";
        ChangeStatus changeStatus = null;
        ZonedDateTime userCreatedOn = null;
        ZonedDateTime userModifiedOn = null;
        boolean includeOnlineConference = false;
        OccurrenceParticipantStatus inviteeParticipantStatus =
                OccurrenceParticipantStatus.ACCEPTED;
        TimedTrigger inviteePrimaryClientReminderTrigger = null;
        Priority inviteePriority = Priority.MEDIUM;
        Transparency inviteeTransparency = Transparency.TRANSPARENT;
        String locationName = "JP-OAC-CONF-17006_17M1";
        List<MeetingParticipantUpdater> participantUpdaters = 
                new ArrayList<MeetingParticipantUpdater>(1);
        participantUpdaters.add(new MeetingParticipantUpdater(
                "mailto:JP-OAC-CONF-17006_17M1@oracle.com", null,
                MeetingParticipantUpdaterOperation.ADD,
                new BeeId("334B:3BF0:bkrs:38893C00F42F38A1E0404498C8A6612B0001DDD86644", null)));
        OccurrenceStatus status = OccurrenceStatus.TENTATIVE;
        String textDescription = "Test String of testDescription.";
        String xhtmlFragmentDescription = null;
        MeetingUpdater meetingUpdater = new MeetingUpdater(
                name, changeStatus, userCreatedOn, userModifiedOn,
                end,
                includeOnlineConference, inviteeParticipantStatus,
                inviteePrimaryClientReminderTrigger, inviteePriority,
                inviteeTransparency, locationName, participantUpdaters,
                start,
                status, textDescription, xhtmlFragmentDescription);
        
        // OccurenceType
        OccurrenceType type = OccurrenceType.MEETING;

        MeetingCreator meetingCreater =
                new MeetingCreator(calendar, meetingUpdater, type);
        InvtCreateInvoker invoker =
                context.getInvoker(BeehiveApiDefinitions.TYPEDEF_INVT_CREATE);
        invoker.setRequestPayload(meetingCreater);
        try {
            ResponseEntity<BeehiveResponse> response = invoker.invoke();
            assertEquals("Response code is expected to be 201 (Created)",
                    HttpStatus.CREATED, response.getStatusCode());
            assertEquals("BeeType is expected to be \"meeting\"",
                    "meeting", response.getBody().getBeeType());
        } catch (Beehive4jException e) {
            System.out.println(e.getMessage());
            fail(e.getMessage());
        }
    }

}

@@ -247,24 +247,48 @@
 util.List;%0A%0A
+import org.junit.After;%0A
 import org.j
@@ -416,16 +416,65 @@
 ntity;%0A%0A
+import com.fasterxml.jackson.databind.JsonNode;%0A%0A
 import j
@@ -1739,16 +1739,58 @@
  null;%0A%0A
+    private String invitation_id = null;%0A%0A
     @Bef
@@ -4981,24 +4981,595 @@
 BeeType());%0A
+            JsonNode json = response.getBody().getJson();%0A            invitation_id = json.get(%22collabId%22).get(%22id%22).asText();%0A        %7D catch (Beehive4jException e) %7B%0A            System.out.println(e.getMessage());%0A            fail(e.getMessage());%0A        %7D%0A    %7D%0A%0A    @After%0A    public void tearDown() throws Exception %7B%0A        InvtDeleteInvoker invoker =%0A                context.getInvoker(BeehiveApiDefinitions.TYPEDEF_INVT_DELETE);%0A        invoker.setPathValue(invitation_id);%0A        try %7B%0A            ResponseEntity%3CBeehiveResponse%3E response = invoker.invoke();%0A
         %7D ca

Improve a junit test.