-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from szjani/feature/reactive-cache-as-operator
Feature/reactive cache as operator
- Loading branch information
Showing
5 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
molten-cache/src/test/java/com/hotels/molten/cache/ReactiveCacheContract.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (c) 2020 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hotels.molten.cache; | ||
|
||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import lombok.Value; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
/** | ||
* {@code ReactiveCache} contract to test the expected behaviours. | ||
*/ | ||
public interface ReactiveCacheContract { | ||
|
||
String VALUE = "one"; | ||
int KEY = 1; | ||
|
||
<T> ReactiveCache<Integer, T> createCacheForContractTest(); | ||
|
||
@Test | ||
default void shouldCacheValueViaOperator() { | ||
ReactiveCache<Integer, String> reactiveCache = spy(createCacheForContractTest()); | ||
Mono.just(VALUE) | ||
.as(reactiveCache.cachingWith(KEY)) | ||
.as(StepVerifier::create) | ||
.expectSubscription() | ||
.thenRequest(1) | ||
.expectNext(VALUE) | ||
.verifyComplete(); | ||
|
||
Mono.<String>error(() -> new RuntimeException("Should not be called as the value is already in cache")) | ||
.as(reactiveCache.cachingWith(KEY)) | ||
.as(StepVerifier::create) | ||
.expectSubscription() | ||
.thenRequest(1) | ||
.expectNext(VALUE) | ||
.verifyComplete(); | ||
|
||
verify(reactiveCache, times(1)).put(KEY, VALUE); | ||
} | ||
|
||
@Test | ||
default void shouldConvertValueViaOperator() { | ||
ReactiveCache<Integer, String> reactiveCache = spy(createCacheForContractTest()); | ||
Mono.just(VALUE) | ||
.as(reactiveCache.cachingWith(KEY, String::toUpperCase, String::toLowerCase)) | ||
.as(StepVerifier::create) | ||
.expectSubscription() | ||
.thenRequest(1) | ||
.expectNext(VALUE) | ||
.verifyComplete(); | ||
|
||
verify(reactiveCache, times(1)).put(KEY, "ONE"); | ||
} | ||
|
||
@Test | ||
default void shouldNotFailIfThereAreNoEmittedItems() { | ||
ReactiveCache<Integer, String> reactiveCache = createCacheForContractTest(); | ||
Mono.<String>empty() | ||
.as(reactiveCache.cachingWith(KEY)) | ||
.as(StepVerifier::create) | ||
.expectSubscription() | ||
.thenRequest(1) | ||
.verifyComplete(); | ||
} | ||
|
||
@Test | ||
default void shouldWorkAsNegativeCacheViaOperator() { | ||
ReactiveCache<Integer, StringWrapper> reactiveNegativeCache = spy(createCacheForContractTest()); | ||
Mono.<String>empty() | ||
.as(reactiveNegativeCache.cachingWith(KEY, StringWrapper::new, StringWrapper::getValue)) | ||
.as(StepVerifier::create) | ||
.expectSubscription() | ||
.thenRequest(1) | ||
.verifyComplete(); | ||
|
||
Mono.<String>error(() -> new RuntimeException("Should not be called as the value is already in cache")) | ||
.as(reactiveNegativeCache.cachingWith(KEY, StringWrapper::new, StringWrapper::getValue)) | ||
.as(StepVerifier::create) | ||
.expectSubscription() | ||
.thenRequest(1) | ||
.verifyComplete(); | ||
|
||
verify(reactiveNegativeCache, times(1)).put(KEY, new StringWrapper(null)); | ||
} | ||
|
||
@Value | ||
class StringWrapper { | ||
private String value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters