Skip to content

Commit 69e0a1d

Browse files
committed
DATACOUCH-666 - Handle immutable entity in replace. (#288)
Co-authored-by: mikereiche <[email protected]>
1 parent bce764c commit 69e0a1d

File tree

4 files changed

+49
-47
lines changed

4 files changed

+49
-47
lines changed

src/main/java/org/springframework/data/couchbase/core/ReactiveReplaceByIdOperationSupport.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ public Mono<T> one(T object) {
7272
return Mono.just(object).flatMap(o -> {
7373
CouchbaseDocument converted = template.support().encodeEntity(o);
7474
return template.getCollection(collection).reactive()
75-
.replace(converted.getId(), converted.export(), buildReplaceOptions(o)).map(result -> {
76-
template.support().applyUpdatedCas(o, result.cas());
77-
return o;
78-
});
75+
.replace(converted.getId(), converted.export(), buildReplaceOptions(o)).map(result ->
76+
(T)template.support().applyUpdatedCas(o, result.cas()));
7977
}).onErrorMap(throwable -> {
8078
if (throwable instanceof RuntimeException) {
8179
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.couchbase.CouchbaseClientFactory;
4141
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
4242
import org.springframework.data.couchbase.domain.Config;
43+
import org.springframework.data.couchbase.domain.PersonValue;
4344
import org.springframework.data.couchbase.domain.User;
4445
import org.springframework.data.couchbase.domain.UserAnnotated;
4546
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
@@ -146,44 +147,6 @@ void upsertWithExpiryAnnotation() {
146147
}
147148
}
148149

149-
@Test
150-
void replaceWithExpiry() {
151-
User user = new User(UUID.randomUUID().toString(), "firstname", "lastname");
152-
try {
153-
User modified = couchbaseTemplate.upsertById(User.class).withExpiry(Duration.ofSeconds(1)).one(user);
154-
couchbaseTemplate.replaceById(User.class).withExpiry(Duration.ofSeconds(1)).one(user);
155-
assertEquals(user, modified);
156-
sleepSecs(2);
157-
User found = couchbaseTemplate.findById(User.class).one(user.getId());
158-
assertNull(found, "found should have been null as document should be expired");
159-
} finally {
160-
try {
161-
couchbaseTemplate.removeById().one(user.getId());
162-
} catch (DataRetrievalFailureException e) {
163-
//
164-
}
165-
}
166-
}
167-
168-
@Test
169-
void replaceWithExpiryAnnotation() {
170-
UserAnnotated user = new UserAnnotated(UUID.randomUUID().toString(), "firstname", "lastname");
171-
try {
172-
UserAnnotated modified = couchbaseTemplate.upsertById(UserAnnotated.class).one(user);
173-
modified = couchbaseTemplate.replaceById(UserAnnotated.class).one(user);
174-
assertEquals(user, modified);
175-
sleepSecs(6);
176-
User found = couchbaseTemplate.findById(UserAnnotated.class).one(user.getId());
177-
assertNull(found, "found should have been null as document should be expired");
178-
} finally {
179-
try {
180-
couchbaseTemplate.removeById().one(user.getId());
181-
} catch (DataRetrievalFailureException e) {
182-
//
183-
}
184-
}
185-
}
186-
187150
@Test
188151
void findDocWhichDoesNotExist() {
189152
assertNull(couchbaseTemplate.findById(User.class).one(UUID.randomUUID().toString()));
@@ -293,6 +256,50 @@ void existsById() {
293256

294257
}
295258

259+
@Test
260+
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
261+
void saveAndFindImmutableById() {
262+
PersonValue personValue = new PersonValue(null, 123, "f", "l");
263+
System.out.println("personValue: " + personValue);
264+
// personValue = personValue.withVersion(123);
265+
PersonValue inserted = null;
266+
PersonValue upserted = null;
267+
PersonValue replaced = null;
268+
269+
try {
270+
271+
inserted = couchbaseTemplate.insertById(PersonValue.class).one(personValue);
272+
assertNotEquals(0, inserted.getVersion());
273+
PersonValue foundInserted = couchbaseTemplate.findById(PersonValue.class).one(inserted.getId());
274+
assertNotNull(foundInserted, "inserted personValue not found");
275+
assertEquals(inserted, foundInserted);
276+
277+
// upsert will be inserted
278+
couchbaseTemplate.removeById().one(inserted.getId());
279+
upserted = couchbaseTemplate.upsertById(PersonValue.class).one(inserted);
280+
assertNotEquals(0, upserted.getVersion());
281+
PersonValue foundUpserted = couchbaseTemplate.findById(PersonValue.class).one(upserted.getId());
282+
assertNotNull(foundUpserted, "upserted personValue not found");
283+
assertEquals(upserted, foundUpserted);
284+
285+
// upsert will be replaced
286+
upserted = couchbaseTemplate.upsertById(PersonValue.class).one(inserted);
287+
assertNotEquals(0, upserted.getVersion());
288+
PersonValue foundUpserted2 = couchbaseTemplate.findById(PersonValue.class).one(upserted.getId());
289+
assertNotNull(foundUpserted2, "upserted personValue not found");
290+
assertEquals(upserted, foundUpserted2);
291+
292+
replaced = couchbaseTemplate.replaceById(PersonValue.class).one(upserted);
293+
assertNotEquals(0, replaced.getVersion());
294+
PersonValue foundReplaced = couchbaseTemplate.findById(PersonValue.class).one(replaced.getId());
295+
assertNotNull(foundReplaced, "replaced personValue not found");
296+
assertEquals(replaced, foundReplaced);
297+
298+
} finally {
299+
couchbaseTemplate.removeById().one(inserted.getId());
300+
}
301+
}
302+
296303
private void sleepSecs(int i) {
297304
try {
298305
Thread.sleep(i * 1000);

src/test/java/org/springframework/data/couchbase/domain/PersonValue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
public class PersonValue {
3636
@Id @GeneratedValue(strategy = GenerationStrategy.UNIQUE)
3737
@With String id;
38-
// @Version @With
38+
@Version
39+
@With
3940
long version;
4041
@Field String firstname;
4142
@Field String lastname;

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryKeyValueIntegrationTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,9 @@ void saveAndFindById() {
9999
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
100100
void saveAndFindImmutableById() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
101101
PersonValue personValue = new PersonValue(null, 0, "f", "l");
102-
//assertFalse(personValueRepository.existsById(personValue.getId()));
103102
personValue = personValueRepository.save(personValue);
104103
Optional<PersonValue> found = personValueRepository.findById(personValue.getId());
105104
assertTrue(found.isPresent());
106-
Method m = PersonValue.class.getMethod("equals", Object.class);
107-
m.invoke(personValue, new Object[] { found.get() });
108-
personValue.equals(found.get());
109105
assertEquals(personValue, found.get());
110106
personValueRepository.delete(personValue);
111107
}

0 commit comments

Comments
 (0)