Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting fixes from v3 to v4 #6012

Merged
merged 5 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.data.cosmos.internal.Strings;
import com.azure.data.cosmos.internal.Utils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -32,14 +33,14 @@ public class JsonSerializable {
private final static ObjectMapper OBJECT_MAPPER = Utils.getSimpleObjectMapper();
private ObjectMapper om;
transient ObjectNode propertyBag = null;

protected JsonSerializable() {
this.propertyBag = OBJECT_MAPPER.createObjectNode();
}

/**
* Constructor.
*
*
* @param jsonString the json string that represents the JsonSerializable.
* @param objectMapper the custom object mapper
*/
Expand All @@ -48,10 +49,10 @@ protected JsonSerializable() {
this.propertyBag = fromJson(jsonString);
this.om = objectMapper;
}

/**
* Constructor.
*
*
* @param jsonString the json string that represents the JsonSerializable.
*/
protected JsonSerializable(String jsonString) {
Expand All @@ -76,7 +77,7 @@ private ObjectMapper getMapper() {
void setMapper(ObjectMapper om) {
this.om = om;
}

private static void checkForValidPOJO(Class<?> c) {
if (c.isAnonymousClass() || c.isLocalClass()) {
throw new IllegalArgumentException(
Expand All @@ -88,6 +89,7 @@ private static void checkForValidPOJO(Class<?> c) {
}
}

@JsonIgnore
public Logger getLogger() {
return logger;
}
Expand All @@ -106,7 +108,7 @@ public Map<String, Object> getMap() {

/**
* Checks whether a property exists.
*
*
* @param propertyName the property to look up.
* @return true if the property exists.
*/
Expand All @@ -116,7 +118,7 @@ public boolean has(String propertyName) {

/**
* Removes a value by propertyName.
*
*
* @param propertyName the property to remove.
*/
void remove(String propertyName) {
Expand All @@ -125,7 +127,7 @@ void remove(String propertyName) {

/**
* Sets the value of a property.
*
*
* @param <T> the type of the object.
* @param propertyName the property to set.
* @param value the value of the property.
Expand Down Expand Up @@ -183,7 +185,7 @@ private <T> void internalSetCollection(String propertyName, Collection<T> collec

/**
* Gets a property value as Object.
*
*
* @param propertyName the property to get.
* @return the value of the property.
*/
Expand All @@ -194,10 +196,10 @@ public Object get(String propertyName) {
return null;
}
}

/**
* Gets a string value.
*
*
* @param propertyName the property to get.
* @return the string value.
*/
Expand All @@ -211,7 +213,7 @@ public String getString(String propertyName) {

/**
* Gets a boolean value.
*
*
* @param propertyName the property to get.
* @return the boolean value.
*/
Expand All @@ -225,7 +227,7 @@ public Boolean getBoolean(String propertyName) {

/**
* Gets an integer value.
*
*
* @param propertyName the property to get.
* @return the boolean value
*/
Expand All @@ -239,7 +241,7 @@ public Integer getInt(String propertyName) {

/**
* Gets a long value.
*
*
* @param propertyName the property to get.
* @return the long value
*/
Expand All @@ -253,7 +255,7 @@ public Long getLong(String propertyName) {

/**
* Gets a double value.
*
*
* @param propertyName the property to get.
* @return the double value.
*/
Expand All @@ -267,7 +269,7 @@ public Double getDouble(String propertyName) {

/**
* Gets an object value.
*
*
* @param <T> the type of the object.
* @param propertyName the property to get.
* @param c the class of the object. If c is a POJO class, it must be a member (and not an anonymous or local)
Expand Down Expand Up @@ -390,7 +392,7 @@ public <T> List<T> getList(String propertyName, Class<T> c, boolean ... convertF

/**
* Gets an object collection.
*
*
* @param <T> the type of the objects in the collection.
* @param propertyName the property to get
* @param c the class of the object. If c is a POJO class, it must be a member (and not an anonymous or local)
Expand All @@ -405,7 +407,7 @@ public <T> Collection<T> getCollection(String propertyName, Class<T> c, boolean

/**
* Gets a JSONObject.
*
*
* @param propertyName the property to get.
* @return the JSONObject.
*/
Expand All @@ -419,7 +421,7 @@ ObjectNode getObject(String propertyName) {

/**
* Gets a JSONObject collection.
*
*
* @param propertyName the property to get.
* @return the JSONObject collection.
*/
Expand All @@ -433,12 +435,12 @@ Collection<ObjectNode> getCollection(String propertyName) {
}
}

return result;
return result;
}

/**
* Gets the value of a property identified by an array of property names that forms the path.
*
*
* @param propertyNames that form the path to the property to get.
* @return the value of the property.
*/
Expand All @@ -462,12 +464,12 @@ public Object getObjectByPath(List<String> propertyNames) {
break;
}
} while (iterator.hasNext());

if (value != null && matchedProperties == propertyNames.size()) {
return getValue(value);
}
}

return null;
}

Expand Down Expand Up @@ -517,7 +519,7 @@ private String toPrettyJson(Object object){

/**
* Converts to an Object (only POJOs and JSONObject are supported).
*
*
* @param <T> the type of the object.
* @param c the class of the object, either a POJO class or JSONObject. If c is a POJO class, it must be a member
* (and not an anonymous or local) and a static one.
Expand Down Expand Up @@ -547,16 +549,16 @@ public <T> T toObject(Class<T> c) {

/**
* Converts to a JSON string.
*
*
* @return the JSON string.
*/
public String toJson() {
return this.toJson(SerializationFormattingPolicy.NONE);
}

/**
* Converts to a JSON string.
*
*
* @param formattingPolicy the formatting policy to be used.
* @return the JSON string.
*/
Expand All @@ -571,10 +573,10 @@ public String toJson(SerializationFormattingPolicy formattingPolicy) {

/**
* Gets Simple STRING representation of property bag.
*
* For proper conversion to json and inclusion of the default values
*
* For proper conversion to json and inclusion of the default values
* use {@link #toJson()}.
*
*
* @return string representation of property bag.
*/
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public RequestRateTooLargeException() {
}

public RequestRateTooLargeException(CosmosError cosmosError, long lsn, String partitionKeyRangeId, Map<String, String> responseHeaders) {
super(HttpConstants.StatusCodes.NOTFOUND, cosmosError, responseHeaders);
super(HttpConstants.StatusCodes.TOO_MANY_REQUESTS, cosmosError, responseHeaders);
BridgeInternal.setLSN(this, lsn);
BridgeInternal.setPartitionKeyRangeId(this, partitionKeyRangeId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ServiceUnavailableException extends CosmosClientException {
}

public ServiceUnavailableException(CosmosError cosmosError, long lsn, String partitionKeyRangeId, Map<String, String> responseHeaders) {
super(HttpConstants.StatusCodes.NOTFOUND, cosmosError, responseHeaders);
super(HttpConstants.StatusCodes.SERVICE_UNAVAILABLE, cosmosError, responseHeaders);
BridgeInternal.setLSN(this, lsn);
BridgeInternal.setPartitionKeyRangeId(this, partitionKeyRangeId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public void onTimeout(AcquireTask task) {

if (elapsedTime > idleEndpointTimeout) {

if (logger.isWarnEnabled()) {
logger.warn(
if (logger.isDebugEnabled()) {
logger.debug(
"{} closing due to inactivity (time elapsed since last request: {} > idleEndpointTimeout: {})",
endpoint, Duration.ofNanos(elapsedTime), Duration.ofNanos(idleEndpointTimeout));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static ObjectNode readTree(final ByteBuf in) {
throw new CorruptedFrameException(cause);
}

@SuppressWarnings("SameParameterValue")
static void registerPropertyFilter(final Class<?> type, final Class<? extends PropertyFilter> filter) {

checkNotNull(type, "type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static <T extends Resource> Flux<? extends IDocumentQueryExecutionContext

// return proxy
Flux<DocumentCollection> collectionObs = Flux.empty();

if (resourceTypeEnum.isCollectionChild()) {
collectionObs = resolveCollection(client, query, resourceTypeEnum, resourceLink).flux();
}
Expand Down Expand Up @@ -109,7 +109,8 @@ public static <T extends Resource> Flux<? extends IDocumentQueryExecutionContext
int initialPageSize = Utils.getValueOrDefault(feedOptions.maxItemCount(), ParallelQueryConfig.ClientInternalPageSize);

BadRequestException validationError = Utils.checkRequestOrReturnException
(initialPageSize > 0, "MaxItemCount", "INVALID MaxItemCount %s", initialPageSize);
(initialPageSize > 0 || initialPageSize == -1, "MaxItemCount", "Invalid MaxItemCount %s",
initialPageSize);
if (validationError != null) {
return Flux.error(validationError);
}
Expand Down Expand Up @@ -144,7 +145,7 @@ public static <T extends Resource> Flux<? extends IDocumentQueryExecutionContext
// initialPageSize = Math.Min(
// (int)Math.Ceiling(initialPageSize / (double)targetRanges.Count) * PageSizeFactorForTop,
// initialPageSize);
// }
// }
}

return PipelinedDocumentQueryExecutionContext.createAsync(
Expand All @@ -160,6 +161,6 @@ public static <T extends Resource> Flux<? extends IDocumentQueryExecutionContext
initialPageSize,
isContinuationExpected,
getLazyFeedResponse,
correlatedActivityId);
correlatedActivityId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@

package com.azure.data.cosmos;

import com.azure.data.cosmos.internal.http.HttpHeaders;
import com.google.common.collect.ImmutableMap;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.BADREQUEST;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.CONFLICT;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.FORBIDDEN;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.GONE;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.INTERNAL_SERVER_ERROR;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.LOCKED;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.METHOD_NOT_ALLOWED;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.NOTFOUND;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.PRECONDITION_FAILED;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.REQUEST_ENTITY_TOO_LARGE;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.REQUEST_TIMEOUT;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.RETRY_WITH;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.SERVICE_UNAVAILABLE;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.TOO_MANY_REQUESTS;
import static com.azure.data.cosmos.internal.HttpConstants.StatusCodes.UNAUTHORIZED;
import static com.google.common.base.Strings.lenientFormat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;

public class CosmosClientExceptionTest {

Expand Down Expand Up @@ -61,4 +82,42 @@ public void headerNotNull7() {
assertThat(dce.getResponseHeaders()).isNotNull();
assertThat(dce.getResponseHeaders()).contains(respHeaders.entrySet().iterator().next());
}

@Test(groups = { "unit" }, dataProvider = "subTypes")
public void statusCodeIsCorrect(Class<CosmosClientException> type, int expectedStatusCode) {
try {
Constructor<CosmosClientException> constructor = type.getDeclaredConstructor(String.class, HttpHeaders.class, String.class);
constructor.setAccessible(true);
final CosmosClientException instance = constructor.newInstance("some-message", null, "some-uri");
assertEquals(instance.getStatusCode(), expectedStatusCode);
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException error) {
String message = lenientFormat("could not create instance of %s due to %s", type, error);
throw new AssertionError(message, error);
}
}

@DataProvider(name = "subTypes")
private static Object[][] subTypes() {
return new Object[][] {
{ BadRequestException.class, BADREQUEST },
{ GoneException.class, GONE },
{ InternalServerErrorException.class, INTERNAL_SERVER_ERROR },
{ RequestTimeoutException.class, REQUEST_TIMEOUT },
{ ConflictException.class, CONFLICT },
{ ForbiddenException.class, FORBIDDEN },
{ InvalidPartitionException.class, GONE },
{ LockedException.class, LOCKED },
{ MethodNotAllowedException.class, METHOD_NOT_ALLOWED },
{ NotFoundException.class, NOTFOUND },
{ PartitionIsMigratingException.class, GONE },
{ PartitionKeyRangeGoneException.class, GONE },
{ PartitionKeyRangeIsSplittingException.class, GONE },
{ PreconditionFailedException.class, PRECONDITION_FAILED },
{ RequestEntityTooLargeException.class, REQUEST_ENTITY_TOO_LARGE },
{ RequestRateTooLargeException.class, TOO_MANY_REQUESTS },
{ RetryWithException.class, RETRY_WITH },
{ ServiceUnavailableException.class, SERVICE_UNAVAILABLE },
{ UnauthorizedException.class, UNAUTHORIZED }
};
}
}
Loading