Skip to content

Commit

Permalink
Datasource access tests (Azure#228)
Browse files Browse the repository at this point in the history
* Adding Data Source tests with Access Conditions
  • Loading branch information
rabee333 authored Nov 6, 2019
1 parent c5c35b9 commit 0fa5e95
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.azure.search.models.SoftDeleteColumnDeletionDetectionPolicy;
import com.azure.search.models.SqlIntegratedChangeTrackingPolicy;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import reactor.test.StepVerifier;
Expand All @@ -24,9 +25,13 @@ public class DataSourceAsyncTests extends DataSourceTestBase {
private SearchServiceAsyncClient client;

@Override
public void createAndListDataSources() {
protected void beforeTest() {
super.beforeTest();
client = getSearchServiceClientBuilder().buildAsyncClient();
}

@Override
public void createAndListDataSources() {
DataSource dataSource1 = createTestBlobDataSource(null);
DataSource dataSource2 = createTestSqlDataSource(null, null);

Expand Down Expand Up @@ -59,9 +64,6 @@ public void createAndListDataSources() {

@Override
public void deleteDataSourceIsIdempotent() {
// Get client
client = getSearchServiceClientBuilder().buildAsyncClient();

DataSource dataSource1 = createTestBlobDataSource(null);

// Try to delete before the data source exists, expect a NOT FOUND return status code
Expand All @@ -81,7 +83,6 @@ public void deleteDataSourceIsIdempotent() {

@Override
public void canUpdateDataSource() {
client = getSearchServiceClientBuilder().buildAsyncClient();
DataSource initial = createTestBlobDataSource(null);

// Create the data source
Expand All @@ -96,9 +97,49 @@ public void canUpdateDataSource() {
}

@Override
public void createDataSourceFailsWithUsefulMessageOnUserError() {
client = getSearchServiceClientBuilder().buildAsyncClient();
public void createOrUpdateDataSourceIfNotExistsFailsOnExistingResource() {
// Create a data source
DataSource initial = createTestBlobDataSource(null);
client.createOrUpdateDataSource(initial).block();

// Create another data source with the same name
DataSource another = createTestBlobDataSource(null);

StepVerifier
.create(client.createOrUpdateDataSource(
another.getName(),
another,
null,
generateIfNotExistsAccessCondition(),
null))
.verifyErrorSatisfies(error -> {
Assert.assertEquals(HttpResponseException.class, error.getClass());
Assert.assertEquals(
HttpResponseStatus.PRECONDITION_FAILED.code(),
((HttpResponseException) error).getResponse().getStatusCode());
Assert.assertTrue(error.getMessage()
.contains("The precondition given in one of the request headers evaluated to false"));
});
}

@Override
public void createOrUpdateIfNotExistsSucceedsOnNoResource() {
// Create a data source
DataSource dataSource = createTestBlobDataSource(null);

StepVerifier
.create(client.createOrUpdateDataSource(
dataSource.getName(),
dataSource,
null,
generateIfNotExistsAccessCondition(),
null))
.assertNext(r -> Assert.assertTrue(StringUtils.isNotBlank(r.getETag())))
.verifyComplete();
}

@Override
public void createDataSourceFailsWithUsefulMessageOnUserError() {
DataSource dataSource = createTestSqlDataSource(null, null);
dataSource.setType(DataSourceType.fromString("thistypedoesnotexist"));

Expand All @@ -114,7 +155,6 @@ public void createDataSourceFailsWithUsefulMessageOnUserError() {

@Override
public void createDataSourceReturnsCorrectDefinition() {
client = getSearchServiceClientBuilder().buildAsyncClient();
SoftDeleteColumnDeletionDetectionPolicy deletionDetectionPolicy =
new SoftDeleteColumnDeletionDetectionPolicy()
.setSoftDeleteColumnName("isDeleted")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.azure.search.models.SoftDeleteColumnDeletionDetectionPolicy;
import com.azure.search.models.SqlIntegratedChangeTrackingPolicy;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.junit.Assert;

Expand All @@ -27,9 +28,13 @@ public class DataSourceSyncTests extends DataSourceTestBase {
private SearchServiceClient client;

@Override
public void createAndListDataSources() {
protected void beforeTest() {
super.beforeTest();
client = getSearchServiceClientBuilder().buildClient();
}

@Override
public void createAndListDataSources() {
DataSource dataSource1 = createTestBlobDataSource(null);
DataSource dataSource2 = createTestSqlDataSource(null, null);

Expand Down Expand Up @@ -59,9 +64,6 @@ public void createAndListDataSources() {

@Override
public void deleteDataSourceIsIdempotent() {
// Get client
client = getSearchServiceClientBuilder().buildClient();

DataSource dataSource1 = createTestBlobDataSource(null);

// Try to delete before the data source exists, expect a NOT FOUND return status code
Expand Down Expand Up @@ -98,7 +100,6 @@ public void createDataSourceFailsWithUsefulMessageOnUserError() {

@Override
public void canUpdateDataSource() {
client = getSearchServiceClientBuilder().buildClient();
DataSource initial = createTestBlobDataSource(null);

// Create the data source
Expand All @@ -112,9 +113,43 @@ public void canUpdateDataSource() {
assertDataSourcesEqual(expected, actual);
}

@Override
public void createOrUpdateDataSourceIfNotExistsFailsOnExistingResource() {
// Create a data source
DataSource initial = createTestBlobDataSource(null);
client.createOrUpdateDataSource(initial);

// Create another data source with the same name
DataSource another = createTestBlobDataSource(null);

assertException(
() -> client.createOrUpdateDataSource(
another.getName(),
another,
null,
generateIfNotExistsAccessCondition(),
null),
HttpResponseException.class,
"The precondition given in one of the request headers evaluated to false");
}

@Override
public void createOrUpdateIfNotExistsSucceedsOnNoResource() {
// Create a data source
DataSource dataSource = createTestBlobDataSource(null);

DataSource result = client.createOrUpdateDataSource(
dataSource.getName(),
dataSource,
null,
generateIfNotExistsAccessCondition(),
null);

Assert.assertTrue(StringUtils.isNoneBlank(result.getETag()));
}

@Override
public void createDataSourceReturnsCorrectDefinition() {
client = getSearchServiceClientBuilder().buildClient();
SoftDeleteColumnDeletionDetectionPolicy deletionDetectionPolicy =
new SoftDeleteColumnDeletionDetectionPolicy()
.setSoftDeleteColumnName("isDeleted")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ protected void beforeTest() {
@Test
public abstract void canUpdateDataSource();

@Test
public abstract void createOrUpdateDataSourceIfNotExistsFailsOnExistingResource();

@Test
public abstract void createOrUpdateIfNotExistsSucceedsOnNoResource();

@Test
public void canUpdateConnectionData() {
// Note: since connection string is not returned when queried from the service, actually saving the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,4 @@ protected void setupIndexFromJsonFile(String jsonFile) {
}
}

void assertException(Runnable exceptionThrower, Class<? extends Exception> expectedExceptionType, String expectedMessage) {
try {
exceptionThrower.run();
Assert.fail();
} catch (Throwable ex) {
Assert.assertEquals(expectedExceptionType, ex.getClass());
Assert.assertTrue(ex.getMessage().contains(expectedMessage));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,21 @@ protected AccessCondition generateIfExistsAccessCondition() {
// Setting this access condition modifies the request to include the HTTP If-Match conditional header set to "*"
return new AccessCondition().setIfMatch("*");
}

void assertException(
Runnable exceptionThrower, Class<? extends Exception> expectedExceptionType, String expectedMessage) {
try {
exceptionThrower.run();
Assert.fail();

} catch (Throwable ex) {
// Check that this is not the "Assert.fail()" above:
Assert.assertNotEquals(AssertionError.class, ex.getClass());

Assert.assertEquals(expectedExceptionType, ex.getClass());
if (expectedMessage != null) {
Assert.assertTrue(ex.getMessage().contains(expectedMessage));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://azs-sdkfb877431ecdf.search.windows.net/datasources('azs-java-test-blob')?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "968d73ab-2701-4f7b-a539-5003028e5b5b",
"StatusCode" : "201",
"Date" : "Wed, 06 Nov 2019 00:53:36 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"ETag" : "W/\"0x8D76253C1A91D8A\"",
"elapsed-time" : "48",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "398",
"Body" : "{\"@odata.context\":\"https://azs-sdkfb877431ecdf.search.windows.net/$metadata#datasources/$entity\",\"@odata.etag\":\"\\\"0x8D76253C1A91D8A\\\"\",\"name\":\"azs-java-test-blob\",\"description\":\"Some data source\",\"type\":\"azureblob\",\"subtype\":null,\"credentials\":{\"connectionString\":null},\"container\":{\"name\":\"fakecontainer\",\"query\":\"/fakefolder/\"},\"dataChangeDetectionPolicy\":null,\"dataDeletionDetectionPolicy\":null}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal",
"Location" : "https://azs-sdkfb877431ecdf.search.windows.net/datasources('azs-java-test-blob')?api-version=2019-05-06"
},
"Exception" : null
}, {
"Method" : "PUT",
"Uri" : "https://azs-sdkfb877431ecdf.search.windows.net/datasources('azs-java-test-blob')?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "d79c7ac2-8f0e-4de8-afbb-835e2f90add0",
"StatusCode" : "412",
"Date" : "Wed, 06 Nov 2019 00:53:36 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "47",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "160",
"Body" : "{\"error\":{\"code\":\"\",\"message\":\"The precondition given in one of the request headers evaluated to false. No change was made to the resource from this request.\"}}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Language" : "en",
"Content-Type" : "application/json; odata.metadata=minimal"
},
"Exception" : null
} ],
"variables" : [ ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://azs-sdke7653508ac1d.search.windows.net/datasources('azs-java-test-blob')?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "643177af-e42c-4abf-a606-e22deac12fbd",
"StatusCode" : "201",
"Date" : "Wed, 06 Nov 2019 18:22:42 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"ETag" : "W/\"0x8D762E650A6F96E\"",
"elapsed-time" : "60",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "398",
"Body" : "{\"@odata.context\":\"https://azs-sdke7653508ac1d.search.windows.net/$metadata#datasources/$entity\",\"@odata.etag\":\"\\\"0x8D762E650A6F96E\\\"\",\"name\":\"azs-java-test-blob\",\"description\":\"Some data source\",\"type\":\"azureblob\",\"subtype\":null,\"credentials\":{\"connectionString\":null},\"container\":{\"name\":\"fakecontainer\",\"query\":\"/fakefolder/\"},\"dataChangeDetectionPolicy\":null,\"dataDeletionDetectionPolicy\":null}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal",
"Location" : "https://azs-sdke7653508ac1d.search.windows.net/datasources('azs-java-test-blob')?api-version=2019-05-06"
},
"Exception" : null
} ],
"variables" : [ ]
}

0 comments on commit 0fa5e95

Please sign in to comment.