From 2b90ac120291a1046c73795f979f449d95375da2 Mon Sep 17 00:00:00 2001 From: chenmliu Date: Mon, 14 Oct 2019 16:12:05 -0700 Subject: [PATCH] Search service client tests scaffolding (#143) * Search service client tests scaffolding --- .../search/IndexManagementAsyncTests.java | 62 +++++++++++++++ .../azure/search/IndexManagementSyncTest.java | 62 +++++++++++++++ .../azure/search/IndexManagementTestBase.java | 74 ++++++++++++++++++ .../search/SearchIndexClientTestBase.java | 72 +---------------- .../azure/search/SearchServiceTestBase.java | 78 +++++++++++++++++++ 5 files changed, 277 insertions(+), 71 deletions(-) create mode 100644 sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementAsyncTests.java create mode 100644 sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementSyncTest.java create mode 100644 sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementTestBase.java create mode 100644 sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceTestBase.java diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementAsyncTests.java b/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementAsyncTests.java new file mode 100644 index 0000000000000..e59dc7364e297 --- /dev/null +++ b/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementAsyncTests.java @@ -0,0 +1,62 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.search; + +public class IndexManagementAsyncTests extends IndexManagementTestBase { + private SearchServiceAsyncClient client; + + @Override + public void createIndexReturnsCorrectDefinition() { + + } + + @Override + public void createIndexReturnsCorrectDefaultValues() { + + } + + @Override + public void createIndexFailsWithUsefulMessageOnUserError() { + + } + + @Override + public void getIndexReturnsCorrectDefinition() { + + } + + @Override + public void getIndexThrowsOnNotFound() { + + } + + @Override + public void existsReturnsTrueForExistingIndex() { + + } + + @Override + public void existsReturnsFalseForNonExistingIndex() { + + } + + @Override + public void deleteIndexIfNotChangedWorksOnlyOnCurrentResource() { + + } + + @Override + public void deleteIndexIfExistsWorksOnlyWhenResourceExists() { + + } + + @Override + public void deleteIndexIsIdempotent() { + + } + + @Override + public void canCreateAndDeleteIndex() { + + } +} diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementSyncTest.java b/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementSyncTest.java new file mode 100644 index 0000000000000..3141e1203ee9b --- /dev/null +++ b/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementSyncTest.java @@ -0,0 +1,62 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.search; + +public class IndexManagementSyncTest extends IndexManagementTestBase { + private SearchServiceClient client; + + @Override + public void createIndexReturnsCorrectDefinition() { + + } + + @Override + public void createIndexReturnsCorrectDefaultValues() { + + } + + @Override + public void createIndexFailsWithUsefulMessageOnUserError() { + + } + + @Override + public void getIndexReturnsCorrectDefinition() { + + } + + @Override + public void getIndexThrowsOnNotFound() { + + } + + @Override + public void existsReturnsTrueForExistingIndex() { + + } + + @Override + public void existsReturnsFalseForNonExistingIndex() { + + } + + @Override + public void deleteIndexIfNotChangedWorksOnlyOnCurrentResource() { + + } + + @Override + public void deleteIndexIfExistsWorksOnlyWhenResourceExists() { + + } + + @Override + public void deleteIndexIsIdempotent() { + + } + + @Override + public void canCreateAndDeleteIndex() { + + } +} diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementTestBase.java b/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementTestBase.java new file mode 100644 index 0000000000000..49d7a82dbfcd5 --- /dev/null +++ b/sdk/search/azure-search/src/test/java/com/azure/search/IndexManagementTestBase.java @@ -0,0 +1,74 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.search; + +import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.policy.HttpLoggingPolicy; +import com.azure.core.http.policy.RetryPolicy; +import com.azure.search.models.Index; +import com.azure.search.test.environment.models.ModelComparer; + +import java.util.Objects; + +public abstract class IndexManagementTestBase extends SearchServiceTestBase { + @Override + protected void beforeTest() { + super.beforeTest(); + } + + protected SearchServiceClientBuilder getSearchServiceClientBuilder() { + if (!interceptorManager.isPlaybackMode()) { + return new SearchServiceClientBuilder() + .serviceName(searchServiceName) + .searchDnsSuffix("search.windows.net") + .httpClient(new NettyAsyncHttpClientBuilder().wiretap(true).build()) + .credential(apiKeyCredentials) + .addPolicy(interceptorManager.getRecordPolicy()) + .addPolicy(new RetryPolicy()) + .addPolicy(new HttpLoggingPolicy( + new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))); + } else { + return new SearchServiceClientBuilder() + .serviceName("searchServiceName") + .searchDnsSuffix("search.windows.net") + .httpClient(interceptorManager.getPlaybackClient()); + } + } + + public abstract void createIndexReturnsCorrectDefinition(); + + public abstract void createIndexReturnsCorrectDefaultValues(); + + public abstract void createIndexFailsWithUsefulMessageOnUserError(); + + public abstract void getIndexReturnsCorrectDefinition(); + + public abstract void getIndexThrowsOnNotFound(); + + public abstract void existsReturnsTrueForExistingIndex(); + + public abstract void existsReturnsFalseForNonExistingIndex(); + + public abstract void deleteIndexIfNotChangedWorksOnlyOnCurrentResource(); + + public abstract void deleteIndexIfExistsWorksOnlyWhenResourceExists(); + + public abstract void deleteIndexIsIdempotent(); + + public abstract void canCreateAndDeleteIndex(); + + protected static boolean assertIndexesEqual(Index expected, Index actual) { + return Objects.equals(expected.getName(), actual.getName()) + && ModelComparer.collectionEquals(expected.getFields(), actual.getFields()) + && ModelComparer.collectionEquals(expected.getScoringProfiles(), actual.getScoringProfiles()) + && Objects.equals(expected.getDefaultScoringProfile(), actual.getDefaultScoringProfile()) + && Objects.equals(expected.getCorsOptions(), actual.getCorsOptions()) + && ModelComparer.collectionEquals(expected.getSuggesters(), actual.getSuggesters()) + && ModelComparer.collectionEquals(expected.getAnalyzers(), actual.getAnalyzers()) + && ModelComparer.collectionEquals(expected.getTokenizers(), actual.getTokenizers()) + && ModelComparer.collectionEquals(expected.getTokenFilters(), actual.getTokenFilters()) + && ModelComparer.collectionEquals(expected.getCharFilters(), actual.getCharFilters()); + } +} diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/SearchIndexClientTestBase.java b/sdk/search/azure-search/src/test/java/com/azure/search/SearchIndexClientTestBase.java index c6ec9b84698ce..b7420b596943b 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/SearchIndexClientTestBase.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/SearchIndexClientTestBase.java @@ -8,23 +8,12 @@ import com.azure.core.http.policy.HttpLogOptions; import com.azure.core.http.policy.HttpLoggingPolicy; import com.azure.core.http.policy.RetryPolicy; -import com.azure.core.test.TestBase; -import com.azure.core.util.Configuration; -import com.azure.core.util.logging.ClientLogger; import com.azure.search.common.jsonwrapper.JsonWrapper; import com.azure.search.common.jsonwrapper.api.JsonApi; import com.azure.search.common.jsonwrapper.api.Type; import com.azure.search.common.jsonwrapper.jacksonwrapper.JacksonDeserializer; import com.azure.search.test.environment.setup.SearchIndexService; -import com.azure.search.test.environment.setup.AzureSearchResources; -import com.microsoft.azure.AzureEnvironment; -import com.microsoft.azure.credentials.ApplicationTokenCredentials; -import com.microsoft.azure.management.resources.fluentcore.arm.Region; import org.junit.Assert; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.rules.TestName; import java.io.IOException; import java.text.DateFormat; @@ -34,56 +23,12 @@ import java.util.Map; -public class SearchIndexClientTestBase extends TestBase { - - private final ClientLogger logger = new ClientLogger(SearchIndexClientTestBase.class); +public class SearchIndexClientTestBase extends SearchServiceTestBase { private static final String HOTELS_TESTS_INDEX_DATA_JSON = "HotelsTestsIndexData.json"; protected static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - - protected String searchServiceName; - protected ApiKeyCredentials apiKeyCredentials; - protected SearchIndexService searchServiceHotelsIndex; - - private static AzureSearchResources azureSearchResources; private JsonApi jsonApi = JsonWrapper.newInstance(JacksonDeserializer.class); - @Rule - public TestName testName = new TestName(); - - @BeforeClass - public static void beforeClass() { - initializeAzureResources(); - } - - @AfterClass - public static void afterClass() { - azureSearchResources.deleteResourceGroup(); - } - - @Override - protected void beforeTest() { - if (!interceptorManager.isPlaybackMode()) { - azureSearchResources.initialize(); - azureSearchResources.createResourceGroup(); - azureSearchResources.createService(); - - searchServiceName = azureSearchResources.getSearchServiceName(); - apiKeyCredentials = new ApiKeyCredentials(azureSearchResources.getSearchAdminKey()); - } - } - - @Override - protected void afterTest() { - super.afterTest(); - azureSearchResources.deleteService(); - } - - @Override - public String getTestName() { - return testName.getMethodName(); - } - protected void uploadDocuments(SearchIndexClient client, List uploadDoc) { client.uploadDocuments(uploadDoc); waitForIndexing(); @@ -182,21 +127,6 @@ protected void setupIndexFromJsonFile(String jsonFile) { } } - private static void initializeAzureResources() { - String appId = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLIENT_ID); - String azureDomainId = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_TENANT_ID); - String secret = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLIENT_SECRET); - String subscriptionId = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_SUBSCRIPTION_ID); - - ApplicationTokenCredentials applicationTokenCredentials = new ApplicationTokenCredentials( - appId, - azureDomainId, - secret, - AzureEnvironment.AZURE); - - azureSearchResources = new AzureSearchResources(applicationTokenCredentials, subscriptionId, Region.US_EAST); - } - protected void waitForIndexing() { // Wait 2 secs to allow index request to finish if (!interceptorManager.isPlaybackMode()) { diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceTestBase.java b/sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceTestBase.java new file mode 100644 index 0000000000000..899fa8d9d183f --- /dev/null +++ b/sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceTestBase.java @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.search; + +import com.azure.core.test.TestBase; +import com.azure.core.util.Configuration; +import com.azure.core.util.logging.ClientLogger; +import com.azure.search.test.environment.setup.AzureSearchResources; +import com.azure.search.test.environment.setup.SearchIndexService; +import com.microsoft.azure.AzureEnvironment; +import com.microsoft.azure.credentials.ApplicationTokenCredentials; +import com.microsoft.azure.management.resources.fluentcore.arm.Region; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.rules.TestName; + +public abstract class SearchServiceTestBase extends TestBase { + + protected final ClientLogger logger = new ClientLogger(SearchIndexClientTestBase.class); + + protected String searchServiceName; + protected ApiKeyCredentials apiKeyCredentials; + protected SearchIndexService searchServiceHotelsIndex; + + private static AzureSearchResources azureSearchResources; + + @Override + public String getTestName() { + return testName.getMethodName(); + } + + @Rule + public TestName testName = new TestName(); + + @BeforeClass + public static void beforeClass() { + initializeAzureResources(); + } + + @AfterClass + public static void afterClass() { + azureSearchResources.deleteResourceGroup(); + } + + @Override + protected void beforeTest() { + if (!interceptorManager.isPlaybackMode()) { + azureSearchResources.initialize(); + azureSearchResources.createResourceGroup(); + azureSearchResources.createService(); + + searchServiceName = azureSearchResources.getSearchServiceName(); + apiKeyCredentials = new ApiKeyCredentials(azureSearchResources.getSearchAdminKey()); + } + } + + @Override + protected void afterTest() { + super.afterTest(); + azureSearchResources.deleteService(); + } + + private static void initializeAzureResources() { + String appId = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLIENT_ID); + String azureDomainId = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_TENANT_ID); + String secret = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_CLIENT_SECRET); + String subscriptionId = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_SUBSCRIPTION_ID); + + ApplicationTokenCredentials applicationTokenCredentials = new ApplicationTokenCredentials( + appId, + azureDomainId, + secret, + AzureEnvironment.AZURE); + + azureSearchResources = new AzureSearchResources(applicationTokenCredentials, subscriptionId, Region.US_EAST); + } +}