-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Wrap JSON Parsing Errors as CosmosException
.
#47040
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
Open
jeet1995
wants to merge
9
commits into
Azure:main
Choose a base branch
from
jeet1995:CfpParsingErrorLoggingChanges
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+247
−61
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b9ec2ae
Adding logging for non-parseable documents.
jeet1995 15d0fb9
Adding logging for non-parseable documents.
jeet1995 0c53e0a
Adding tests.
jeet1995 2b57962
Refactor.
jeet1995 e39f20c
Update CHANGELOG.md
jeet1995 63f05ca
Modified tests.
jeet1995 da5ed46
Merge branch 'main' of github.com:jeet1995/azure-sdk-for-java into Cf…
jeet1995 c76d45a
Modified tests.
jeet1995 aebaba1
Fixing tests.
jeet1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
...re-cosmos-tests/src/test/java/com/azure/cosmos/implementation/MalformedResponseTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.cosmos.implementation; | ||
|
||
import com.azure.cosmos.CosmosAsyncClient; | ||
import com.azure.cosmos.CosmosAsyncContainer; | ||
import com.azure.cosmos.CosmosClientBuilder; | ||
import com.azure.cosmos.CosmosException; | ||
import com.azure.cosmos.TestObject; | ||
import com.azure.cosmos.models.PartitionKey; | ||
import com.azure.cosmos.rx.TestSuiteBase; | ||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.testng.annotations.AfterSuite; | ||
import org.testng.annotations.BeforeSuite; | ||
import org.testng.annotations.Factory; | ||
import org.testng.annotations.Ignore; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Fail.fail; | ||
|
||
@Ignore("MalformedResponseTests is only safe to run in isolation as it leverages Reflection to override the ObjectMapper instance responsible for deserialization.") | ||
public class MalformedResponseTests extends TestSuiteBase { | ||
|
||
@Factory(dataProvider = "clientBuildersWithSessionConsistency") | ||
public MalformedResponseTests(CosmosClientBuilder clientBuilder) { | ||
super(clientBuilder); | ||
} | ||
|
||
@BeforeSuite(groups = {"emulator"}) | ||
public void beforeSuite() { | ||
System.setProperty("COSMOS.CHARSET_DECODER_ERROR_ACTION_ON_MALFORMED_INPUT", "IGNORE"); | ||
System.setProperty("COSMOS.IS_NON_PARSEABLE_DOCUMENT_LOGGING_ENABLED", "true"); | ||
super.beforeSuite(); | ||
} | ||
|
||
/** | ||
* Validate that a CosmosException is thrown with the appropriate status code and sub-status code | ||
* when the response from the server is malformed and cannot be deserialized | ||
* and fallback decoder is set / not set | ||
* <p> | ||
* NOTE: Run this test with MalformedResponseTests#beforeSuite and MalformedResponseTests#afterSuite commented out for no fallback decoder. | ||
* NOTE: Run this test with MalformedResponseTests#beforeSuite and MalformedResponseTests#afterSuite enabled for fallback decoder. | ||
* */ | ||
@Test(groups = { "emulator" }) | ||
public void validateCosmosExceptionThrownOnMalformedResponse() throws NoSuchFieldException, IllegalAccessException { | ||
|
||
CosmosAsyncClient cosmosAsyncClient = null; | ||
ObjectMapper originalMapper = null; | ||
|
||
try { | ||
cosmosAsyncClient = getClientBuilder() | ||
.key(TestConfigurations.MASTER_KEY) | ||
.endpoint(TestConfigurations.HOST) | ||
.buildAsyncClient(); | ||
CosmosAsyncContainer cosmosAsyncContainer = getSharedSinglePartitionCosmosContainer(cosmosAsyncClient); | ||
|
||
TestObject testObject = TestObject.create(); | ||
cosmosAsyncContainer.createItem(testObject).block(); | ||
|
||
Field field = Utils.class.getDeclaredField("simpleObjectMapper"); | ||
field.setAccessible(true); | ||
|
||
// Save original | ||
originalMapper = (ObjectMapper) field.get(null); | ||
|
||
// Create a bad ObjectMapper | ||
ObjectMapper badMapper = new FailingObjectMapper(); | ||
// Override | ||
field.set(null, badMapper); | ||
|
||
cosmosAsyncContainer.readItem(testObject.getId(), new PartitionKey(testObject.getMypk()), TestObject.class).block(); | ||
fail("The read operation should have failed"); | ||
} catch (CosmosException cosmosException) { | ||
assertThat(cosmosException.getStatusCode()).isEqualTo(HttpConstants.StatusCodes.INTERNAL_SERVER_ERROR); | ||
assertThat(cosmosException.getSubStatusCode()).isEqualTo(HttpConstants.SubStatusCodes.FAILED_TO_PARSE_SERVER_RESPONSE); | ||
assertThat(cosmosException.getDiagnostics()).isNotNull(); | ||
assertThat(cosmosException.getResponseHeaders()).isNotNull(); | ||
assertThat(cosmosException.getResponseHeaders()).isNotEmpty(); | ||
} catch (IllegalAccessException e) { | ||
fail("An IllegalAccessException shouldn't have occurred", e); | ||
} finally { | ||
// Restore original | ||
Field field = Utils.class.getDeclaredField("simpleObjectMapper"); | ||
field.setAccessible(true); | ||
field.set(null, originalMapper); | ||
field.setAccessible(false); | ||
|
||
safeClose(cosmosAsyncClient); | ||
} | ||
} | ||
|
||
private class FailingObjectMapper extends ObjectMapper { | ||
@Override | ||
public JsonNode readTree(byte[] bytes) throws IOException { | ||
throw new IOException("Simulated failure"); | ||
} | ||
|
||
@Override | ||
public JsonNode readTree(String content) throws JsonProcessingException { | ||
throw new JsonParseException("Simulated failure"); | ||
} | ||
} | ||
|
||
@AfterSuite(groups = {"emulator"}) | ||
public void afterSuite() { | ||
System.clearProperty("COSMOS.CHARSET_DECODER_ERROR_ACTION_ON_MALFORMED_INPUT"); | ||
System.clearProperty("COSMOS.IS_NON_PARSEABLE_DOCUMENT_LOGGING_ENABLED"); | ||
super.afterSuite(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.