From 70958547150ad8da3dcb8c23e7200a73951cc20a Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:19:00 -0700 Subject: [PATCH 1/2] Refactored DTOs to use Java records --- .../controller/SwitcherRelayController.java | 14 ++-- .../switcherapi/ac/model/GitHubDetail.java | 23 ++---- .../switcherapi/ac/model/dto/AccountDTO.java | 14 +--- .../switcherapi/ac/model/dto/AdminDTO.java | 12 +-- .../ac/model/dto/GitHubAuthDTO.java | 13 +--- .../switcherapi/ac/model/dto/Metadata.java | 7 +- .../switcherapi/ac/model/dto/PlanDTO.java | 12 +-- .../ac/model/dto/RequestRelayDTO.java | 10 +-- .../ac/model/dto/ResponseRelayDTO.java | 32 +++----- .../ac/model/mapper/AccountMapper.java | 7 +- .../ac/model/mapper/DefaultMapper.java | 2 - .../ac/model/mapper/GitHubAuthMapper.java | 12 +-- .../ac/model/mapper/PlanMapper.java | 75 +++++++++++-------- .../switcherapi/ac/service/AdminService.java | 6 +- .../ac/service/ValidatorService.java | 2 +- .../validator/beans/ValidateRateLimit.java | 2 +- .../github/switcherapi/ac/PlanUtilsTests.java | 7 +- .../AdminAccountControllerTests.java | 4 +- .../controller/AdminAuthControllerTests.java | 6 +- .../AdminGitHubAuthControllerTests.java | 15 ++-- .../SwitcherRelayControllerErrorTests.java | 3 +- .../SwitcherRelayControllerTests.java | 26 +++---- .../fixture/ControllerTestUtils.java | 17 +---- .../ac/service/ValidatorServiceTest.java | 8 +- 24 files changed, 133 insertions(+), 196 deletions(-) diff --git a/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java b/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java index a9819be..27b395a 100644 --- a/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java +++ b/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java @@ -51,10 +51,10 @@ public ResponseEntity verify() { @PostMapping(value = "/create") public ResponseEntity loadAccount(@RequestBody RequestRelayDTO request) { try { - accountService.createAccount(request.getValue()); + accountService.createAccount(request.value()); return ResponseEntity.ok(ResponseRelayDTO.create(true)); } catch (Exception e) { - return ResponseEntity.status(500).body(ResponseRelayDTO.create(false).withMessage(e.getMessage())); + return ResponseEntity.status(500).body(ResponseRelayDTO.fail(e.getMessage())); } } @@ -62,10 +62,10 @@ public ResponseEntity loadAccount(@RequestBody RequestRelayDTO @PostMapping(value = "/remove") public ResponseEntity removeAccount(@RequestBody RequestRelayDTO request) { try { - accountService.deleteAccount(request.getValue()); + accountService.deleteAccount(request.value()); return ResponseEntity.ok(ResponseRelayDTO.create(true)); } catch (Exception e) { - return ResponseEntity.status(500).body(ResponseRelayDTO.create(false).withMessage(e.getMessage())); + return ResponseEntity.status(500).body(ResponseRelayDTO.fail(e.getMessage())); } } @@ -80,7 +80,7 @@ public ResponseEntity limiter(@RequestParam String value) { return ResponseEntity.ok(validatorFactory.runValidator(request)); } catch (ResponseStatusException e) { - return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.create(false).withMessage(e.getMessage())); + return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.fail(e.getMessage())); } } @@ -88,10 +88,10 @@ public ResponseEntity limiter(@RequestParam String value) { @PostMapping(value = "/validate") public ResponseEntity validate(@RequestBody RequestRelayDTO request) { try { - var featureRequest = gson.fromJson(request.getPayload(), FeaturePayload.class); + var featureRequest = gson.fromJson(request.payload(), FeaturePayload.class); return ResponseEntity.ok(validatorService.execute(featureRequest)); } catch (ResponseStatusException e) { - return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.create(false).withMessage(e.getMessage())); + return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.fail(e.getMessage())); } } diff --git a/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java b/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java index 370b342..0fc7775 100644 --- a/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java +++ b/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java @@ -3,21 +3,12 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Builder; -import lombok.Data; - @JsonIgnoreProperties(ignoreUnknown = true) -@Data -@Builder -public class GitHubDetail { - - private String id; - - private String name; - - private String login; - - @JsonProperty("avatar_url") - private String avatarUrl; - +public record GitHubDetail( + String id, + String name, + String login, + @JsonProperty("avatar_url") + String avatarUrl +) { } diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/AccountDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/AccountDTO.java index 41ed2c7..f816b0f 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/AccountDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/AccountDTO.java @@ -2,16 +2,10 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.Data; @JsonInclude(Include.NON_NULL) -@Data -public class AccountDTO { - - private String id; - - private String adminId; - - private PlanDTO plan; - +public record AccountDTO( + String id, + String adminId, + PlanDTO plan) { } diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/AdminDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/AdminDTO.java index d7692c6..cbe7653 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/AdminDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/AdminDTO.java @@ -3,14 +3,8 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.Data; - @JsonInclude(Include.NON_NULL) -@Data -public class AdminDTO { - - private String id; - - private String gitHubId; - +public record AdminDTO( + String id, + String gitHubId) { } diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java index 32dbc92..6ab3ce3 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java @@ -6,13 +6,8 @@ import lombok.Data; @JsonInclude(Include.NON_NULL) -@Data -public class GitHubAuthDTO { - - private AdminDTO admin; - - private String token; - - private String refreshToken; - +public record GitHubAuthDTO( + AdminDTO admin, + String token, + String refreshToken) { } diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/Metadata.java b/src/main/java/com/github/switcherapi/ac/model/dto/Metadata.java index a57c819..b2cea51 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/Metadata.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/Metadata.java @@ -6,8 +6,7 @@ @Builder public record Metadata( - @JsonProperty("rate_limit") - @SerializedName("rate_limit") - int rateLimit -) { + @JsonProperty("rate_limit") + @SerializedName("rate_limit") + int rateLimit) { } diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/PlanDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/PlanDTO.java index f34fa6e..2e10b74 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/PlanDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/PlanDTO.java @@ -2,11 +2,13 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.github.switcherapi.ac.model.domain.Plan; -import lombok.Generated; +import com.github.switcherapi.ac.model.domain.PlanAttribute; + +import java.util.List; -@Generated @JsonInclude(Include.NON_NULL) -public class PlanDTO extends Plan { - +public record PlanDTO( + String id, + String name, + List attributes) { } diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java index d3afe75..26ad715 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java @@ -6,11 +6,7 @@ import lombok.Data; @JsonInclude(Include.NON_NULL) -@Data -public class RequestRelayDTO { - - private String value; - - private String payload; - +public record RequestRelayDTO( + String value, + String payload) { } diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/ResponseRelayDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/ResponseRelayDTO.java index efa8076..2faf2eb 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/ResponseRelayDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/ResponseRelayDTO.java @@ -2,37 +2,23 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.AccessLevel; -import lombok.Data; -import lombok.Generated; -import lombok.NoArgsConstructor; -@Generated @JsonInclude(Include.NON_NULL) -@Data -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ResponseRelayDTO { - - private boolean result; - - private String message; - - private Metadata metadata; +public record ResponseRelayDTO( + boolean result, + String message, + Metadata metadata) { public static ResponseRelayDTO create(boolean result) { - var response = new ResponseRelayDTO(); - response.setResult(result); - return response; + return new ResponseRelayDTO(result, null, null); } - public ResponseRelayDTO withMessage(String message) { - this.message = message; - return this; + public static ResponseRelayDTO fail(String message) { + return new ResponseRelayDTO(false, message, null); } - public ResponseRelayDTO withMetadata(Metadata metadata) { - this.metadata = metadata; - return this; + public static ResponseRelayDTO success(Metadata metadata) { + return new ResponseRelayDTO(true, null, metadata); } } diff --git a/src/main/java/com/github/switcherapi/ac/model/mapper/AccountMapper.java b/src/main/java/com/github/switcherapi/ac/model/mapper/AccountMapper.java index 8d79222..3fa30d3 100644 --- a/src/main/java/com/github/switcherapi/ac/model/mapper/AccountMapper.java +++ b/src/main/java/com/github/switcherapi/ac/model/mapper/AccountMapper.java @@ -4,17 +4,14 @@ import com.github.switcherapi.ac.model.dto.AccountDTO; import com.github.switcherapi.ac.model.dto.PlanDTO; import lombok.AccessLevel; -import lombok.Generated; import lombok.NoArgsConstructor; -@Generated @NoArgsConstructor(access = AccessLevel.PRIVATE) public class AccountMapper { public static AccountDTO createCopy(Account from) { - var to = DefaultMapper.createCopy(from, AccountDTO.class); - to.setPlan(DefaultMapper.createCopy(from.getPlan(), PlanDTO.class)); - return to; + final var plan = new PlanDTO(from.getPlan().getId(), from.getPlan().getName(), from.getPlan().getAttributes()); + return new AccountDTO(from.getId(), from.getAdminId(), plan); } } diff --git a/src/main/java/com/github/switcherapi/ac/model/mapper/DefaultMapper.java b/src/main/java/com/github/switcherapi/ac/model/mapper/DefaultMapper.java index 7c22395..10702ec 100644 --- a/src/main/java/com/github/switcherapi/ac/model/mapper/DefaultMapper.java +++ b/src/main/java/com/github/switcherapi/ac/model/mapper/DefaultMapper.java @@ -1,7 +1,6 @@ package com.github.switcherapi.ac.model.mapper; import com.github.switcherapi.client.exception.SwitcherException; -import lombok.Generated; import lombok.experimental.UtilityClass; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanWrapper; @@ -12,7 +11,6 @@ import java.util.ArrayList; import java.util.Arrays; -@Generated @UtilityClass public class DefaultMapper { diff --git a/src/main/java/com/github/switcherapi/ac/model/mapper/GitHubAuthMapper.java b/src/main/java/com/github/switcherapi/ac/model/mapper/GitHubAuthMapper.java index ff9f9d7..ce53034 100644 --- a/src/main/java/com/github/switcherapi/ac/model/mapper/GitHubAuthMapper.java +++ b/src/main/java/com/github/switcherapi/ac/model/mapper/GitHubAuthMapper.java @@ -3,22 +3,16 @@ import com.github.switcherapi.ac.model.domain.Admin; import com.github.switcherapi.ac.model.dto.AdminDTO; import com.github.switcherapi.ac.model.dto.GitHubAuthDTO; - import lombok.AccessLevel; -import lombok.Generated; import lombok.NoArgsConstructor; import org.apache.commons.lang3.tuple.Pair; -@Generated @NoArgsConstructor(access = AccessLevel.PRIVATE) public class GitHubAuthMapper { - + public static GitHubAuthDTO createCopy(Admin from, Pair tokens) { - var to = new GitHubAuthDTO(); - to.setToken(tokens.getLeft()); - to.setRefreshToken(tokens.getRight()); - to.setAdmin(DefaultMapper.createCopy(from, AdminDTO.class)); - return to; + final var admin = new AdminDTO(from.getId(), from.getGitHubId()); + return new GitHubAuthDTO(admin, tokens.getLeft(), tokens.getRight()); } } diff --git a/src/main/java/com/github/switcherapi/ac/model/mapper/PlanMapper.java b/src/main/java/com/github/switcherapi/ac/model/mapper/PlanMapper.java index 65573c5..1e4c352 100644 --- a/src/main/java/com/github/switcherapi/ac/model/mapper/PlanMapper.java +++ b/src/main/java/com/github/switcherapi/ac/model/mapper/PlanMapper.java @@ -5,43 +5,56 @@ import com.github.switcherapi.ac.model.domain.PlanAttribute; import com.github.switcherapi.ac.model.dto.PlanDTO; import lombok.AccessLevel; -import lombok.Generated; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.List; -@Generated @NoArgsConstructor(access = AccessLevel.PRIVATE) public class PlanMapper { - public static List createCopy(List from) { - return from.stream() - .map(item -> DefaultMapper.createCopy(item, PlanDTO.class)) - .toList(); - } - - public static PlanDTO createCopy(Plan from) { - var to = DefaultMapper.createCopy(from, PlanDTO.class); - var attributes = new ArrayList(); - from.getAttributes().forEach(planAttribute -> - attributes.add(DefaultMapper.createCopy(planAttribute, PlanAttribute.class)) - ); - - to.setAttributes(attributes); - return to; - } - - public static void copyProperties(Plan from, Plan to) { - DefaultMapper.copyProperties(from, to, "attributes"); - from.getAttributes().forEach(planAttribute -> { - var feature = Feature.getFeatureEnum(planAttribute.getFeature()); - - if (to.hasFeature(feature)) { - to.getFeature(feature).setValue(planAttribute.getValue()); - } else { - to.addFeature(feature, planAttribute.getValue()); - } - }); - } + public static List createCopy(List from) { + return from.stream() + .map(item -> { + if (item instanceof Plan plan) { + return new PlanDTO(plan.getId(), plan.getName(), plan.getAttributes()); + } + + return null; + }).toList(); + } + + public static PlanDTO createCopy(Plan from) { + final var attributes = new ArrayList(); + from.getAttributes().forEach(planAttribute -> + attributes.add(DefaultMapper.createCopy(planAttribute, PlanAttribute.class))); + + return new PlanDTO(from.getId(), from.getName(), attributes); + } + + public static void copyProperties(Plan from, Plan to) { + DefaultMapper.copyProperties(from, to, "attributes"); + from.getAttributes().forEach(planAttribute -> { + var feature = Feature.getFeatureEnum(planAttribute.getFeature()); + + if (to.hasFeature(feature)) { + to.getFeature(feature).setValue(planAttribute.getValue()); + } else { + to.addFeature(feature, planAttribute.getValue()); + } + }); + } + + public static void copyProperties(PlanDTO from, Plan to) { + DefaultMapper.copyProperties(from, to, "attributes"); + from.attributes().forEach(planAttribute -> { + var feature = Feature.getFeatureEnum(planAttribute.getFeature()); + + if (to.hasFeature(feature)) { + to.getFeature(feature).setValue(planAttribute.getValue()); + } else { + to.addFeature(feature, planAttribute.getValue()); + } + }); + } } diff --git a/src/main/java/com/github/switcherapi/ac/service/AdminService.java b/src/main/java/com/github/switcherapi/ac/service/AdminService.java index 56955c4..e9d1385 100644 --- a/src/main/java/com/github/switcherapi/ac/service/AdminService.java +++ b/src/main/java/com/github/switcherapi/ac/service/AdminService.java @@ -43,12 +43,12 @@ public GitHubAuthDTO gitHubAuth(String code) { final var gitHubToken = githubService.getToken(code); final var gitHubDetail = githubService.getGitHubDetail(gitHubToken); - if (isNotAvailable(gitHubDetail.getId())) { + if (isNotAvailable(gitHubDetail.id())) { throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not allowed"); } - var admin = Optional.ofNullable(adminRepository.findByGitHubId(gitHubDetail.getId())) - .orElse(createAdminAccount(gitHubDetail.getId())); + var admin = Optional.ofNullable(adminRepository.findByGitHubId(gitHubDetail.id())) + .orElse(createAdminAccount(gitHubDetail.id())); final var tokens = jwtService.generateToken(admin.getId()); updateAdminAccountToken(admin, tokens.getLeft()); diff --git a/src/main/java/com/github/switcherapi/ac/service/ValidatorService.java b/src/main/java/com/github/switcherapi/ac/service/ValidatorService.java index 52956f5..b6c5cb0 100644 --- a/src/main/java/com/github/switcherapi/ac/service/ValidatorService.java +++ b/src/main/java/com/github/switcherapi/ac/service/ValidatorService.java @@ -30,7 +30,7 @@ protected ResponseRelayDTO executeValidator(final Account account) { final var value = maxPlanValue.getValue(); if (validate(value)) { - return ResponseRelayDTO.create(false).withMessage(MSG_FEATURE_LIMIT_REACHED.getValue()); + return ResponseRelayDTO.fail(MSG_FEATURE_LIMIT_REACHED.getValue()); } return ResponseRelayDTO.create(true); diff --git a/src/main/java/com/github/switcherapi/ac/service/validator/beans/ValidateRateLimit.java b/src/main/java/com/github/switcherapi/ac/service/validator/beans/ValidateRateLimit.java index 1e6c517..de02fe9 100644 --- a/src/main/java/com/github/switcherapi/ac/service/validator/beans/ValidateRateLimit.java +++ b/src/main/java/com/github/switcherapi/ac/service/validator/beans/ValidateRateLimit.java @@ -20,7 +20,7 @@ protected ResponseRelayDTO executeValidator(final Account account) { final var plan = account.getPlan(); final var max = Integer.parseInt(plan.getFeature(RATE_LIMIT).getValue().toString()); - return ResponseRelayDTO.create(true).withMetadata(Metadata.builder().rateLimit(max).build()); + return ResponseRelayDTO.success(Metadata.builder().rateLimit(max).build()); } } diff --git a/src/test/java/com/github/switcherapi/ac/PlanUtilsTests.java b/src/test/java/com/github/switcherapi/ac/PlanUtilsTests.java index 2e34f9f..8c03787 100644 --- a/src/test/java/com/github/switcherapi/ac/PlanUtilsTests.java +++ b/src/test/java/com/github/switcherapi/ac/PlanUtilsTests.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test; import java.util.Arrays; +import java.util.List; import static com.github.switcherapi.ac.model.domain.Feature.*; import static org.assertj.core.api.Assertions.assertThat; @@ -30,9 +31,9 @@ void shouldConvertDTO() { PlanAttribute.builder().feature(HISTORY.getValue()).value(false).build(), PlanAttribute.builder().feature(METRICS.getValue()).value(false).build() )).build(); - - final PlanDTO from = new PlanDTO(); - from.addFeature(DOMAIN, 2); + + final var attributes = List.of(new PlanAttribute(DOMAIN.getValue(), 2)); + final PlanDTO from = new PlanDTO(to.getId(), to.getName(), attributes); //test PlanMapper.copyProperties(from, to); diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java index 073bb85..e962e9f 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java @@ -85,8 +85,8 @@ void shouldChangeAccountPlan() throws Exception { .andReturn().getResponse().getContentAsString(); var accountDto = new ObjectMapper().readValue(json, AccountDTO.class); - assertThat(accountDto.getAdminId()).isEqualTo(ADMIN_ID); - assertThat(accountDto.getPlan().getName()).isEqualTo("BASIC"); + assertThat(accountDto.adminId()).isEqualTo(ADMIN_ID); + assertThat(accountDto.plan().name()).isEqualTo("BASIC"); account = accountService.getAccountByAdminId(ADMIN_ID); assertThat(account.getPlan().getName()).isEqualTo("BASIC"); diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java index 3286763..64b2769 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java @@ -61,9 +61,9 @@ void shouldRefreshToken() throws Exception { .andReturn().getResponse().getContentAsString(); var authDto = new ObjectMapper().readValue(json, GitHubAuthDTO.class); - assertThat(authDto.getAdmin().getGitHubId()).isEqualTo("123456"); - assertThat(authDto.getToken()).isNotEqualTo(tokens.getLeft()); - assertThat(authDto.getRefreshToken()).isNotEqualTo(tokens.getRight()); + assertThat(authDto.admin().gitHubId()).isEqualTo("123456"); + assertThat(authDto.token()).isNotEqualTo(tokens.getLeft()); + assertThat(authDto.refreshToken()).isNotEqualTo(tokens.getRight()); } @Test diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java index 5acef1e..5dc5cc9 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java @@ -42,7 +42,7 @@ class AdminGitHubAuthControllerTests { @Autowired ApplicationContext applicationContext; public static MockWebServer mockBackend; - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper mapper = new ObjectMapper(); @BeforeAll static void setup() throws IOException { @@ -87,9 +87,9 @@ void shouldLoginWithGitHub() throws Exception { .andReturn().getResponse().getContentAsString(); var authDto = new ObjectMapper().readValue(json, GitHubAuthDTO.class); - assertThat(authDto.getAdmin()).isNotNull(); - assertThat(authDto.getToken()).isNotNull(); - assertThat(authDto.getRefreshToken()).isNotNull(); + assertThat(authDto.admin()).isNotNull(); + assertThat(authDto.token()).isNotNull(); + assertThat(authDto.refreshToken()).isNotNull(); } @SwitcherTest(key = SwitcherFeatures.SWITCHER_AC_ADM, result = false) @@ -178,13 +178,10 @@ private void givenResponseInvalidCode() { } private void givenResponseSuccess() throws JsonProcessingException { - var githubAccountDetail = GitHubDetail.builder() - .id("123") - .login("login") - .name("UserName").build(); + final var githubAccountDetail = new GitHubDetail("123", "UserName", "login", "http://avatar.com"); mockBackend.enqueue(new MockResponse() - .setBody(MAPPER.writeValueAsString(githubAccountDetail)) + .setBody(mapper.writeValueAsString(githubAccountDetail)) .addHeader("Content-Type", MediaType.APPLICATION_JSON)); } diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java index 54a4d73..e471a0f 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java @@ -52,8 +52,7 @@ void shouldNotCreateAccount() throws Exception { .thenThrow(new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR)); //given - var request = new RequestRelayDTO(); - request.setValue("adminid"); + var request = new RequestRelayDTO("adminid", null); var jsonRequest = new Gson().toJson(request); //test diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java index 81b227d..5472f2f 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java @@ -68,8 +68,8 @@ void shoutNotCreate_notAuthenticated() throws Exception { void shouldCreateAccount() throws Exception { //given var jsonRequest = givenRequest("adminid"); - var jsonResponse = givenTrueAsExpectedResponse(); - + var jsonResponse = gson.toJson(ResponseRelayDTO.create(true)); + //test this.mockMvc.perform(post("/switcher/v1/create") .contentType(MediaType.APPLICATION_JSON) @@ -86,7 +86,7 @@ void shouldRemoveAccount() throws Exception { //given givenAccount("adminid"); var jsonRequest = givenRequest("adminid"); - var jsonResponse = givenTrueAsExpectedResponse(); + var jsonResponse = gson.toJson(ResponseRelayDTO.create(true)); //test this.mockMvc.perform(post("/switcher/v1/remove") @@ -103,8 +103,7 @@ void shouldRemoveAccount() throws Exception { void shouldNotRemoveAccount_accountNotFound() throws Exception { //given var jsonRequest = givenRequest("NOT_FOUND"); - var jsonResponse = givenExpectedResponse(false, - "404 NOT_FOUND \"Unable to find account NOT_FOUND\""); + var jsonResponse = gson.toJson(ResponseRelayDTO.fail("404 NOT_FOUND \"Unable to find account NOT_FOUND\"")); //test this.mockMvc.perform(post("/switcher/v1/remove") @@ -123,18 +122,14 @@ void shouldBeOkWhenValidate_limiter() throws Exception { givenAccount("adminid"); //test - var expectedResponse = ResponseRelayDTO - .create(true) - .withMetadata(Metadata.builder().rateLimit(100).build()); + var expectedResponse = ResponseRelayDTO.success(Metadata.builder().rateLimit(100).build()); this.assertLimiter("adminid", expectedResponse, 200); } @Test void shouldNotBeOkWhenValidate_limiter_accountNotFound() throws Exception { - var expectedResponse = ResponseRelayDTO - .create(false) - .withMessage("404 NOT_FOUND \"Account not found\""); + var expectedResponse = ResponseRelayDTO.fail("404 NOT_FOUND \"Account not found\""); this.assertLimiter("NOT_FOUND", expectedResponse, 404); } @@ -174,8 +169,7 @@ void shouldReturnFalse() throws Exception { givenAccount("adminid_nok", "TEST"); //test - var expectedResponse = ResponseRelayDTO.create(false) - .withMessage("Feature limit has been reached"); + var expectedResponse = ResponseRelayDTO.fail("Feature limit has been reached"); this.assertValidate("adminid_nok", DOMAIN.getValue(), 1, expectedResponse, 200); @@ -183,8 +177,7 @@ void shouldReturnFalse() throws Exception { @Test void shouldNotBeOkWhenValidate_accountNotFound() throws Exception { - var expectedResponse = ResponseRelayDTO.create(false) - .withMessage("404 NOT_FOUND \"Account not found\""); + var expectedResponse = ResponseRelayDTO.fail("404 NOT_FOUND \"Account not found\""); this.assertValidate("NOT_FOUND", DOMAIN.getValue(), 0, expectedResponse, 404); @@ -196,8 +189,7 @@ void shouldNotBeOkWhenValidate_invalidFeatureName() throws Exception { givenAccount("adminid"); //test - var expectedResponse = ResponseRelayDTO.create(false) - .withMessage("400 BAD_REQUEST \"Invalid feature: INVALID_FEATURE\""); + var expectedResponse = ResponseRelayDTO.fail("400 BAD_REQUEST \"Invalid feature: INVALID_FEATURE\""); this.assertValidate("adminid", "INVALID_FEATURE", 0, expectedResponse, 400); diff --git a/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java b/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java index 2b640b5..9838e87 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java +++ b/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java @@ -40,8 +40,7 @@ protected void assertValidate(String adminId, String featureName, .total(value) .build(); - var request = new RequestRelayDTO(); - request.setPayload(gson.toJson(feature)); + var request = new RequestRelayDTO(null, gson.toJson(feature)); //test this.mockMvc.perform(post("/switcher/v1/validate") @@ -66,18 +65,8 @@ protected void assertLimiter(String value, ResponseRelayDTO expectedResponse, .andExpect(content().string(containsString(gson.toJson(expectedResponse)))); } - protected String givenTrueAsExpectedResponse() { - return givenExpectedResponse(true, null); - } - - protected String givenExpectedResponse(boolean result, String message) { - var response = ResponseRelayDTO.create(result).withMessage(message); - return gson.toJson(response); - } - protected String givenRequest(String adminId) { - var request = new RequestRelayDTO(); - request.setValue(adminId); + var request = new RequestRelayDTO(adminId, null); return gson.toJson(request); } @@ -92,7 +81,7 @@ protected void givenAccount(String adminId, String plan) { protected void assertDtoResponse(Plan planObj, String response) throws JsonProcessingException { var planDto = new ObjectMapper().readValue(response, PlanDTO.class); - assertThat(planDto.getName()).isEqualTo(planObj.getName()); + assertThat(planDto.name()).isEqualTo(planObj.getName()); } } diff --git a/src/test/java/com/github/switcherapi/ac/service/ValidatorServiceTest.java b/src/test/java/com/github/switcherapi/ac/service/ValidatorServiceTest.java index 5a222b2..b22d9f0 100644 --- a/src/test/java/com/github/switcherapi/ac/service/ValidatorServiceTest.java +++ b/src/test/java/com/github/switcherapi/ac/service/ValidatorServiceTest.java @@ -48,7 +48,7 @@ void shouldValidateFromRequest(Feature feature, Integer total) { //test var responseRelayDTO = validatorService.execute(request); - assertTrue(responseRelayDTO.isResult()); + assertTrue(responseRelayDTO.result()); } static Stream validatorLimitInput() { @@ -74,8 +74,8 @@ void shouldNotValidateFromRequest(Feature feature, Integer total) { //test var responseRelayDTO = validatorService.execute(request); - assertFalse(responseRelayDTO.isResult()); - assertEquals(MSG_FEATURE_LIMIT_REACHED.getValue(), responseRelayDTO.getMessage()); + assertFalse(responseRelayDTO.result()); + assertEquals(MSG_FEATURE_LIMIT_REACHED.getValue(), responseRelayDTO.message()); } @Test @@ -111,7 +111,7 @@ void shouldValidateFromRequest_undeterminedValue() { //test var responseRelayDTO = validatorService.execute(request); - assertTrue(responseRelayDTO.isResult()); + assertTrue(responseRelayDTO.result()); } @Test From 3f77a032885ed172145e0c05ce98bc916b2c9068 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:22:17 -0700 Subject: [PATCH 2/2] chore: removed unused imports --- .github/workflows/master.yml | 2 +- .github/workflows/re-release.yml | 2 +- .github/workflows/release.yml | 2 +- .../java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java | 2 -- .../com/github/switcherapi/ac/model/dto/RequestRelayDTO.java | 2 -- 5 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 2b1efed..0ed12a4 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -54,7 +54,7 @@ jobs: - name: Build and push id: docker_build - uses: docker/build-push-action@v5 + uses: docker/build-push-action@v6 with: push: true platforms: linux/amd64,linux/arm64 diff --git a/.github/workflows/re-release.yml b/.github/workflows/re-release.yml index 593e1b4..3fddcd9 100644 --- a/.github/workflows/re-release.yml +++ b/.github/workflows/re-release.yml @@ -62,7 +62,7 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v5 + uses: docker/build-push-action@v6 with: context: . push: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cd27121..6d9856d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -49,7 +49,7 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - uses: docker/build-push-action@v5 + uses: docker/build-push-action@v6 with: context: . push: true diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java index 6ab3ce3..235bce5 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/GitHubAuthDTO.java @@ -3,8 +3,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.Data; - @JsonInclude(Include.NON_NULL) public record GitHubAuthDTO( AdminDTO admin, diff --git a/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java b/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java index 26ad715..92e1b26 100644 --- a/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java +++ b/src/main/java/com/github/switcherapi/ac/model/dto/RequestRelayDTO.java @@ -3,8 +3,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.Data; - @JsonInclude(Include.NON_NULL) public record RequestRelayDTO( String value,