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

Added spock test framework and tests for queue service #4940

Merged
merged 14 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
17 changes: 4 additions & 13 deletions sdk/storage/azure-storage-queue/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,22 @@
<version>1.0.0-preview.4</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.spockframework/spock-core -->
<dependency>
<groupId>com.azure</groupId>
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>azure-identity</artifactId>
<version>1.0.0-preview.3</version>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.3-groovy-2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.azure.storage.queue.models.SignedIdentifier;
import com.azure.storage.queue.models.StorageErrorException;
import com.azure.storage.queue.models.UpdatedMessage;
import java.util.Objects;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -62,6 +63,7 @@ public final class QueueAsyncClient {
* @param queueName Name of the queue
*/
QueueAsyncClient(AzureQueueStorageImpl client, String queueName) {
Objects.requireNonNull(queueName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should be in QueueServiceClient as it allows for a null queue name to be passed, the builders should already be checking for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the entries which asked the queueName will fall into this constructor.
I think it is better to have null checking here and remove the one in buildAsyncClient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that is true, having it remain in each individually will offer a closer indication to what went wrong and in the case of the client builder it will early out much sooner.

this.queueName = queueName;

this.client = new AzureQueueStorageBuilder().pipeline(client.getHttpPipeline())
Expand All @@ -79,6 +81,7 @@ public final class QueueAsyncClient {
* @param queueName Name of the queue
*/
QueueAsyncClient(URL endpoint, HttpPipeline httpPipeline, String queueName) {
Objects.requireNonNull(queueName);
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
this.queueName = queueName;

this.client = new AzureQueueStorageBuilder().pipeline(httpPipeline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public QueueClient buildClient() {
*/
public QueueAsyncClient buildAsyncClient() {
Objects.requireNonNull(endpoint);
Objects.requireNonNull(queueName);

if (sasTokenCredential == null && sharedKeyCredential == null) {
LOGGER.error("Credentials are required for authorization");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public final class StorageErrorCode extends ExpandableStringEnum<StorageErrorCod
*/
public static final StorageErrorCode ACCOUNT_IS_DISABLED = fromString("AccountIsDisabled");

/**
* Static value AuthenticationError for StorageErrorCode.
*/
public static final StorageErrorCode AUTHENTICATION_ERROR = fromString("AuthenticationError");
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved

/**
* Static value AuthenticationFailed for StorageErrorCode.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Contains helper methods for unit tests.
*/
class TestHelpers {
private final String azureStorageConnectionString = "AZURE_STORAGE_CONNECTION_STRING";
private final String azureStorageConnectionString = "AZURE_STORAGE_QUEUE_CONNECTION_STRING";
private final String azureStorageQueueEndpoint = "AZURE_STORAGE_QUEUE_ENDPOINT";

<T> T setupClient(BiFunction<String, String, T> clientBuilder, boolean isPlaybackMode, ClientLogger logger) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.storage.queue.spock

import com.azure.core.test.InterceptorManager
import com.azure.core.test.TestMode
import com.azure.core.test.utils.TestResourceNamer
import com.azure.core.util.configuration.ConfigurationManager
import com.azure.core.util.logging.ClientLogger
import com.azure.storage.queue.QueueClientBuilder
import com.azure.storage.queue.QueueServiceClient
import com.azure.storage.queue.QueueServiceClientBuilder
import spock.lang.Specification

class APISpec extends Specification {
// Field common used for all APIs.
def logger = new ClientLogger(APISpec.class)
def AZURE_TEST_MODE = "AZURE_TEST_MODE"
def interceptorManager
def testResourceName

// Clients for API tests
def primaryQueueServiceClient
def primaryQueueServiceAsyncClient


// Test name for test method name.
def methodName
def testMode = getTestMode()
def connectionString = ConfigurationManager.getConfiguration().get("AZURE_STORAGE_QUEUE_CONNECTION_STRING")

/**
* Setup the QueueServiceClient and QueueClient common used for the API tests.
*/
def setup() {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
String testName = refactorName(specificationContext.currentIteration.getName())
String className = specificationContext.currentSpec.getFilename().split("\\.")[0]
methodName = className + testName
logger.info("Test Mode: {}, Name: {}", testMode, methodName)
interceptorManager = new InterceptorManager(methodName, testMode)
testResourceName = new TestResourceNamer(methodName, testMode,
interceptorManager.getRecordedData())
}

/**
* Clean up the test queues and messages for the account.
*/
def cleanup() {
interceptorManager.close()
if (getTestMode() == TestMode.RECORD) {
QueueServiceClient cleanupQueueServiceClient = new QueueServiceClientBuilder()
.connectionString(connectionString)
.buildClient()
cleanupQueueServiceClient.listQueues().each {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
queueItem -> cleanupQueueServiceClient.deleteQueue(queueItem.name())
}
}
}

/**
* Test mode is initialized whenever test is executed. Helper method which is used to determine what to do under
* certain test mode.
* @return The TestMode:
* <ul>
* <li>Playback: (default if no test mode setup)</li>
* </ul>
*/
def getTestMode() {
def azureTestMode = ConfigurationManager.getConfiguration().get(AZURE_TEST_MODE)

if (azureTestMode != null) {
try {
return TestMode.valueOf(azureTestMode.toUpperCase(Locale.US))
} catch (IllegalArgumentException e) {
logger.error("Could not parse '{}' into TestEnum. Using 'Playback' mode.", azureTestMode)
return TestMode.PLAYBACK
}
}

logger.info("Environment variable '{}' has not been set yet. Using 'Playback' mode.", AZURE_TEST_MODE)
return TestMode.PLAYBACK
}

def queueServiceBuilderHelper(final InterceptorManager interceptorManager) {
if (testMode == TestMode.RECORD) {
return new QueueServiceClientBuilder()
.connectionString(connectionString)
.addPolicy(interceptorManager.getRecordPolicy())
} else {
return new QueueServiceClientBuilder()
.connectionString(connectionString)
.httpClient(interceptorManager.getPlaybackClient())
}
}

def queueBuilderHelper(final InterceptorManager interceptorManager) {
def queueName = testResourceName.randomName("queue", 16)
if (testMode == TestMode.RECORD) {
return new QueueClientBuilder()
.connectionString(connectionString)
.queueName(queueName)
.addPolicy(interceptorManager.getRecordPolicy())
} else {
return new QueueClientBuilder()
.connectionString(connectionString)
.queueName(queueName)
.httpClient(interceptorManager.getPlaybackClient())
}
}

private def refactorName(String text) {
def fullName = text.split(" ").collect { it.capitalize() }.join("")
def matcher = (fullName =~ /(.*)(\[)(.*)(\])/)

if (!matcher.find()) {
return fullName
}
return matcher[0][1] + matcher[0][3]
}
}
Loading