From e9f5518981457854f592ed724efe69a1014d25bf Mon Sep 17 00:00:00 2001 From: Kaiser-Yang <Kaiser-Yang@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:20:27 +0000 Subject: [PATCH] Apply Google Java Style Format --- .../gcs/controller/RepositoryController.java | 86 ++++++++++--------- .../gcs/controller/SshKeyController.java | 10 ++- .../cmipt/gcs/service/UserServiceImpl.java | 9 +- .../controller/RepositoryControllerTest.java | 56 ++++++------ .../gcs/controller/UserControllerTest.java | 3 - 5 files changed, 90 insertions(+), 74 deletions(-) diff --git a/src/main/java/edu/cmipt/gcs/controller/RepositoryController.java b/src/main/java/edu/cmipt/gcs/controller/RepositoryController.java index b1d94f4..c82c7a4 100644 --- a/src/main/java/edu/cmipt/gcs/controller/RepositoryController.java +++ b/src/main/java/edu/cmipt/gcs/controller/RepositoryController.java @@ -13,6 +13,7 @@ import edu.cmipt.gcs.util.JwtUtil; import edu.cmipt.gcs.validation.group.CreateGroup; import edu.cmipt.gcs.validation.group.UpdateGroup; + import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; @@ -58,7 +59,9 @@ public void createRepository( @Validated(CreateGroup.class) @RequestBody RepositoryDTO repository, @RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken) { if (repository.isPrivate() != null && repository.isPrivate()) { - throw new GenericException(ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED, "private repository is not implemented"); + throw new GenericException( + ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED, + "private repository is not implemented"); } String userId = JwtUtil.getId(accessToken); RepositoryPO repositoryPO = new RepositoryPO(repository, userId); @@ -75,25 +78,22 @@ public void createRepository( @DeleteMapping(ApiPathConstant.REPOSITORY_DELETE_REPOSITORY_API_PATH) @Operation( - summary = "Delete a repository", - description = "Delete a repository with the given id", - tags = {"Repository", "Delete Method"} - ) + summary = "Delete a repository", + description = "Delete a repository with the given id", + tags = {"Repository", "Delete Method"}) @Parameters({ @Parameter( - name = HeaderParameter.ACCESS_TOKEN, - description = "Access token", - required = true, - in = ParameterIn.HEADER, - schema = @Schema(implementation = String.class) - ), + name = HeaderParameter.ACCESS_TOKEN, + description = "Access token", + required = true, + in = ParameterIn.HEADER, + schema = @Schema(implementation = String.class)), @Parameter( - name = "id", - description = "Repository id", - required = true, - in = ParameterIn.QUERY, - schema = @Schema(implementation = Long.class) - ) + name = "id", + description = "Repository id", + required = true, + in = ParameterIn.QUERY, + schema = @Schema(implementation = Long.class)) }) @ApiResponses({ @ApiResponse(responseCode = "200", description = "Repository deleted successfully"), @@ -101,16 +101,18 @@ public void createRepository( @ApiResponse(responseCode = "404", description = "Repository not found") }) public void deleteRepository( - @RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken, - @RequestParam("id") Long id - ) { + @RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken, + @RequestParam("id") Long id) { var repository = repositoryService.getById(id); if (repository == null) { throw new GenericException(ErrorCodeEnum.REPOSITORY_NOT_FOUND, id); } String userId = JwtUtil.getId(accessToken); if (!userId.equals(repository.getUserId().toString())) { - logger.info("User[{}] tried to delete repository of user[{}]", userId, repository.getUserId()); + logger.info( + "User[{}] tried to delete repository of user[{}]", + userId, + repository.getUserId()); throw new GenericException(ErrorCodeEnum.ACCESS_DENIED); } if (!repositoryService.removeById(id)) { @@ -120,27 +122,26 @@ public void deleteRepository( @PostMapping(ApiPathConstant.REPOSITORY_UPDATE_REPOSITORY_API_PATH) @Operation( - summary = "Update a repository", - description = "Update a repository with the given information", - tags = {"Repository", "Post Method"} - ) + summary = "Update a repository", + description = "Update a repository with the given information", + tags = {"Repository", "Post Method"}) @Parameter( - name = HeaderParameter.ACCESS_TOKEN, - description = "Access token", - required = true, - in = ParameterIn.HEADER, - schema = @Schema(implementation = String.class) - ) + name = HeaderParameter.ACCESS_TOKEN, + description = "Access token", + required = true, + in = ParameterIn.HEADER, + schema = @Schema(implementation = String.class)) @ApiResponses({ @ApiResponse(responseCode = "200", description = "Repository updated successfully"), @ApiResponse(responseCode = "403", description = "Access denied"), @ApiResponse(responseCode = "404", description = "Repository not found"), - @ApiResponse(responseCode = "501", description = "Update repository name is not implemented") + @ApiResponse( + responseCode = "501", + description = "Update repository name is not implemented") }) public ResponseEntity<RepositoryVO> updateRepository( - @Validated(UpdateGroup.class) @RequestBody RepositoryDTO repository, - @RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken - ) { + @Validated(UpdateGroup.class) @RequestBody RepositoryDTO repository, + @RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken) { Long id = null; try { id = Long.valueOf(repository.id()); @@ -154,12 +155,19 @@ public ResponseEntity<RepositoryVO> updateRepository( } String userId = JwtUtil.getId(accessToken); if (!userId.equals(repositoryPO.getUserId().toString())) { - logger.info("User[{}] tried to update repository of user[{}]", userId, repositoryPO.getUserId()); + logger.info( + "User[{}] tried to update repository of user[{}]", + userId, + repositoryPO.getUserId()); throw new GenericException(ErrorCodeEnum.ACCESS_DENIED); } - if (repository.repositoryName() != null && - !repository.repositoryName().equals(repositoryService.getById(id).getRepositoryName())) { - throw new GenericException(ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED, "update repository name is not implemented"); + if (repository.repositoryName() != null + && !repository + .repositoryName() + .equals(repositoryService.getById(id).getRepositoryName())) { + throw new GenericException( + ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED, + "update repository name is not implemented"); } if (!repositoryService.updateById(new RepositoryPO(repository))) { throw new GenericException(ErrorCodeEnum.REPOSITORY_UPDATE_FAILED, repository); diff --git a/src/main/java/edu/cmipt/gcs/controller/SshKeyController.java b/src/main/java/edu/cmipt/gcs/controller/SshKeyController.java index 020046f..2916660 100644 --- a/src/main/java/edu/cmipt/gcs/controller/SshKeyController.java +++ b/src/main/java/edu/cmipt/gcs/controller/SshKeyController.java @@ -107,7 +107,10 @@ public void deleteSshKey( } String idInToken = JwtUtil.getId(accessToken); if (!idInToken.equals(sshKeyPO.getUserId().toString())) { - logger.info("User[{}] tried to delete SSH key of user[{}]", idInToken, sshKeyPO.getUserId()); + logger.info( + "User[{}] tried to delete SSH key of user[{}]", + idInToken, + sshKeyPO.getUserId()); throw new GenericException(ErrorCodeEnum.ACCESS_DENIED); } if (!sshKeyService.removeById(id)) { @@ -151,7 +154,10 @@ public ResponseEntity<SshKeyVO> updateSshKey( } String idInToken = JwtUtil.getId(accessToken); if (!idInToken.equals(sshKeyPO.getUserId().toString())) { - logger.info("User[{}] tried to update SSH key of user[{}]", idInToken, sshKeyPO.getUserId()); + logger.info( + "User[{}] tried to update SSH key of user[{}]", + idInToken, + sshKeyPO.getUserId()); throw new GenericException(ErrorCodeEnum.ACCESS_DENIED); } if (!sshKeyService.updateById(new SshKeyPO(sshKeyDTO))) { diff --git a/src/main/java/edu/cmipt/gcs/service/UserServiceImpl.java b/src/main/java/edu/cmipt/gcs/service/UserServiceImpl.java index 95a30f3..da1f473 100644 --- a/src/main/java/edu/cmipt/gcs/service/UserServiceImpl.java +++ b/src/main/java/edu/cmipt/gcs/service/UserServiceImpl.java @@ -4,10 +4,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import edu.cmipt.gcs.dao.UserMapper; -import edu.cmipt.gcs.pojo.user.UserPO; import edu.cmipt.gcs.pojo.ssh.SshKeyPO; - -import java.io.Serializable; +import edu.cmipt.gcs.pojo.user.UserPO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,12 +13,13 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.Serializable; + @Service public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService { private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); - @Autowired - SshKeyService sshKeyService; + @Autowired SshKeyService sshKeyService; @Override @Transactional diff --git a/src/test/java/edu/cmipt/gcs/controller/RepositoryControllerTest.java b/src/test/java/edu/cmipt/gcs/controller/RepositoryControllerTest.java index 359d88d..1c73609 100644 --- a/src/test/java/edu/cmipt/gcs/controller/RepositoryControllerTest.java +++ b/src/test/java/edu/cmipt/gcs/controller/RepositoryControllerTest.java @@ -6,9 +6,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import edu.cmipt.gcs.constant.ApiPathConstant; import edu.cmipt.gcs.constant.HeaderParameter; import edu.cmipt.gcs.constant.TestConstant; @@ -25,6 +22,9 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Tests for RepositoryController * @@ -55,17 +55,22 @@ public void testCreateRepositoryValid() throws Exception { .formatted(repositoryName))) .andExpect(status().isOk()); } - var content = + var content = mvc.perform( - get(ApiPathConstant.USER_PAGE_USER_REPOSITORY_API_PATH) - .header(HeaderParameter.ACCESS_TOKEN, TestConstant.ACCESS_TOKEN) - .param("id", TestConstant.ID) - .param("page", "1") - .param("size", TestConstant.REPOSITORY_SIZE.toString())) - .andExpectAll( - status().isOk(), - jsonPath("$").isArray(), - jsonPath("$.length()").value(TestConstant.REPOSITORY_SIZE)).andReturn().getResponse().getContentAsString(); + get(ApiPathConstant.USER_PAGE_USER_REPOSITORY_API_PATH) + .header( + HeaderParameter.ACCESS_TOKEN, + TestConstant.ACCESS_TOKEN) + .param("id", TestConstant.ID) + .param("page", "1") + .param("size", TestConstant.REPOSITORY_SIZE.toString())) + .andExpectAll( + status().isOk(), + jsonPath("$").isArray(), + jsonPath("$.length()").value(TestConstant.REPOSITORY_SIZE)) + .andReturn() + .getResponse() + .getContentAsString(); content = JsonParserFactory.getJsonParser().parseList(content).get(0).toString(); Matcher matcher = Pattern.compile("id=(\\d+),").matcher(content); matcher.find(); @@ -91,18 +96,20 @@ public void testUpdateRepositoryValid() throws Exception { "id": "%s", "repositoryDescription": "%s" } - """.formatted(TestConstant.REPOSITORY_ID, newDescription))) + """ + .formatted( + TestConstant.REPOSITORY_ID, + newDescription))) .andExpectAll( - status().isOk(), - jsonPath("$.id").value(TestConstant.REPOSITORY_ID), - jsonPath("$.repositoryName").value(TestConstant.REPOSITORY_NAME), - jsonPath("$.repositoryDescription").value(newDescription), - jsonPath("$.isPrivate").value(false), - jsonPath("$.userId").value(TestConstant.ID), - jsonPath("$.star").value(0), - jsonPath("$.fork").value(0), - jsonPath("$.watcher").value(0) - ); + status().isOk(), + jsonPath("$.id").value(TestConstant.REPOSITORY_ID), + jsonPath("$.repositoryName").value(TestConstant.REPOSITORY_NAME), + jsonPath("$.repositoryDescription").value(newDescription), + jsonPath("$.isPrivate").value(false), + jsonPath("$.userId").value(TestConstant.ID), + jsonPath("$.star").value(0), + jsonPath("$.fork").value(0), + jsonPath("$.watcher").value(0)); } @Test @@ -117,5 +124,4 @@ public void testDeleteRepositoryValid() throws Exception { TestConstant.REPOSITORY_NAME = null; TestConstant.REPOSITORY_SIZE--; } - } diff --git a/src/test/java/edu/cmipt/gcs/controller/UserControllerTest.java b/src/test/java/edu/cmipt/gcs/controller/UserControllerTest.java index 77d0d9f..1d821cb 100644 --- a/src/test/java/edu/cmipt/gcs/controller/UserControllerTest.java +++ b/src/test/java/edu/cmipt/gcs/controller/UserControllerTest.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.json.JsonParserFactory; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.Ordered; @@ -28,8 +27,6 @@ import org.springframework.test.web.servlet.MockMvc; import java.util.Date; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Tests for UserController