* Added consistent and better exception handling,
Added reactive repository integration test.
Fixed few API bugs

* Added request charnge and activity id as part of ResponseDiagnostics using CosmosResponseStatistics

* Updated innerException to CosmosClientException

* Updated the instance fields to final
This commit is contained in:
Kushagra Thapar 2019-12-31 00:21:00 -08:00 коммит произвёл GitHub
Родитель 8c240abc91
Коммит 0449d5acc8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 116 добавлений и 4 удалений

Просмотреть файл

@ -46,16 +46,19 @@ public class CosmosdbUtils {
cosmosResponseDiagnostics = cosmosResponse.cosmosResponseDiagnosticsString();
}
FeedResponseDiagnostics feedResponseDiagnostics = null;
ResponseDiagnostics.CosmosResponseStatistics cosmosResponseStatistics = null;
if (feedResponse != null) {
feedResponseDiagnostics = feedResponse.feedResponseDiagnostics();
cosmosResponseStatistics = new ResponseDiagnostics.CosmosResponseStatistics(feedResponse);
}
if (cosmosResponseDiagnostics == null &&
(feedResponseDiagnostics == null || feedResponseDiagnostics.toString().isEmpty())) {
(feedResponseDiagnostics == null || feedResponseDiagnostics.toString().isEmpty()) &&
cosmosResponseStatistics == null) {
log.debug("Empty response diagnostics");
return;
}
final ResponseDiagnostics responseDiagnostics =
new ResponseDiagnostics(cosmosResponseDiagnostics, feedResponseDiagnostics);
new ResponseDiagnostics(cosmosResponseDiagnostics, feedResponseDiagnostics, cosmosResponseStatistics);
// Process response diagnostics
responseDiagnosticsProcessor.processResponseDiagnostics(responseDiagnostics);

Просмотреть файл

@ -7,18 +7,33 @@
package com.microsoft.azure.spring.data.cosmosdb.core;
import com.azure.data.cosmos.CosmosResponseDiagnostics;
import com.azure.data.cosmos.FeedResponse;
import com.azure.data.cosmos.FeedResponseDiagnostics;
import lombok.AllArgsConstructor;
import com.azure.data.cosmos.Resource;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class ResponseDiagnostics {
private CosmosResponseDiagnostics cosmosResponseDiagnostics;
private FeedResponseDiagnostics feedResponseDiagnostics;
private CosmosResponseStatistics cosmosResponseStatistics;
public ResponseDiagnostics(CosmosResponseDiagnostics cosmosResponseDiagnostics,
FeedResponseDiagnostics feedResponseDiagnostics) {
this.cosmosResponseDiagnostics = cosmosResponseDiagnostics;
this.feedResponseDiagnostics = feedResponseDiagnostics;
}
public ResponseDiagnostics(CosmosResponseDiagnostics cosmosResponseDiagnostics,
FeedResponseDiagnostics feedResponseDiagnostics,
CosmosResponseStatistics cosmosResponseStatistics) {
this.cosmosResponseDiagnostics = cosmosResponseDiagnostics;
this.feedResponseDiagnostics = feedResponseDiagnostics;
this.cosmosResponseStatistics = cosmosResponseStatistics;
}
@Override
public String toString() {
@ -29,10 +44,41 @@ public class ResponseDiagnostics {
.append("}");
}
if (feedResponseDiagnostics != null) {
if (diagnostics.length() != 0) {
diagnostics.append(", ");
}
diagnostics.append("feedResponseDiagnostics={")
.append(feedResponseDiagnostics)
.append("}");
}
if (cosmosResponseStatistics != null) {
if (diagnostics.length() != 0) {
diagnostics.append(", ");
}
diagnostics.append("cosmosResponseStatistics={")
.append(cosmosResponseStatistics)
.append("}");
}
return diagnostics.toString();
}
@Getter
public static class CosmosResponseStatistics {
private final double requestCharge;
private final String activityId;
public <T extends Resource> CosmosResponseStatistics(FeedResponse<T> feedResponse) {
this.requestCharge = feedResponse.requestCharge();
this.activityId = feedResponse.activityId();
}
@Override
public String toString() {
return "CosmosResponseStatistics{" +
"requestCharge=" + requestCharge +
", activityId='" + activityId + '\'' +
'}';
}
}
}

Просмотреть файл

@ -33,4 +33,8 @@ public class ResponseDiagnosticsTestUtils {
public FeedResponseDiagnostics getFeedResponseDiagnostics() {
return diagnostics == null ? null : diagnostics.getFeedResponseDiagnostics();
}
public ResponseDiagnostics.CosmosResponseStatistics getCosmosResponseStatistics() {
return diagnostics == null ? null : diagnostics.getCosmosResponseStatistics();
}
}

Просмотреть файл

@ -140,6 +140,8 @@ public class CosmosTemplateIT {
assertThat(result.size()).isEqualTo(1);
assertThat(result.get(0)).isEqualTo(TEST_PERSON);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -148,6 +150,8 @@ public class CosmosTemplateIT {
TEST_PERSON.getId(), Person.class);
assertEquals(result, TEST_PERSON);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
final Person nullResult = cosmosTemplate.findById(Person.class.getSimpleName(),
NOT_EXIST_ID, Person.class);
@ -165,6 +169,8 @@ public class CosmosTemplateIT {
final List<Person> result = cosmosTemplate.findByIds(ids, Person.class, collectionName);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
final List<Person> expected = Lists.newArrayList(TEST_PERSON, TEST_PERSON_2, TEST_PERSON_3);
assertThat(result.size()).isEqualTo(expected.size());
@ -186,10 +192,13 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
final List<Person> result = cosmosTemplate.findAll(Person.class);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertThat(result.size()).isEqualTo(1);
assertEquals(result.get(0).getFirstName(), firstName);
@ -205,11 +214,14 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
final Person result = cosmosTemplate.findById(Person.class.getSimpleName(),
updated.getId(), Person.class);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertEquals(result, updated);
}
@ -247,10 +259,13 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
final List<Person> result = cosmosTemplate.findAll(Person.class);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertThat(result.size()).isEqualTo(1);
assertEquals(result.get(0), TEST_PERSON_2);
}
@ -261,6 +276,8 @@ public class CosmosTemplateIT {
assertThat(prevCount).isEqualTo(1);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
cosmosTemplate.insert(TEST_PERSON_2,
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_2)));
@ -272,6 +289,8 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -281,6 +300,7 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName",
Collections.singletonList(TEST_PERSON_2.getFirstName()));
@ -290,6 +310,8 @@ public class CosmosTemplateIT {
assertThat(count).isEqualTo(1);
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -299,6 +321,7 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
final CosmosPageRequest pageRequest = new CosmosPageRequest(0, PAGE_SIZE_1, null);
final Page<Person> page1 = cosmosTemplate.findAll(pageRequest, Person.class, collectionName);
@ -308,6 +331,8 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
final Page<Person> page2 = cosmosTemplate.findAll(page1.getPageable(), Person.class,
collectionName);
@ -316,6 +341,8 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -325,6 +352,7 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "firstName",
Collections.singletonList(FIRST_NAME));
@ -337,6 +365,8 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -348,6 +378,7 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
final Sort sort = Sort.by(Sort.Direction.DESC, "firstName");
final PageRequest pageRequest = new CosmosPageRequest(0, PAGE_SIZE_3, null, sort);
@ -363,6 +394,8 @@ public class CosmosTemplateIT {
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}

Просмотреть файл

@ -143,6 +143,8 @@ public class ReactiveCosmosTemplateIT {
.consumeNextWith(actual -> Assert.assertEquals(actual, TEST_PERSON))
.verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -157,6 +159,8 @@ public class ReactiveCosmosTemplateIT {
}).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -166,6 +170,8 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(flux).expectNextCount(1).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -176,6 +182,8 @@ public class ReactiveCosmosTemplateIT {
.verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
}
@Test
@ -185,6 +193,7 @@ public class ReactiveCosmosTemplateIT {
.expectNext(TEST_PERSON_3).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
}
@ -196,6 +205,7 @@ public class ReactiveCosmosTemplateIT {
.expectNext(TEST_PERSON_3).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
}
@ -206,6 +216,7 @@ public class ReactiveCosmosTemplateIT {
.expectNext(TEST_PERSON_2).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
}
@ -220,6 +231,7 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(upsert).expectNextCount(1).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
}
@ -235,6 +247,7 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(upsert).expectNextCount(1).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
}
@ -249,6 +262,7 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(upsert).expectNextCount(1).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
}
@ -258,12 +272,15 @@ public class ReactiveCosmosTemplateIT {
new PartitionKey(personInfo.getPartitionKeyFieldValue(TEST_PERSON_4))).block();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
Flux<Person> flux = cosmosTemplate.findAll(Person.class.getSimpleName(), Person.class);
StepVerifier.create(flux).expectNextCount(2).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
final Mono<Void> voidMono = cosmosTemplate.deleteById(Person.class.getSimpleName(),
@ -272,12 +289,15 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(voidMono).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNotNull();
flux = cosmosTemplate.findAll(Person.class.getSimpleName(), Person.class);
StepVerifier.create(flux).expectNextCount(1).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
}
@ -306,6 +326,8 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(personFlux).expectNextCount(1).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
}
@ -318,6 +340,8 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(exists).expectNext(true).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
}
@ -327,6 +351,8 @@ public class ReactiveCosmosTemplateIT {
StepVerifier.create(count).expectNext((long) 1).verifyComplete();
assertThat(responseDiagnosticsTestUtils.getFeedResponseDiagnostics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics()).isNotNull();
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
assertThat(responseDiagnosticsTestUtils.getCosmosResponseDiagnostics()).isNull();
}