diff --git a/src/main/java/com/dispose/controller/AuthController.java b/src/main/java/com/dispose/controller/AuthController.java index d6933d41..7e69fcc4 100644 --- a/src/main/java/com/dispose/controller/AuthController.java +++ b/src/main/java/com/dispose/controller/AuthController.java @@ -15,7 +15,6 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import java.security.NoSuchAlgorithmException; import javax.annotation.Resource; -import lombok.Builder; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; @@ -52,7 +51,6 @@ public class AuthController { @PostMapping("/login") @ResponseBody @ApiOperation("登录") - @Builder public ProtocolRespDTO userLogin(@RequestBody ProtocolReqDTO mr) throws JsonProcessingException, NoSuchAlgorithmException { ErrorCode err = mr.verifyRequest(); @@ -91,7 +89,6 @@ public class AuthController { @PostMapping("/logout") @ResponseBody @ApiOperation("注销") - @Builder public ProtocolRespDTO userLogout(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { diff --git a/src/main/java/com/dispose/controller/DisposeNodeInfoController.java b/src/main/java/com/dispose/controller/DisposeNodeInfoController.java index 9adbbc37..7b8ccea7 100644 --- a/src/main/java/com/dispose/controller/DisposeNodeInfoController.java +++ b/src/main/java/com/dispose/controller/DisposeNodeInfoController.java @@ -23,7 +23,6 @@ import io.swagger.annotations.ApiOperation; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; -import lombok.Builder; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; @@ -63,7 +62,6 @@ public class DisposeNodeInfoController { @PostMapping("/linkstatus") @ResponseBody @ApiOperation("链接状态") - @Builder public ProtocolRespDTO getLinkStatus(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -123,7 +121,6 @@ public class DisposeNodeInfoController { @PostMapping("/version") @ResponseBody @ApiOperation("处置设备版本") - @Builder public ProtocolRespDTO getVersion(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -186,7 +183,6 @@ public class DisposeNodeInfoController { @PostMapping("/deviceinfo") @ResponseBody @ApiOperation("处置设备信息") - @Builder public ProtocolRespDTO getDeviceInfo(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -261,7 +257,6 @@ public class DisposeNodeInfoController { @PostMapping("/capacity") @ResponseBody @ApiOperation("处置设备处置能力") - @Builder //@JsonView(DisposeCapacity.CapacityView.class) public ProtocolRespDTO getCapacity(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) @@ -324,7 +319,6 @@ public class DisposeNodeInfoController { @PostMapping("/protected_ip") @ResponseBody @ApiOperation("处置设备防护IP") - @Builder //@JsonView(DisposeCapacity.CapacityView.class) public ProtocolRespDTO getProtectedIp(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) @@ -387,7 +381,6 @@ public class DisposeNodeInfoController { @PostMapping("/node_list") @ResponseBody @ApiOperation("处置设备列表") - @Builder //@JsonView(DisposeCapacity.CapacityView.class) public ProtocolRespDTO getDisposeNodeList(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) @@ -456,7 +449,6 @@ public class DisposeNodeInfoController { @PostMapping("/node_details") @ResponseBody @ApiOperation("处置节点详细信息") - @Builder //@JsonView(DisposeCapacity.CapacityView.class) public ProtocolRespDTO getDisposeNodeDetails(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) diff --git a/src/main/java/com/dispose/controller/DisposeNodeManagerController.java b/src/main/java/com/dispose/controller/DisposeNodeManagerController.java index d95c5aa4..6fda3a6b 100644 --- a/src/main/java/com/dispose/controller/DisposeNodeManagerController.java +++ b/src/main/java/com/dispose/controller/DisposeNodeManagerController.java @@ -18,7 +18,6 @@ import io.swagger.annotations.ApiOperation; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; -import lombok.Builder; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; @@ -56,7 +55,6 @@ public class DisposeNodeManagerController { @PutMapping("/device") @ResponseBody @ApiOperation("添加处置能力节点") - @Builder public ProtocolRespDTO addDisposeNode(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -117,7 +115,6 @@ public class DisposeNodeManagerController { @DeleteMapping("/device") @ResponseBody @ApiOperation("删除处置能力节点") - @Builder public ProtocolRespDTO removeDisposeNode(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { diff --git a/src/main/java/com/dispose/controller/DisposeTaskController.java b/src/main/java/com/dispose/controller/DisposeTaskController.java index b0e8086b..e5affc44 100644 --- a/src/main/java/com/dispose/controller/DisposeTaskController.java +++ b/src/main/java/com/dispose/controller/DisposeTaskController.java @@ -12,7 +12,6 @@ import com.dispose.pojo.vo.task.TaskInfoData; import com.fasterxml.jackson.core.JsonProcessingException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import lombok.Builder; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; @@ -43,7 +42,6 @@ public class DisposeTaskController { @PostMapping("/start") @ResponseBody @ApiOperation("启动处置任务") - @Builder public ProtocolRespDTO taskStart(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -74,7 +72,6 @@ public class DisposeTaskController { @PostMapping("/stop") @ResponseBody @ApiOperation("启动处置任务") - @Builder public ProtocolRespDTO taskStop(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -117,7 +114,6 @@ public class DisposeTaskController { @PostMapping("/stop_ip") @ResponseBody @ApiOperation("启动处置任务") - @Builder public ProtocolRespDTO taskStopByIp(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -156,7 +152,6 @@ public class DisposeTaskController { @PostMapping("/stop_node") @ResponseBody @ApiOperation("启动处置任务") - @Builder public ProtocolRespDTO taskStopAllOfDisposeNode(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -174,7 +169,6 @@ public class DisposeTaskController { @PostMapping("/stop_all") @ResponseBody @ApiOperation("启动处置任务") - @Builder public ProtocolRespDTO taskStopAll(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { @@ -191,7 +185,6 @@ public class DisposeTaskController { @PostMapping("/get_node") @ResponseBody @ApiOperation("启动处置任务") - @Builder public ProtocolRespDTO getNodeTask(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { return getAllTask(mr, headers); @@ -208,7 +201,6 @@ public class DisposeTaskController { @PostMapping("/get") @ResponseBody @ApiOperation("启动处置任务") - @Builder public ProtocolRespDTO getAllTask(@RequestBody ProtocolReqDTO mr, @RequestHeader HttpHeaders headers) throws JsonProcessingException { diff --git a/src/test/java/com/dispose/Global/InitTestEnvironment.java b/src/test/java/com/dispose/Global/InitTestEnvironment.java index 14fa9cb8..95d3bc0c 100644 --- a/src/test/java/com/dispose/Global/InitTestEnvironment.java +++ b/src/test/java/com/dispose/Global/InitTestEnvironment.java @@ -1,11 +1,16 @@ package com.dispose.Global; import com.dispose.common.GlobalVar; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.junit.BeforeClass; @Slf4j +@Getter public class InitTestEnvironment { + private final String USER_NAME = "admin"; + private final String PASSWORD = "c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58"; + @BeforeClass public static void initVirtualDevice(){ GlobalVar.USED_VIRTUAL_DISPOSE_MODE = true; diff --git a/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java b/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java index d6741f97..91235711 100644 --- a/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java +++ b/src/test/java/com/dispose/TestSuit/AllDisposePlatformTest.java @@ -5,6 +5,7 @@ import com.dispose.controller.AuthControllerTest; import com.dispose.controller.DeviceNodeInfoControllerTest; import com.dispose.controller.DeviceNodeManagerControllerTest; import com.dispose.dptech.DPTechInterfaceTestCase; +import com.dispose.manager.UserAccountManagerTest; import com.dispose.mapper.DisposeDeviceMapperTest; import com.dispose.mapper.UserAccountMapperTest; import com.dispose.service.UserAccountServiceTest; @@ -16,8 +17,9 @@ import org.junit.runners.Suite; */ @RunWith(Suite.class) @Suite.SuiteClasses({ -// DPTechInterfaceTestCase.class, + DPTechInterfaceTestCase.class, UserAccountMapperTest.class, + UserAccountManagerTest.class, DisposeDeviceMapperTest.class, AuthControllerTest.class, DeviceNodeManagerControllerTest.class, diff --git a/src/test/java/com/dispose/controller/AuthControllerTest.java b/src/test/java/com/dispose/controller/AuthControllerTest.java index cf93b9aa..50e05123 100644 --- a/src/test/java/com/dispose/controller/AuthControllerTest.java +++ b/src/test/java/com/dispose/controller/AuthControllerTest.java @@ -2,10 +2,13 @@ package com.dispose.controller; import com.dispose.Global.InitTestEnvironment; import com.dispose.common.ConstValue; +import com.dispose.common.ErrorCode; import com.dispose.pojo.dto.ProtocolReqDTO; import com.dispose.pojo.dto.ProtocolRespDTO; +import com.dispose.pojo.po.MReturnType; import com.dispose.pojo.vo.auth.LoginReq; import com.dispose.pojo.vo.auth.LoginRsp; +import com.dispose.service.UserAccountService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; @@ -42,35 +45,20 @@ public class AuthControllerTest extends InitTestEnvironment { @Resource private ObjectMapper objectMapper; + @Resource + private UserAccountService userAccountService; + private static String logToken = ""; @Before public void userLogin() throws Exception { - LoginReq logReq = LoginReq.builder() - .userName("admin") - .password("c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58") - .build(); + MReturnType ret = userAccountService.loginService(getUSER_NAME(), getPASSWORD()); - ProtocolReqDTO reqInfo = new ProtocolReqDTO(); - reqInfo.setVer(ConstValue.Protocol.VERSION); - reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); - reqInfo.setTimeStamp(System.currentTimeMillis()); - reqInfo.setMsgContent(objectMapper.writeValueAsString(logReq)); + Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); + logToken = ret.getSecondParam(); - 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(200)) - .andReturn() - .getResponse() - .getContentAsString(); - - ProtocolRespDTO rspInfo = objectMapper.readValue(ret, ProtocolRespDTO.class); - LoginRsp logRsp = objectMapper.readValue(rspInfo.getMsgContent(), LoginRsp.class); - - logToken = logRsp.getToken(); + Assert.assertNotNull(logToken); + Assert.assertNotEquals(logToken.length(), 0); } /** diff --git a/src/test/java/com/dispose/exception/ExceptionTest.java b/src/test/java/com/dispose/exception/ExceptionTest.java new file mode 100644 index 00000000..335ff3cd --- /dev/null +++ b/src/test/java/com/dispose/exception/ExceptionTest.java @@ -0,0 +1,90 @@ +package com.dispose.exception; + +import com.dispose.Global.InitTestEnvironment; +import com.dispose.common.ConstValue; +import com.dispose.mapper.DisposeDeviceMapper; +import com.dispose.pojo.dto.ProtocolReqDTO; +import com.dispose.pojo.dto.ProtocolRespDTO; +import com.dispose.pojo.vo.auth.LoginReq; +import com.dispose.pojo.vo.auth.LoginRsp; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import javax.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.junit.FixMethodOrder; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +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 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; + +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +@Slf4j +public class ExceptionTest{ + @Resource + private MockMvc mockMvc; + + @Resource + private ObjectMapper objectMapper; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test(expected = Throwable.class) + public void t1_inputException() throws Exception { + expectedEx.expect(Throwable.class); + expectedEx.expectMessage("Expected exception: java.lang.Throwable"); + + LoginReq logReq = null; + + ProtocolReqDTO reqInfo = new ProtocolReqDTO(); + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent(objectMapper.writeValueAsString(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(521)) + .andReturn() + .getResponse() + .getContentAsString(); + } + + @Test(expected = Exception.class) + public void t2_globalException() throws Exception { + expectedEx.expect(Exception.class); + + ProtocolReqDTO reqInfo = new ProtocolReqDTO(); + reqInfo.setVer(ConstValue.Protocol.VERSION); + reqInfo.setCryptoType(ConstValue.Protocol.CRYPTO_NONE); + reqInfo.setTimeStamp(System.currentTimeMillis()); + reqInfo.setMsgContent("{}"); + + mockMvc.perform(MockMvcRequestBuilders + .post("/information/protected_ip") + .contentType(MediaType.APPLICATION_JSON) + .content("{}")) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + } +} \ No newline at end of file diff --git a/src/test/java/com/dispose/manager/UserAccountManagerTest.java b/src/test/java/com/dispose/manager/UserAccountManagerTest.java new file mode 100644 index 00000000..c7028cb0 --- /dev/null +++ b/src/test/java/com/dispose/manager/UserAccountManagerTest.java @@ -0,0 +1,99 @@ +package com.dispose.manager; + +import com.dispose.Global.InitTestEnvironment; +import com.dispose.common.ErrorCode; +import com.dispose.pojo.po.MReturnType; +import com.dispose.service.UserAccountService; +import com.fasterxml.jackson.core.JsonProcessingException; +import java.security.NoSuchAlgorithmException; +import javax.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@Slf4j +@SpringBootTest +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class UserAccountManagerTest extends InitTestEnvironment { + @Resource + private UserAccountCacheManager userAccountCacheManager; + + @Resource + private UserAccountService userAccountService; + + private static String userToken; + + @Before + public void userLogin() throws NoSuchAlgorithmException { + MReturnType ret = userAccountService.loginService(getUSER_NAME(), getPASSWORD()); + + Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); + userToken = ret.getSecondParam(); + + Assert.assertNotNull(userToken); + Assert.assertNotEquals(userToken.length(), 0); + } + + @Test + public void t1_permissionTest() { + Assert.assertEquals(userAccountCacheManager.verifyPermission(userToken), ErrorCode.ERR_OK); + } + + @Test + public void t2_getUsernameByTokenTest() { + Assert.assertEquals(userAccountCacheManager.getUsernameByToken(userToken), getUSER_NAME()); + } + + @Test + public void t3_verifyTokenTest() { + Assert.assertEquals(userAccountCacheManager.verifyToken(userToken), ErrorCode.ERR_OK); + Assert.assertEquals(userAccountCacheManager.verifyToken(userToken + "123"), ErrorCode.ERR_LOGOUT); + } + + @Test + public void t4_verifyUserLoginTest() { + Assert.assertEquals(userAccountCacheManager.verifyUserLogin(getUSER_NAME(), userToken), ErrorCode.ERR_OK); + } + + @Test + public void t5_getCacheUser() throws JsonProcessingException { + String userInfo = userAccountCacheManager.getCacheUser(); + + Assert.assertNotNull(userInfo); + Assert.assertNotEquals(userInfo.length(), 0); + } + + @Test + public void t6_getUsrPwdErrTimesTest() { + Assert.assertEquals(userAccountCacheManager.getUsrPwdErrTimes(getUSER_NAME()), 0); + } + + @Test + public void t6_setUsrPwdErrTimesTest() { + for(int i = 0; i < 6; i++) { + userAccountCacheManager.setUserPwdErrTimes(getUSER_NAME(), i); + Assert.assertEquals(userAccountCacheManager.getUsrPwdErrTimes(getUSER_NAME()), i); + } + + userAccountCacheManager.setUserPwdErrTimes(getUSER_NAME(), 0); + Assert.assertEquals(userAccountCacheManager.getUsrPwdErrTimes(getUSER_NAME()), 0); + } + + @Test + public void t7_getUserTokenTest() throws NoSuchAlgorithmException { + Assert.assertEquals(userAccountCacheManager.getUserToken(getUSER_NAME()), userToken); + } + + @Test + public void t99_cleanUserTokenTest() { + userAccountCacheManager.cleanUserToken(getUSER_NAME()); + Assert.assertEquals(userAccountCacheManager.verifyToken(userToken), ErrorCode.ERR_LOGOUT); + } +}