From 87cee52c57c09f361794aa17df55270c6d082e9a Mon Sep 17 00:00:00 2001 From: Connie Date: Fri, 13 Aug 2021 14:12:12 -0700 Subject: [PATCH] Adding integration tests. --- .../azure-data-schemaregistry/pom.xml | 6 + ...maRegistryAsyncClientIntegrationTests.java | 156 ++++++++++++++++++ ...ationTests.registerAndGetCachedSchema.json | 29 ++++ ...IntegrationTests.registerAndGetSchema.json | 53 ++++++ 4 files changed, 244 insertions(+) create mode 100644 sdk/schemaregistry/azure-data-schemaregistry/src/test/java/com/azure/data/schemaregistry/SchemaRegistryAsyncClientIntegrationTests.java create mode 100644 sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetCachedSchema.json create mode 100644 sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetSchema.json diff --git a/sdk/schemaregistry/azure-data-schemaregistry/pom.xml b/sdk/schemaregistry/azure-data-schemaregistry/pom.xml index a0971f2d9af3b..21b7b774d3685 100644 --- a/sdk/schemaregistry/azure-data-schemaregistry/pom.xml +++ b/sdk/schemaregistry/azure-data-schemaregistry/pom.xml @@ -87,6 +87,12 @@ 1.10.1 test + + com.azure + azure-core-test + 1.7.0 + test + com.azure azure-identity diff --git a/sdk/schemaregistry/azure-data-schemaregistry/src/test/java/com/azure/data/schemaregistry/SchemaRegistryAsyncClientIntegrationTests.java b/sdk/schemaregistry/azure-data-schemaregistry/src/test/java/com/azure/data/schemaregistry/SchemaRegistryAsyncClientIntegrationTests.java new file mode 100644 index 0000000000000..9c6cb9a6094be --- /dev/null +++ b/sdk/schemaregistry/azure-data-schemaregistry/src/test/java/com/azure/data/schemaregistry/SchemaRegistryAsyncClientIntegrationTests.java @@ -0,0 +1,156 @@ +package com.azure.data.schemaregistry; + +import com.azure.core.credential.AccessToken; +import com.azure.core.credential.TokenCredential; +import com.azure.core.credential.TokenRequestContext; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.core.test.TestBase; +import com.azure.data.schemaregistry.models.SerializationType; +import com.azure.identity.DefaultAzureCredentialBuilder; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import java.nio.charset.StandardCharsets; +import java.time.OffsetDateTime; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SchemaRegistryAsyncClientIntegrationTests extends TestBase { + private static final int RESOURCE_LENGTH = 16; + private static final String AZURE_EVENTHUBS_FULLY_QUALIFIED_DOMAIN_NAME = "AZURE_EVENTHUBS_FULLY_QUALIFIED_DOMAIN_NAME"; + private static final String SCHEMA_CONTENT = "{\"type\" : \"record\",\"namespace\" : \"TestSchema\",\"name\" : \"Employee\",\"fields\" : [{ \"name\" : \"Name\" , \"type\" : \"string\" },{ \"name\" : \"Age\", \"type\" : \"int\" }]}"; + private static final String SCHEMA_GROUP = "at"; + + SchemaRegistryClientBuilder builder; + + @Override + protected void afterTest() { + Mockito.framework().clearInlineMocks(); + super.afterTest(); + } + + /** + * Verifies that we can register a schema and then get it by its schemaId. + */ + @Test + public void registerAndGetSchema() { + // Arrange + initializeBuilder(); + + final String schemaName = testResourceNamer.randomName("sch", RESOURCE_LENGTH); + final SchemaRegistryAsyncClient client1 = builder.buildAsyncClient(); + final SchemaRegistryAsyncClient client2 = builder.buildAsyncClient(); + + final AtomicReference schemaId = new AtomicReference<>(); + + // Act & Assert + StepVerifier.create(client1.registerSchema(SCHEMA_GROUP, schemaName, SCHEMA_CONTENT, SerializationType.AVRO)) + .assertNext(response -> { + assertEquals(schemaName, response.getSchemaName()); + assertNotNull(response.getSchemaId()); + schemaId.set(response.getSchemaId()); + + final String contents = new String(response.getSchema(), StandardCharsets.UTF_8); + assertEquals(SCHEMA_CONTENT, contents); + }).verifyComplete(); + + + // Assert that we can get a schema based on its id. We registered a schema with client1 and its response is + // cached, so it won't make a network call when getting the schema. client2 will not have this information. + final String schemaIdToGet = schemaId.get(); + assertNotNull(schemaIdToGet); + + // Act & Assert + StepVerifier.create(client2.getSchema(schemaIdToGet)) + .assertNext(schema -> { + assertEquals(schemaIdToGet, schema.getSchemaId()); + assertEquals(SerializationType.AVRO, schema.getSerializationType()); + + final String contents = new String(schema.getSchema(), StandardCharsets.UTF_8); + assertEquals(SCHEMA_CONTENT, contents); + }) + .verifyComplete(); + } + + /** + * Verifies that we can register a schema and then get it by its schemaId. + */ + @Test + public void registerAndGetCachedSchema() { + // Arrange + initializeBuilder(); + + final String schemaName = testResourceNamer.randomName("sch", RESOURCE_LENGTH); + final SchemaRegistryAsyncClient client1 = builder.buildAsyncClient(); + + final AtomicReference schemaId = new AtomicReference<>(); + + // Act & Assert + StepVerifier.create(client1.registerSchema(SCHEMA_GROUP, schemaName, SCHEMA_CONTENT, SerializationType.AVRO)) + .assertNext(response -> { + assertEquals(schemaName, response.getSchemaName()); + assertNotNull(response.getSchemaId()); + schemaId.set(response.getSchemaId()); + + final String contents = new String(response.getSchema(), StandardCharsets.UTF_8); + assertEquals(SCHEMA_CONTENT, contents); + }).verifyComplete(); + + // Assert that we can get a schema based on its id. We registered a schema with client1 and its response is + // cached, so it won't make a network call when getting the schema. client2 will not have this information. + final String schemaIdToGet = schemaId.get(); + assertNotNull(schemaIdToGet); + + // Act & Assert + StepVerifier.create(client1.getSchema(schemaIdToGet)) + .assertNext(schema -> { + assertEquals(schemaIdToGet, schema.getSchemaId()); + assertEquals(SerializationType.AVRO, schema.getSerializationType()); + + final String contents = new String(schema.getSchema(), StandardCharsets.UTF_8); + assertEquals(SCHEMA_CONTENT, contents); + }) + .verifyComplete(); + } + + void initializeBuilder() { + final String endpoint; + TokenCredential tokenCredential; + if (interceptorManager.isPlaybackMode()) { + tokenCredential = mock(TokenCredential.class); + when(tokenCredential.getToken(any(TokenRequestContext.class))) + .thenReturn(Mono.fromCallable(() -> { + return new AccessToken("foo", OffsetDateTime.now().plusMinutes(20)); + })); + endpoint = "https://foo.servicebus.windows.net"; + } else { + tokenCredential = new DefaultAzureCredentialBuilder().build(); + endpoint = System.getenv(AZURE_EVENTHUBS_FULLY_QUALIFIED_DOMAIN_NAME); + + assertNotNull(endpoint, "'endpoint' cannot be null in LIVE/RECORD mode."); + } + + builder = new SchemaRegistryClientBuilder() + .credential(tokenCredential) + .httpLogOptions(new HttpLogOptions() + .setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS) + .setPrettyPrintBody(true)) + .endpoint(endpoint); + + if (interceptorManager.isPlaybackMode()) { + builder.httpClient(interceptorManager.getPlaybackClient()); + } else { + builder.addPolicy(new RetryPolicy()) + .addPolicy(interceptorManager.getRecordPolicy()); + } + } +} diff --git a/sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetCachedSchema.json b/sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetCachedSchema.json new file mode 100644 index 0000000000000..c7a0a3c4659e0 --- /dev/null +++ b/sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetCachedSchema.json @@ -0,0 +1,29 @@ +{ + "networkCallRecords" : [ { + "Method" : "PUT", + "Uri" : "https://REDACTED.servicebus.windows.net/$schemagroups/at/schemas/sch561229205?api-version=2020-09-01-preview", + "Headers" : { + "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.5; Windows 10; 10.0)", + "x-ms-client-request-id" : "11d0df64-0a71-4f4b-8521-5b5ddc2803fb", + "Content-Type" : "text/plain; charset=utf-8" + }, + "Response" : { + "Transfer-Encoding" : "chunked", + "Schema-Version" : "1", + "Server" : "Microsoft-HTTPAPI/2.0", + "retry-after" : "0", + "Schema-Id-Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/getschemabyid/039a07d691a8408f90806aa1faaf4f0e?api-version=2020-09-01-preview", + "StatusCode" : "200", + "Date" : "Fri, 13 Aug 2021 20:51:26 GMT", + "Strict-Transport-Security" : "max-age=31536000", + "Schema-Id" : "039a07d691a8408f90806aa1faaf4f0e", + "Serialization-Type" : "Avro", + "Body" : "{\"id\":\"039a07d691a8408f90806aa1faaf4f0e\"}", + "Content-Type" : "application/json", + "Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/at/schemas/sch561229205/versions/1?api-version=2020-09-01-preview", + "Schema-Versions-Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/at/schemas/sch561229205/versions?api-version=2020-09-01-preview" + }, + "Exception" : null + } ], + "variables" : [ "sch561229205" ] +} \ No newline at end of file diff --git a/sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetSchema.json b/sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetSchema.json new file mode 100644 index 0000000000000..a545f58bb0a8b --- /dev/null +++ b/sdk/schemaregistry/azure-data-schemaregistry/src/test/resources/session-records/SchemaRegistryAsyncClientIntegrationTests.registerAndGetSchema.json @@ -0,0 +1,53 @@ +{ + "networkCallRecords" : [ { + "Method" : "PUT", + "Uri" : "https://REDACTED.servicebus.windows.net/$schemagroups/at/schemas/sch37030b272?api-version=2020-09-01-preview", + "Headers" : { + "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.5; Windows 10; 10.0)", + "x-ms-client-request-id" : "4562b10b-9f58-462d-bf96-1752292c365b", + "Content-Type" : "text/plain; charset=utf-8" + }, + "Response" : { + "Transfer-Encoding" : "chunked", + "Schema-Version" : "1", + "Server" : "Microsoft-HTTPAPI/2.0", + "retry-after" : "0", + "Schema-Id-Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/getschemabyid/e082f67552fc4ba380b791978fe9c4ad?api-version=2020-09-01-preview", + "StatusCode" : "200", + "Date" : "Fri, 13 Aug 2021 20:51:25 GMT", + "Strict-Transport-Security" : "max-age=31536000", + "Schema-Id" : "e082f67552fc4ba380b791978fe9c4ad", + "Serialization-Type" : "Avro", + "Body" : "{\"id\":\"e082f67552fc4ba380b791978fe9c4ad\"}", + "Content-Type" : "application/json", + "Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/at/schemas/sch37030b272/versions/1?api-version=2020-09-01-preview", + "Schema-Versions-Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/at/schemas/sch37030b272/versions?api-version=2020-09-01-preview" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://REDACTED.servicebus.windows.net/$schemagroups/getSchemaById/e082f67552fc4ba380b791978fe9c4ad?api-version=2020-09-01-preview", + "Headers" : { + "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.5; Windows 10; 10.0)", + "x-ms-client-request-id" : "19b06b5d-bc17-49f5-b81b-9e9d65476e52" + }, + "Response" : { + "Transfer-Encoding" : "chunked", + "Schema-Version" : "1", + "Server" : "Microsoft-HTTPAPI/2.0", + "retry-after" : "0", + "Schema-Id-Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/getschemabyid/e082f67552fc4ba380b791978fe9c4ad?api-version=2020-09-01-preview", + "StatusCode" : "200", + "Date" : "Fri, 13 Aug 2021 20:51:36 GMT", + "Strict-Transport-Security" : "max-age=31536000", + "Schema-Id" : "e082f67552fc4ba380b791978fe9c4ad", + "Serialization-Type" : "Avro", + "Body" : "{\"type\":\"record\",\"namespace\":\"TestSchema\",\"name\":\"Employee\",\"fields\":[{\"name\":\"Name\",\"type\":\"string\"},{\"name\":\"Age\",\"type\":\"int\"}]}", + "Content-Type" : "application/json", + "Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/at/schemas/sch37030b272/versions/1?api-version=2020-09-01-preview", + "Schema-Versions-Location" : "https://conniey.servicebus.windows.net:443/$schemagroups/at/schemas/sch37030b272/versions?api-version=2020-09-01-preview" + }, + "Exception" : null + } ], + "variables" : [ "sch37030b272" ] +} \ No newline at end of file