diff --git a/src/main/java/pl/materus/ghrepo/controller/GitHubController.java b/src/main/java/pl/materus/ghrepo/controller/GitHubController.java index dce0fc9..856fe2c 100644 --- a/src/main/java/pl/materus/ghrepo/controller/GitHubController.java +++ b/src/main/java/pl/materus/ghrepo/controller/GitHubController.java @@ -32,51 +32,51 @@ public class GitHubController { @GetMapping("/{username}") public ResponseEntity> getUserRepos( @RequestHeader(name = HttpHeaders.ACCEPT) String acceptHeader, @PathVariable String username) { - + if (!acceptHeader.contains("application/json")) { throw new WrongHeaderException("Request does not contain the 'application/json' header."); } - HttpHeaders httpHeaders= new HttpHeaders(); + HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); return ResponseEntity.ok().headers(httpHeaders).body(gitHubService.getUserRepos(username)); } @ExceptionHandler(WebClientResponseException.class) public ResponseEntity handleResponseException(WebClientResponseException ex) { - ResponseErrorModel responseErrorModel = new ResponseErrorModel(); + HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; - HttpHeaders httpHeaders= new HttpHeaders(); + HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); - responseErrorModel.setStatus(ex.getStatusCode().value()); - switch (responseErrorModel.getStatus()) { + String message; + Integer statusCode = ex.getStatusCode().value(); + switch (statusCode) { case 404: - responseErrorModel.setMessage("User not found"); + message = "User not found"; status = HttpStatus.NOT_FOUND; break; case 403: - responseErrorModel.setMessage("Github API limit"); + message = "Github API limit"; status = HttpStatus.FORBIDDEN; break; default: - responseErrorModel.setMessage("Unknown github webclient error"); + message = "Unknown github webclient error"; break; } + ResponseErrorModel responseErrorModel = new ResponseErrorModel(statusCode,message); return ResponseEntity.status(status).headers(httpHeaders).body(responseErrorModel); } @ExceptionHandler(WrongHeaderException.class) public ResponseEntity handleResponseException(WrongHeaderException ex) { - ResponseErrorModel responseErrorModel = new ResponseErrorModel(); - responseErrorModel.setStatus(HttpStatus.BAD_REQUEST.value()); - responseErrorModel.setMessage(ex.getMessage()); - - HttpHeaders httpHeaders= new HttpHeaders(); + ResponseErrorModel responseErrorModel = new ResponseErrorModel(HttpStatus.BAD_REQUEST.value(),ex.getMessage()); + + HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel); diff --git a/src/main/java/pl/materus/ghrepo/model/GitHubBranchCommitModel.java b/src/main/java/pl/materus/ghrepo/model/GitHubBranchCommitModel.java index dce44a5..d172bd4 100644 --- a/src/main/java/pl/materus/ghrepo/model/GitHubBranchCommitModel.java +++ b/src/main/java/pl/materus/ghrepo/model/GitHubBranchCommitModel.java @@ -1,13 +1,4 @@ package pl.materus.ghrepo.model; -public class GitHubBranchCommitModel { - String sha; - - public String getSha() { - return sha; - } - - public void setSha(String sha) { - this.sha = sha; - } +public record GitHubBranchCommitModel(String sha) { } diff --git a/src/main/java/pl/materus/ghrepo/model/GitHubBranchModel.java b/src/main/java/pl/materus/ghrepo/model/GitHubBranchModel.java index 3a2c27c..e3a5328 100644 --- a/src/main/java/pl/materus/ghrepo/model/GitHubBranchModel.java +++ b/src/main/java/pl/materus/ghrepo/model/GitHubBranchModel.java @@ -1,24 +1,6 @@ package pl.materus.ghrepo.model; - -public class GitHubBranchModel { - String name; - - GitHubBranchCommitModel commit; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public GitHubBranchCommitModel getCommit() { - return commit; - } - - public void setCommit(GitHubBranchCommitModel commit) { - this.commit = commit; - } +public record GitHubBranchModel( + String name, + GitHubBranchCommitModel commit) { } diff --git a/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryModel.java b/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryModel.java index 6398c39..8f08ecc 100644 --- a/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryModel.java +++ b/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryModel.java @@ -1,33 +1,7 @@ package pl.materus.ghrepo.model; -public class GitHubRepositoryModel { - String name; - - GitHubRepositoryOwnerModel owner; - - Boolean fork; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public GitHubRepositoryOwnerModel getOwner() { - return owner; - } - - public void setOwner(GitHubRepositoryOwnerModel owner) { - this.owner = owner; - } - - public Boolean getFork() { - return fork; - } - - public void setFork(Boolean fork) { - this.fork = fork; - } +public record GitHubRepositoryModel( + String name, + GitHubRepositoryOwnerModel owner, + Boolean fork) { } diff --git a/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryOwnerModel.java b/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryOwnerModel.java index af9ee62..6231ef6 100644 --- a/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryOwnerModel.java +++ b/src/main/java/pl/materus/ghrepo/model/GitHubRepositoryOwnerModel.java @@ -1,14 +1,4 @@ package pl.materus.ghrepo.model; -public class GitHubRepositoryOwnerModel { - String login; - - public String getLogin() { - return login; - } - - public void setLogin(String login) { - this.login = login; - } - +public record GitHubRepositoryOwnerModel(String login) { } diff --git a/src/main/java/pl/materus/ghrepo/model/ResponseBranchModel.java b/src/main/java/pl/materus/ghrepo/model/ResponseBranchModel.java index 3153400..e8bc115 100644 --- a/src/main/java/pl/materus/ghrepo/model/ResponseBranchModel.java +++ b/src/main/java/pl/materus/ghrepo/model/ResponseBranchModel.java @@ -1,20 +1,11 @@ package pl.materus.ghrepo.model; -public class ResponseBranchModel{ - String name; - String sha; - - - public String getName() { - return name; - } - public void setName(String name) { +public record ResponseBranchModel( + String name, + String sha) { + public ResponseBranchModel(String name, String sha) + { this.name = name; - } - public String getSha() { - return sha; - } - public void setSha(String sha) { this.sha = sha; } } diff --git a/src/main/java/pl/materus/ghrepo/model/ResponseErrorModel.java b/src/main/java/pl/materus/ghrepo/model/ResponseErrorModel.java index b454bf2..59edaad 100644 --- a/src/main/java/pl/materus/ghrepo/model/ResponseErrorModel.java +++ b/src/main/java/pl/materus/ghrepo/model/ResponseErrorModel.java @@ -1,22 +1,11 @@ package pl.materus.ghrepo.model; -public class ResponseErrorModel{ - Integer status; - String message; +public record ResponseErrorModel( + Integer status, + String message) { - public Integer getStatus() { - return status; - } - - public void setStatus(Integer status) { + public ResponseErrorModel(Integer status, String message) { this.status = status; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { this.message = message; } } diff --git a/src/main/java/pl/materus/ghrepo/model/ResponseRepositoryModel.java b/src/main/java/pl/materus/ghrepo/model/ResponseRepositoryModel.java index 3742888..ee7dbaf 100644 --- a/src/main/java/pl/materus/ghrepo/model/ResponseRepositoryModel.java +++ b/src/main/java/pl/materus/ghrepo/model/ResponseRepositoryModel.java @@ -1,28 +1,9 @@ package pl.materus.ghrepo.model; + import java.util.List; -public class ResponseRepositoryModel { - - String name; - String owner; - List branches; - - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public String getOwner() { - return owner; - } - public void setOwner(String owner) { - this.owner = owner; - } - public List getBranches() { - return branches; - } - public void setBranches(List branches) { - this.branches = branches; - } +public record ResponseRepositoryModel( + String name, + String owner, + List branches) { } diff --git a/src/main/java/pl/materus/ghrepo/service/GitHubService.java b/src/main/java/pl/materus/ghrepo/service/GitHubService.java index 5bc52ad..049a63f 100644 --- a/src/main/java/pl/materus/ghrepo/service/GitHubService.java +++ b/src/main/java/pl/materus/ghrepo/service/GitHubService.java @@ -28,13 +28,12 @@ public class GitHubService { .uri("/users/{username}/repos", username) .retrieve() .bodyToFlux(GitHubRepositoryModel.class) - .filter(repo -> !repo.getFork()) + .filter(repo -> !repo.fork()) .flatMap(this::createResponse) - .flatMap(repo -> getRepoBranches(repo.getOwner(), repo.getName()) + .flatMap(loginName -> getRepoBranches(loginName[0], loginName[1]) .collectList() .map(branches -> { - repo.setBranches(branches); - return repo; + return new ResponseRepositoryModel(loginName[1], loginName[0], branches); })) .subscribeOn(Schedulers.boundedElastic()); @@ -49,18 +48,14 @@ public class GitHubService { } - private Mono createResponse(GitHubRepositoryModel repository) { - ResponseRepositoryModel responseRepositoryModel = new ResponseRepositoryModel(); - responseRepositoryModel.setName(repository.getName()); - responseRepositoryModel.setOwner(repository.getOwner().getLogin()); - return Mono.just(responseRepositoryModel); + private Mono createResponse(GitHubRepositoryModel repository) { + String[] ret = { repository.owner().login(),repository.name() }; + return Mono.just(ret); } private Mono createResponseBranch(GitHubBranchModel branch) { - ResponseBranchModel responseBranchModel = new ResponseBranchModel(); - responseBranchModel.setName(branch.getName()); - responseBranchModel.setSha(branch.getCommit().getSha()); + ResponseBranchModel responseBranchModel = new ResponseBranchModel(branch.name(), branch.commit().sha()); return Mono.just(responseBranchModel); } -} \ No newline at end of file +} diff --git a/src/test/java/pl/materus/ghrepo/controller/GitHubControllerTest.java b/src/test/java/pl/materus/ghrepo/controller/GitHubControllerTest.java index 393c094..d3a44e0 100644 --- a/src/test/java/pl/materus/ghrepo/controller/GitHubControllerTest.java +++ b/src/test/java/pl/materus/ghrepo/controller/GitHubControllerTest.java @@ -49,8 +49,8 @@ public class GitHubControllerTest { var response = githubController.handleResponseException(e); assertNotNull(response.getBody()); - assertEquals(404, response.getBody().getStatus()); - assertEquals("User not found", response.getBody().getMessage()); + assertEquals(404, response.getBody().status()); + assertEquals("User not found", response.getBody().message()); } @SuppressWarnings("null") @@ -61,8 +61,8 @@ public class GitHubControllerTest { var response = githubController.handleResponseException(e); assertNotNull(response.getBody()); - assertEquals(400, response.getBody().getStatus()); - assertEquals("Request does not contain the 'application/json' header.", response.getBody().getMessage()); + assertEquals(400, response.getBody().status()); + assertEquals("Request does not contain the 'application/json' header.", response.getBody().message()); } @SuppressWarnings("null") @@ -82,7 +82,7 @@ public class GitHubControllerTest { var response = githubController.handleResponseException(e); assertNotNull(response.getBody()); - assertEquals(403, response.getBody().getStatus()); - assertEquals("Github API limit", response.getBody().getMessage()); + assertEquals(403, response.getBody().status()); + assertEquals("Github API limit", response.getBody().message()); } } diff --git a/src/test/java/pl/materus/ghrepo/service/GitHubServiceTest.java b/src/test/java/pl/materus/ghrepo/service/GitHubServiceTest.java index ece6f9c..8305ff1 100644 --- a/src/test/java/pl/materus/ghrepo/service/GitHubServiceTest.java +++ b/src/test/java/pl/materus/ghrepo/service/GitHubServiceTest.java @@ -74,27 +74,27 @@ public class GitHubServiceTest { var response = gitHubService.getUserRepos("materusPL").collectList().block(); response.sort((repo1, repo2) -> { - return repo1.getName().compareTo(repo2.getName()); + return repo1.name().compareTo(repo2.name()); }); - assertEquals("Nixerus", response.get(0).getName()); - assertEquals("SNOL", response.get(1).getName()); - assertEquals("materusPL", response.get(2).getName()); - assertEquals("nixos-config", response.get(3).getName()); + assertEquals("Nixerus", response.get(0).name()); + assertEquals("SNOL", response.get(1).name()); + assertEquals("materusPL", response.get(2).name()); + assertEquals("nixos-config", response.get(3).name()); String owner = "materusPL"; - assertEquals(owner, response.get(0).getOwner()); - assertEquals(owner, response.get(1).getOwner()); - assertEquals(owner, response.get(2).getOwner()); - assertEquals(owner, response.get(3).getOwner()); + assertEquals(owner, response.get(0).owner()); + assertEquals(owner, response.get(1).owner()); + assertEquals(owner, response.get(2).owner()); + assertEquals(owner, response.get(3).owner()); - assertEquals("master", response.get(2).getBranches().get(0).getName()); - assertEquals("fd6867d8963147ba40d3df428045aec82f14dbe3", response.get(2).getBranches().get(0).getSha()); + assertEquals("master", response.get(2).branches().get(0).name()); + assertEquals("fd6867d8963147ba40d3df428045aec82f14dbe3", response.get(2).branches().get(0).sha()); - assertEquals(3, response.get(0).getBranches().size()); - assertEquals(2, response.get(3).getBranches().size()); - assertEquals(1, response.get(2).getBranches().size()); + assertEquals(3, response.get(0).branches().size()); + assertEquals(2, response.get(3).branches().size()); + assertEquals(1, response.get(2).branches().size()); }