From 7df18a064e673485553e38fa74cd54b196cc4fd7 Mon Sep 17 00:00:00 2001 From: Nava Vaisman Levy Date: Thu, 7 Nov 2019 09:12:33 -0800 Subject: [PATCH] Create IndexClient from ServiceClient UT (#232) * SearchClient generate IndexClient UT * codestyle fix --- ....java => SearchServiceSubClientTests.java} | 58 ++++++++++++++++--- ...dexAsyncClientAfterUsingServiceClient.json | 4 ++ ...anGetIndexAsyncClientFromSearchClient.json | 4 ++ ...GetIndexClientAfterUsingServiceClient.json | 4 ++ .../canGetIndexClientFromSearchClient.json | 4 ++ 5 files changed, 65 insertions(+), 9 deletions(-) rename sdk/search/azure-search/src/test/java/com/azure/search/{SearchServiceSubclientTests.java => SearchServiceSubClientTests.java} (59%) create mode 100644 sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientAfterUsingServiceClient.json create mode 100644 sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientFromSearchClient.json create mode 100644 sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientAfterUsingServiceClient.json create mode 100644 sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientFromSearchClient.json diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceSubclientTests.java b/sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceSubClientTests.java similarity index 59% rename from sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceSubclientTests.java rename to sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceSubClientTests.java index 3af98348f59e5..6784a7bddafa0 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceSubclientTests.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/SearchServiceSubClientTests.java @@ -6,14 +6,12 @@ import org.junit.Assert; import org.junit.Test; -public class SearchServiceSubclientTests { +public class SearchServiceSubClientTests extends SearchServiceTestBase { + @Test public void canGetIndexClientFromSearchClient() { - SearchServiceClient serviceClient = new SearchServiceClientBuilder() - .endpoint("https://test1.search.windows.net") - .credential(new ApiKeyCredentials("api-key")) - .buildClient(); + SearchServiceClient serviceClient = getSearchService(); SearchIndexClient indexClient = serviceClient.getIndexClient("hotels"); @@ -36,10 +34,8 @@ public void canGetIndexClientFromSearchClient() { @Test public void canGetIndexAsyncClientFromSearchClient() { - SearchServiceAsyncClient serviceClient = new SearchServiceClientBuilder() - .endpoint("https://test1.search.windows.net") - .credential(new ApiKeyCredentials("api-key")) - .buildAsyncClient(); + SearchServiceAsyncClient serviceClient = getAsyncSearchService(); + SearchIndexAsyncClient indexClient = serviceClient.getIndexClient("hotels"); // Validate the client was created @@ -58,4 +54,48 @@ public void canGetIndexAsyncClientFromSearchClient() { // Validate that the client uses the specified index Assert.assertEquals("hotels", indexClient.getIndexName()); } + + @Test + public void canGetIndexClientAfterUsingServiceClient() { + SearchServiceClient serviceClient = getSearchService(); + try { + // this is expected to fail + serviceClient.deleteIndex("thisindexdoesnotexist"); + } catch (Exception e) { + // deleting the index should fail as it does not exist + } + + // This should not fail + SearchIndexClient indexClient = serviceClient.getIndexClient("hotels"); + Assert.assertEquals("hotels", indexClient.getIndexName()); + } + + @Test + public void canGetIndexAsyncClientAfterUsingServiceClient() { + SearchServiceAsyncClient serviceClient = getAsyncSearchService(); + try { + // this is expected to fail + serviceClient.deleteIndex("thisindexdoesnotexist"); + } catch (Exception e) { + // deleting the index should fail as it does not exist + } + + // This should not fail + SearchIndexAsyncClient indexClient = serviceClient.getIndexClient("hotels"); + Assert.assertEquals("hotels", indexClient.getIndexName()); + } + + private SearchServiceClient getSearchService() { + return new SearchServiceClientBuilder() + .endpoint("https://test1.search.windows.net") + .credential(new ApiKeyCredentials("api-key")) + .buildClient(); + } + + private SearchServiceAsyncClient getAsyncSearchService() { + return new SearchServiceClientBuilder() + .endpoint("https://test1.search.windows.net") + .credential(new ApiKeyCredentials("api-key")) + .buildAsyncClient(); + } } diff --git a/sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientAfterUsingServiceClient.json b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientAfterUsingServiceClient.json new file mode 100644 index 0000000000000..ba5f37f8f8555 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientAfterUsingServiceClient.json @@ -0,0 +1,4 @@ +{ + "networkCallRecords" : [ ], + "variables" : [ ] +} \ No newline at end of file diff --git a/sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientFromSearchClient.json b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientFromSearchClient.json new file mode 100644 index 0000000000000..ba5f37f8f8555 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexAsyncClientFromSearchClient.json @@ -0,0 +1,4 @@ +{ + "networkCallRecords" : [ ], + "variables" : [ ] +} \ No newline at end of file diff --git a/sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientAfterUsingServiceClient.json b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientAfterUsingServiceClient.json new file mode 100644 index 0000000000000..ba5f37f8f8555 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientAfterUsingServiceClient.json @@ -0,0 +1,4 @@ +{ + "networkCallRecords" : [ ], + "variables" : [ ] +} \ No newline at end of file diff --git a/sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientFromSearchClient.json b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientFromSearchClient.json new file mode 100644 index 0000000000000..ba5f37f8f8555 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/canGetIndexClientFromSearchClient.json @@ -0,0 +1,4 @@ +{ + "networkCallRecords" : [ ], + "variables" : [ ] +} \ No newline at end of file