diff --git a/RollCallServer/src/main/java/cc/aspark/domain/dto/UserLoginDTO.java b/RollCallServer/src/main/java/cc/aspark/domain/dto/UserLoginDTO.java index c13da36..5dd06f5 100644 --- a/RollCallServer/src/main/java/cc/aspark/domain/dto/UserLoginDTO.java +++ b/RollCallServer/src/main/java/cc/aspark/domain/dto/UserLoginDTO.java @@ -2,9 +2,11 @@ package cc.aspark.domain.dto; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; +import lombok.Builder; import lombok.Data; @Data +@Builder public class UserLoginDTO { @Min(3) diff --git a/RollCallServer/src/main/java/cc/aspark/domain/dto/UserRegisterDTO.java b/RollCallServer/src/main/java/cc/aspark/domain/dto/UserRegisterDTO.java index a8abb00..54d6c46 100644 --- a/RollCallServer/src/main/java/cc/aspark/domain/dto/UserRegisterDTO.java +++ b/RollCallServer/src/main/java/cc/aspark/domain/dto/UserRegisterDTO.java @@ -2,9 +2,11 @@ package cc.aspark.domain.dto; import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; +import lombok.Builder; import lombok.Data; @Data +@Builder public class UserRegisterDTO { @Min(3) @Max(20) diff --git a/RollCallServer/src/main/java/cc/aspark/domain/entity/Admin.java b/RollCallServer/src/main/java/cc/aspark/domain/entity/Admin.java index ead30d6..7390678 100644 --- a/RollCallServer/src/main/java/cc/aspark/domain/entity/Admin.java +++ b/RollCallServer/src/main/java/cc/aspark/domain/entity/Admin.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @@ -15,6 +16,7 @@ import java.time.LocalDateTime; * @TableName tb_admin */ @Data +@Builder @NoArgsConstructor @AllArgsConstructor @Tag(name = "管理员实体类") diff --git a/RollCallServer/src/main/java/cc/aspark/domain/entity/Student.java b/RollCallServer/src/main/java/cc/aspark/domain/entity/Student.java index 6b3daf1..2d67cd4 100644 --- a/RollCallServer/src/main/java/cc/aspark/domain/entity/Student.java +++ b/RollCallServer/src/main/java/cc/aspark/domain/entity/Student.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @@ -15,6 +16,7 @@ import java.time.LocalDateTime; * @TableName tb_stu */ @Data +@Builder @NoArgsConstructor @AllArgsConstructor @Tag(name = "学生实体类") diff --git a/RollCallServer/src/main/java/cc/aspark/domain/vo/UserLoginVO.java b/RollCallServer/src/main/java/cc/aspark/domain/vo/UserLoginVO.java index 5489bf0..434bb8c 100644 --- a/RollCallServer/src/main/java/cc/aspark/domain/vo/UserLoginVO.java +++ b/RollCallServer/src/main/java/cc/aspark/domain/vo/UserLoginVO.java @@ -1,8 +1,10 @@ package cc.aspark.domain.vo; +import lombok.Builder; import lombok.Data; @Data +@Builder public class UserLoginVO { private Integer id; diff --git a/RollCallServer/src/test/java/cc/aspark/controller/AdminControllerTest.java b/RollCallServer/src/test/java/cc/aspark/controller/AdminControllerTest.java new file mode 100644 index 0000000..6a183ba --- /dev/null +++ b/RollCallServer/src/test/java/cc/aspark/controller/AdminControllerTest.java @@ -0,0 +1,87 @@ +package cc.aspark.controller; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + + +import cc.aspark.domain.dto.UserLoginDTO; +import cc.aspark.domain.dto.UserRegisterDTO; +import cc.aspark.domain.vo.UserLoginVO; +import cc.aspark.service.AdminService; +import net.bytebuddy.implementation.bytecode.Throw; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@SpringBootTest +@AutoConfigureMockMvc +public class AdminControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private AdminService adminService; + + @BeforeEach + public void setup() { + UserLoginDTO userLoginDTO = UserLoginDTO.builder() + .username("test") + .password("abcd1234") + .build(); + + UserRegisterDTO userRegisterDTO = UserRegisterDTO.builder() + .username("test") + .password("abcd1234") + .build(); + + UserLoginVO userLoginVO = UserLoginVO.builder() + .id(1) + .username("test") + .token("test") + .build(); + + Mockito.when(adminService.login(userLoginDTO)).thenReturn(userLoginVO); + Mockito.doNothing().when(adminService).register(userRegisterDTO); + + } + + + @Test + public void testLogin() throws Exception { + ResultActions resultActions = mockMvc.perform(post("/admin/login") + .contentType("application/json") + .content("{\"username\": \"test\", \"password\": \"abcd1234\"}")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(1)) + .andExpect(jsonPath("$.msg").value("success")) + .andExpect(jsonPath("$.data.id").value(1)) + .andExpect(jsonPath("$.data.username").value("test")) + .andExpect(jsonPath("$.data.token").value("test")) + .andDo(print()); + } + + @Test + public void testRegister() throws Exception { + ResultActions resultActions = mockMvc.perform(post("/admin/register") + .contentType("application/json") + .content("{\"username\": \"test\", \"password\": \"abcd1234\"}")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(1)) + .andExpect(jsonPath("$.msg").value("success")) + .andDo(print()); + } +}