|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.data.couchbase.cache; |
| 18 | + |
| 19 | +import com.couchbase.client.java.query.QueryOptions; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.springframework.data.couchbase.util.Capabilities; |
| 23 | +import org.springframework.data.couchbase.util.ClusterType; |
| 24 | +import org.springframework.data.couchbase.util.CollectionAwareIntegrationTests; |
| 25 | +import org.springframework.data.couchbase.util.IgnoreWhen; |
| 26 | + |
| 27 | +import java.util.UUID; |
| 28 | + |
| 29 | +import static com.couchbase.client.java.query.QueryScanConsistency.REQUEST_PLUS; |
| 30 | +import static org.junit.Assert.assertNotNull; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 33 | + |
| 34 | +/** |
| 35 | + * CouchbaseCache tests Theses tests rely on a cb server running. |
| 36 | + * |
| 37 | + * @author Michael Reiche |
| 38 | + */ |
| 39 | +@IgnoreWhen(clusterTypes = ClusterType.MOCKED, missesCapabilities = { Capabilities.COLLECTIONS }) |
| 40 | +class CouchbaseCacheCollectionIntegrationTests extends CollectionAwareIntegrationTests { |
| 41 | + |
| 42 | + volatile CouchbaseCache cache; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + @Override |
| 46 | + public void beforeEach() { |
| 47 | + super.beforeEach(); |
| 48 | + cache = CouchbaseCacheManager.create(couchbaseTemplate.getCouchbaseClientFactory()).createCouchbaseCache("myCache", |
| 49 | + CouchbaseCacheConfiguration.defaultCacheConfig().collection("my_collection")); |
| 50 | + clear(cache); |
| 51 | + } |
| 52 | + |
| 53 | + private void clear(CouchbaseCache c) { |
| 54 | + couchbaseTemplate.getCouchbaseClientFactory().getCluster().query("SELECT count(*) from `" + bucketName() + "`", |
| 55 | + QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)); |
| 56 | + c.clear(); |
| 57 | + couchbaseTemplate.getCouchbaseClientFactory().getCluster().query("SELECT count(*) from `" + bucketName() + "`", |
| 58 | + QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Test |
| 63 | + void cachePutGet() { |
| 64 | + CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1"); |
| 65 | + CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2"); |
| 66 | + assertNull(cache.get(user1.getId())); // was not put -> cacheMiss |
| 67 | + cache.put(user1.getId(), user1); // put user1 |
| 68 | + cache.put(user2.getId(), user2); // put user2 |
| 69 | + assertEquals(user1, cache.get(user1.getId()).get()); // get user1 |
| 70 | + assertEquals(user2, cache.get(user2.getId()).get()); // get user2 |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void cacheEvict() { |
| 75 | + CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1"); |
| 76 | + CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2"); |
| 77 | + cache.put(user1.getId(), user1); // put user1 |
| 78 | + cache.put(user2.getId(), user2); // put user2 |
| 79 | + cache.evict(user1.getId()); // evict user1 |
| 80 | + assertEquals(user2, cache.get(user2.getId()).get()); // get user2 -> present |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void cacheHitMiss() { |
| 85 | + CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1"); |
| 86 | + CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2"); |
| 87 | + assertNull(cache.get(user2.getId())); // get user2 -> cacheMiss |
| 88 | + cache.put(user1.getId(), null); // cache a null |
| 89 | + assertNotNull(cache.get(user1.getId())); // cacheHit null |
| 90 | + assertNull(cache.get(user1.getId()).get()); // fetch cached null |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void cachePutIfAbsent() { |
| 95 | + CacheUser user1 = new CacheUser(UUID.randomUUID().toString(), "first1", "last1"); |
| 96 | + CacheUser user2 = new CacheUser(UUID.randomUUID().toString(), "first2", "last2"); |
| 97 | + assertNull(cache.putIfAbsent(user1.getId(), user1)); // should put user1, return null |
| 98 | + assertEquals(user1, cache.putIfAbsent(user1.getId(), user2).get()); // should not put user2, should return user1 |
| 99 | + assertEquals(user1, cache.get(user1.getId()).get()); // user1.getId() is still user1 |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments