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
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,16 @@ private Function<PayloadTransfer, PayloadWrapper> payloadWrapperTransformer(Supp
// TODO: 26.09.18 is this correct behaviour to put no key in case of generated key
new Payload(transfer.getType(), transfer.getKey(), transfer.valueAsString()),
transfer.getExpiry(),
currentDateProvider.get()
currentDateProvider.get(),
RandomUUID.isExternalUUID(transfer)
);
}

private void validateUUID(final PayloadWrapper payload, final SynchronousSink<PayloadWrapper> sink) {
if(payload.isExternalId() && !config.isAllowExternalUUID()) {
sink.error(new InvalidUUIDException("Prebid cache host forbids specifying UUID in request."));
return;
}
if (RandomUUID.isValidUUID(payload.getId())) {
sink.next(payload);
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/prebid/cache/helpers/RandomUUID.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public static String extractUUID(final PayloadTransfer payload) {
payload.getKey() : String.valueOf(UUID.randomUUID());
}

public static boolean isExternalUUID(final PayloadTransfer payload) {
return payload.getKey() != null;
}

public static boolean isValidUUID(String uuid) {
if (uuid == null || uuid.length() == 0) {
log.error("UUID cannot be NULL or zero length !!");
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/prebid/cache/model/PayloadWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class PayloadWrapper {
Long expiry;
Date lastModified;

transient boolean isExternalId;

public String getNormalizedId() throws PayloadWrapperPropertyException {
if (prefix != null || id != null) {
return String.format("%s%s", prefix, id);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/prebid/cache/repository/CacheConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CacheConfig {
private int timeoutMs;
private long minExpiry;
private long maxExpiry;
private boolean allowExternalUUID;
private List<String> secondaryUris;
private String secondaryCachePath;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ cache.expiry_sec: 300
cache:
min_expiry: 60
max_expiry: 28800
allow_external_UUID: true
secondary_uris:
- "http://localhost:8080"
secondary_cache_path : "cache"

metrics.graphite.enabled: false
spring:
aerospike:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ private static PayloadWrapper createPayloadWrapper(MediaType mediaType) {
}

val payload = new Payload("json", "1234567890", payloadValue);
return new PayloadWrapper("","prefix", payload, 200L, new Date());
return new PayloadWrapper("","prefix", payload, 200L, new Date(), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private static PayloadWrapper createPayloadWrapper(MediaType mediaType) {
}

val payload = new Payload("json", "1234567890", payloadValue);
return new PayloadWrapper("", "prefix", payload, 200L, new Date());
return new PayloadWrapper("", "prefix", payload, 200L, new Date(), false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void testVerifyError() {
@Test
void tesVerifyFetch() {
val payload = new Payload("json", "2be04ba5-8f9b-4a1e-8100-d573c40312f8", "");
val payloadWrapper = new PayloadWrapper("12", "prebid_", payload, 1800L, new Date());
val payloadWrapper = new PayloadWrapper("12", "prebid_", payload, 1800L, new Date(), true);
given(repository.findById("prebid_a8db2208-d085-444c-9721-c1161d7f09ce")).willReturn(Mono.just(payloadWrapper));

val requestMono = MockServerRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import org.prebid.cache.metrics.GraphiteMetricsRecorder;
import org.prebid.cache.metrics.GraphiteTestConfig;
import org.prebid.cache.model.PayloadTransfer;
import org.prebid.cache.model.PayloadWrapper;
import org.prebid.cache.model.RequestObject;
import org.prebid.cache.repository.CacheConfig;
import org.prebid.cache.repository.ReactiveRepository;
import org.prebid.cache.repository.ReactiveTestAerospikeRepositoryContext;
import org.prebid.cache.routers.ApiConfig;
import lombok.val;
Expand All @@ -29,7 +31,9 @@
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.Date;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -55,10 +59,24 @@
class PostCacheHandlerTests extends CacheHandlerTests {

@Autowired
PostCacheHandler handler;
GraphiteMetricsRecorder metricsRecorder;

@Autowired
ReactiveRepository<PayloadWrapper, String> repository;

@Autowired
PrebidServerResponseBuilder builder;

@Autowired
CacheConfig cacheConfig;

@Autowired
Supplier<Date> currentDateProvider;

@Test
void testVerifyError() {
PostCacheHandler handler = new PostCacheHandler(repository, cacheConfig, metricsRecorder, builder, currentDateProvider);

verifyJacksonError(handler);
verifyRepositoryError(handler);
}
Expand All @@ -68,6 +86,8 @@ void testVerifyError() {

@Test
void testVerifySave() {
PostCacheHandler handler = new PostCacheHandler(repository, cacheConfig, metricsRecorder, builder, currentDateProvider);

val payload = new PayloadTransfer("json", "2be04ba5-8f9b-4a1e-8100-d573c40312f8", "", 1800L, "prebid_");
val request = Mono.just(new RequestObject(ImmutableList.of(payload)));
val requestMono = MockServerRequest.builder()
Expand Down Expand Up @@ -115,4 +135,30 @@ void testSecondaryCacheSuccess() throws InterruptedException {

verify(postRequestedFor(urlEqualTo("/cache?secondaryCache=yes")));
}

@Test
void testExternalUUIDInvalid() {
//given
CacheConfig cacheConfigLocal = new CacheConfig(cacheConfig.getPrefix(), cacheConfig.getExpirySec(), cacheConfig.getTimeoutMs(),
cacheConfig.getMinExpiry(), cacheConfig.getMaxExpiry(), false);
PostCacheHandler handler = new PostCacheHandler(repository, cacheConfigLocal, metricsRecorder, builder, currentDateProvider);

val payload = new PayloadTransfer("json", "2be04ba5-8f9b-4a1e-8100-d573c40312f8", "", 1800L, "prebid_");
val request = Mono.just(new RequestObject(ImmutableList.of(payload)));
val requestMono = MockServerRequest.builder()
.method(HttpMethod.POST)
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.body(request);

val responseMono = handler.save(requestMono);

Consumer<ServerResponse> consumer = serverResponse -> {
assertEquals(400, serverResponse.statusCode().value());
};

StepVerifier.create(responseMono)
.consumeNextWith(consumer)
.expectComplete()
.verify();
}
}
1 change: 1 addition & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cors.allowCredentials=true
cache.prefix=prebid_
cache.expiry_sec=300
cache.timeout_ms=300
cache.allow_external_UUID=true
cache.min_expiry=60
cache.max_expiry=28800
cache.secondary_uris=http://localhost:8080
Expand Down