package api;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import models.User;
import org.junit.Test;
import com.google.gson.Gson;
import play.mvc.Http.Cookie;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Http.StatusCode;
import play.test.FunctionalTest;
import base.BaseTest;
public class UserChangeProfileTest extends BaseTest {
private static String api_base = "/api/me/profile";
@Test
public void crud() {
String username = "eduardo.perrino@A";
String password = "secret1";
Map<String, String> loginUserParams = new HashMap<String, String>();
loginUserParams.put("username", username);
loginUserParams.put("password", password);
// 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);
response = GET(request, api_base);
assertStatus(StatusCode.OK, response);
InputStream is = new ByteArrayInputStream(response.out.toByteArray());
Reader reader = new InputStreamReader(is);
User user = new Gson().fromJson(reader, User.class);
assertTrue("El nombre del usuario ha de ser eduardo.perrino ", username.startsWith(user.username));
user.basic_information.firstName = "Juan lucas";
request = newRequest();
request.cookies.putAll(cookies);
response = FunctionalTest.PUT(request, api_base,"application/json", new Gson().toJson(user, User.class));
assertStatus(StatusCode.OK, response);
String location = response.getHeader("location");
assertNotNull(location);
request = newRequest();
request.cookies.putAll(cookies);
response = GET(request, api_base);
assertStatus(StatusCode.OK, response);
is = new ByteArrayInputStream(response.out.toByteArray());
reader = new InputStreamReader(is);
user = new Gson().fromJson(reader, User.class);
assertTrue("El nombre del usuario ha de ser Juan lucas", user.basic_information.firstName.equals("Juan lucas"));
}
}