Skip to content

Commit

Permalink
Merge pull request #204 from googleinterns/TripTests
Browse files Browse the repository at this point in the history
Trip tests
  • Loading branch information
RCAVelez authored Aug 6, 2020
2 parents 38fc1cf + ebd0c03 commit 89890d8
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 47 deletions.
81 changes: 50 additions & 31 deletions backend/src/main/java/com/google/sps/TripCrud.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.google.gson.JsonParser;
import java.util.ArrayList;

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

Expand All @@ -25,10 +25,23 @@ public class TripCrud {
* @return tripEntity a trip entity
*/
public static Entity createTrip(String email, String tripData) {
Entity tripEntity = toEntity(tripData, "", null);
Entity tripEntity = toEntity(tripData, null, null);
datastore.put(tripEntity);
Long id = tripEntity.getKey().getId();
UserCrud.addTripId(email, id);
TripCrud.addTripId(id);
return TripCrud.readTrip(id);
}

/**
* Add tripId property to Trip Entity
*
* @param id the trip id
*/
private static void addTripId(Long id) {
Entity tripEntity = TripCrud.readTrip(id);
tripEntity.setProperty("tripId", id);
datastore.put(tripEntity);
UserCrud.addTripId(email, tripEntity.getKey().getId());
return tripEntity;
}

/**
Expand All @@ -39,8 +52,8 @@ public static Entity createTrip(String email, String tripData) {
* @param tripKey trip key, can be null
* @return tripEntity from tripData
*/
public static Entity toEntity(String tripData, String tripId, Key tripKey) {
Entity tripEntity = tripId == "" ? new Entity("Trip") : new Entity("Trip", tripId, tripKey);
public static Entity toEntity(String tripData, Long tripId, Key tripKey) {
Entity tripEntity = tripId == null ? new Entity("Trip") : new Entity("Trip", tripId, tripKey);
setProperties(tripEntity, tripData);
return tripEntity;
}
Expand All @@ -55,22 +68,24 @@ private static void setProperties(Entity tripEntity, String tripData) {
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(tripData);
JsonObject jsonObject = jsonElement.getAsJsonObject();

tripEntity.setProperty("isOptimized", jsonObject.get("isOptimized").getAsBoolean());
tripEntity.setProperty("searchText", jsonObject.get("searchText").getAsString());
tripEntity.setProperty("tripName", jsonObject.get("tripName").getAsString());
ArrayList<EmbeddedEntity> attractions = new ArrayList<EmbeddedEntity>();

Integer centerLat = jsonObject.get("centerLat").getAsInt();
Integer centerLng = jsonObject.get("centerLng").getAsInt();
tripEntity.setProperty("centerLng", centerLng);
tripEntity.setProperty("centerLat", centerLat);

ArrayList<EmbeddedEntity> attractions = new ArrayList<EmbeddedEntity>();
for (JsonElement attractionElement : jsonObject.getAsJsonArray("attractions")) {
JsonObject attraction =
attractionElement.getAsJsonObject(); // needed? might only need JsonElement
JsonObject attraction = attractionElement.getAsJsonObject();
EmbeddedEntity embeddedAttraction = new EmbeddedEntity();
embeddedAttraction.setProperty(
"attractionName", attraction.get("attractionName").getAsString());
embeddedAttraction.setProperty(
"photoReference", attraction.get("photoReference").getAsString());
embeddedAttraction.setProperty("name", attraction.get("name").getAsString());
embeddedAttraction.setProperty("photoUrl", attraction.get("photoUrl").getAsString());
embeddedAttraction.setProperty("routeIndex", attraction.get("routeIndex").getAsInt());
embeddedAttraction.setProperty("coordinates", attraction.get("coordinates").getAsString());
embeddedAttraction.setProperty("lat", attraction.get("lat").getAsString());
embeddedAttraction.setProperty("lng", attraction.get("lng").getAsString());
attractions.add(embeddedAttraction);
}
tripEntity.setProperty("attractions", attractions);
Expand All @@ -80,12 +95,17 @@ private static void setProperties(Entity tripEntity, String tripData) {
* Finds a trip entity by id
*
* @param tripId id of the trip to find
* @throws EntityNotFoundException if trip entity is not found
* @return Trip entity
* @return Trip entity if found, null if EntityNotFoundException is caught
*/
public static Entity readTrip(String tripId) throws EntityNotFoundException {
Key entityKey = KeyFactory.createKey("Trip", Long.parseLong(tripId));
return datastore.get(entityKey);
public static Entity readTrip(Long tripId) {
Key entityKey = KeyFactory.createKey("Trip", tripId);
Entity tripEntity;
try {
tripEntity = datastore.get(entityKey);
} catch (EntityNotFoundException e) {
return null;
}
return tripEntity;
}

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

jsonTrip.addProperty("centerLng", (Long) tripEntity.getProperty("centerLng"));
jsonTrip.addProperty("centerLat", (Long) tripEntity.getProperty("centerLat"));
JsonArray attractions = new JsonArray();
for (EmbeddedEntity attraction :
(ArrayList<EmbeddedEntity>) tripEntity.getProperty("attractions")) {
JsonObject attractionJson = new JsonObject();
attractionJson.addProperty(
"attractionName", (String) attraction.getProperty("attractionName"));
attractionJson.addProperty(
"photoReference", (String) attraction.getProperty("photoReference"));
attractionJson.addProperty("routeIndex", (Integer) attraction.getProperty("routeIndex"));
attractionJson.addProperty("coordinates", (String) attraction.getProperty("coordinates"));
attractionJson.addProperty("name", (String) attraction.getProperty("name"));
attractionJson.addProperty("photoUrl", (String) attraction.getProperty("photoUrl"));
attractionJson.addProperty("routeIndex", (Long) attraction.getProperty("routeIndex"));
attractionJson.addProperty("lat", (String) attraction.getProperty("lat"));
attractionJson.addProperty("lng", (String) attraction.getProperty("lng"));
attractions.add(attractionJson);
}
jsonTrip.add("attractions", attractions);
Expand All @@ -123,11 +144,9 @@ public static JsonObject toJson(Entity tripEntity) {
* @param tripId id of the trip to find
* @param tripData string representation of tripData json
*/
public static void updateTrip(String tripId, String tripData) {
Entity tripEntity;
try {
tripEntity = TripCrud.readTrip(tripId);
} catch (EntityNotFoundException e) {
public static void updateTrip(Long tripId, String tripData) {
Entity tripEntity = TripCrud.readTrip(tripId);
if (tripEntity == null) {
return;
}
setProperties(tripEntity, tripData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.sps.servlets;

import com.google.appengine.api.datastore.Entity;
import com.google.gson.JsonObject;
import com.google.sps.TripCrud;
import com.google.sps.UserCrud;
import java.io.IOException;
Expand All @@ -27,7 +28,7 @@
@WebServlet("/api/v1/createTrip")
public class CreateTripServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String email = request.getParameter("email");

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

TripCrud.createTrip(email, tripData);
Entity tripEntity = TripCrud.createTrip(email, tripData);
JsonObject jsonResults = new JsonObject();
jsonResults.addProperty("tripId", (String) tripEntity.getProperty("tripId"));
response.setContentType("application/json;");
response.getWriter().println(jsonResults);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.google.sps.servlets;

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

Entity tripEntity;
try {
tripEntity = TripCrud.readTrip(tripId);
} catch (EntityNotFoundException e) {
Entity tripEntity = TripCrud.readTrip(Long.parseLong(tripId));
if (tripEntity == null) {
throw new IllegalArgumentException("Trip not found");
}

JsonObject tripJson = TripCrud.toJson(tripEntity);
JsonObject jsonResults = new JsonObject();
jsonResults.addProperty("tripId", tripId);
jsonResults.addProperty("tripData", tripJson.toString());
response.setContentType("application/json;");
response.getWriter().println(TripCrud.toJson(tripEntity).toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
throw new IllegalArgumentException("trip passed is not valid");
}

TripCrud.updateTrip(tripId, tripData);
TripCrud.updateTrip(Long.parseLong(tripId), tripData);
}
}
Loading

0 comments on commit 89890d8

Please sign in to comment.