package com.google.sps.servlets;
 
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import com.google.maps.errors.ApiException;
import com.google.sps.data.DialogFlowClient;
import com.google.sps.data.Location;
import com.google.sps.data.Output;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.*;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import javax.servlet.http.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
 
@RunWith(MockitoJUnitRunner.Silent.class)
public class DateTest {
  Location losAngeles = null;
  Location london = null;

  @Before
  public void init() {
    try {
      losAngeles = Location.create("Los Angeles");
      london = Location.create("London");
    } catch (IllegalStateException
        | IOException
        | ApiException
        | InterruptedException
        | ArrayIndexOutOfBoundsException e) {
      Assert.fail("Should not have thrown any exception");
    }
  }

  @Test
  public void getDateRequest() throws Exception {
    TestHelper tester =
        new TestHelper(
            // User input text
            "What day is it in Los Angeles?",
            // Parameter JSON string (copy paste from Dialogflow)
            "{\"location\": {\"admin-area\": \"\","
                + "\"city\": \"Los Angeles\","
                + "\"street-address\": \"\","
                + "\"island\": \"\","
                + "\"zip-code\": \"\","
                + "\"country\": \"\","
                + "\"shortcut\": \"\","
                + "\"business-name\": \"\","
                + "\"subadmin-area\": \"\"}}",
            "date.get",
            true);
 
    Output output = tester.getOutput();
    ZonedDateTime currentTime = ZonedDateTime.now(ZoneId.of(losAngeles.getTimeZoneID()));
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM d uuuu");
    String dateString = currentTime.format(formatter);
 
    String actual = output.getFulfillmentText();
    String expected = "It is " + dateString + " in Los Angeles.";
    assertEquals(actual, expected);
  }
 
  @Test
  public void getDateContextRequest() throws Exception {
    TestHelper tester =
        new TestHelper(
            "What about in london?",
            "{\"location\": {\"admin-area\": \"\","
                + "\"city\": \"London\","
                + "\"street-address\": \"\","
                + "\"island\": \"\","
                + "\"zip-code\": \"\","
                + "\"country\": \"\","
                + "\"shortcut\": \"\","
                + "\"business-name\": \"\","
                + "\"subadmin-area\": \"\"}}",
            "date.context:date",
            true);
 
    Output output = tester.getOutput();
    ZonedDateTime currentTime = ZonedDateTime.now(ZoneId.of(london.getTimeZoneID()));
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM d uuuu");
    String dateString = currentTime.format(formatter);
 
    String actual = output.getFulfillmentText();
    String expected = "It is " + dateString + " in London.";
    assertEquals(expected, actual);
  }

  @Test
  public void checkNotAllParamsPresent() throws Exception {
    TestHelper tester = new TestHelper("What day is it?", "{\"location\": \"\"}", "date.get", false);
 
    Output output = tester.getOutput();
    String actual = output.getFulfillmentText();
    String expected = "I'm sorry, I didn't catch that. Can you repeat that?";
    assertEquals(expected, actual);
  }
 
  @Test
  public void checkMissingLocation() throws Exception {
    TestHelper tester = new TestHelper("What day is it?");
 
    Output output = tester.getOutput();
    String actual = output.getFulfillmentText();
    String expected = "Where?";
    assertEquals(expected, actual);
  }
 
  @Test
  public void checkDateContextPast() throws Exception {
    TestHelper tester =
        new TestHelper(
            "what day was august 2, 1999",
            "{\"date-time\": \"1999-08-02T12:00:00-07:00\"," + "\"unit-time\": \"day\"}",
            "date.day-of-date",
            true);
 
    Output output = tester.getOutput();
    String actual = output.getFulfillmentText();
    String expected = "August 2, 1999 was a Monday.";
    assertEquals(expected, actual);
  }
 
  @Test
  public void checkDateContextFuture() throws Exception
   {
    TestHelper tester =
        new TestHelper(
            "what day is july 4, 3000",
            "{\"date-time\": \"3000-07-04T12:00:00-07:00\"," + "\"unit-time\": \"day\"}",
            "date.day-of-date",
            true);
 
    Output output = tester.getOutput();
 
    String actual = output.getFulfillmentText();
    String expected = "July 4, 3000 is a Friday.";
    assertEquals(expected, actual);
  }
}
 

@@ -26,17 +26,16 @@
 rvlets;%0A
- 
 %0Aimport 
@@ -147,53 +147,8 @@
 on;%0A
-import com.google.sps.data.DialogFlowClient;%0A
 impo
@@ -219,67 +219,8 @@
 ut;%0A
-import com.google.protobuf.InvalidProtocolBufferException;%0A
 impo
@@ -254,25 +254,18 @@
 ime.Zone
+I
 d
-DateTime
 ;%0Aimport
@@ -271,34 +271,41 @@
 t java.time.Zone
-I
 d
+DateTime
 ;%0Aimport java.ti
@@ -492,65 +492,8 @@
 th;%0A
-import org.mockito.InjectMocks;%0Aimport org.mockito.Mock;%0A
 impo
@@ -533,17 +533,16 @@
 Runner;%0A
- 
 %0A@RunWit
@@ -1698,33 +1698,32 @@
          true);%0A
- 
 %0A    Output outp
@@ -1968,33 +1968,32 @@
 mat(formatter);%0A
- 
 %0A    String actu
@@ -2128,25 +2128,24 @@
 ected);%0A  %7D%0A
- 
 %0A  @Test%0A  p
@@ -2716,33 +2716,32 @@
          true);%0A
- 
 %0A    Output outp
@@ -2990,17 +2990,16 @@
 atter);%0A
- 
 %0A    Str
@@ -3225,32 +3225,40 @@
 tHelper tester =
+%0A       
  new TestHelper(
@@ -3316,25 +3316,24 @@
 t%22, false);%0A
- 
 %0A    Output 
@@ -3520,33 +3520,32 @@
 d, actual);%0A  %7D%0A
- 
 %0A  @Test%0A  publi
@@ -3652,17 +3652,16 @@
  it?%22);%0A
- 
 %0A    Out
@@ -3806,33 +3806,32 @@
 d, actual);%0A  %7D%0A
- 
 %0A  @Test%0A  publi
@@ -4103,33 +4103,32 @@
          true);%0A
- 
 %0A    Output outp
@@ -4295,17 +4295,16 @@
 l);%0A  %7D%0A
- 
 %0A  @Test
@@ -4359,19 +4359,16 @@
 xception
-%0A  
  %7B%0A    T
@@ -4591,17 +4591,16 @@
  true);%0A
- 
 %0A    Out
@@ -4632,17 +4632,16 @@
 tput();%0A
- 
 %0A    Str
@@ -4779,6 +4779,4 @@
 %7D%0A%7D%0A
- %0A

Add Date Testing using Dialogflow Mock