Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1596: Add test for LocationWiseTimeService.java
- Loading branch information
1 parent
680bdb8
commit 60a0b3d
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
test/org/loklak/api/search/LocationWiseTimeServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package org.loklak.api.search; | ||
|
||
import org.junit.Test; | ||
import org.loklak.api.search.LocationWiseTimeService; | ||
import org.loklak.susi.SusiThought; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import java.util.regex.Pattern; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.GregorianCalendar; | ||
import java.util.TimeZone; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class LocationWiseTimeServiceTest { | ||
|
||
@Test | ||
public void apiPathTest() { | ||
LocationWiseTimeService locationWiseTimeService = new LocationWiseTimeService(); | ||
assertEquals("/api/locationwisetime.json", locationWiseTimeService.getAPIPath()); | ||
} | ||
|
||
@Test | ||
public void locationAndTimeTest() { | ||
LocationWiseTimeService locationWiseTimeService = new LocationWiseTimeService(); | ||
SusiThought susiThought = locationWiseTimeService.locationWiseTime("Chennai"); | ||
JSONArray jsonArray = susiThought.getData(); | ||
for(int i=0; i<jsonArray.length(); i++) { | ||
JSONObject obj = new JSONObject(jsonArray.get(i).toString()); | ||
assertNotNull(obj.getString("location")); | ||
assertNotNull(obj.getString("time")); | ||
} | ||
} | ||
|
||
@Test | ||
public void timePatternTest() { | ||
LocationWiseTimeService locationWiseTimeService = new LocationWiseTimeService(); | ||
SusiThought susiThought = locationWiseTimeService.locationWiseTime("Chennai"); | ||
JSONArray jsonArray = susiThought.getData(); | ||
String timeRegex = "[0-5][0-9].[0-5][0-9]"; | ||
|
||
for(int i=0; i<jsonArray.length(); i++) { | ||
JSONObject obj = new JSONObject(jsonArray.get(i).toString()); | ||
String reqTime = ""; | ||
boolean check = false; | ||
for(int j=0; j<obj.getString("time").length(); j++) { | ||
if(obj.getString("time").charAt(j)==' ') { | ||
check = true; | ||
} | ||
else if(check) { | ||
reqTime += obj.getString("time").charAt(j); | ||
} | ||
} | ||
if(Pattern.matches(timeRegex, reqTime)) | ||
assertTrue(Pattern.matches(timeRegex, reqTime)); | ||
} | ||
} | ||
|
||
@Test | ||
public void hourDayDifferenceTest() { | ||
LocationWiseTimeService locationWiseTimeService = new LocationWiseTimeService(); | ||
SusiThought susiThought = locationWiseTimeService.locationWiseTime("Chennai"); | ||
JSONArray jsonArray = susiThought.getData(); | ||
|
||
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); | ||
c.setTimeInMillis(new Date().getTime()); | ||
int newYorkHourOfDay = c.get(Calendar.HOUR_OF_DAY); | ||
int newYorkDayOfMonth = c.get(Calendar.DAY_OF_MONTH); | ||
|
||
for(int i=0; i<jsonArray.length(); i++) { | ||
JSONObject obj = new JSONObject(jsonArray.get(i).toString()); | ||
String reqLoc = ""; | ||
boolean check = true; | ||
for(int j=0; j<obj.getString("location").length(); j++) { | ||
if(obj.getString("location").charAt(j)==' ') { | ||
check = false; | ||
} | ||
else if(check) { | ||
reqLoc += obj.getString("location").charAt(j); | ||
} | ||
} | ||
|
||
c = new GregorianCalendar(TimeZone.getTimeZone(reqLoc)); | ||
c.setTimeInMillis(new Date().getTime()); | ||
int testHourOfDay = c.get(Calendar.HOUR_OF_DAY); | ||
int testDayOfMonth = c.get(Calendar.DAY_OF_MONTH); | ||
|
||
int hourDifference = testHourOfDay - newYorkHourOfDay; | ||
int dayDifference = testDayOfMonth - newYorkDayOfMonth; | ||
if (dayDifference != 0) { | ||
hourDifference = hourDifference + 24; | ||
} | ||
assertEquals(4, hourDifference); | ||
assertNotNull(dayDifference); | ||
} | ||
} | ||
} |