Package api

Source Code of api.CRUDPhotosTest

package api;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import models.Photo;
import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import controllers.Data;
import play.Play;
import play.mvc.Http.Response;
import play.mvc.Http.StatusCode;
import play.vfs.VirtualFile;
import base.BaseTest;

public class CRUDPhotosTest extends BaseTest {
 
  @Test
  public void crudTest() throws Exception {
    Response response = GET("/api/me/photos");
    assertStatus(StatusCode.FOUND, response);
    String location = response.getHeader("location");
    assertNotNull(location);
    Map<String, String> loginUserParams = new HashMap<String, String>();
    loginUserParams.put("username", "eva.perez@B");
    loginUserParams.put("password", "secret1");
    // Login here so the following request will be authenticated:
    response = POST(location, loginUserParams);
    assertStatus(StatusCode.FOUND, response);
    response = GET("/api/me/photos");
    assertStatus(StatusCode.OK, response);
    InputStream is = new ByteArrayInputStream(response.out.toByteArray());
    Reader reader = new InputStreamReader(is);
    Type dataType = new TypeToken<Data<Photo>>(){}.getType();
    Data<Photo> data = new Gson().fromJson(reader, dataType);
    assertTrue("La usuario eva perez no tiene ninguna foto", data.data.size()==0);
    Map<String, String> parameters = new HashMap<String, String>();
    Map<java.lang.String, java.io.File> files = new HashMap<java.lang.String, java.io.File>();
    VirtualFile vfFile;
    File image = null;
    for (VirtualFile vf : Play.javaPath) {
      vfFile = vf.child("atom-1.jpeg");
      if(vfFile!=null && vfFile.exists()) {
        image = vfFile.getRealFile();
        break;
      }
    }
    assertNotNull("El fichero atom-1.jpeg especificado no existe", image);
    System.out.println("Exists: " + image.getAbsolutePath());
    files.put("data", image);
    response =  POST("/api/me/photos", parameters, files);
    location = response.getHeader("location");
    response = GET(location);
    assertStatus(StatusCode.OK, response);
    dataType = new TypeToken<Photo>(){}.getType();
    is = new ByteArrayInputStream(response.out.toByteArray());
    reader = new InputStreamReader(is);
    Photo photo = new Gson().fromJson(reader, dataType);
    response = GET(photo.original.source);
    assertStatus(StatusCode.OK, response);
    is = new ByteArrayInputStream(response.out.toByteArray());
    File newFile =new File("atom-returned.jpeg");
    FileOutputStream fos = new FileOutputStream(newFile);
    int dataI;
    while((dataI=is.read())!=-1) {
      char ch = (char)dataI;
      fos.write(ch);
    }
    fos.flush();
    fos.close();
    assertTrue("El tamaño ha de ser el mismo ", image.getTotalSpace() == newFile.getTotalSpace());
    response = GET("/api/me/photos");
    assertStatus(StatusCode.OK, response);
    is = new ByteArrayInputStream(response.out.toByteArray());
    reader = new InputStreamReader(is);
    dataType = new TypeToken<Data<Photo>>(){}.getType();
    data = new Gson().fromJson(reader, dataType);
    System.out.println("Photo size: " + data.data.size());
    response = DELETE("/api/me/photos/" + photo.id);
    assertStatus(StatusCode.NO_RESPONSE, response);
    response = GET(location);
    assertStatus(StatusCode.NOT_FOUND, response);
    response = GET(photo.original.source);
    assertStatus(StatusCode.NOT_FOUND, response);
  }
}
TOP

Related Classes of api.CRUDPhotosTest

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.