Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
<artifactId>java-dotenv</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/google/sps/data/BusinessesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public class BusinessesService {
public BusinessesService(List<Listing> allBusinesses) {
this.allBusinesses = allBusinesses;
}

// Accessor method for testing purposes
public List<Listing> getAllBusinesses() {
return allBusinesses;
}

public List<Listing>
getBusinessesFromTextSearch(MapLocation mapLocation, String product) {
Expand Down
98 changes: 98 additions & 0 deletions src/test/java/com/google/sps/BusinessesServiceTest.java
Original file line number Diff line number Diff line change
@@ -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<Listing> 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<Listing> 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<Listing> 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<Listing> expected = mockedBusinessesService
.getBusinessesFromTextSearch(TEST_MAP_LOCATION, TEST_PRODUCT);
List<Listing> 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<Listing> expected = mockedBusinessesService
.getBusinessesFromNearbySearch(TEST_MAP_LOCATION);
List<Listing> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down