change models from class to records

This commit is contained in:
Mateusz Słodkowicz 2024-03-11 14:52:42 +01:00
parent d15cf46ac3
commit d1c233068c
Signed by: materus
GPG Key ID: 28D140BCA60B4FD1
11 changed files with 65 additions and 172 deletions

View File

@ -32,51 +32,51 @@ public class GitHubController {
@GetMapping("/{username}") @GetMapping("/{username}")
public ResponseEntity<Flux<ResponseRepositoryModel>> getUserRepos( public ResponseEntity<Flux<ResponseRepositoryModel>> getUserRepos(
@RequestHeader(name = HttpHeaders.ACCEPT) String acceptHeader, @PathVariable String username) { @RequestHeader(name = HttpHeaders.ACCEPT) String acceptHeader, @PathVariable String username) {
if (!acceptHeader.contains("application/json")) { if (!acceptHeader.contains("application/json")) {
throw new WrongHeaderException("Request does not contain the 'application/json' header."); throw new WrongHeaderException("Request does not contain the 'application/json' header.");
} }
HttpHeaders httpHeaders= new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return ResponseEntity.ok().headers(httpHeaders).body(gitHubService.getUserRepos(username)); return ResponseEntity.ok().headers(httpHeaders).body(gitHubService.getUserRepos(username));
} }
@ExceptionHandler(WebClientResponseException.class) @ExceptionHandler(WebClientResponseException.class)
public ResponseEntity<ResponseErrorModel> handleResponseException(WebClientResponseException ex) { public ResponseEntity<ResponseErrorModel> handleResponseException(WebClientResponseException ex) {
ResponseErrorModel responseErrorModel = new ResponseErrorModel();
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
HttpHeaders httpHeaders= new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.setContentType(MediaType.APPLICATION_JSON);
responseErrorModel.setStatus(ex.getStatusCode().value()); String message;
switch (responseErrorModel.getStatus()) { Integer statusCode = ex.getStatusCode().value();
switch (statusCode) {
case 404: case 404:
responseErrorModel.setMessage("User not found"); message = "User not found";
status = HttpStatus.NOT_FOUND; status = HttpStatus.NOT_FOUND;
break; break;
case 403: case 403:
responseErrorModel.setMessage("Github API limit"); message = "Github API limit";
status = HttpStatus.FORBIDDEN; status = HttpStatus.FORBIDDEN;
break; break;
default: default:
responseErrorModel.setMessage("Unknown github webclient error"); message = "Unknown github webclient error";
break; break;
} }
ResponseErrorModel responseErrorModel = new ResponseErrorModel(statusCode,message);
return ResponseEntity.status(status).headers(httpHeaders).body(responseErrorModel); return ResponseEntity.status(status).headers(httpHeaders).body(responseErrorModel);
} }
@ExceptionHandler(WrongHeaderException.class) @ExceptionHandler(WrongHeaderException.class)
public ResponseEntity<ResponseErrorModel> handleResponseException(WrongHeaderException ex) { public ResponseEntity<ResponseErrorModel> handleResponseException(WrongHeaderException ex) {
ResponseErrorModel responseErrorModel = new ResponseErrorModel(); ResponseErrorModel responseErrorModel = new ResponseErrorModel(HttpStatus.BAD_REQUEST.value(),ex.getMessage());
responseErrorModel.setStatus(HttpStatus.BAD_REQUEST.value());
responseErrorModel.setMessage(ex.getMessage()); HttpHeaders httpHeaders = new HttpHeaders();
HttpHeaders httpHeaders= new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel); return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel);

View File

@ -1,13 +1,4 @@
package pl.materus.ghrepo.model; package pl.materus.ghrepo.model;
public class GitHubBranchCommitModel { public record GitHubBranchCommitModel(String sha) {
String sha;
public String getSha() {
return sha;
}
public void setSha(String sha) {
this.sha = sha;
}
} }

View File

@ -1,24 +1,6 @@
package pl.materus.ghrepo.model; package pl.materus.ghrepo.model;
public record GitHubBranchModel(
public class GitHubBranchModel { String name,
String name; GitHubBranchCommitModel commit) {
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;
}
} }

View File

@ -1,33 +1,7 @@
package pl.materus.ghrepo.model; package pl.materus.ghrepo.model;
public class GitHubRepositoryModel { public record GitHubRepositoryModel(
String name; String name,
GitHubRepositoryOwnerModel owner,
GitHubRepositoryOwnerModel owner; Boolean fork) {
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;
}
} }

View File

@ -1,14 +1,4 @@
package pl.materus.ghrepo.model; package pl.materus.ghrepo.model;
public class GitHubRepositoryOwnerModel { public record GitHubRepositoryOwnerModel(String login) {
String login;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
} }

View File

@ -1,20 +1,11 @@
package pl.materus.ghrepo.model; package pl.materus.ghrepo.model;
public class ResponseBranchModel{ public record ResponseBranchModel(
String name; String name,
String sha; String sha) {
public ResponseBranchModel(String name, String sha)
{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name; this.name = name;
}
public String getSha() {
return sha;
}
public void setSha(String sha) {
this.sha = sha; this.sha = sha;
} }
} }

View File

@ -1,22 +1,11 @@
package pl.materus.ghrepo.model; package pl.materus.ghrepo.model;
public class ResponseErrorModel{ public record ResponseErrorModel(
Integer status; Integer status,
String message; String message) {
public Integer getStatus() { public ResponseErrorModel(Integer status, String message) {
return status;
}
public void setStatus(Integer status) {
this.status = status; this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message; this.message = message;
} }
} }

View File

@ -1,28 +1,9 @@
package pl.materus.ghrepo.model; package pl.materus.ghrepo.model;
import java.util.List; import java.util.List;
public class ResponseRepositoryModel { public record ResponseRepositoryModel(
String name,
String name; String owner,
String owner; List<ResponseBranchModel> branches) {
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;
}
} }

View File

@ -28,13 +28,12 @@ public class GitHubService {
.uri("/users/{username}/repos", username) .uri("/users/{username}/repos", username)
.retrieve() .retrieve()
.bodyToFlux(GitHubRepositoryModel.class) .bodyToFlux(GitHubRepositoryModel.class)
.filter(repo -> !repo.getFork()) .filter(repo -> !repo.fork())
.flatMap(this::createResponse) .flatMap(this::createResponse)
.flatMap(repo -> getRepoBranches(repo.getOwner(), repo.getName()) .flatMap(loginName -> getRepoBranches(loginName[0], loginName[1])
.collectList() .collectList()
.map(branches -> { .map(branches -> {
repo.setBranches(branches); return new ResponseRepositoryModel(loginName[1], loginName[0], branches);
return repo;
})) }))
.subscribeOn(Schedulers.boundedElastic()); .subscribeOn(Schedulers.boundedElastic());
@ -49,18 +48,14 @@ public class GitHubService {
} }
private Mono<ResponseRepositoryModel> createResponse(GitHubRepositoryModel repository) { private Mono<String[]> createResponse(GitHubRepositoryModel repository) {
ResponseRepositoryModel responseRepositoryModel = new ResponseRepositoryModel(); String[] ret = { repository.owner().login(),repository.name() };
responseRepositoryModel.setName(repository.getName()); return Mono.just(ret);
responseRepositoryModel.setOwner(repository.getOwner().getLogin());
return Mono.just(responseRepositoryModel);
} }
private Mono<ResponseBranchModel> createResponseBranch(GitHubBranchModel branch) { private Mono<ResponseBranchModel> createResponseBranch(GitHubBranchModel branch) {
ResponseBranchModel responseBranchModel = new ResponseBranchModel(); ResponseBranchModel responseBranchModel = new ResponseBranchModel(branch.name(), branch.commit().sha());
responseBranchModel.setName(branch.getName());
responseBranchModel.setSha(branch.getCommit().getSha());
return Mono.just(responseBranchModel); return Mono.just(responseBranchModel);
} }
} }

View File

@ -49,8 +49,8 @@ public class GitHubControllerTest {
var response = githubController.handleResponseException(e); var response = githubController.handleResponseException(e);
assertNotNull(response.getBody()); assertNotNull(response.getBody());
assertEquals(404, response.getBody().getStatus()); assertEquals(404, response.getBody().status());
assertEquals("User not found", response.getBody().getMessage()); assertEquals("User not found", response.getBody().message());
} }
@SuppressWarnings("null") @SuppressWarnings("null")
@ -61,8 +61,8 @@ public class GitHubControllerTest {
var response = githubController.handleResponseException(e); var response = githubController.handleResponseException(e);
assertNotNull(response.getBody()); assertNotNull(response.getBody());
assertEquals(400, response.getBody().getStatus()); assertEquals(400, response.getBody().status());
assertEquals("Request does not contain the 'application/json' header.", response.getBody().getMessage()); assertEquals("Request does not contain the 'application/json' header.", response.getBody().message());
} }
@SuppressWarnings("null") @SuppressWarnings("null")
@ -82,7 +82,7 @@ public class GitHubControllerTest {
var response = githubController.handleResponseException(e); var response = githubController.handleResponseException(e);
assertNotNull(response.getBody()); assertNotNull(response.getBody());
assertEquals(403, response.getBody().getStatus()); assertEquals(403, response.getBody().status());
assertEquals("Github API limit", response.getBody().getMessage()); assertEquals("Github API limit", response.getBody().message());
} }
} }

View File

@ -74,27 +74,27 @@ public class GitHubServiceTest {
var response = gitHubService.getUserRepos("materusPL").collectList().block(); var response = gitHubService.getUserRepos("materusPL").collectList().block();
response.sort((repo1, repo2) -> { response.sort((repo1, repo2) -> {
return repo1.getName().compareTo(repo2.getName()); return repo1.name().compareTo(repo2.name());
}); });
assertEquals("Nixerus", response.get(0).getName()); assertEquals("Nixerus", response.get(0).name());
assertEquals("SNOL", response.get(1).getName()); assertEquals("SNOL", response.get(1).name());
assertEquals("materusPL", response.get(2).getName()); assertEquals("materusPL", response.get(2).name());
assertEquals("nixos-config", response.get(3).getName()); assertEquals("nixos-config", response.get(3).name());
String owner = "materusPL"; String owner = "materusPL";
assertEquals(owner, response.get(0).getOwner()); assertEquals(owner, response.get(0).owner());
assertEquals(owner, response.get(1).getOwner()); assertEquals(owner, response.get(1).owner());
assertEquals(owner, response.get(2).getOwner()); assertEquals(owner, response.get(2).owner());
assertEquals(owner, response.get(3).getOwner()); assertEquals(owner, response.get(3).owner());
assertEquals("master", response.get(2).getBranches().get(0).getName()); assertEquals("master", response.get(2).branches().get(0).name());
assertEquals("fd6867d8963147ba40d3df428045aec82f14dbe3", response.get(2).getBranches().get(0).getSha()); assertEquals("fd6867d8963147ba40d3df428045aec82f14dbe3", response.get(2).branches().get(0).sha());
assertEquals(3, response.get(0).getBranches().size()); assertEquals(3, response.get(0).branches().size());
assertEquals(2, response.get(3).getBranches().size()); assertEquals(2, response.get(3).branches().size());
assertEquals(1, response.get(2).getBranches().size()); assertEquals(1, response.get(2).branches().size());
} }