30
30
import org .mockito .ArgumentCaptor ;
31
31
import org .mockito .Mock ;
32
32
import org .mockito .runners .MockitoJUnitRunner ;
33
+ import org .springframework .core .convert .converter .Converter ;
34
+ import org .springframework .core .convert .support .GenericConversionService ;
35
+ import org .springframework .dao .InvalidDataAccessApiUsageException ;
33
36
import org .springframework .data .redis .connection .RedisConnection ;
34
- import org .springframework .data .redis .core .convert .PathIndexResolver ;
35
37
import org .springframework .data .redis .core .convert .IndexedData ;
36
38
import org .springframework .data .redis .core .convert .MappingRedisConverter ;
39
+ import org .springframework .data .redis .core .convert .PathIndexResolver ;
37
40
import org .springframework .data .redis .core .convert .ReferenceResolver ;
38
41
import org .springframework .data .redis .core .convert .SimpleIndexedPropertyValue ;
39
42
import org .springframework .data .redis .core .mapping .RedisMappingContext ;
43
+ import org .springframework .util .ObjectUtils ;
40
44
41
45
/**
42
46
* @author Christoph Strobl
@@ -50,15 +54,15 @@ public class IndexWriterUnitTests {
50
54
private static final String KEY = "key-1" ;
51
55
private static final byte [] KEY_BIN = KEY .getBytes (CHARSET );
52
56
IndexWriter writer ;
57
+ MappingRedisConverter converter ;
53
58
54
59
@ Mock RedisConnection connectionMock ;
55
60
@ Mock ReferenceResolver referenceResolverMock ;
56
61
57
62
@ Before
58
63
public void setUp () {
59
64
60
- MappingRedisConverter converter = new MappingRedisConverter (new RedisMappingContext (), new PathIndexResolver (),
61
- referenceResolverMock );
65
+ converter = new MappingRedisConverter (new RedisMappingContext (), new PathIndexResolver (), referenceResolverMock );
62
66
converter .afterPropertiesSet ();
63
67
64
68
writer = new IndexWriter (connectionMock , converter );
@@ -143,6 +147,37 @@ public void removeAllIndexesShouldDeleteAllIndexKeys() {
143
147
assertThat (captor .getAllValues (), hasItems (indexKey1 , indexKey2 ));
144
148
}
145
149
150
+ /**
151
+ * @see DATAREDIS-425
152
+ */
153
+ @ Test (expected = InvalidDataAccessApiUsageException .class )
154
+ public void addToIndexShouldThrowDataAccessExceptionWhenAddingDataThatConnotBeConverted () {
155
+ writer .addKeyToIndex (KEY_BIN , new SimpleIndexedPropertyValue (KEYSPACE , "firstname" , new DummyObject ()));
156
+
157
+ }
158
+
159
+ /**
160
+ * @see DATAREDIS-425
161
+ */
162
+ @ Test
163
+ public void addToIndexShouldUseRegisteredConverterWhenAddingData () {
164
+
165
+ DummyObject value = new DummyObject ();
166
+ final String identityHexString = ObjectUtils .getIdentityHexString (value );
167
+
168
+ ((GenericConversionService ) converter .getConversionService ()).addConverter (new Converter <DummyObject , byte []>() {
169
+
170
+ @ Override
171
+ public byte [] convert (DummyObject source ) {
172
+ return identityHexString .getBytes (CHARSET );
173
+ }
174
+ });
175
+
176
+ writer .addKeyToIndex (KEY_BIN , new SimpleIndexedPropertyValue (KEYSPACE , "firstname" , value ));
177
+
178
+ verify (connectionMock ).sAdd (eq (("persons:firstname:" + identityHexString ).getBytes (CHARSET )), eq (KEY_BIN ));
179
+ }
180
+
146
181
static class StubIndxedData implements IndexedData {
147
182
148
183
@ Override
@@ -154,6 +189,9 @@ public String getIndexName() {
154
189
public String getKeySpace () {
155
190
return KEYSPACE ;
156
191
}
192
+ }
193
+
194
+ static class DummyObject {
157
195
158
196
}
159
197
}
0 commit comments