Skip to content

Commit

Permalink
Apply Google Java Style Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiser-Yang authored and github-actions[bot] committed Sep 25, 2024
1 parent 821025b commit e9f5518
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 74 deletions.
86 changes: 47 additions & 39 deletions src/main/java/edu/cmipt/gcs/controller/RepositoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -75,42 +78,41 @@ 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"),
@ApiResponse(responseCode = "403", description = "Access denied"),
@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)) {
Expand All @@ -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());
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/edu/cmipt/gcs/controller/SshKeyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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))) {
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/edu/cmipt/gcs/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
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;
import org.springframework.beans.factory.annotation.Autowired;
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -117,5 +124,4 @@ public void testDeleteRepositoryValid() throws Exception {
TestConstant.REPOSITORY_NAME = null;
TestConstant.REPOSITORY_SIZE--;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@
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;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Tests for UserController
Expand Down

0 comments on commit e9f5518

Please sign in to comment.