Skip to content

Commit

Permalink
fix async tests (Azure#348)
Browse files Browse the repository at this point in the history
* fix async tests
  • Loading branch information
LizaShak authored and navalev committed Dec 23, 2019
1 parent f944262 commit 12820d1
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,10 @@ public void canCreateAndListDataSources() {
client.createOrUpdateDataSource(dataSource1).block();
client.createOrUpdateDataSource(dataSource2).block();

PagedFlux<DataSource> results = client.listDataSources();

StepVerifier
.create(results.collectList())
.assertNext(result -> {
Assert.assertEquals(2, result.size());
Assert.assertEquals(dataSource1.getName(), result.get(0).getName());
Assert.assertEquals(dataSource2.getName(), result.get(1).getName());
})
.create(client.listDataSources())
.assertNext(ds1 -> Assert.assertEquals(dataSource1.getName(), ds1.getName()))
.assertNext(ds2 -> Assert.assertEquals(dataSource2.getName(), ds2.getName()))
.verifyComplete();
}

Expand All @@ -93,15 +88,10 @@ public void canCreateAndListDataSourcesWithResponse() {
client.createOrUpdateDataSourceWithResponse(dataSource1, new AccessCondition(), new RequestOptions()).block();
client.createOrUpdateDataSourceWithResponse(dataSource2, new AccessCondition(), new RequestOptions()).block();

PagedFlux<DataSource> results = client.listDataSources("name", new RequestOptions());

StepVerifier
.create(results.collectList())
.assertNext(result -> {
Assert.assertEquals(2, result.size());
Assert.assertEquals(dataSource1.getName(), result.get(0).getName());
Assert.assertEquals(dataSource2.getName(), result.get(1).getName());
})
.create(client.listDataSources("name", new RequestOptions()))
.assertNext(ds1 -> Assert.assertEquals(dataSource1.getName(), ds1.getName()))
.assertNext(ds2 -> Assert.assertEquals(dataSource2.getName(), ds2.getName()))
.verifyComplete();
}

Expand All @@ -110,22 +100,24 @@ public void deleteDataSourceIsIdempotent() {
DataSource dataSource1 = createTestBlobDataSource(null);

// Try to delete before the data source exists, expect a NOT FOUND return status code
Response<Void> res = client.deleteDataSourceWithResponse(dataSource1.getName(), new AccessCondition(), generateRequestOptions()).block();
assert res != null;
Assert.assertEquals(HttpStatus.SC_NOT_FOUND, res.getStatusCode());
StepVerifier
.create(client.deleteDataSourceWithResponse(dataSource1.getName(), new AccessCondition(), generateRequestOptions()))
.assertNext(res -> Assert.assertEquals(HttpStatus.SC_NOT_FOUND, res.getStatusCode()))
.verifyComplete();

// Create the data source
client.createOrUpdateDataSource(dataSource1).block();

// Delete twice, expect the first to succeed (with NO CONTENT status code) and the second to return NOT FOUND
res = client.deleteDataSourceWithResponse(dataSource1.getName(), new AccessCondition(), generateRequestOptions()).block();
assert res != null;
Assert.assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode());
StepVerifier
.create(client.deleteDataSourceWithResponse(dataSource1.getName(), new AccessCondition(), generateRequestOptions()))
.assertNext(res -> Assert.assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()))
.verifyComplete();

// Again, expect to fail
res = client.deleteDataSourceWithResponse(dataSource1.getName(), new AccessCondition(), generateRequestOptions()).block();
assert res != null;
Assert.assertEquals(HttpStatus.SC_NOT_FOUND, res.getStatusCode());
StepVerifier
.create(client.deleteDataSourceWithResponse(dataSource1.getName(), new AccessCondition(), generateRequestOptions()))
.assertNext(res -> Assert.assertEquals(HttpStatus.SC_NOT_FOUND, res.getStatusCode()))
.verifyComplete();
}

@Test
Expand Down Expand Up @@ -172,10 +164,10 @@ public void createOrUpdateDatasourceIfNotExistsSucceedsOnNoResource() {
@Test
public void canCreateAndDeleteDatasource() {
DataSource dataSource = createTestBlobDataSource(null);
client.deleteDataSource(dataSource.getName()).block();

StepVerifier
.create(client.dataSourceExists(dataSource.getName()))
.create(client.deleteDataSource(dataSource.getName())
.then(client.dataSourceExists(dataSource.getName())))
.assertNext(Assert::assertFalse)
.verifyComplete();
}
Expand Down Expand Up @@ -253,21 +245,21 @@ public void existsReturnsFalseForNonExistingDatasource() {
@Test
public void existsReturnsTrueForExistingDatasource() {
DataSource dataSource = createTestSqlDataSourceObject(SQL_DATASOURCE_NAME);
client.createOrUpdateDataSource(dataSource).block();

StepVerifier
.create(client.dataSourceExists(dataSource.getName()))
.create(client.createOrUpdateDataSource(dataSource)
.then(client.dataSourceExists(dataSource.getName())))
.assertNext(Assert::assertTrue)
.verifyComplete();
}

@Test
public void existsReturnsTrueForExistingDatasourceWithResponse() {
DataSource dataSource = createTestSqlDataSourceObject(SQL_DATASOURCE_NAME);
client.createOrUpdateDataSource(dataSource).block();

StepVerifier
.create(client.dataSourceExistsWithResponse(dataSource.getName(), generateRequestOptions()))
.create(client.createOrUpdateDataSource(dataSource)
.then(client.dataSourceExistsWithResponse(dataSource.getName(), generateRequestOptions())))
.assertNext(res -> Assert.assertTrue(res.getValue()))
.verifyComplete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ public void createIndexFailsWithUsefulMessageOnUserError() {
@Test
public void getIndexReturnsCorrectDefinition() {
Index index = createTestIndex();
client.createIndex(index).block();

StepVerifier
.create(client.getIndex(index.getName()))
.create(client.createIndex(index)
.then(client.getIndex(index.getName())))
.assertNext(res -> assertIndexesEqual(index, res))
.verifyComplete();
}

@Test
public void getIndexReturnsCorrectDefinitionWithResponse() {
Index index = createTestIndex();
client.createIndex(index).block();

StepVerifier
.create(client.getIndexWithResponse(index.getName(), generateRequestOptions()))
.create(client.createIndex(index)
.then(client.getIndexWithResponse(index.getName(), generateRequestOptions())))
.assertNext(res -> assertIndexesEqual(index, res.getValue()))
.verifyComplete();
}
Expand All @@ -149,21 +149,21 @@ public void getIndexThrowsOnNotFound() {
@Test
public void existsReturnsTrueForExistingIndex() {
Index index = createTestIndex();
client.createIndex(index).block();

StepVerifier
.create(client.indexExists(index.getName()))
.create(client.createIndex(index)
.then(client.indexExists(index.getName())))
.assertNext(Assert::assertTrue)
.verifyComplete();
}

@Test
public void existsReturnsTrueForExistingIndexWithResponse() {
Index index = createTestIndex();
client.createIndex(index).block();

StepVerifier
.create(client.indexExistsWithResponse(index.getName(), generateRequestOptions()))
.create(client.createIndex(index)
.then(client.indexExistsWithResponse(index.getName(), generateRequestOptions())))
.assertNext(res -> Assert.assertTrue(res.getValue()))
.verifyComplete();
}
Expand Down Expand Up @@ -242,11 +242,12 @@ public void deleteIndexIsIdempotent() {
@Test
public void canCreateAndDeleteIndex() {
Index index = createTestIndex();
client.createIndex(index).block();
client.deleteIndex(index.getName()).block();

StepVerifier
.create(client.indexExists(index.getName()))
.create(
client.createIndex(index)
.then(client.deleteIndex(index.getName()))
.then(client.indexExists(index.getName())))
.assertNext(Assert::assertFalse)
.verifyComplete();
}
Expand All @@ -256,9 +257,6 @@ public void canCreateAndListIndexes() {
Index index1 = createTestIndex();
Index index2 = createTestIndex().setName("hotels2");

client.createIndex(index1).block();
client.createIndex(index2).block();

PagedFlux<Index> listResponse = client.listIndexes();

StepVerifier
Expand All @@ -276,44 +274,27 @@ public void canListIndexesWithSelectedField() {
Index index1 = createTestIndex();
Index index2 = createTestIndex().setName("hotels2");

client.createIndex(index1).block();
client.createIndex(index2).block();

PagedFlux<Index> selectedFieldListResponse = client.listIndexes("name", generateRequestOptions());

StepVerifier
.create(selectedFieldListResponse.collectList())
.assertNext(result ->
result.forEach(res -> {
Assert.assertNotNull(res.getName());
Assert.assertNull(res.getFields());
Assert.assertNull(res.getDefaultScoringProfile());
Assert.assertNull(res.getCorsOptions());
Assert.assertNull(res.getScoringProfiles());
Assert.assertNull(res.getSuggesters());
Assert.assertNull(res.getAnalyzers());
Assert.assertNull(res.getTokenizers());
Assert.assertNull(res.getTokenFilters());
Assert.assertNull(res.getCharFilters());
})
)
.verifyComplete();

StepVerifier
.create(selectedFieldListResponse.collectList())
.assertNext(result -> {
Assert.assertEquals(2, result.size());
Assert.assertEquals(index1.getName(), result.get(0).getName());
Assert.assertEquals(index2.getName(), result.get(1).getName());
.create(client.createIndex(index1)
.then(client.createIndex(index2))
.thenMany(client.listIndexes("name", generateRequestOptions())))
.assertNext(resultIndex1 -> {
Assert.assertEquals(index1.getName(), resultIndex1.getName());
indexeWithSelectedFieldAssertions(resultIndex1);
})
.assertNext(resultIndex2 -> {
Assert.assertEquals(index2.getName(), resultIndex2.getName());
indexeWithSelectedFieldAssertions(resultIndex2);
})
.verifyComplete();

}

@Test
public void canAddSynonymFieldProperty() {
String synonymMapName = "names";
SynonymMap synonymMap = new SynonymMap().setName(synonymMapName).setSynonyms("hotel,motel");
client.createSynonymMap(synonymMap).block();

Index index = new Index()
.setName(HOTEL_INDEX_NAME)
Expand All @@ -329,7 +310,8 @@ public void canAddSynonymFieldProperty() {
));

StepVerifier
.create(client.createIndex(index))
.create(client.createSynonymMap(synonymMap)
.then(client.createIndex(index)))
.assertNext(createdIndex -> {
List<String> actualSynonym = index.getFields().get(1).getSynonymMaps();
List<String> expectedSynonym = createdIndex.getFields().get(1).getSynonymMaps();
Expand All @@ -345,8 +327,6 @@ public void canUpdateSynonymFieldProperty() {
.setName(synonymMapName)
.setSynonyms("hotel,motel");

client.createSynonymMap(synonymMap).block();

// Create an index
Index index = createTestIndex();
Field hotelNameField = getFieldByName(index, "HotelName");
Expand Down Expand Up @@ -456,19 +436,19 @@ true, new AccessCondition(), generateRequestOptions()))
@Test
public void createOrUpdateIndexThrowsWhenUpdatingSuggesterWithExistingIndexFields() {
Index index = createTestIndex();
client.createIndex(index).block();

Index existingIndex = client.getIndex(index.getName()).block();
assert existingIndex != null;

String existingFieldName = "Category";
existingIndex.setSuggesters(Collections.singletonList(new Suggester()
.setName("Suggestion")
.setSourceFields(Collections.singletonList(existingFieldName))
));

assertHttpResponseExceptionAsync(
client.createOrUpdateIndex(existingIndex),
Mono<Index> updatedIndex = client.createIndex(index)
.then(client.getIndex(index.getName()))
.flatMap(existingIndex -> {
existingIndex.setSuggesters(Collections.singletonList(new Suggester()
.setName("Suggestion")
.setSourceFields(Collections.singletonList(existingFieldName))
));
return client.createOrUpdateIndex(existingIndex);
});

assertHttpResponseExceptionAsync(updatedIndex,
HttpResponseStatus.BAD_REQUEST,
String.format("Fields that were already present in an index (%s) cannot be referenced by a new suggester."
+ " Only new fields added in the same index update operation are allowed.", existingFieldName));
Expand Down Expand Up @@ -574,11 +554,10 @@ public void createOrUpdateIndexIfNotChangedFailsWhenResourceChanged() {
@Test
public void canCreateAndGetIndexStats() {
Index testIndex = createTestIndex();
Index index = client.createOrUpdateIndex(testIndex).block();
assert index != null;

StepVerifier
.create(client.getIndexStatistics(index.getName()))
.create(client.createOrUpdateIndex(testIndex)
.flatMap(index -> client.getIndexStatistics(index.getName())))
.assertNext(stats -> {
Assert.assertEquals(0, stats.getDocumentCount());
Assert.assertEquals(0, stats.getStorageSize());
Expand All @@ -589,11 +568,10 @@ public void canCreateAndGetIndexStats() {
@Test
public void canCreateAndGetIndexStatsWithResponse() {
Index testIndex = createTestIndex();
Index index = client.createOrUpdateIndex(testIndex).block();
assert index != null;

StepVerifier
.create(client.getIndexStatisticsWithResponse(index.getName(), generateRequestOptions()))
.create(client.createOrUpdateIndex(testIndex)
.flatMap(index -> client.getIndexStatisticsWithResponse(index.getName(), generateRequestOptions())))
.assertNext(stats -> {
Assert.assertEquals(0, stats.getValue().getDocumentCount());
Assert.assertEquals(0, stats.getValue().getStorageSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import com.azure.search.models.Field;
import com.azure.search.models.Index;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;
import static org.unitils.reflectionassert.ReflectionComparatorMode.IGNORE_DEFAULTS;

Expand Down Expand Up @@ -123,4 +125,17 @@ Field getFieldByName(Index index, String name) {
.filter(f -> f.getName().equals(name))
.findFirst().get();
}

protected void indexeWithSelectedFieldAssertions(Index actualIndex) {
Assert.assertNull(actualIndex.getFields());
Assert.assertNull(actualIndex.getDefaultScoringProfile());
Assert.assertNull(actualIndex.getCorsOptions());
Assert.assertNull(actualIndex.getScoringProfiles());
Assert.assertNull(actualIndex.getSuggesters());
Assert.assertNull(actualIndex.getAnalyzers());
Assert.assertNull(actualIndex.getTokenizers());
Assert.assertNull(actualIndex.getTokenFilters());
Assert.assertNull(actualIndex.getCharFilters());
}

}
Loading

0 comments on commit 12820d1

Please sign in to comment.