From b7b4ab9f769a363a5eb8ad2a5bc2700b076817ae Mon Sep 17 00:00:00 2001 From: Rabeea Abu-Saleh Date: Thu, 14 Nov 2019 08:33:48 +0200 Subject: [PATCH] datasourceExists() implementation and tests (#258) Adding implementation and tests of datasourceExists() --- .../search/SearchServiceAsyncClient.java | 42 +++++++++++++++ .../com/azure/search/SearchServiceClient.java | 39 ++++++++++++++ .../azure/search/DataSourceAsyncTests.java | 19 +++++++ .../com/azure/search/DataSourceSyncTests.java | 13 +++++ .../com/azure/search/DataSourceTestBase.java | 6 +++ ...sReturnsFalseForNonExistingDatasource.json | 26 ++++++++++ ...xistsReturnsTrueForExistingDatasource.json | 51 +++++++++++++++++++ 7 files changed, 196 insertions(+) create mode 100644 sdk/search/azure-search/src/test/resources/session-records/existsReturnsFalseForNonExistingDatasource.json create mode 100644 sdk/search/azure-search/src/test/resources/session-records/existsReturnsTrueForExistingDatasource.json diff --git a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java index aceb223ffdc94..675386bd54cb7 100644 --- a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java +++ b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceAsyncClient.java @@ -344,6 +344,48 @@ Mono> deleteDataSourceWithResponse(String dataSourceName, context).map(Function.identity()); } + + /** + * Determines whether or not the given datasource exists. + * + * @param datasourceName the name of the datasource + * @return true if the datasource exists; false otherwise. + */ + public Mono datasourceExists(String datasourceName) { + return this.datasourceExistsWithResponse(datasourceName, null).map(Response::getValue); + } + + /** + * Determines whether or not the given datasource exists. + * + * @param datasourceName the name of the datasource + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @return true if the datasource exists; false otherwise. + */ + public Mono datasourceExists(String datasourceName, RequestOptions requestOptions) { + return this.datasourceExistsWithResponse(datasourceName, requestOptions).map(Response::getValue); + } + + /** + * Determines whether or not the given datasource exists. + * + * @param datasourceName the name of the datasource + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @return true if the datasource exists; false otherwise. + */ + public Mono> datasourceExistsWithResponse(String datasourceName, RequestOptions requestOptions) { + return withContext(context -> this.datasourceExistsWithResponse(datasourceName, requestOptions, context)); + } + + Mono> datasourceExistsWithResponse(String datasourceName, + RequestOptions requestOptions, + Context context) { + return resourceExistsWithResponse(() -> + this.getDataSourceWithResponse(datasourceName, requestOptions, context)); + } + /** * Creates a new Azure Search indexer or updates an indexer if it already exists. * diff --git a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java index 5173928d16da4..3b601812b9ca4 100644 --- a/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java +++ b/sdk/search/azure-search/src/main/java/com/azure/search/SearchServiceClient.java @@ -236,6 +236,45 @@ public Response deleteDataSourceWithResponse(String dataSourceName, accessCondition, requestOptions, context).block(); } + /** + * Determines whether or not the given datasource exists. + * + * @param datasourceName the name of the datasource + * @return true if the datasource exists; false otherwise. + */ + public Boolean datasourceExists(String datasourceName) { + return asyncClient.datasourceExists(datasourceName).block(); + } + + /** + * Determines whether or not the given datasource exists. + * + * @param datasourceName the name of the datasource + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @return true if the datasource exists; false otherwise. + */ + public Boolean datasourceExists(String datasourceName, RequestOptions requestOptions) { + return asyncClient.datasourceExists(datasourceName, requestOptions).block(); + } + + /** + * Determines whether or not the given datasource exists. + * + * @param datasourceName the name of the datasource + * @param requestOptions additional parameters for the operation. + * Contains the tracking ID sent with the request to help with debugging + * @param context additional context that is passed through the HTTP pipeline during the service call + * @return true if the datasource exists; false otherwise. + */ + public Response datasourceExistsWithResponse(String datasourceName, + RequestOptions requestOptions, + Context context) { + return asyncClient + .datasourceExistsWithResponse(datasourceName, requestOptions, context) + .block(); + } + /** * @return the created Indexer. * @throws NotImplementedException not implemented diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceAsyncTests.java b/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceAsyncTests.java index 6c6c95452c9f3..a135128f27596 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceAsyncTests.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceAsyncTests.java @@ -283,6 +283,25 @@ public void updateDataSourceIfNotChangedSucceedsWhenResourceUnchanged() { }); } + @Override + public void existsReturnsFalseForNonExistingDatasource() { + StepVerifier + .create(client.datasourceExists("inExistentDataSourceName")) + .assertNext(Assert::assertFalse) + .verifyComplete(); + } + + @Override + public void existsReturnsTrueForExistingDatasource() { + DataSource dataSource = createTestDataSource(); + client.createOrUpdateDataSource(dataSource).block(); + + StepVerifier + .create(client.datasourceExists(dataSource.getName())) + .assertNext(Assert::assertTrue) + .verifyComplete(); + } + @Override public void createDataSourceFailsWithUsefulMessageOnUserError() { DataSource dataSource = createTestSqlDataSource(null, null); diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceSyncTests.java b/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceSyncTests.java index 1d093e771cb18..1f5cccbee6f32 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceSyncTests.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceSyncTests.java @@ -256,6 +256,19 @@ public void updateDataSourceIfNotChangedSucceedsWhenResourceUnchanged() { Assert.assertNotEquals(createdETag, updatedDataSource.getETag()); } + @Override + public void existsReturnsFalseForNonExistingDatasource() { + Assert.assertFalse(client.datasourceExists("inExistentDataSourceName")); + } + + @Override + public void existsReturnsTrueForExistingDatasource() { + DataSource dataSource = createTestDataSource(); + client.createOrUpdateDataSource(dataSource); + + Assert.assertTrue(client.datasourceExists(dataSource.getName())); + } + @Override public void createDataSourceReturnsCorrectDefinition() { SoftDeleteColumnDeletionDetectionPolicy deletionDetectionPolicy = diff --git a/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceTestBase.java b/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceTestBase.java index 8e0e32c28460e..83bcde5a3f3a6 100644 --- a/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceTestBase.java +++ b/sdk/search/azure-search/src/test/java/com/azure/search/DataSourceTestBase.java @@ -77,6 +77,12 @@ protected void beforeTest() { @Test public abstract void updateDataSourceIfNotChangedSucceedsWhenResourceUnchanged(); + @Test + public abstract void existsReturnsFalseForNonExistingDatasource(); + + @Test + public abstract void existsReturnsTrueForExistingDatasource(); + @Test public void canUpdateConnectionData() { // Note: since connection string is not returned when queried from the service, actually saving the diff --git a/sdk/search/azure-search/src/test/resources/session-records/existsReturnsFalseForNonExistingDatasource.json b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsFalseForNonExistingDatasource.json new file mode 100644 index 0000000000000..a0416989684cf --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsFalseForNonExistingDatasource.json @@ -0,0 +1,26 @@ +{ + "networkCallRecords" : [ { + "Method" : "GET", + "Uri" : "https://azs-sdk04a58790fc78.search.windows.net/datasources('inExistentDataSourceName')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "d14df4eb-219d-4345-b735-f32438ddf446", + "StatusCode" : "404", + "Date" : "Wed, 13 Nov 2019 15:09:18 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "elapsed-time" : "50", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "133", + "Body" : "{\"error\":{\"code\":\"\",\"message\":\"No data source with the name 'inExistentDataSourceName' was found in service 'azs-sdk04a58790fc78'.\"}}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Language" : "en", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + } ], + "variables" : [ ] +} \ No newline at end of file diff --git a/sdk/search/azure-search/src/test/resources/session-records/existsReturnsTrueForExistingDatasource.json b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsTrueForExistingDatasource.json new file mode 100644 index 0000000000000..dd14b20ff9cb3 --- /dev/null +++ b/sdk/search/azure-search/src/test/resources/session-records/existsReturnsTrueForExistingDatasource.json @@ -0,0 +1,51 @@ +{ + "networkCallRecords" : [ { + "Method" : "PUT", + "Uri" : "https://azs-sdk77f119396ba3.search.windows.net/datasources('azs-java-test-sql')?api-version=2019-05-06", + "Headers" : { + "Content-Type" : "application/json; charset=utf-8" + }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "2614d0c0-9103-479d-8099-473200f761d8", + "StatusCode" : "201", + "Date" : "Wed, 13 Nov 2019 10:07:25 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "ETag" : "W/\"0x8D7682148C4E1B5\"", + "elapsed-time" : "80", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "383", + "Body" : "{\"@odata.context\":\"https://azs-sdk77f119396ba3.search.windows.net/$metadata#datasources/$entity\",\"@odata.etag\":\"\\\"0x8D7682148C4E1B5\\\"\",\"name\":\"azs-java-test-sql\",\"description\":\"Some data source\",\"type\":\"azuresql\",\"subtype\":null,\"credentials\":{\"connectionString\":null},\"container\":{\"name\":\"GeoNamesRI\",\"query\":null},\"dataChangeDetectionPolicy\":null,\"dataDeletionDetectionPolicy\":null}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal", + "Location" : "https://azs-sdk77f119396ba3.search.windows.net/datasources('azs-java-test-sql')?api-version=2019-05-06" + }, + "Exception" : null + }, { + "Method" : "GET", + "Uri" : "https://azs-sdk77f119396ba3.search.windows.net/datasources('azs-java-test-sql')?api-version=2019-05-06", + "Headers" : { }, + "Response" : { + "Pragma" : "no-cache", + "retry-after" : "0", + "request-id" : "4700d870-fc1f-419a-84d5-59c1925c5b6b", + "StatusCode" : "200", + "Date" : "Wed, 13 Nov 2019 10:07:25 GMT", + "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", + "Cache-Control" : "no-cache", + "ETag" : "W/\"0x8D7682148C4E1B5\"", + "elapsed-time" : "52", + "OData-Version" : "4.0", + "Expires" : "-1", + "Content-Length" : "383", + "Body" : "{\"@odata.context\":\"https://azs-sdk77f119396ba3.search.windows.net/$metadata#datasources/$entity\",\"@odata.etag\":\"\\\"0x8D7682148C4E1B5\\\"\",\"name\":\"azs-java-test-sql\",\"description\":\"Some data source\",\"type\":\"azuresql\",\"subtype\":null,\"credentials\":{\"connectionString\":null},\"container\":{\"name\":\"GeoNamesRI\",\"query\":null},\"dataChangeDetectionPolicy\":null,\"dataDeletionDetectionPolicy\":null}", + "Preference-Applied" : "odata.include-annotations=\"*\"", + "Content-Type" : "application/json; odata.metadata=minimal" + }, + "Exception" : null + } ], + "variables" : [ ] +} \ No newline at end of file