package com.google.sps.data;

import com.google.maps.errors.ApiException;
import com.google.maps.model.LatLng;
import com.google.sps.utils.LocationUtils;
import java.io.IOException;
import java.util.TimeZone;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for creating valid Location objects */
@RunWith(JUnit4.class)
public final class LocationTest {
  Location location = null;
  Location invalidLocation = null;

  @Before
  public void init() {
    try {
      location = LocationUtils.getLocationObject("Los Angeles");
    } catch (IllegalStateException
        | IOException
        | ApiException
        | InterruptedException
        | ArrayIndexOutOfBoundsException e) {
      System.out.println("Error in creating valid location object.");
    }
  }

  @Test
  public void checkAddressField() {
    String actual = location.getAddress();
    String expected = "Los Angeles";
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void checkFormattedAddressField() {
    String actual = location.getAddressFormatted();
    String expected = "Los Angeles, CA, USA";
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void checkLatitude() {
    Double actual = location.getLat();
    Double expected = 34.05223420;
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void checkLongitude() {
    Double actual = location.getLng();
    Double expected = -118.24368490;
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void checkCoords() {
    LatLng actual = location.getCoords();
    LatLng expected = new LatLng(34.05223420, -118.24368490);
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void checkTimeZoneID() {
    String actual = location.getTimeZoneID();
    String expected = "America/Los_Angeles";
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void checkTimeZoneName() {
    String actual = location.getTimeZoneName();
    String expected = "Pacific Standard Time";
    Assert.assertEquals(expected, actual);
  }

  @Test
  public void checkTimeZone() {
    TimeZone actual = location.getTimeZone();
    TimeZone expected = TimeZone.getTimeZone("America/Los_Angeles");
    Assert.assertEquals(expected, actual);
  }

  /** Invalid location input should not create a location object */
  @Test
  public void checkInvalidLocation() {
    try {
      invalidLocation = LocationUtils.getLocationObject("gibberish location input");
    } catch (IllegalStateException
        | IOException
        | ApiException
        | InterruptedException
        | ArrayIndexOutOfBoundsException e) {
      System.out.println("Correct error in creating invalid location object.");
    }
    Location actual = invalidLocation;
    Location expected = null;
    Assert.assertEquals(expected, actual);
  }
}

@@ -108,51 +108,8 @@
 ng;%0A
-import com.google.sps.utils.LocationUtils;%0A
 impo
@@ -436,43 +436,8 @@
 ull;
-%0A  Location invalidLocation = null;
 %0A%0A  
@@ -502,39 +502,23 @@
 Location
-Utils.getLocationObject
+.create
 (%22Los An
@@ -2263,195 +2263,116 @@
 uld 
-not create a location object */%0A  @Test%0A  public void checkInvalidLocation() %7B%0A    try %7B%0A      invalidLocation = LocationUtils.getLocationObject(%22gibberish location input%22);%0A    %7D catch (
+throw an exception */%0A  @Test(expected = Exception.class)%0A  public void checkInvalidLocation()%0A      throws 
 Ille
@@ -2384,34 +2384,25 @@
 ateException
-%0A        %7C
+,
  IOException
@@ -2397,34 +2397,25 @@
  IOException
-%0A        %7C
+,
  ApiExceptio
@@ -2411,34 +2411,25 @@
 ApiException
-%0A        %7C
+,
  Interrupted
@@ -2433,33 +2433,35 @@
 tedException
-%0A
+,%0A 
         
 %7C ArrayIndex
@@ -2440,33 +2440,32 @@
 ption,%0A         
-%7C
  ArrayIndexOutOf
@@ -2483,73 +2483,31 @@
 tion
- e)
  %7B%0A    
-  System.out.println(%22Correct error in cre
+Loc
 ati
+o
 n
-g
  invalid
  loc
@@ -2498,34 +2498,33 @@
 tion invalid
- l
+L
 ocation 
 object.%22);%0A 
@@ -2515,134 +2515,52 @@
 ion 
-object.%22);%0A    %7D%0A    Location actual = invalidLocation;%0A    Location expected = null;%0A    Assert.assertEquals(expected, actual
+= Location.create(%22gibberish location input%22
 );%0A 

Change invalid location test to check for exception