From 6101cd3c162c53ac158dc007d2ff7789d930eaac Mon Sep 17 00:00:00 2001 From: Lindsey Weiskopf Date: Tue, 4 Aug 2020 13:05:49 +0000 Subject: [PATCH 1/4] Rename files --- .../com/google/sps/BusinessesServiceTest.java | 34 +++++++++++++++++++ ...sesTest.java => SmallCityServiceTest.java} | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/google/sps/BusinessesServiceTest.java rename src/test/java/com/google/sps/{BigBusinessesTest.java => SmallCityServiceTest.java} (99%) 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..5174601 --- /dev/null +++ b/src/test/java/com/google/sps/BusinessesServiceTest.java @@ -0,0 +1,34 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import com.google.sps.data.SmallCityService; +import com.google.sps.data.UserService; +import com.google.sps.data.Listing; +import com.google.sps.data.MapLocation; +import com.google.sps.data.SearchObject; +import com.google.sps.data.User; +import com.googe.maps.model.PlaceSearchResult; +import com.google.maps.model.Geometry; + +@RunWith(JUnit4.class) +public final class BusinessesServiceTest { + + private List testBusinesses = new LinkedList(); + private BusinessesService testBusinessesService = new BusinessesService(testBusinesses); + private PlaceSearchResult TEST_PLACE_SEARCH_RESULT = new PlaceSearchResult(); + TEST_PLACE_SEARCH_RESULT.name = "TEST_NAME"; + TEST_PLACE_SEARCH_RESULT.vicinity = "TEST_VICINITY"; + TEST_PLACE_SEARCH_RESULT.formattedAddress = "TEST_FORMATTED_ADDRESS"; + TEST_PLACE_SEARCH_RESULT.geometry = new Geometry(); + TEST_PLACE_SEARCH_RESULT.rating = 0; + TEST_PLACE_SEARCH_RESULT.photos = null; + TEST_PLACE_SEARCH_RESULT.types = null; +} + +@Test +public void testAddListingToBusinesses() { + Assert.assertEquals(testBusinesses.size(), testBusinessesService.allBusinesses.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); From 9f3fa1a4b4ef41461160ce56ae4d5ec379687142 Mon Sep 17 00:00:00 2001 From: Lindsey Weiskopf Date: Wed, 5 Aug 2020 13:58:15 +0000 Subject: [PATCH 2/4] Write tests --- pom.xml | 5 ++ .../google/sps/data/BusinessesService.java | 5 ++ .../com/google/sps/BusinessesServiceTest.java | 66 +++++++++++++++++-- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 194becf..6e603d2 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,11 @@ java-dotenv 5.2.1 + + org.mockito + mockito-core + 3.4.4 + 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 index 5174601..edfe8fa 100644 --- a/src/test/java/com/google/sps/BusinessesServiceTest.java +++ b/src/test/java/com/google/sps/BusinessesServiceTest.java @@ -1,3 +1,5 @@ +import java.util.List; +import java.util.LinkedList; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -9,12 +11,12 @@ import com.google.sps.data.MapLocation; import com.google.sps.data.SearchObject; import com.google.sps.data.User; -import com.googe.maps.model.PlaceSearchResult; import com.google.maps.model.Geometry; +import static org.Mockito.*; @RunWith(JUnit4.class) public final class BusinessesServiceTest { - +/* private List testBusinesses = new LinkedList(); private BusinessesService testBusinessesService = new BusinessesService(testBusinesses); private PlaceSearchResult TEST_PLACE_SEARCH_RESULT = new PlaceSearchResult(); @@ -25,10 +27,64 @@ public final class BusinessesServiceTest { TEST_PLACE_SEARCH_RESULT.rating = 0; TEST_PLACE_SEARCH_RESULT.photos = null; TEST_PLACE_SEARCH_RESULT.types = null; -} + */ + private final int MIN_LISTINGS_RETURNED = 20; + private final int MAX_LISTINGS_RETURNED = 60; + + private final MapLocation TEST_MAP_LOCATION = new MapLocation(39.459836, -76.742536); + private final String TEST_PRODUCT = "pizza"; + + private final List TEST_PASSED_LIST = new LinkedList<>(); + + 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))); + @Test -public void testAddListingToBusinesses() { - Assert.assertEquals(testBusinesses.size(), testBusinessesService.allBusinesses.size()); +public void testGetBusinessesFromTextSearch() { + BusinessesService mockedBusinessesService = mock(BusinessesService.class); + BusinessesService testBusinessesService = new BusinessesService(TEST_PASSED_LIST); + 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_RETURNED); + Assert.assertTrue(actual.size() <= MAX_LISTINGS_RETURNED); + boolean containsExpected = false; + for (Listing expectedListing : expected) { + containsExpected = false; + for (Listing actualListing : actual) { + if (expectedListing.name == actualListing.name) { + containsExpected = true; + break; + } + } + if (containsExpected == false) { + break; + } + } + + Assert.assertTrue(containsExpected); +} } \ No newline at end of file From d6d4ba92f4a3d3217898cbd8216e19039713a9a3 Mon Sep 17 00:00:00 2001 From: Lindsey Weiskopf Date: Wed, 5 Aug 2020 16:40:07 +0000 Subject: [PATCH 3/4] Fix bug in test --- pom.xml | 7 +- .../com/google/sps/BusinessesServiceTest.java | 132 ++++++++++-------- 2 files changed, 74 insertions(+), 65 deletions(-) diff --git a/pom.xml b/pom.xml index 6e603d2..e5438f1 100644 --- a/pom.xml +++ b/pom.xml @@ -84,9 +84,10 @@ 5.2.1 - org.mockito - mockito-core - 3.4.4 + org.mockito + mockito-all + 1.8.4 + test diff --git a/src/test/java/com/google/sps/BusinessesServiceTest.java b/src/test/java/com/google/sps/BusinessesServiceTest.java index edfe8fa..378d63f 100644 --- a/src/test/java/com/google/sps/BusinessesServiceTest.java +++ b/src/test/java/com/google/sps/BusinessesServiceTest.java @@ -1,90 +1,98 @@ import java.util.List; import java.util.LinkedList; +import java.util.Arrays; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import com.google.sps.data.SmallCityService; -import com.google.sps.data.UserService; +import com.google.sps.data.BusinessesService; import com.google.sps.data.Listing; import com.google.sps.data.MapLocation; -import com.google.sps.data.SearchObject; -import com.google.sps.data.User; -import com.google.maps.model.Geometry; -import static org.Mockito.*; +import static org.mockito.Mockito.*; @RunWith(JUnit4.class) public final class BusinessesServiceTest { -/* - private List testBusinesses = new LinkedList(); - private BusinessesService testBusinessesService = new BusinessesService(testBusinesses); - private PlaceSearchResult TEST_PLACE_SEARCH_RESULT = new PlaceSearchResult(); - TEST_PLACE_SEARCH_RESULT.name = "TEST_NAME"; - TEST_PLACE_SEARCH_RESULT.vicinity = "TEST_VICINITY"; - TEST_PLACE_SEARCH_RESULT.formattedAddress = "TEST_FORMATTED_ADDRESS"; - TEST_PLACE_SEARCH_RESULT.geometry = new Geometry(); - TEST_PLACE_SEARCH_RESULT.rating = 0; - TEST_PLACE_SEARCH_RESULT.photos = null; - TEST_PLACE_SEARCH_RESULT.types = null; - */ + private final int MIN_LISTINGS_RETURNED = 20; private final int MAX_LISTINGS_RETURNED = 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_PASSED_LIST = new LinkedList<>(); - 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), + private BusinessesService mockedBusinessesService = mock(BusinessesService.class); + private BusinessesService testBusinessesService = new BusinessesService(TEST_PASSED_LIST); + + // 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), + "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))); - + "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() { - BusinessesService mockedBusinessesService = mock(BusinessesService.class); - BusinessesService testBusinessesService = new BusinessesService(TEST_PASSED_LIST); - when(mockedBusinessesService.getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT)) - .thenReturn(TEST_TEXT_SEARCH_RESULT); + @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); + List expected = mockedBusinessesService + .getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT); + List actual = testBusinessesService + .getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT); - Assert.assertTrue(actual.size() >= MIN_LISTINGS_RETURNED); - Assert.assertTrue(actual.size() <= MAX_LISTINGS_RETURNED); - - boolean containsExpected = false; - for (Listing expectedListing : expected) { - containsExpected = false; - for (Listing actualListing : actual) { - if (expectedListing.name == actualListing.name) { - containsExpected = true; - break; + Assert.assertTrue(actual.size() >= MIN_LISTINGS_RETURNED); + Assert.assertTrue(actual.size() <= MAX_LISTINGS_RETURNED); + + int containsCounter = 0; + for (Listing expectedListing : expected) { + for (Listing actualListing : actual) { + if (expectedListing.getName().equals(actualListing.getName())) { + containsCounter++; + } } } - if (containsExpected == false) { - break; - } + Assert.assertEquals(containsCounter, expected.size()); } - Assert.assertTrue(containsExpected); -} + @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_RETURNED); + Assert.assertTrue(actual.size() <= MAX_LISTINGS_RETURNED); + + int containsCounter = 0; + for (Listing expectedListing : expected) { + for (Listing actualListing : actual) { + if (expectedListing.getName().equals(actualListing.getName())) { + containsCounter++; + } + } + } + Assert.assertEquals(containsCounter, expected.size()); + } } \ No newline at end of file From 9f20e1e4aea2f3311343b91a7f2a344960c71f5d Mon Sep 17 00:00:00 2001 From: Lindsey Weiskopf Date: Fri, 7 Aug 2020 13:53:59 +0000 Subject: [PATCH 4/4] Fix naming convention --- .../com/google/sps/BusinessesServiceTest.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/google/sps/BusinessesServiceTest.java b/src/test/java/com/google/sps/BusinessesServiceTest.java index 378d63f..8df44df 100644 --- a/src/test/java/com/google/sps/BusinessesServiceTest.java +++ b/src/test/java/com/google/sps/BusinessesServiceTest.java @@ -13,17 +13,17 @@ @RunWith(JUnit4.class) public final class BusinessesServiceTest { - private final int MIN_LISTINGS_RETURNED = 20; - private final int MAX_LISTINGS_RETURNED = 60; + 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_PASSED_LIST = new LinkedList<>(); + private final List TEST_ALL_BUSINESSES = new LinkedList<>(); private BusinessesService mockedBusinessesService = mock(BusinessesService.class); - private BusinessesService testBusinessesService = new BusinessesService(TEST_PASSED_LIST); + private BusinessesService testBusinessesService = new BusinessesService(TEST_ALL_BUSINESSES); // 3 known returned businesses private List TEST_TEXT_SEARCH_RESULT = @@ -59,18 +59,18 @@ public void testGetBusinessesFromTextSearch() { List actual = testBusinessesService .getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT); - Assert.assertTrue(actual.size() >= MIN_LISTINGS_RETURNED); - Assert.assertTrue(actual.size() <= MAX_LISTINGS_RETURNED); + Assert.assertTrue(actual.size() >= MIN_LISTINGS_ALLOWED); + Assert.assertTrue(actual.size() <= MAX_LISTINGS_ALLOWED); - int containsCounter = 0; + int expectedListingsFound = 0; for (Listing expectedListing : expected) { for (Listing actualListing : actual) { if (expectedListing.getName().equals(actualListing.getName())) { - containsCounter++; + expectedListingsFound++; } } } - Assert.assertEquals(containsCounter, expected.size()); + Assert.assertEquals(expectedListingsFound, expected.size()); } @Test @@ -82,17 +82,17 @@ public void testGetBusinessesFromNearbySearch() { List actual = testBusinessesService .getBusinessesFromNearbySearch(TEST_MAP_LOCATION); - Assert.assertTrue(actual.size() >= MIN_LISTINGS_RETURNED); - Assert.assertTrue(actual.size() <= MAX_LISTINGS_RETURNED); + Assert.assertTrue(actual.size() >= MIN_LISTINGS_ALLOWED); + Assert.assertTrue(actual.size() <= MAX_LISTINGS_ALLOWED); - int containsCounter = 0; + int expectedListingsFound = 0; for (Listing expectedListing : expected) { for (Listing actualListing : actual) { if (expectedListing.getName().equals(actualListing.getName())) { - containsCounter++; + expectedListingsFound++; } } } - Assert.assertEquals(containsCounter, expected.size()); + Assert.assertEquals(expectedListingsFound, expected.size()); } } \ No newline at end of file