DATACASS-696 - Fix return of null values through selectOne(…).
Null values are now correctly handled when selecting a single element.
This commit is contained in:
@@ -294,9 +294,8 @@ public class AsyncCassandraTemplate implements AsyncCassandraOperations, Applica
|
||||
*/
|
||||
@Override
|
||||
public <T> ListenableFuture<T> selectOne(Statement statement, Class<T> entityClass) {
|
||||
|
||||
return new MappingListenableFutureAdapter<>(select(statement, entityClass),
|
||||
list -> list.stream().findFirst().orElse(null));
|
||||
list -> list.isEmpty() ? null : list.get(0));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@@ -299,7 +299,9 @@ public class CassandraTemplate implements CassandraOperations, ApplicationEventP
|
||||
*/
|
||||
@Override
|
||||
public <T> T selectOne(Statement statement, Class<T> entityClass) {
|
||||
return select(statement, entityClass).stream().findFirst().orElse(null);
|
||||
|
||||
List<T> result = select(statement, entityClass);
|
||||
return result.isEmpty() ? null : result.get(0);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@@ -696,7 +696,9 @@ public class ReactiveCassandraTemplate implements ReactiveCassandraOperations, A
|
||||
|
||||
T result = (T) (targetType.isInterface() ? getProjectionFactory().createProjection(targetType, source) : source);
|
||||
|
||||
maybeEmitEvent(new AfterConvertEvent<>(row, result, tableName));
|
||||
if (result != null) {
|
||||
maybeEmitEvent(new AfterConvertEvent<>(row, result, tableName));
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
package org.springframework.data.cassandra.core;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -36,6 +34,7 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.data.cassandra.CassandraConnectionFailureException;
|
||||
import org.springframework.data.cassandra.core.query.Query;
|
||||
import org.springframework.data.cassandra.domain.User;
|
||||
@@ -185,6 +184,16 @@ public class AsyncCassandraTemplateUnitTests {
|
||||
assertThat(statementCaptor.getValue().toString()).isEqualTo("SELECT * FROM users WHERE id='myid';");
|
||||
}
|
||||
|
||||
@Test // DATACASS-696
|
||||
public void selectOneShouldNull() {
|
||||
|
||||
when(resultSet.iterator()).thenReturn(Collections.singleton(row).iterator());
|
||||
|
||||
ListenableFuture<String> future = template.selectOne("SELECT id FROM users WHERE id='myid';", String.class);
|
||||
|
||||
assertThat(getUninterruptibly(future)).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACASS-292
|
||||
public void existsShouldReturnExistingElement() {
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -146,6 +147,14 @@ public class CassandraTemplateIntegrationTests extends AbstractKeyspaceCreatingI
|
||||
assertThat(loaded).isEqualTo(token1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSelectNullByQuery() {
|
||||
|
||||
UUID loaded = template.selectOne("SELECT MAX(user_id) FROM user_tokens", UUID.class);
|
||||
|
||||
System.out.println(loaded);
|
||||
}
|
||||
|
||||
@Test // DATACASS-292, DATACASS-573
|
||||
public void insertShouldInsertEntity() {
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ public class CassandraTemplateUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATACASS-292
|
||||
public void selectShouldTranslateException() throws Exception {
|
||||
public void selectShouldTranslateException() {
|
||||
|
||||
when(resultSet.iterator()).thenThrow(new NoHostAvailableException(Collections.emptyMap()));
|
||||
|
||||
@@ -130,6 +130,16 @@ public class CassandraTemplateUnitTests {
|
||||
assertThat(statementCaptor.getValue().toString()).isEqualTo("SELECT * FROM users WHERE id='myid';");
|
||||
}
|
||||
|
||||
@Test // DATACASS-696
|
||||
public void selectOneShouldNull() {
|
||||
|
||||
when(resultSet.iterator()).thenReturn(Collections.singleton(row).iterator());
|
||||
|
||||
String nullValue = template.selectOne("SELECT id FROM users WHERE id='myid';", String.class);
|
||||
|
||||
assertThat(nullValue).isNull();
|
||||
}
|
||||
|
||||
@Test // DATACASS-292
|
||||
public void selectOneByIdShouldReturnMappedResults() {
|
||||
|
||||
|
||||
@@ -15,20 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collections;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -131,6 +127,15 @@ public class ReactiveCassandraTemplateUnitTests {
|
||||
assertThat(statementCaptor.getValue().toString()).isEqualTo("SELECT * FROM users WHERE id='myid';");
|
||||
}
|
||||
|
||||
@Test // DATACASS-696
|
||||
public void selectOneShouldNull() {
|
||||
|
||||
when(reactiveResultSet.rows()).thenReturn(Flux.just(row));
|
||||
|
||||
template.selectOne("SELECT id FROM users WHERE id='myid';", String.class).as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATACASS-335
|
||||
public void existsShouldReturnExistingElement() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user