|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.admin.indices.cache.hunspell; |
| 10 | + |
| 11 | +import org.opensearch.common.settings.Settings; |
| 12 | +import org.opensearch.indices.analysis.HunspellService; |
| 13 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 14 | +import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | + |
| 21 | +import static org.hamcrest.Matchers.equalTo; |
| 22 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 23 | + |
| 24 | +/** |
| 25 | + * Integration tests for hunspell cache info and invalidation APIs. |
| 26 | + * |
| 27 | + * <p>Tests the full REST→Transport→HunspellService flow on a real cluster: |
| 28 | + * <ul> |
| 29 | + * <li>GET /_hunspell/cache (HunspellCacheInfoAction)</li> |
| 30 | + * <li>POST /_hunspell/cache/_invalidate (HunspellCacheInvalidateAction)</li> |
| 31 | + * </ul> |
| 32 | + */ |
| 33 | +@ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST) |
| 34 | +public class HunspellCacheIT extends OpenSearchIntegTestCase { |
| 35 | + |
| 36 | + @Override |
| 37 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 38 | + // Enable lazy loading so dictionaries are only loaded on-demand |
| 39 | + return Settings.builder().put(super.nodeSettings(nodeOrdinal)).put(HunspellService.HUNSPELL_LAZY_LOAD.getKey(), true).build(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Sets up hunspell dictionary files in the node's config directory. |
| 44 | + * Creates both traditional and package-based dictionaries. |
| 45 | + */ |
| 46 | + private void setupDictionaries(Path configDir) throws IOException { |
| 47 | + // Traditional dictionary: config/hunspell/en_US/ |
| 48 | + Path traditionalDir = configDir.resolve("hunspell").resolve("en_US"); |
| 49 | + Files.createDirectories(traditionalDir); |
| 50 | + Files.write(traditionalDir.resolve("en_US.aff"), "SET UTF-8\n".getBytes(StandardCharsets.UTF_8)); |
| 51 | + Files.write(traditionalDir.resolve("en_US.dic"), "1\nhello\n".getBytes(StandardCharsets.UTF_8)); |
| 52 | + |
| 53 | + // Package-based dictionary: config/packages/test-pkg/hunspell/en_US/ |
| 54 | + Path packageDir = configDir.resolve("packages").resolve("test-pkg").resolve("hunspell").resolve("en_US"); |
| 55 | + Files.createDirectories(packageDir); |
| 56 | + Files.write(packageDir.resolve("en_US.aff"), "SET UTF-8\n".getBytes(StandardCharsets.UTF_8)); |
| 57 | + Files.write(packageDir.resolve("en_US.dic"), "1\nworld\n".getBytes(StandardCharsets.UTF_8)); |
| 58 | + } |
| 59 | + |
| 60 | + // ==================== Cache Info Tests ==================== |
| 61 | + |
| 62 | + public void testCacheInfoReturnsEmptyWhenNoDictionariesLoaded() { |
| 63 | + HunspellCacheInfoResponse response = client().execute(HunspellCacheInfoAction.INSTANCE, new HunspellCacheInfoRequest()).actionGet(); |
| 64 | + |
| 65 | + assertThat(response.getTotalCachedCount(), equalTo(0)); |
| 66 | + assertThat(response.getPackageBasedCount(), equalTo(0)); |
| 67 | + assertThat(response.getTraditionalLocaleCount(), equalTo(0)); |
| 68 | + assertTrue(response.getPackageBasedKeys().isEmpty()); |
| 69 | + assertTrue(response.getTraditionalLocaleKeys().isEmpty()); |
| 70 | + } |
| 71 | + |
| 72 | + // ==================== Cache Invalidation Tests ==================== |
| 73 | + |
| 74 | + public void testInvalidateAllOnEmptyCache() { |
| 75 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 76 | + request.setInvalidateAll(true); |
| 77 | + |
| 78 | + HunspellCacheInvalidateResponse response = client().execute(HunspellCacheInvalidateAction.INSTANCE, request).actionGet(); |
| 79 | + |
| 80 | + assertTrue(response.isAcknowledged()); |
| 81 | + assertThat(response.getInvalidatedCount(), equalTo(0)); |
| 82 | + } |
| 83 | + |
| 84 | + public void testInvalidateByPackageIdOnEmptyCache() { |
| 85 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 86 | + request.setPackageId("nonexistent-pkg"); |
| 87 | + |
| 88 | + HunspellCacheInvalidateResponse response = client().execute(HunspellCacheInvalidateAction.INSTANCE, request).actionGet(); |
| 89 | + |
| 90 | + assertTrue(response.isAcknowledged()); |
| 91 | + assertThat(response.getInvalidatedCount(), equalTo(0)); |
| 92 | + assertThat(response.getPackageId(), equalTo("nonexistent-pkg")); |
| 93 | + } |
| 94 | + |
| 95 | + public void testInvalidateByCacheKeyOnEmptyCache() { |
| 96 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 97 | + request.setCacheKey("nonexistent-key"); |
| 98 | + |
| 99 | + HunspellCacheInvalidateResponse response = client().execute(HunspellCacheInvalidateAction.INSTANCE, request).actionGet(); |
| 100 | + |
| 101 | + assertTrue(response.isAcknowledged()); |
| 102 | + assertThat(response.getInvalidatedCount(), equalTo(0)); |
| 103 | + } |
| 104 | + |
| 105 | + // ==================== Request Validation Tests ==================== |
| 106 | + |
| 107 | + public void testInvalidateRequestValidationFailsWithNoParameters() { |
| 108 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 109 | + // No parameters set — should fail validation |
| 110 | + |
| 111 | + assertNotNull(request.validate()); |
| 112 | + } |
| 113 | + |
| 114 | + public void testInvalidateRequestValidationFailsWithConflictingParameters() { |
| 115 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 116 | + request.setInvalidateAll(true); |
| 117 | + request.setPackageId("some-pkg"); |
| 118 | + |
| 119 | + assertNotNull(request.validate()); |
| 120 | + } |
| 121 | + |
| 122 | + public void testInvalidateRequestValidationFailsWithEmptyPackageId() { |
| 123 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 124 | + request.setPackageId(" "); |
| 125 | + |
| 126 | + assertNotNull(request.validate()); |
| 127 | + } |
| 128 | + |
| 129 | + public void testInvalidateRequestValidationPassesWithPackageIdOnly() { |
| 130 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 131 | + request.setPackageId("valid-pkg"); |
| 132 | + |
| 133 | + assertNull(request.validate()); |
| 134 | + } |
| 135 | + |
| 136 | + public void testInvalidateRequestValidationPassesWithPackageIdAndLocale() { |
| 137 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 138 | + request.setPackageId("valid-pkg"); |
| 139 | + request.setLocale("en_US"); |
| 140 | + |
| 141 | + assertNull(request.validate()); |
| 142 | + } |
| 143 | + |
| 144 | + public void testInvalidateRequestValidationFailsWithLocaleWithoutPackageId() { |
| 145 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 146 | + request.setLocale("en_US"); |
| 147 | + |
| 148 | + assertNotNull(request.validate()); |
| 149 | + } |
| 150 | + |
| 151 | + // ==================== Response Schema Tests ==================== |
| 152 | + |
| 153 | + public void testInvalidateResponseAlwaysIncludesAllFields() { |
| 154 | + HunspellCacheInvalidateRequest request = new HunspellCacheInvalidateRequest(); |
| 155 | + request.setInvalidateAll(true); |
| 156 | + |
| 157 | + HunspellCacheInvalidateResponse response = client().execute(HunspellCacheInvalidateAction.INSTANCE, request).actionGet(); |
| 158 | + |
| 159 | + // Response should always include these fields for consistent schema |
| 160 | + assertTrue(response.isAcknowledged()); |
| 161 | + assertThat(response.getInvalidatedCount(), greaterThanOrEqualTo(0)); |
| 162 | + // Null fields should still be accessible (consistent schema) |
| 163 | + assertNull(response.getPackageId()); |
| 164 | + assertNull(response.getLocale()); |
| 165 | + assertNull(response.getCacheKey()); |
| 166 | + } |
| 167 | + |
| 168 | + public void testCacheInfoResponseSchema() { |
| 169 | + HunspellCacheInfoResponse response = client().execute(HunspellCacheInfoAction.INSTANCE, new HunspellCacheInfoRequest()).actionGet(); |
| 170 | + |
| 171 | + // Verify response schema has all expected fields |
| 172 | + assertThat(response.getTotalCachedCount(), greaterThanOrEqualTo(0)); |
| 173 | + assertThat(response.getPackageBasedCount(), greaterThanOrEqualTo(0)); |
| 174 | + assertThat(response.getTraditionalLocaleCount(), greaterThanOrEqualTo(0)); |
| 175 | + assertNotNull(response.getPackageBasedKeys()); |
| 176 | + assertNotNull(response.getTraditionalLocaleKeys()); |
| 177 | + } |
| 178 | +} |
0 commit comments