Skip to content

Commit 6508492

Browse files
committed
test: add test fixtures and update SpEL indexing tests
Add new test entity fixtures: - ComplexSpelEntity: Entity with complex SpEL expressions for testing - SystemPropertyEntity: Entity using system property SpEL expressions Add BulkIndexOperationsIntegrationTest for testing batch index operations and update DynamicIndexingSpelTest and MultiTenantIndexIsolationIntegrationTest with improved test cases for dynamic index resolution.
1 parent ab8dcc5 commit 6508492

File tree

5 files changed

+315
-46
lines changed

5 files changed

+315
-46
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.redis.om.spring.fixtures.document.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
import com.redis.om.spring.annotations.Document;
6+
import com.redis.om.spring.annotations.IndexCreationMode;
7+
import com.redis.om.spring.annotations.IndexingOptions;
8+
9+
/**
10+
* Test entity with complex SpEL expression for conditional index naming.
11+
* Uses environment property to determine index name based on environment.
12+
*/
13+
@Document
14+
@IndexingOptions(
15+
indexName = "#{@environment.getProperty('app.environment') == 'production' ? 'prod_complex_idx' : 'dev_complex_idx'}",
16+
creationMode = IndexCreationMode.DROP_AND_RECREATE
17+
)
18+
public class ComplexSpelEntity {
19+
@Id
20+
private String id;
21+
private String data;
22+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getData() {
32+
return data;
33+
}
34+
35+
public void setData(String data) {
36+
this.data = data;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.redis.om.spring.fixtures.document.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
import com.redis.om.spring.annotations.Document;
6+
import com.redis.om.spring.annotations.IndexCreationMode;
7+
import com.redis.om.spring.annotations.IndexingOptions;
8+
9+
/**
10+
* Test entity that uses System properties in SpEL expression for index naming.
11+
* Demonstrates dynamic index name resolution using JVM system properties.
12+
*/
13+
@Document
14+
@IndexingOptions(
15+
indexName = "system_idx_#{T(java.lang.System).getProperty('custom.index.suffix')}",
16+
creationMode = IndexCreationMode.DROP_AND_RECREATE
17+
)
18+
public class SystemPropertyEntity {
19+
@Id
20+
private String id;
21+
private String content;
22+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getContent() {
32+
return content;
33+
}
34+
35+
public void setContent(String content) {
36+
this.content = content;
37+
}
38+
}
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
package com.redis.om.spring.indexing;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Set;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.annotation.Id;
10+
import org.springframework.test.annotation.DirtiesContext;
11+
12+
import com.redis.om.spring.AbstractBaseDocumentTest;
13+
import com.redis.om.spring.annotations.Document;
14+
import com.redis.om.spring.annotations.Indexed;
15+
16+
/**
17+
* Integration tests for bulk index operations in RediSearchIndexer.
18+
* Tests createIndexes(), dropIndexes(), and listIndexes() methods.
19+
*/
20+
@DirtiesContext
21+
public class BulkIndexOperationsIntegrationTest extends AbstractBaseDocumentTest {
22+
23+
@Autowired
24+
private RediSearchIndexer indexer;
25+
26+
@Test
27+
void testListIndexes_returnsAllManagedIndexNames() {
28+
// Given: Several entity classes have been registered with the indexer
29+
// (The AbstractBaseDocumentTest scans fixtures and creates indexes at startup)
30+
31+
// When: Listing all managed indexes
32+
Set<String> managedIndexes = indexer.listIndexes();
33+
34+
// Then: Should return a non-empty set of index names
35+
assertThat(managedIndexes)
36+
.as("listIndexes should return managed index names")
37+
.isNotNull()
38+
.isNotEmpty();
39+
}
40+
41+
@Test
42+
void testListIndexes_returnsDefensiveCopy() {
43+
// When: Getting the list of indexes
44+
Set<String> managedIndexes = indexer.listIndexes();
45+
46+
// Then: Should return a HashSet (defensive copy), not internal collection
47+
assertThat(managedIndexes.getClass().getSimpleName())
48+
.as("Should return a HashSet copy")
49+
.isEqualTo("HashSet");
50+
}
51+
52+
@Test
53+
void testCreateIndexes_doesNotThrowException() {
54+
// Given: Indexes already exist from startup
55+
56+
// When: Calling createIndexes (should be idempotent)
57+
// Then: Should not throw exception
58+
indexer.createIndexes();
59+
60+
// Verify indexes still exist
61+
Set<String> indexes = indexer.listIndexes();
62+
assertThat(indexes).isNotEmpty();
63+
}
64+
65+
@Test
66+
void testCreateIndexes_createsNewlyRegisteredIndex() {
67+
// Given: A new entity class is registered
68+
String indexName = indexer.getIndexName(BulkTestEntity1.class);
69+
70+
// First, ensure it doesn't exist
71+
if (indexer.indexExistsFor(BulkTestEntity1.class)) {
72+
indexer.dropIndexFor(BulkTestEntity1.class);
73+
}
74+
75+
// Manually add to keyspace mapping to register it
76+
indexer.addKeySpaceMapping("bulktestentity1", BulkTestEntity1.class);
77+
78+
// When: Creating all indexes
79+
indexer.createIndexes();
80+
81+
// Then: The new index should be created
82+
assertThat(indexer.indexExistsFor(BulkTestEntity1.class))
83+
.as("BulkTestEntity1 index should exist after createIndexes()")
84+
.isTrue();
85+
86+
// Cleanup
87+
indexer.dropIndexFor(BulkTestEntity1.class);
88+
}
89+
90+
@Test
91+
void testDropIndexes_removesIndexesFromTracking() {
92+
// Given: We have some indexes - create a test index first
93+
indexer.createIndexFor(BulkTestEntity1.class);
94+
Set<String> indexesBefore = indexer.listIndexes();
95+
int countBefore = indexesBefore.size();
96+
assertThat(countBefore).isGreaterThan(0);
97+
98+
// Verify our test index is in the list
99+
String testIndexName = BulkTestEntity1.class.getName() + "Idx";
100+
assertThat(indexesBefore).contains(testIndexName);
101+
102+
// When: Dropping all indexes
103+
indexer.dropIndexes();
104+
105+
// Then: The tracking should be cleared or reduced
106+
Set<String> indexesAfter = indexer.listIndexes();
107+
108+
// The test index should no longer be tracked
109+
assertThat(indexesAfter)
110+
.as("Test index should be removed from tracking after dropIndexes")
111+
.doesNotContain(testIndexName);
112+
}
113+
114+
@Test
115+
void testDropIndexes_doesNotThrowWhenCalledMultipleTimes() {
116+
// Given: Indexes may or may not exist
117+
118+
// When: Calling dropIndexes multiple times
119+
// Then: Should not throw exception
120+
indexer.dropIndexes();
121+
indexer.dropIndexes();
122+
123+
// No exception means success
124+
}
125+
126+
@Test
127+
void testCreateIndexForSpecificEntity() {
128+
// Given: A specific entity class
129+
String indexName = BulkTestEntity2.class.getName() + "Idx";
130+
131+
// Ensure clean state
132+
if (indexer.indexExistsFor(BulkTestEntity2.class)) {
133+
indexer.dropIndexFor(BulkTestEntity2.class);
134+
}
135+
136+
// When: Creating index for specific entity
137+
indexer.createIndexFor(BulkTestEntity2.class);
138+
139+
// Then: Index should exist
140+
assertThat(indexer.indexExistsFor(BulkTestEntity2.class))
141+
.as("Index should exist after createIndexFor")
142+
.isTrue();
143+
144+
assertThat(indexer.listIndexes())
145+
.as("listIndexes should include the newly created index")
146+
.contains(indexName);
147+
148+
// Cleanup
149+
indexer.dropIndexFor(BulkTestEntity2.class);
150+
}
151+
152+
// Test entities for bulk operations
153+
@Document
154+
static class BulkTestEntity1 {
155+
@Id
156+
private String id;
157+
158+
@Indexed
159+
private String name;
160+
161+
@Indexed
162+
private String category;
163+
164+
public String getId() {
165+
return id;
166+
}
167+
168+
public void setId(String id) {
169+
this.id = id;
170+
}
171+
172+
public String getName() {
173+
return name;
174+
}
175+
176+
public void setName(String name) {
177+
this.name = name;
178+
}
179+
180+
public String getCategory() {
181+
return category;
182+
}
183+
184+
public void setCategory(String category) {
185+
this.category = category;
186+
}
187+
}
188+
189+
@Document
190+
static class BulkTestEntity2 {
191+
@Id
192+
private String id;
193+
194+
@Indexed
195+
private String title;
196+
197+
@Indexed
198+
private Integer count;
199+
200+
public String getId() {
201+
return id;
202+
}
203+
204+
public void setId(String id) {
205+
this.id = id;
206+
}
207+
208+
public String getTitle() {
209+
return title;
210+
}
211+
212+
public void setTitle(String title) {
213+
this.title = title;
214+
}
215+
216+
public Integer getCount() {
217+
return count;
218+
}
219+
220+
public void setCount(Integer count) {
221+
this.count = count;
222+
}
223+
}
224+
}

tests/src/test/java/com/redis/om/spring/indexing/DynamicIndexingSpelTest.java

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
package com.redis.om.spring.indexing;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.junit.jupiter.api.Assertions.assertThrows;
54

65
import java.util.Optional;
76

87
import org.junit.jupiter.api.BeforeEach;
98
import org.junit.jupiter.api.Test;
109
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.boot.test.context.SpringBootTest;
12-
import org.springframework.boot.test.context.TestConfiguration;
1310
import org.springframework.context.ApplicationContext;
1411
import org.springframework.context.annotation.Bean;
1512
import org.springframework.context.annotation.Configuration;
1613
import org.springframework.core.env.Environment;
17-
import org.springframework.test.annotation.DirtiesContext;
1814
import org.springframework.test.context.TestPropertySource;
1915

2016
import com.redis.om.spring.AbstractBaseDocumentTest;
21-
import com.redis.om.spring.annotations.Document;
22-
import com.redis.om.spring.annotations.EnableRedisDocumentRepositories;
23-
import com.redis.om.spring.annotations.IndexCreationMode;
2417
import com.redis.om.spring.annotations.IndexingOptions;
18+
import com.redis.om.spring.fixtures.document.model.BeanReferenceIndexEntity;
19+
import com.redis.om.spring.fixtures.document.model.ComplexSpelEntity;
2520
import com.redis.om.spring.fixtures.document.model.DynamicIndexEntity;
2621
import com.redis.om.spring.fixtures.document.model.DynamicPrefixEntity;
2722
import com.redis.om.spring.fixtures.document.model.EnvironmentBasedIndexEntity;
28-
import com.redis.om.spring.fixtures.document.model.BeanReferenceIndexEntity;
2923
import com.redis.om.spring.fixtures.document.model.FallbackIndexEntity;
24+
import com.redis.om.spring.fixtures.document.model.SystemPropertyEntity;
3025
import com.redis.om.spring.fixtures.document.model.TenantAwareEntity;
3126
import com.redis.om.spring.fixtures.document.model.VersionedIndexEntity;
3227
import com.redis.om.spring.fixtures.document.repository.DynamicIndexEntityRepository;
@@ -241,38 +236,4 @@ void testRepositoryOperationsWithDynamicIndex() {
241236
}
242237

243238

244-
// Additional test entity classes for complex scenarios
245-
@Document
246-
@IndexingOptions(
247-
indexName = "#{@environment.getProperty('app.environment') == 'production' ? 'prod_complex_idx' : 'dev_complex_idx'}",
248-
creationMode = IndexCreationMode.DROP_AND_RECREATE
249-
)
250-
static class ComplexSpelEntity {
251-
@org.springframework.data.annotation.Id
252-
private String id;
253-
private String data;
254-
255-
// getters and setters
256-
public String getId() { return id; }
257-
public void setId(String id) { this.id = id; }
258-
public String getData() { return data; }
259-
public void setData(String data) { this.data = data; }
260-
}
261-
262-
@Document
263-
@IndexingOptions(
264-
indexName = "system_idx_#{T(java.lang.System).getProperty('custom.index.suffix')}",
265-
creationMode = IndexCreationMode.DROP_AND_RECREATE
266-
)
267-
static class SystemPropertyEntity {
268-
@org.springframework.data.annotation.Id
269-
private String id;
270-
private String content;
271-
272-
// getters and setters
273-
public String getId() { return id; }
274-
public void setId(String id) { this.id = id; }
275-
public String getContent() { return content; }
276-
public void setContent(String content) { this.content = content; }
277-
}
278239
}

0 commit comments

Comments
 (0)