|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.apache.hc.client5.http.impl.cache.caffeine; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 34 | + |
| 35 | +import java.time.Instant; |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import com.github.benmanes.caffeine.cache.Cache; |
| 40 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 41 | + |
| 42 | +import org.apache.hc.client5.http.cache.HttpCacheCASOperation; |
| 43 | +import org.apache.hc.client5.http.cache.HttpCacheEntry; |
| 44 | +import org.apache.hc.client5.http.cache.HttpCacheStorageEntry; |
| 45 | +import org.apache.hc.client5.http.cache.Resource; |
| 46 | +import org.apache.hc.client5.http.cache.ResourceIOException; |
| 47 | +import org.apache.hc.client5.http.impl.cache.CacheConfig; |
| 48 | +import org.apache.hc.client5.http.impl.cache.HeapResource; |
| 49 | +import org.apache.hc.core5.http.Header; |
| 50 | +import org.apache.hc.core5.http.HttpStatus; |
| 51 | +import org.apache.hc.core5.http.message.HeaderGroup; |
| 52 | +import org.junit.jupiter.api.Test; |
| 53 | + |
| 54 | +class TestCaffeineHttpCacheStorage { |
| 55 | + |
| 56 | + private static HttpCacheEntry newEntry(final int status) throws ResourceIOException { |
| 57 | + final Instant now = Instant.now(); |
| 58 | + final Header[] responseHeaders = new Header[0]; |
| 59 | + final Resource resource = new HeapResource(new byte[]{1, 2, 3}); |
| 60 | + |
| 61 | + final HeaderGroup requestHeaderGroup = new HeaderGroup(); |
| 62 | + final HeaderGroup responseHeaderGroup = new HeaderGroup(); |
| 63 | + responseHeaderGroup.setHeaders(responseHeaders); |
| 64 | + |
| 65 | + // Use the non-deprecated @Internal constructor |
| 66 | + return new HttpCacheEntry( |
| 67 | + now, |
| 68 | + now, |
| 69 | + "GET", |
| 70 | + "/", |
| 71 | + requestHeaderGroup, |
| 72 | + status, |
| 73 | + responseHeaderGroup, |
| 74 | + resource, |
| 75 | + null); |
| 76 | + } |
| 77 | + |
| 78 | + private static CacheConfig newConfig() { |
| 79 | + return CacheConfig.custom() |
| 80 | + .setMaxUpdateRetries(3) |
| 81 | + .build(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testPutGetRemoveObjectCache() throws Exception { |
| 86 | + final Cache<String, HttpCacheStorageEntry> cache = Caffeine.newBuilder().build(); |
| 87 | + final CacheConfig config = newConfig(); |
| 88 | + final CaffeineHttpCacheStorage<HttpCacheStorageEntry> storage = |
| 89 | + CaffeineHttpCacheStorage.createObjectCache(cache, config); |
| 90 | + |
| 91 | + final String key = "foo"; |
| 92 | + final HttpCacheEntry entry = newEntry(HttpStatus.SC_OK); |
| 93 | + |
| 94 | + storage.putEntry(key, entry); |
| 95 | + |
| 96 | + final HttpCacheEntry result = storage.getEntry(key); |
| 97 | + assertNotNull(result); |
| 98 | + assertEquals(HttpStatus.SC_OK, result.getStatus()); |
| 99 | + |
| 100 | + storage.removeEntry(key); |
| 101 | + assertNull(storage.getEntry(key)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testUpdateEntryObjectCache() throws Exception { |
| 106 | + final Cache<String, HttpCacheStorageEntry> cache = Caffeine.newBuilder().build(); |
| 107 | + final CacheConfig config = newConfig(); |
| 108 | + final CaffeineHttpCacheStorage<HttpCacheStorageEntry> storage = |
| 109 | + CaffeineHttpCacheStorage.createObjectCache(cache, config); |
| 110 | + |
| 111 | + final String key = "bar"; |
| 112 | + final HttpCacheEntry original = newEntry(HttpStatus.SC_OK); |
| 113 | + storage.putEntry(key, original); |
| 114 | + |
| 115 | + final HttpCacheCASOperation casOperation = existing -> { |
| 116 | + assertNotNull(existing); |
| 117 | + |
| 118 | + final HeaderGroup requestHeaderGroup = new HeaderGroup(); |
| 119 | + requestHeaderGroup.setHeaders(existing.requestHeaders().getHeaders()); |
| 120 | + |
| 121 | + final HeaderGroup responseHeaderGroup = new HeaderGroup(); |
| 122 | + responseHeaderGroup.setHeaders(existing.responseHeaders().getHeaders()); |
| 123 | + |
| 124 | + return new HttpCacheEntry( |
| 125 | + existing.getRequestInstant(), |
| 126 | + existing.getResponseInstant(), |
| 127 | + existing.getRequestMethod(), |
| 128 | + existing.getRequestURI(), |
| 129 | + requestHeaderGroup, |
| 130 | + HttpStatus.SC_NOT_MODIFIED, |
| 131 | + responseHeaderGroup, |
| 132 | + existing.getResource(), |
| 133 | + existing.getVariants()); |
| 134 | + }; |
| 135 | + |
| 136 | + storage.updateEntry(key, casOperation); |
| 137 | + |
| 138 | + final HttpCacheEntry updated = storage.getEntry(key); |
| 139 | + assertNotNull(updated); |
| 140 | + assertEquals(HttpStatus.SC_NOT_MODIFIED, updated.getStatus()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testGetEntriesUsesBulkRestore() throws Exception { |
| 145 | + final Cache<String, HttpCacheStorageEntry> cache = Caffeine.newBuilder().build(); |
| 146 | + final CacheConfig config = newConfig(); |
| 147 | + final CaffeineHttpCacheStorage<HttpCacheStorageEntry> storage = |
| 148 | + CaffeineHttpCacheStorage.createObjectCache(cache, config); |
| 149 | + |
| 150 | + final HttpCacheEntry entry1 = newEntry(HttpStatus.SC_OK); |
| 151 | + final HttpCacheEntry entry2 = newEntry(HttpStatus.SC_CREATED); |
| 152 | + |
| 153 | + storage.putEntry("k1", entry1); |
| 154 | + storage.putEntry("k2", entry2); |
| 155 | + |
| 156 | + final Map<String, HttpCacheEntry> result = |
| 157 | + storage.getEntries(Arrays.asList("k1", "k2", "k3")); |
| 158 | + |
| 159 | + assertEquals(2, result.size()); |
| 160 | + assertEquals(HttpStatus.SC_OK, result.get("k1").getStatus()); |
| 161 | + assertEquals(HttpStatus.SC_CREATED, result.get("k2").getStatus()); |
| 162 | + assertFalse(result.containsKey("k3")); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + void testSerializedCacheStoresBytes() throws Exception { |
| 167 | + final Cache<String, byte[]> cache = Caffeine.<String, byte[]>newBuilder().build(); |
| 168 | + final CacheConfig config = newConfig(); |
| 169 | + final CaffeineHttpCacheStorage<byte[]> storage = |
| 170 | + CaffeineHttpCacheStorage.createSerializedCache(cache, config); |
| 171 | + |
| 172 | + final String key = "baz"; |
| 173 | + final HttpCacheEntry entry = newEntry(HttpStatus.SC_OK); |
| 174 | + |
| 175 | + storage.putEntry(key, entry); |
| 176 | + |
| 177 | + // Underlying cache should contain serialized bytes |
| 178 | + final byte[] stored = cache.getIfPresent(key); |
| 179 | + assertNotNull(stored); |
| 180 | + assertTrue(stored.length > 0); |
| 181 | + |
| 182 | + final HttpCacheEntry result = storage.getEntry(key); |
| 183 | + assertNotNull(result); |
| 184 | + assertEquals(HttpStatus.SC_OK, result.getStatus()); |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments