Package api

Source Code of api.CRUDSubscribersTest

package api;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import models.ContactInformation;
import models.Subscriber;
import models.User;
import org.junit.Before;
import org.junit.Test;
import base.BaseTest;
import com.google.appengine.api.NamespaceManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import controllers.Data;
import play.libs.WS;
import play.libs.WS.HttpResponse;
import play.libs.WS.WSRequest;
import play.modules.siena.SienaFixtures;
import play.mvc.Http.Cookie;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Http.StatusCode;
import play.test.FunctionalTest;


public class CRUDSubscribersTest extends BaseTest {
 
  private final static Gson gson = new Gson();
 
  @Test
  public void crudubscriber() {
    Map<String, String> loginUserParams = new HashMap<String, String>();
    loginUserParams.put("username", "root");
    loginUserParams.put("password", "m4f2010");
    // Login here so the following request will be authenticated:
    Response response = POST("/login", loginUserParams);
    assertStatus(StatusCode.FOUND, response);
    Map<String, Cookie> cookies = new HashMap<String, Cookie>();
    cookies.putAll(response.cookies);
    Request request = newRequest();
        request.cookies.putAll(cookies);
    //Create
    String name = "olympia";
    Subscriber subscriber = new Subscriber();
    subscriber.name = name;
    ContactInformation contact_information = new ContactInformation();
    contact_information.email = "lucas@olympia.com";
    subscriber.contact_information = contact_information;
    response = POST(request,
        "/api/subscribers", "application/json", gson.toJson(subscriber));
    assertStatus(StatusCode.CREATED, response);
    String location = response.getHeader("location");
    assertNotNull("Location header musn't be empty", location);
    response = GET(location);
    subscriber = parseJSON(response.out.toByteArray());
    assertEquals(subscriber.name, name);
    //Update
    name = subscriber.name + "_modified";
    subscriber.name = name;
    request = newRequest();
    request.cookies.putAll(cookies);
    response = PUT(request, "/api/subscribers/" + subscriber.id, "application/json", gson.toJson(subscriber));
    assertStatus(StatusCode.OK, response);
   
   
    /*name = subscriber.name + "_modified";
    subscriber.name = name;
    response = PUT("/api/subscribers/" + subscriber.id, "application/json", gson.toJson(subscriber));
    assertStatus(StatusCode.OK, response);
    location = response.getHeader("location");
    assertNotNull("Location header musn't be empty", location);
    response = GET(location);
    subscriber = parseJSON(response.out.toByteArray());
    assertEquals(subscriber.name, name);
    response = DELETE(location);
    assertStatus(StatusCode.NO_RESPONSE, response);
    response = GET("/api/subscribers");
    Type dataType = new TypeToken<Data<Subscriber>>(){}.getType();
    Data<Subscriber> data = gson.fromJson(new String(response.out.toByteArray()), dataType);
    for(Subscriber su : data.data) {
      checkDetail(su);
    }
    subscriber = new Subscriber();
    response = POST("/api/subscribers", "application/json", gson.toJson(subscriber));
    assertStatus(StatusCode.BAD_REQUEST, response);*/
  }
 
 
 
  private static void checkDetail(Subscriber originalSubscriber) {
    Response response = GET("/api/subscribers/" + originalSubscriber.id);
    assertIsOk(response);
    assertContentType("application/json", response);
    assertCharset("utf-8", response);
    Subscriber remoteSubscriber = parseJSON(response.out.toByteArray());
    assertEquals("Debe ser el mismo subscriber", remoteSubscriber.id, originalSubscriber.id);
  }
 
  private static Subscriber parseJSON(byte[] content) {
    InputStream is = new ByteArrayInputStream(content);
    Reader reader = new InputStreamReader(is);
    Type dataType = new TypeToken<Subscriber>(){}.getType();
    Subscriber subscriber = gson.fromJson(reader, dataType);
    return subscriber;
  }
 
}
TOP

Related Classes of api.CRUDSubscribersTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.