Skip to content

Commit 89890d8

Browse files
authored
Merge pull request #204 from googleinterns/TripTests
Trip tests
2 parents 38fc1cf + ebd0c03 commit 89890d8

File tree

9 files changed

+369
-47
lines changed

9 files changed

+369
-47
lines changed

backend/src/main/java/com/google/sps/TripCrud.java

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import com.google.gson.JsonParser;
1414
import java.util.ArrayList;
1515

16-
/** Class to handles CRUD related to the Trip */
16+
/** Class to handle CRU functions related to the Trip */
1717
public class TripCrud {
1818
private static final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
1919

@@ -25,10 +25,23 @@ public class TripCrud {
2525
* @return tripEntity a trip entity
2626
*/
2727
public static Entity createTrip(String email, String tripData) {
28-
Entity tripEntity = toEntity(tripData, "", null);
28+
Entity tripEntity = toEntity(tripData, null, null);
29+
datastore.put(tripEntity);
30+
Long id = tripEntity.getKey().getId();
31+
UserCrud.addTripId(email, id);
32+
TripCrud.addTripId(id);
33+
return TripCrud.readTrip(id);
34+
}
35+
36+
/**
37+
* Add tripId property to Trip Entity
38+
*
39+
* @param id the trip id
40+
*/
41+
private static void addTripId(Long id) {
42+
Entity tripEntity = TripCrud.readTrip(id);
43+
tripEntity.setProperty("tripId", id);
2944
datastore.put(tripEntity);
30-
UserCrud.addTripId(email, tripEntity.getKey().getId());
31-
return tripEntity;
3245
}
3346

3447
/**
@@ -39,8 +52,8 @@ public static Entity createTrip(String email, String tripData) {
3952
* @param tripKey trip key, can be null
4053
* @return tripEntity from tripData
4154
*/
42-
public static Entity toEntity(String tripData, String tripId, Key tripKey) {
43-
Entity tripEntity = tripId == "" ? new Entity("Trip") : new Entity("Trip", tripId, tripKey);
55+
public static Entity toEntity(String tripData, Long tripId, Key tripKey) {
56+
Entity tripEntity = tripId == null ? new Entity("Trip") : new Entity("Trip", tripId, tripKey);
4457
setProperties(tripEntity, tripData);
4558
return tripEntity;
4659
}
@@ -55,22 +68,24 @@ private static void setProperties(Entity tripEntity, String tripData) {
5568
JsonParser parser = new JsonParser();
5669
JsonElement jsonElement = parser.parse(tripData);
5770
JsonObject jsonObject = jsonElement.getAsJsonObject();
58-
5971
tripEntity.setProperty("isOptimized", jsonObject.get("isOptimized").getAsBoolean());
6072
tripEntity.setProperty("searchText", jsonObject.get("searchText").getAsString());
6173
tripEntity.setProperty("tripName", jsonObject.get("tripName").getAsString());
62-
ArrayList<EmbeddedEntity> attractions = new ArrayList<EmbeddedEntity>();
6374

75+
Integer centerLat = jsonObject.get("centerLat").getAsInt();
76+
Integer centerLng = jsonObject.get("centerLng").getAsInt();
77+
tripEntity.setProperty("centerLng", centerLng);
78+
tripEntity.setProperty("centerLat", centerLat);
79+
80+
ArrayList<EmbeddedEntity> attractions = new ArrayList<EmbeddedEntity>();
6481
for (JsonElement attractionElement : jsonObject.getAsJsonArray("attractions")) {
65-
JsonObject attraction =
66-
attractionElement.getAsJsonObject(); // needed? might only need JsonElement
82+
JsonObject attraction = attractionElement.getAsJsonObject();
6783
EmbeddedEntity embeddedAttraction = new EmbeddedEntity();
68-
embeddedAttraction.setProperty(
69-
"attractionName", attraction.get("attractionName").getAsString());
70-
embeddedAttraction.setProperty(
71-
"photoReference", attraction.get("photoReference").getAsString());
84+
embeddedAttraction.setProperty("name", attraction.get("name").getAsString());
85+
embeddedAttraction.setProperty("photoUrl", attraction.get("photoUrl").getAsString());
7286
embeddedAttraction.setProperty("routeIndex", attraction.get("routeIndex").getAsInt());
73-
embeddedAttraction.setProperty("coordinates", attraction.get("coordinates").getAsString());
87+
embeddedAttraction.setProperty("lat", attraction.get("lat").getAsString());
88+
embeddedAttraction.setProperty("lng", attraction.get("lng").getAsString());
7489
attractions.add(embeddedAttraction);
7590
}
7691
tripEntity.setProperty("attractions", attractions);
@@ -80,12 +95,17 @@ private static void setProperties(Entity tripEntity, String tripData) {
8095
* Finds a trip entity by id
8196
*
8297
* @param tripId id of the trip to find
83-
* @throws EntityNotFoundException if trip entity is not found
84-
* @return Trip entity
98+
* @return Trip entity if found, null if EntityNotFoundException is caught
8599
*/
86-
public static Entity readTrip(String tripId) throws EntityNotFoundException {
87-
Key entityKey = KeyFactory.createKey("Trip", Long.parseLong(tripId));
88-
return datastore.get(entityKey);
100+
public static Entity readTrip(Long tripId) {
101+
Key entityKey = KeyFactory.createKey("Trip", tripId);
102+
Entity tripEntity;
103+
try {
104+
tripEntity = datastore.get(entityKey);
105+
} catch (EntityNotFoundException e) {
106+
return null;
107+
}
108+
return tripEntity;
89109
}
90110

91111
/**
@@ -101,16 +121,17 @@ public static JsonObject toJson(Entity tripEntity) {
101121
jsonTrip.addProperty("searchText", (String) tripEntity.getProperty("searchText"));
102122
jsonTrip.addProperty("tripName", (String) tripEntity.getProperty("tripName"));
103123

124+
jsonTrip.addProperty("centerLng", (Long) tripEntity.getProperty("centerLng"));
125+
jsonTrip.addProperty("centerLat", (Long) tripEntity.getProperty("centerLat"));
104126
JsonArray attractions = new JsonArray();
105127
for (EmbeddedEntity attraction :
106128
(ArrayList<EmbeddedEntity>) tripEntity.getProperty("attractions")) {
107129
JsonObject attractionJson = new JsonObject();
108-
attractionJson.addProperty(
109-
"attractionName", (String) attraction.getProperty("attractionName"));
110-
attractionJson.addProperty(
111-
"photoReference", (String) attraction.getProperty("photoReference"));
112-
attractionJson.addProperty("routeIndex", (Integer) attraction.getProperty("routeIndex"));
113-
attractionJson.addProperty("coordinates", (String) attraction.getProperty("coordinates"));
130+
attractionJson.addProperty("name", (String) attraction.getProperty("name"));
131+
attractionJson.addProperty("photoUrl", (String) attraction.getProperty("photoUrl"));
132+
attractionJson.addProperty("routeIndex", (Long) attraction.getProperty("routeIndex"));
133+
attractionJson.addProperty("lat", (String) attraction.getProperty("lat"));
134+
attractionJson.addProperty("lng", (String) attraction.getProperty("lng"));
114135
attractions.add(attractionJson);
115136
}
116137
jsonTrip.add("attractions", attractions);
@@ -123,11 +144,9 @@ public static JsonObject toJson(Entity tripEntity) {
123144
* @param tripId id of the trip to find
124145
* @param tripData string representation of tripData json
125146
*/
126-
public static void updateTrip(String tripId, String tripData) {
127-
Entity tripEntity;
128-
try {
129-
tripEntity = TripCrud.readTrip(tripId);
130-
} catch (EntityNotFoundException e) {
147+
public static void updateTrip(Long tripId, String tripData) {
148+
Entity tripEntity = TripCrud.readTrip(tripId);
149+
if (tripEntity == null) {
131150
return;
132151
}
133152
setProperties(tripEntity, tripData);

backend/src/main/java/com/google/sps/servlets/CreateTripServlet.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.sps.servlets;
1616

1717
import com.google.appengine.api.datastore.Entity;
18+
import com.google.gson.JsonObject;
1819
import com.google.sps.TripCrud;
1920
import com.google.sps.UserCrud;
2021
import java.io.IOException;
@@ -27,7 +28,7 @@
2728
@WebServlet("/api/v1/createTrip")
2829
public class CreateTripServlet extends HttpServlet {
2930
@Override
30-
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
31+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
3132
String email = request.getParameter("email");
3233

3334
if (email == "" || email == null) {
@@ -44,6 +45,10 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
4445
throw new IllegalArgumentException("Email passed is not linked to user");
4546
}
4647

47-
TripCrud.createTrip(email, tripData);
48+
Entity tripEntity = TripCrud.createTrip(email, tripData);
49+
JsonObject jsonResults = new JsonObject();
50+
jsonResults.addProperty("tripId", (String) tripEntity.getProperty("tripId"));
51+
response.setContentType("application/json;");
52+
response.getWriter().println(jsonResults);
4853
}
4954
}

backend/src/main/java/com/google/sps/servlets/ReadTripServlet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package com.google.sps.servlets;
1616

1717
import com.google.appengine.api.datastore.Entity;
18-
import com.google.appengine.api.datastore.EntityNotFoundException;
18+
import com.google.gson.JsonObject;
1919
import com.google.sps.TripCrud;
2020
import java.io.IOException;
2121
import javax.servlet.annotation.WebServlet;
@@ -34,13 +34,15 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
3434
throw new IllegalArgumentException("tripId passed is not valid");
3535
}
3636

37-
Entity tripEntity;
38-
try {
39-
tripEntity = TripCrud.readTrip(tripId);
40-
} catch (EntityNotFoundException e) {
37+
Entity tripEntity = TripCrud.readTrip(Long.parseLong(tripId));
38+
if (tripEntity == null) {
4139
throw new IllegalArgumentException("Trip not found");
4240
}
4341

42+
JsonObject tripJson = TripCrud.toJson(tripEntity);
43+
JsonObject jsonResults = new JsonObject();
44+
jsonResults.addProperty("tripId", tripId);
45+
jsonResults.addProperty("tripData", tripJson.toString());
4446
response.setContentType("application/json;");
4547
response.getWriter().println(TripCrud.toJson(tripEntity).toString());
4648
}

backend/src/main/java/com/google/sps/servlets/UpdateTripServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
3333
throw new IllegalArgumentException("trip passed is not valid");
3434
}
3535

36-
TripCrud.updateTrip(tripId, tripData);
36+
TripCrud.updateTrip(Long.parseLong(tripId), tripData);
3737
}
3838
}

0 commit comments

Comments
 (0)