Skip to content

Added caching for switcher relay validation route #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public Mono<ResponseEntity<ResponseRelayDTO>> limiter(@RequestParam String value

@Operation(summary = "Perform account validation given input value")
@PostMapping(value = "/validate")
@Cacheable(value = "validateCache", key = "#request.payload().toString()")
public Mono<ResponseEntity<ResponseRelayDTO>> validate(@RequestBody RequestRelayDTO request) {
try {
var featureRequest = gson.fromJson(request.payload().toString(), FeaturePayload.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.List;

import static com.github.switcherapi.ac.model.domain.Feature.DOMAIN;
import static com.github.switcherapi.ac.model.domain.Feature.RATE_LIMIT;

@SpringBootTest
Expand All @@ -31,7 +32,10 @@
})
class SwitcherRelayCacheLimiterControllerTests extends ControllerTestUtils {

@Autowired PlanService planService;
private static final String TEST_PLAN = "TEST";

@Autowired
PlanService planService;

@BeforeEach
void setupPlan() {
Expand All @@ -41,40 +45,56 @@ void setupPlan() {
@Test
void shouldReturnUnchangedRateLimit() {
//given
givenAccount("adminid", "TEST");
givenAccount("adminid", TEST_PLAN);

//test
var expectedResponse = ResponseRelayDTO.success(Metadata.builder().rateLimit(100).build());
this.assertLimiter("adminid", expectedResponse, 200);

//update plan rate limit to 200
updatePlanRateLimit();
updatePlanFeature(RATE_LIMIT.getValue(), 200);

//test again
this.assertLimiter("adminid", expectedResponse, 200);
}

private void updatePlanRateLimit() {
StepVerifier.create(planService.updatePlan("TEST", Plan.builder()
.attributes(List.of(
PlanAttribute.builder()
.feature(RATE_LIMIT.getValue())
.value(200)
.build()))
.build()))
.expectNextCount(1)
.verifyComplete();
@Test
void shouldReturnUnchangedValidationResponse() {
//given
givenAccount("adminid", TEST_PLAN);

//test
var expectedResponse = ResponseRelayDTO.fail("Feature limit has been reached");
this.assertValidate("adminid", DOMAIN.getValue(),
1, expectedResponse, 200);

//update Domain feature limit to 2
updatePlanFeature(DOMAIN.getValue(), 2);

//test again
this.assertValidate("adminid", DOMAIN.getValue(),
1, expectedResponse, 200);
}

private void createPlan() {
StepVerifier.create(planService.createPlan(Plan.builder()
.name("TEST")
.attributes(List.of(
PlanAttribute.builder()
.feature(RATE_LIMIT.getValue())
.value(100)
.build()))
.build()))
.name(TEST_PLAN)
.attributes(List.of(
PlanAttribute.builder().feature(DOMAIN.getValue()).value(1).build(),
PlanAttribute.builder().feature(RATE_LIMIT.getValue()).value(100).build()))
.build()))
.expectNextCount(1)
.verifyComplete();
}

private void updatePlanFeature(String feature, int value) {
StepVerifier.create(planService.updatePlan(TEST_PLAN, Plan.builder()
.attributes(List.of(
PlanAttribute.builder()
.feature(feature)
.value(value)
.build()))
.build()))
.expectNextCount(1)
.verifyComplete();
}
Expand Down