diff --git a/pom.xml b/pom.xml index 194becf..e5438f1 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,12 @@ java-dotenv 5.2.1 + + org.mockito + mockito-all + 1.8.4 + test + diff --git a/src/main/java/com/google/sps/data/BusinessesService.java b/src/main/java/com/google/sps/data/BusinessesService.java index bc860fc..5386003 100644 --- a/src/main/java/com/google/sps/data/BusinessesService.java +++ b/src/main/java/com/google/sps/data/BusinessesService.java @@ -69,6 +69,11 @@ public class BusinessesService { public BusinessesService(List allBusinesses) { this.allBusinesses = allBusinesses; } + + // Accessor method for testing purposes + public List getAllBusinesses() { + return allBusinesses; + } public List getBusinessesFromTextSearch(MapLocation mapLocation, String product) { diff --git a/src/test/java/com/google/sps/BusinessesServiceTest.java b/src/test/java/com/google/sps/BusinessesServiceTest.java new file mode 100644 index 0000000..8df44df --- /dev/null +++ b/src/test/java/com/google/sps/BusinessesServiceTest.java @@ -0,0 +1,98 @@ +import java.util.List; +import java.util.LinkedList; +import java.util.Arrays; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import com.google.sps.data.BusinessesService; +import com.google.sps.data.Listing; +import com.google.sps.data.MapLocation; +import static org.mockito.Mockito.*; + +@RunWith(JUnit4.class) +public final class BusinessesServiceTest { + + private final int MIN_LISTINGS_ALLOWED = 20; + private final int MAX_LISTINGS_ALLOWED = 60; + + // Address location in Owings Mills, MD + private final MapLocation TEST_MAP_LOCATION = new MapLocation(39.459836, -76.742536); + private final String TEST_PRODUCT = "pizza"; + + private final List TEST_ALL_BUSINESSES = new LinkedList<>(); + + private BusinessesService mockedBusinessesService = mock(BusinessesService.class); + private BusinessesService testBusinessesService = new BusinessesService(TEST_ALL_BUSINESSES); + + // 3 known returned businesses + private List TEST_TEXT_SEARCH_RESULT = + new LinkedList<>(Arrays.asList(new Listing("Pizza Connection", + "11215 York Rd, Cockeysville, MD 21030, United States", null, + 4.4, null, null), + new Listing("Brookside's Pizzeria", + "9419 Common Brook Rd #109, Owings Mills, MD 21117, United States", null, + 4.2, null, null), + new Listing("Village Pizza", + "38 Main St, Reisterstown, MD 21136, United States", null, + 4.2, null, null))); + + // 3 known returned businesses + private List TEST_NEARBY_SEARCH_RESULT = + new LinkedList<>(Arrays.asList(new Listing("Perfect Windows Inc", + "2927 Walnut Avenue, Owings Mills", null, + 0.0, null, null), + new Listing("Fortuna Liquor & Food", + "12147 Park Heights Avenue, Owings Mills", null, + 0.0, null, null), + new Listing("Dantech, Inc", + "12149 Park Heights Avenue, Owings Mills", null, + 5.0, null, null))); + + @Test + public void testGetBusinessesFromTextSearch() { + when(mockedBusinessesService.getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT)) + .thenReturn(TEST_TEXT_SEARCH_RESULT); + + List expected = mockedBusinessesService + .getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT); + List actual = testBusinessesService + .getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT); + + Assert.assertTrue(actual.size() >= MIN_LISTINGS_ALLOWED); + Assert.assertTrue(actual.size() <= MAX_LISTINGS_ALLOWED); + + int expectedListingsFound = 0; + for (Listing expectedListing : expected) { + for (Listing actualListing : actual) { + if (expectedListing.getName().equals(actualListing.getName())) { + expectedListingsFound++; + } + } + } + Assert.assertEquals(expectedListingsFound, expected.size()); + } + + @Test + public void testGetBusinessesFromNearbySearch() { + when(mockedBusinessesService.getBusinessesFromNearbySearch(TEST_MAP_LOCATION)) + .thenReturn(TEST_NEARBY_SEARCH_RESULT); + List expected = mockedBusinessesService + .getBusinessesFromNearbySearch(TEST_MAP_LOCATION); + List actual = testBusinessesService + .getBusinessesFromNearbySearch(TEST_MAP_LOCATION); + + Assert.assertTrue(actual.size() >= MIN_LISTINGS_ALLOWED); + Assert.assertTrue(actual.size() <= MAX_LISTINGS_ALLOWED); + + int expectedListingsFound = 0; + for (Listing expectedListing : expected) { + for (Listing actualListing : actual) { + if (expectedListing.getName().equals(actualListing.getName())) { + expectedListingsFound++; + } + } + } + Assert.assertEquals(expectedListingsFound, expected.size()); + } +} \ No newline at end of file diff --git a/src/test/java/com/google/sps/BigBusinessesTest.java b/src/test/java/com/google/sps/SmallCityServiceTest.java similarity index 99% rename from src/test/java/com/google/sps/BigBusinessesTest.java rename to src/test/java/com/google/sps/SmallCityServiceTest.java index 69d63ab..63e4e77 100644 --- a/src/test/java/com/google/sps/BigBusinessesTest.java +++ b/src/test/java/com/google/sps/SmallCityServiceTest.java @@ -45,7 +45,7 @@ import com.google.appengine.api.datastore.KeyFactory; @RunWith(JUnit4.class) -public final class BigBusinessesTest { +public final class SmallCityServiceTest { private Photo[] samplePhotos = new Photo[0]; private String[] sampleBusinessTypes = new String[0]; private MapLocation testLocation = new MapLocation(40.457177, -79.916696);