parent
4216d24a12
commit
33e439a9b7
|
@ -0,0 +1,138 @@
|
|||
package com.dispose.test.controller;
|
||||
|
||||
import com.dispose.common.ConstValue;
|
||||
import com.dispose.common.ErrorCode;
|
||||
import com.dispose.common.ProtoCryptoType;
|
||||
import com.dispose.pojo.dto.protocol.auth.LoginReq;
|
||||
import com.dispose.pojo.dto.protocol.auth.LoginRsp;
|
||||
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
|
||||
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
|
||||
import com.dispose.test.Global.InitTestEnvironment;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jodd.net.HttpStatus;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Assert;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* The type Auth controller test.
|
||||
*
|
||||
* @author <huangxin@cmhi.chinamoblie.com>
|
||||
*/
|
||||
@AutoConfigureMockMvc
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
@Slf4j
|
||||
public class AuthControllerTest extends InitTestEnvironment {
|
||||
/**
|
||||
* The Mock mvc.
|
||||
*/
|
||||
@Resource
|
||||
private MockMvc mockMvc;
|
||||
/**
|
||||
* The Object mapper.
|
||||
*/
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* A 1 login.
|
||||
*
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
@Test
|
||||
public void a1_login() throws Exception {
|
||||
LoginReq logReq = LoginReq.builder()
|
||||
.userName(getUSER_NAME())
|
||||
.password(getPASSWORD())
|
||||
.build();
|
||||
|
||||
ProtocolReqDTO<LoginReq> reqInfo = new ProtocolReqDTO<>();
|
||||
|
||||
reqInfo.setVer(ConstValue.Protocol.VERSION);
|
||||
reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode());
|
||||
reqInfo.setTimeStamp(System.currentTimeMillis());
|
||||
reqInfo.setMsgContent(logReq);
|
||||
|
||||
String ret = mockMvc.perform(MockMvcRequestBuilders
|
||||
.post("/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(reqInfo)))
|
||||
.andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(HttpStatus.ok().status()))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
|
||||
|
||||
ProtocolRespDTO<LoginRsp> rspInfo = objectMapper.readValue(ret,
|
||||
new TypeReference<ProtocolRespDTO<LoginRsp>>(){});
|
||||
|
||||
verifyRespProtocol(rspInfo);
|
||||
|
||||
log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
|
||||
|
||||
Assert.assertEquals(getLoginToken(), rspInfo.getMsgContent().getToken());
|
||||
Assert.assertEquals(getUSER_NAME(), rspInfo.getMsgContent().getUserName());
|
||||
Assert.assertEquals(ErrorCode.ERR_OK.getCode(), (long)rspInfo.getMsgContent().getStatus());
|
||||
}
|
||||
|
||||
/**
|
||||
* B 1 logout.
|
||||
*
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
@Test
|
||||
public void b1_logout() throws Exception {
|
||||
LoginReq logReq = LoginReq.builder()
|
||||
.userName(getUSER_NAME())
|
||||
.build();
|
||||
|
||||
ProtocolReqDTO<LoginReq> reqInfo = new ProtocolReqDTO<>();
|
||||
|
||||
reqInfo.setVer(ConstValue.Protocol.VERSION);
|
||||
reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode());
|
||||
reqInfo.setTimeStamp(System.currentTimeMillis());
|
||||
reqInfo.setMsgContent(logReq);
|
||||
|
||||
String ret = mockMvc.perform(MockMvcRequestBuilders
|
||||
.post("/auth/logout")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken())
|
||||
.content(objectMapper.writeValueAsString(reqInfo)))
|
||||
.andDo(print()).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.code").value(HttpStatus.ok().status()))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString();
|
||||
|
||||
ProtocolRespDTO<LoginRsp> rspInfo = objectMapper.readValue(ret,
|
||||
new TypeReference<ProtocolRespDTO<LoginRsp>>(){});
|
||||
|
||||
verifyRespProtocol(rspInfo);
|
||||
|
||||
log.info(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
|
||||
Assert.assertEquals(getUSER_NAME(), rspInfo.getMsgContent().getUserName());
|
||||
Assert.assertEquals(ErrorCode.ERR_OK.getCode(), (long)rspInfo.getMsgContent().getStatus());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue