From 33e439a9b7d4dade07b83791e3560ed9538d5dbe Mon Sep 17 00:00:00 2001 From: HuangXin Date: Mon, 27 Jul 2020 14:14:54 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E3=80=81=E6=B3=A8=E9=94=80=E5=8A=9F=E8=83=BD=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/controller/AuthControllerTest.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/test/java/com/dispose/test/controller/AuthControllerTest.java diff --git a/src/test/java/com/dispose/test/controller/AuthControllerTest.java b/src/test/java/com/dispose/test/controller/AuthControllerTest.java new file mode 100644 index 00000000..b02d5385 --- /dev/null +++ b/src/test/java/com/dispose/test/controller/AuthControllerTest.java @@ -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 + */ +@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 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 rspInfo = objectMapper.readValue(ret, + new TypeReference>(){}); + + 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 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 rspInfo = objectMapper.readValue(ret, + new TypeReference>(){}); + + 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()); + } +}