package org.xmx0632.deliciousfruit.api.v1;
import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springside.modules.mapper.JsonMapper;
import org.xmx0632.deliciousfruit.api.v1.bo.RegisterRequest;
import org.xmx0632.deliciousfruit.api.v1.bo.RegisterResponse;
import org.xmx0632.deliciousfruit.functional.BaseControllerTestCase;
public class UserAccountApiControllerRegisterRestTemplateTest extends
BaseControllerTestCase {
private final RestTemplate restTemplate = new RestTemplate();
private static String url;
@BeforeClass
public static void initUrl() {
url = baseUrl + "/register";
}
@Test
public void testRegisterSuccess() throws URISyntaxException {
long t = System.currentTimeMillis();
RegisterRequest registerRequest = new RegisterRequest(t
+ "aaaa@bbb.com", "", "password", "");
URI uri = new URI(url);
ResponseEntity<RegisterResponse> result = restTemplate.postForEntity(
uri, registerRequest, RegisterResponse.class);
RegisterResponse response = result.getBody();
String expected = "RegisterResponse [result=Result [msg=, value=0], err=null]";
assertEquals(expected, response.toString());
formatHttpInfoPrint(HttpMethod.POST, url, null, "用户注册成功",
new JsonMapper().toJson(registerRequest),
jsonMapper.toJson(response));
}
@Test
public void testRegisterMobileSuccess() throws URISyntaxException {
String mobileNo = String.valueOf(System.currentTimeMillis()).substring(
2, 13);
RegisterRequest registerRequest = new RegisterRequest("", mobileNo,
null, "password1");
URI uri = new URI(url);
ResponseEntity<RegisterResponse> result = restTemplate.postForEntity(
uri, registerRequest, RegisterResponse.class);
RegisterResponse response = result.getBody();
String expected = "RegisterResponse [result=Result [msg=, value=0], err=null]";
assertEquals(expected, response.toString());
formatHttpInfoPrint(HttpMethod.POST, url, null, "用户手机号注册成功",
new JsonMapper().toJson(registerRequest),
jsonMapper.toJson(response));
}
@Test
public void testRegisterFail_email_as_username_exists()
throws URISyntaxException {
RegisterRequest registerRequest = new RegisterRequest(
"user3@gmail.com", "", "password", "");
URI uri = new URI(url);
ResponseEntity<RegisterResponse> result = restTemplate.postForEntity(
uri, registerRequest, RegisterResponse.class);
RegisterResponse response = result.getBody();
String expected = "RegisterResponse [result=Result [msg=username exist, value=2], err=null]";
assertEquals(expected, response.toString());
formatHttpInfoPrint(HttpMethod.POST, url, null, "用户名已存在",
new JsonMapper().toJson(registerRequest),
jsonMapper.toJson(response));
}
@Test
public void testRegisterFail_mobile_as_username_exists_1()
throws URISyntaxException {
RegisterRequest registerRequest = new RegisterRequest("",
"13800138003", "password", "");
URI uri = new URI(url);
ResponseEntity<RegisterResponse> result = restTemplate.postForEntity(
uri, registerRequest, RegisterResponse.class);
RegisterResponse response = result.getBody();
String expected = "RegisterResponse [result=Result [msg=username exist, value=2], err=null]";
assertEquals(expected, response.toString());
formatHttpInfoPrint(HttpMethod.POST, url, null, "用户名已存在",
new JsonMapper().toJson(registerRequest),
jsonMapper.toJson(response));
}
@Test
public void testRegisterFail_mobile_as_username_exists_2()
throws URISyntaxException {
RegisterRequest registerRequest = new RegisterRequest(
"user3@gmail.com", "13800138003", "password", "");
URI uri = new URI(url);
ResponseEntity<RegisterResponse> result = restTemplate.postForEntity(
uri, registerRequest, RegisterResponse.class);
RegisterResponse response = result.getBody();
String expected = "RegisterResponse [result=Result [msg=username exist, value=2], err=null]";
assertEquals(expected, response.toString());
formatHttpInfoPrint(HttpMethod.POST, url, null, "用户名已存在",
new JsonMapper().toJson(registerRequest),
jsonMapper.toJson(response));
}
@Test
public void testRegisterFail() throws URISyntaxException {
String password = null;
RegisterRequest registerRequest = new RegisterRequest(null, null,
password, "");
URI uri = new URI(url);
ResponseEntity<RegisterResponse> result = restTemplate.postForEntity(
uri, registerRequest, RegisterResponse.class);
RegisterResponse response = result.getBody();
String expected = "RegisterResponse [result=Result [msg=invalid property, value=1], err={username=不能为null}]";
assertEquals(expected, response.toString());
formatHttpInfoPrint(HttpMethod.POST, url, null, "用户注册失败",
new JsonMapper().toJson(registerRequest),
jsonMapper.toJson(response));
}
}