Skip to content

Commit

Permalink
Adding integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
conniey committed Aug 13, 2021
1 parent a00b577 commit 87cee52
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sdk/schemaregistry/azure-data-schemaregistry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
<version>1.10.1</version> <!-- {x-version-update;org.apache.avro:avro;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.7.0</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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" ]
}
Original file line number Diff line number Diff line change
@@ -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" ]
}

0 comments on commit 87cee52

Please sign in to comment.