change models from class to records
This commit is contained in:
		
							parent
							
								
									d15cf46ac3
								
							
						
					
					
						commit
						d1c233068c
					
				| @ -36,47 +36,47 @@ public class GitHubController { | ||||
|         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<ResponseErrorModel> 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<ResponseErrorModel> handleResponseException(WrongHeaderException ex) { | ||||
|         ResponseErrorModel responseErrorModel = new ResponseErrorModel(); | ||||
|         responseErrorModel.setStatus(HttpStatus.BAD_REQUEST.value()); | ||||
|         responseErrorModel.setMessage(ex.getMessage()); | ||||
|         ResponseErrorModel responseErrorModel = new ResponseErrorModel(HttpStatus.BAD_REQUEST.value(),ex.getMessage()); | ||||
| 
 | ||||
|         HttpHeaders httpHeaders= new HttpHeaders(); | ||||
|         HttpHeaders httpHeaders = new HttpHeaders(); | ||||
|         httpHeaders.setContentType(MediaType.APPLICATION_JSON); | ||||
| 
 | ||||
|         return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel); | ||||
|  | ||||
| @ -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) { | ||||
| } | ||||
|  | ||||
| @ -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) { | ||||
| } | ||||
|  | ||||
| @ -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) { | ||||
| } | ||||
|  | ||||
| @ -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) { | ||||
| } | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,28 +1,9 @@ | ||||
| package pl.materus.ghrepo.model; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class ResponseRepositoryModel { | ||||
|      | ||||
|     String name; | ||||
|     String owner; | ||||
|     List<ResponseBranchModel> 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<ResponseBranchModel> getBranches() { | ||||
|         return branches; | ||||
|     } | ||||
|     public void setBranches(List<ResponseBranchModel> branches) { | ||||
|         this.branches = branches; | ||||
|     } | ||||
| public record ResponseRepositoryModel( | ||||
|         String name, | ||||
|         String owner, | ||||
|         List<ResponseBranchModel> branches) { | ||||
| } | ||||
|  | ||||
| @ -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,17 +48,13 @@ public class GitHubService { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     private Mono<ResponseRepositoryModel> createResponse(GitHubRepositoryModel repository) { | ||||
|         ResponseRepositoryModel responseRepositoryModel = new ResponseRepositoryModel(); | ||||
|         responseRepositoryModel.setName(repository.getName()); | ||||
|         responseRepositoryModel.setOwner(repository.getOwner().getLogin()); | ||||
|         return Mono.just(responseRepositoryModel); | ||||
|     private Mono<String[]> createResponse(GitHubRepositoryModel repository) { | ||||
|         String[] ret = { repository.owner().login(),repository.name() }; | ||||
|         return Mono.just(ret); | ||||
|     } | ||||
| 
 | ||||
|     private Mono<ResponseBranchModel> 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); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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()); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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()); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|  | ||||
		Reference in New Issue
	
	Block a user