Skip to content
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
2 changes: 1 addition & 1 deletion src/main/java/org/prebid/cache/handlers/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class ErrorHandler extends MetricsHandler {
private static final String RESOURCE_NOT_FOUND_BAD_URL = "Resource Not Found - Bad URL.";
private static final String RESOURCE_NOT_FOUND = "Resource Not Found: uuid %s";
private static final String INVALID_PARAMETERS = "Invalid Parameter(s): uuid not found.";
private static final String INVALID_PARAMETERS = "Invalid Parameter(s): uuid not found or is empty.";
private static final String NO_ELEMENTS_FOUND = "No Elements Found.";

@Autowired
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/prebid/cache/handlers/cache/GetCacheHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ private static Map<String, WebClient> createClientsCache(final int ttl, final in
}

public Mono<ServerResponse> fetch(ServerRequest request) {
// metrics
metricsRecorder.markMeterForTag(this.metricTagPrefix, MeasurementTag.REQUEST);
final var timerContext = metricsRecorder.createRequestTimerForServiceType(this.type);

return request.queryParam(ID_KEY).map(id -> fetch(request, id, timerContext)).orElseGet(() -> {
final var responseMono = ErrorHandler.createInvalidParameters();
return finalizeResult(responseMono, request, timerContext);
});
metricsRecorder.markMeterForTag(metricTagPrefix, MeasurementTag.REQUEST);
final var timerContext = metricsRecorder.createRequestTimerForServiceType(type);

return request.queryParam(ID_KEY)
.filter(StringUtils::isNotBlank)
.map(id -> fetch(request, id, timerContext))
.orElseGet(() -> {
final var responseMono = ErrorHandler.createInvalidParameters();
return finalizeResult(responseMono, request, timerContext);
});
}

private Mono<ServerResponse> fetch(final ServerRequest request,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.prebid.cache.handlers;

import org.prebid.cache.exceptions.RequestParsingException;
import org.prebid.cache.exceptions.RepositoryException;
import org.prebid.cache.exceptions.RequestParsingException;
import org.prebid.cache.handlers.cache.CacheHandler;
import org.prebid.cache.model.Payload;
import org.prebid.cache.model.PayloadTransfer;
Expand Down
103 changes: 69 additions & 34 deletions src/test/java/org/prebid/cache/handlers/GetCacheHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {
GetCacheHandler.class,
PrebidServerResponseBuilder.class,
CacheConfig.class,
CacheConfig.class,
MetricsRecorderTest.class,
MetricsRecorder.class,
ApiConfig.class,
CircuitBreakerPropertyConfiguration.class
GetCacheHandler.class,
PrebidServerResponseBuilder.class,
CacheConfig.class,
CacheConfig.class,
MetricsRecorderTest.class,
MetricsRecorder.class,
ApiConfig.class,
CircuitBreakerPropertyConfiguration.class
})
@EnableConfigurationProperties
@SpringBootTest
Expand Down Expand Up @@ -110,18 +110,14 @@ void testVerifyError() {
verifyRepositoryError(handler);
}

private static Consumer<ServerResponse> assertNotFoundStatusCode() {
return response -> assertEquals(response.statusCode().value(), 404);
}

@Test
void testVerifyFetch() {
given(repository.findById("prebid_a8db2208-d085-444c-9721-c1161d7f09ce")).willReturn(Mono.just(PAYLOAD_WRAPPER));

final var requestMono = MockServerRequest.builder()
.method(HttpMethod.GET)
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.build();
.method(HttpMethod.GET)
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.build();

final var responseMono = handler.fetch(requestMono);

Expand All @@ -142,18 +138,18 @@ void testVerifyFetchWithCacheHostParam() {
final var requestMono = MockServerRequest.builder()
.method(HttpMethod.GET)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.queryParam("ch", "localhost:8080")
.build();
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.queryParam("ch", "localhost:8080")
.build();

final var responseMono = handler.fetch(requestMono);
responseMono.doOnEach(assertSignalStatusCode(200)).subscribe();

StepVerifier.create(responseMono)
.expectSubscription()
.expectNextMatches(t -> true)
.expectComplete()
.verify();
.expectSubscription()
.expectNextMatches(t -> true)
.expectComplete()
.verify();

verify(getRequestedFor(urlPathEqualTo("/cache"))
.withQueryParam("uuid", equalTo("a8db2208-d085-444c-9721-c1161d7f09ce"))
Expand All @@ -164,34 +160,34 @@ void testVerifyFetchWithCacheHostParam() {
@Test
void testVerifyFailForNotFoundResourceWithCacheHostParam() {
final var requestMono = MockServerRequest.builder()
.method(HttpMethod.GET)
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.queryParam("ch", "localhost:8080")
.build();
.method(HttpMethod.GET)
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.queryParam("ch", "localhost:8080")
.build();

final var responseMono = handler.fetch(requestMono);

responseMono.doOnEach(assertSignalStatusCode(404)).subscribe();
StepVerifier.create(responseMono)
.consumeNextWith(assertNotFoundStatusCode())
.expectComplete()
.verify();
.expectComplete()
.verify();
}

@Test
void testVerifyFetchReturnsBadRequestWhenResponseStatusIsNotOk() {

serverMock.stubFor(get(urlPathEqualTo("/cache"))
.willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=utf-8")
.withStatus(201)
.withBody("{\"uuid\":\"2be04ba5-8f9b-4a1e-8100-d573c40312f8\"}")));
.willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=utf-8")
.withStatus(201)
.withBody("{\"uuid\":\"2be04ba5-8f9b-4a1e-8100-d573c40312f8\"}")));

final var requestMono = MockServerRequest.builder()
.method(HttpMethod.GET)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.queryParam("ch", "localhost:8080")
.build();
.queryParam("uuid", "a8db2208-d085-444c-9721-c1161d7f09ce")
.queryParam("ch", "localhost:8080")
.build();

final var responseMono = handler.fetch(requestMono);

Expand All @@ -208,10 +204,49 @@ void testVerifyFetchReturnsBadRequestWhenResponseStatusIsNotOk() {
);
}

@Test
void testVerifyFetchReturnsBadRequestWhenNoUuid() {
final var requestMono = MockServerRequest.builder()
.method(HttpMethod.GET)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.build();

final var responseMono = handler.fetch(requestMono);

StepVerifier.create(responseMono)
.consumeNextWith(assertBadRequestStatusCode())
.expectComplete()
.verify();
}

@Test
void testVerifyFetchReturnsBadRequestWhenUuidIsEmpty() {
final var requestMono = MockServerRequest.builder()
.method(HttpMethod.GET)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.queryParam("uuid", "")
.build();

final var responseMono = handler.fetch(requestMono);

StepVerifier.create(responseMono)
.consumeNextWith(assertBadRequestStatusCode())
.expectComplete()
.verify();
}

private static Consumer<Signal<ServerResponse>> assertSignalStatusCode(int statusCode) {
return signal -> {
assertTrue(signal.isOnComplete());
assertEquals(signal.get().statusCode().value(), statusCode);
};
}

private static Consumer<ServerResponse> assertNotFoundStatusCode() {
return response -> assertEquals(response.statusCode().value(), 404);
}

private static Consumer<ServerResponse> assertBadRequestStatusCode() {
return response -> assertEquals(response.statusCode().value(), 400);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {
PostCacheHandler.class,
PrebidServerResponseBuilder.class,
CacheConfig.class,
MetricsRecorderTest.class,
MetricsRecorder.class,
ApiConfig.class,
CircuitBreakerPropertyConfiguration.class
PostCacheHandler.class,
PrebidServerResponseBuilder.class,
CacheConfig.class,
MetricsRecorderTest.class,
MetricsRecorder.class,
ApiConfig.class,
CircuitBreakerPropertyConfiguration.class
})
@EnableConfigurationProperties
@SpringBootTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import org.prebid.cache.builders.PrebidServerResponseBuilder;
import org.prebid.cache.config.StorageConfig;
import org.prebid.cache.handlers.storage.PostStorageHandler;
import org.prebid.cache.model.StoragePayload;
import org.prebid.cache.model.Payload;
import org.prebid.cache.model.PayloadWrapper;
import org.prebid.cache.model.StoragePayload;
import org.prebid.cache.repository.redis.module.storage.ModuleCompositeRepository;
import org.prebid.cache.routers.ApiConfig;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.prebid.cache.metrics;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.core.instrument.MockClock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

public class MetricsRecorderTest {

Expand Down
13 changes: 12 additions & 1 deletion src/test/kotlin/org/prebid/cache/functional/GeneralCacheSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ class GeneralCacheSpec : ShouldSpec({
// then: Bad Request exception is thrown
assertSoftly {
exception.statusCode shouldBe BAD_REQUEST.value()
exception.responseBody shouldContain "\"message\":\"Invalid Parameter(s): uuid not found.\""
exception.responseBody shouldContain "\"message\":\"Invalid Parameter(s): uuid not found or is empty.\""
}
}

should("throw an exception when 'uuid' query parameter is empty") {
// when: GET cache endpoint is called with empty 'uuid' query parameter
val exception = shouldThrowExactly<ApiException> { BaseSpec.getPrebidCacheApi().getCache("") }

// then: Bad Request exception is thrown
assertSoftly {
exception.statusCode shouldBe BAD_REQUEST.value()
exception.responseBody shouldContain "\"message\":\"Invalid Parameter(s): uuid not found or is empty.\""
}
}

Expand Down
Loading