Esempio n. 1
0
  @Test
  public void createTransactionReturnsOk() {
    // create "cars" transaction
    Response response =
        client
            .path("/transactionservice/transaction/10")
            .request()
            .put(Entity.json(new Transaction("cars", new BigDecimal(5000), 0l)));

    assertEquals(200, response.getStatus());
    assertEquals(
        TransactionService.OperationResult.OK,
        response.readEntity(TransactionService.OperationResult.class));

    // create "shopping" transaction
    response =
        client
            .path("/transactionservice/transaction/11")
            .request()
            .put(Entity.json(new Transaction("shopping", new BigDecimal(10000), 10l)));

    assertEquals(200, response.getStatus());
    assertEquals(
        TransactionService.OperationResult.OK,
        response.readEntity(TransactionService.OperationResult.class));

    // get "cars" transactions
    response = client.path("/transactionservice/type/cars").request().get();

    assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked")
    List<Integer> ids = response.readEntity(List.class);
    assertEquals(1, ids.size());
    assertEquals(10, ids.get(0).intValue());

    // get "sum" for transaction 10
    response = client.path("/transactionservice/sum/10").request().get();

    assertEquals(200, response.getStatus());
    AggregatorService.Sum sum = response.readEntity(AggregatorService.Sum.class);
    assertEquals(15000, sum.getSum().intValue());

    // get "sum" for transaction 11
    response = client.path("/transactionservice/sum/11").request().get();

    assertEquals(200, response.getStatus());
    sum = response.readEntity(AggregatorService.Sum.class);
    assertEquals(10000, sum.getSum().intValue());
  }
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public String addRoute(Route route, @Context HttpServletResponse servletResponse)
      throws IOException {
    try {
      Entity routeEntity = new Entity("Route");
      routeEntity.setProperty("originPlaceID", route.getOrigin().getGooglePlaceId());
      routeEntity.setProperty("destinationPlaceID", route.getDestination().getGooglePlaceId());
      routeEntity.setProperty("duration", route.getDuration());
      routeEntity.setProperty("distance", route.getDistance());
      routeEntity.setProperty("origin", route.getOrigin().getAddress());
      routeEntity.setProperty("destination", route.getDestination().getAddress());

      Text mapJsonAsText = route.getMapJsonAsText();
      routeEntity.setProperty("routeJSON", mapJsonAsText);

      syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
      String cacheKey = "route-" + key.nextLong();
      syncCache.put(cacheKey, routeEntity);
      return new JSONObject().append("status", "OK").append("key", cacheKey).toString();
    } catch (Exception e) {
      return new JSONObject()
          .append("status", "fail")
          .append("message", "Could not cache object.")
          .toString();
    }
  }
 // for the application
 @GET
 @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 public TaskData getTaskData() {
   // same code as above method
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   if (syncCache.get(keyname) != null) {
     TaskData ts = (TaskData) syncCache.get(keyname);
     return ts;
   }
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   try {
     Entity ent = datastore.get(entKey);
     TaskData ts =
         new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date"));
     return ts;
   } catch (EntityNotFoundException e) {
     throw new RuntimeException("Get: TaskData with " + keyname + " not found");
   }
 }
 @PUT
 @Consumes(MediaType.APPLICATION_XML)
 public Response putTaskData(String value) {
   Response res = null;
   // add your code here
   // first check if the Entity exists in the datastore
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   Date date = new Date();
   try {
     // if it is, signal that we updated the entity
     Entity ts = datastore.get(entKey);
     ts.setProperty("value", value);
     ts.setProperty("date", date);
     datastore.put(ts);
     res = Response.noContent().build();
   } catch (EntityNotFoundException e) {
     // if it is not, create it and
     // signal that we created the entity in the datastore
     Entity taskdata = new Entity("TaskData", keyname);
     taskdata.setProperty("value", value);
     taskdata.setProperty("date", date);
     datastore.put(taskdata);
     res = Response.created(uriInfo.getAbsolutePath()).build();
   }
   TaskData td = new TaskData(keyname, value, date);
   syncCache.put(keyname, td);
   return res;
 }
 // for the browser
 @GET
 @Produces(MediaType.TEXT_XML)
 public TaskData getTaskDataHTML() {
   // add your code here (get Entity from datastore using this.keyname)
   // throw new RuntimeException("Get: TaskData with " + keyname +  " not found");
   // if not found
   DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
   MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
   if (syncCache.get(keyname) != null) {
     TaskData ts = (TaskData) syncCache.get(keyname);
     return ts;
   }
   Key entKey = KeyFactory.createKey("TaskData", keyname);
   try {
     Entity ent = datastore.get(entKey);
     TaskData ts =
         new TaskData(keyname, (String) ent.getProperty("value"), (Date) ent.getProperty("date"));
     return ts;
   } catch (EntityNotFoundException e) {
     throw new RuntimeException("Get: TaskData with " + keyname + " not found");
   }
 }