Find by id api fix (#463)
* Updated version to 2.1.8 * Fixed find by id API to return empty on error * Added another test for findById Not Found scenario * changed this.repository to repository
This commit is contained in:
Родитель
6149596828
Коммит
a02ce1f0da
|
@ -9,6 +9,7 @@ package com.microsoft.azure.spring.data.cosmosdb.core;
|
|||
import com.azure.data.cosmos.AccessCondition;
|
||||
import com.azure.data.cosmos.AccessConditionType;
|
||||
import com.azure.data.cosmos.CosmosClient;
|
||||
import com.azure.data.cosmos.CosmosClientException;
|
||||
import com.azure.data.cosmos.CosmosContainerResponse;
|
||||
import com.azure.data.cosmos.CosmosItemProperties;
|
||||
import com.azure.data.cosmos.CosmosItemRequestOptions;
|
||||
|
@ -16,6 +17,7 @@ import com.azure.data.cosmos.CosmosItemResponse;
|
|||
import com.azure.data.cosmos.FeedOptions;
|
||||
import com.azure.data.cosmos.FeedResponse;
|
||||
import com.azure.data.cosmos.SqlQuerySpec;
|
||||
import com.azure.data.cosmos.internal.HttpConstants;
|
||||
import com.microsoft.azure.documentdb.DocumentCollection;
|
||||
import com.microsoft.azure.documentdb.PartitionKey;
|
||||
import com.microsoft.azure.spring.data.cosmosdb.CosmosDbFactory;
|
||||
|
@ -145,7 +147,15 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont
|
|||
.stream()
|
||||
.map(cosmosItem -> mappingDocumentDbConverter.read(domainClass, cosmosItem))
|
||||
.findFirst()))
|
||||
.onErrorResume(this::databaseAccessExceptionHandler)
|
||||
.onErrorResume(e -> {
|
||||
if (e instanceof CosmosClientException) {
|
||||
final CosmosClientException cosmosClientException = (CosmosClientException) e;
|
||||
if (cosmosClientException.statusCode() == HttpConstants.StatusCodes.NOTFOUND) {
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
return Mono.error(e);
|
||||
})
|
||||
.blockFirst();
|
||||
|
||||
} catch (Exception e) {
|
||||
|
@ -170,7 +180,15 @@ public class DocumentDbTemplate implements DocumentDbOperations, ApplicationCont
|
|||
.read()
|
||||
.flatMap(cosmosItemResponse -> Mono.justOrEmpty(toDomainObject(entityClass,
|
||||
cosmosItemResponse.properties())))
|
||||
.onErrorResume(this::databaseAccessExceptionHandler)
|
||||
.onErrorResume(e -> {
|
||||
if (e instanceof CosmosClientException) {
|
||||
final CosmosClientException cosmosClientException = (CosmosClientException) e;
|
||||
if (cosmosClientException.statusCode() == HttpConstants.StatusCodes.NOTFOUND) {
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
return Mono.error(e);
|
||||
})
|
||||
.block();
|
||||
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -166,12 +166,12 @@ public class AddressRepositoryIT {
|
|||
assertThat(result.get(0).getCity()).isNotEqualTo(TEST_ADDRESS1_PARTITION1.getCity());
|
||||
}
|
||||
|
||||
@Test(expected = DocumentDBAccessException.class)
|
||||
@Test
|
||||
public void testDeleteByIdAndPartitionKey() {
|
||||
final long count = repository.count();
|
||||
assertThat(count).isEqualTo(4);
|
||||
|
||||
final Optional<Address> addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
|
||||
Optional<Address> addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
|
||||
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
|
||||
assertThat(addressById.isPresent()).isTrue();
|
||||
|
||||
|
@ -181,8 +181,9 @@ public class AddressRepositoryIT {
|
|||
final List<Address> result = TestUtils.toList(repository.findAll());
|
||||
assertThat(result.size()).isEqualTo(3);
|
||||
|
||||
repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
|
||||
addressById = repository.findById(TEST_ADDRESS1_PARTITION1.getPostalCode(),
|
||||
new PartitionKey(TEST_ADDRESS1_PARTITION1.getCity()));
|
||||
assertThat(addressById.isPresent()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -160,7 +160,7 @@ public class ContactRepositoryIT {
|
|||
@Ignore // TODO(kuthapar): v3 doesn't support creation of items without id.
|
||||
public void testNullIdContact() {
|
||||
final Contact nullIdContact = new Contact(null, "testTitile");
|
||||
final Contact savedContact = this.repository.save(nullIdContact);
|
||||
final Contact savedContact = repository.save(nullIdContact);
|
||||
|
||||
Assert.assertNotNull(savedContact.getLogicId());
|
||||
Assert.assertEquals(nullIdContact.getTitle(), savedContact.getTitle());
|
||||
|
@ -168,10 +168,17 @@ public class ContactRepositoryIT {
|
|||
|
||||
@Test
|
||||
public void testFindById() {
|
||||
final Optional<Contact> optional = this.repository.findById(TEST_CONTACT.getLogicId());
|
||||
final Optional<Contact> optional = repository.findById(TEST_CONTACT.getLogicId());
|
||||
|
||||
Assert.assertTrue(optional.isPresent());
|
||||
Assert.assertEquals(TEST_CONTACT, optional.get());
|
||||
Assert.assertFalse(this.repository.findById("").isPresent());
|
||||
Assert.assertFalse(repository.findById("").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByIdNotFound() {
|
||||
final Optional<Contact> optional = repository.findById("unknown-id");
|
||||
|
||||
Assert.assertFalse(optional.isPresent());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -358,6 +358,14 @@ public class ProjectRepositoryIT {
|
|||
Assert.assertEquals(project.get(), PROJECT_0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByIdWithPartitionKeyNotFound() {
|
||||
final Optional<Project> project = repository.findById("unknown-id",
|
||||
new PartitionKey("unknown-partition-key"));
|
||||
|
||||
Assert.assertFalse(project.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByIn() {
|
||||
List<Project> projects = repository.findByCreatorIn(Collections.singleton(FAKE_CREATOR));
|
||||
|
|
Загрузка…
Ссылка в новой задаче