Skip to content

Commit

Permalink
Indexers - createOrUpdate, List indexers and matching UTs (Azure#222)
Browse files Browse the repository at this point in the history
* indexers create - wip

* Adds APIs for checking synonym map existence (Azure#209)

* Adds APIs for checking synonym map existence

* Adds createOrUpdateSynonymMap client methods (Azure#210)

* createOrUpdateSynonym map if not exists tests (Azure#216)

createOrUpdateSynonymMapIfNotExistsFailsOnExistingResource and createOrUpdateSynonymMapIfNotExistsSucceedsOnNoResource tests

* createOrUpdateSynonym map if exists tests (Azure#217)

createOrUpdateSynonymMapIfExistsSucceedsOnExistingResource and createOrUpdateSynonymMapIfExistsSucceedsOnExistingResource tests

* assertReflectionEquals - index management objects (Azure#214)

* removing index management equal comparison annd replacing with reflection equal method

* Indexers - createOrUpdate, List indexers and matching UTs

* style fix

* fix merge issues

* style fixes

* changed list overload

* changed list overload

* changed list overload

* fix style
  • Loading branch information
eladiw authored and itye-msft committed Nov 7, 2019
1 parent 2fb4937 commit 978b95e
Show file tree
Hide file tree
Showing 11 changed files with 973 additions and 323 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;

public abstract class DataSourceTestBase extends SearchServiceTestBase {

private static final String FAKE_DESCRIPTION = "Some data source";
private static final String RESOURCE_NAME_PREFIX = "azs-";
// The connection string we use here, as well as table name and target index schema, use the USGS database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.azure.search.models.Field;
import com.azure.search.models.Index;
import org.junit.Test;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search;

import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.rest.PagedFlux;
import com.azure.search.models.DataSource;
import com.azure.search.models.Index;
import com.azure.search.models.Indexer;
import com.azure.search.models.IndexingParameters;
import org.junit.Assert;
import reactor.test.StepVerifier;

import java.util.HashMap;
import java.util.Map;

public class IndexersManagementAsyncTests extends IndexersManagementTestBase {
private SearchServiceAsyncClient client;

@Override
public void createIndexerReturnsCorrectDefinition() {
client = getSearchServiceClientBuilder().buildAsyncClient();
Indexer expectedIndexer = createTestIndexer("indexer");
expectedIndexer.setIsDisabled(true);
expectedIndexer.setParameters(
new IndexingParameters()
.setBatchSize(50)
.setMaxFailedItems(10)
.setMaxFailedItemsPerBatch(10));

Indexer actualIndexer = client.createOrUpdateIndexer(expectedIndexer).block();

IndexingParameters ip = new IndexingParameters();
Map<String, Object> config = new HashMap<>();
ip.setConfiguration(config);
expectedIndexer.setParameters(ip); // Get returns empty dictionary.
expectSameStartTime(expectedIndexer, actualIndexer);

assertIndexersEqual(expectedIndexer, actualIndexer);
}

@Override
public void canCreateAndListIndexers() {
client = getSearchServiceClientBuilder().buildAsyncClient();

// Create the data source, note it a valid DS with actual
// connection string
DataSource datasource = createTestSqlDataSource();
client.createOrUpdateDataSource(datasource).block();

// Create an index
Index index = createTestIndexForLiveDatasource();
client.createOrUpdateIndex(index).block();


// Create two indexers
Indexer indexer1 = createTestIndexer("i1");
Indexer indexer2 = createTestIndexer("i2");
client.createOrUpdateIndexer(indexer1).block();
client.createOrUpdateIndexer(indexer2).block();

PagedFlux<Indexer> returnedIndexersLst = client.listIndexers();
StepVerifier
.create(returnedIndexersLst.collectList())
.assertNext(indexers -> {
Assert.assertEquals(2, indexers.size());
Assert.assertTrue(indexers.get(0).getName().equals(indexer1.getName()));
Assert.assertTrue(indexers.get(1).getName().equals(indexer2.getName()));
})
.verifyComplete();
}

@Override
public void createIndexerFailsWithUsefulMessageOnUserError() {
client = getSearchServiceClientBuilder().buildAsyncClient();
Indexer indexer = createTestIndexer("indexer");
indexer.setDataSourceName("thisdatasourcedoesnotexist");

assertException(
() -> client.createOrUpdateIndexer(indexer).block(),
HttpResponseException.class,
"This indexer refers to a data source 'thisdatasourcedoesnotexist' that doesn't exist");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search;

import com.azure.core.exception.HttpResponseException;
import com.azure.search.models.DataSource;
import com.azure.search.models.Index;
import com.azure.search.models.Indexer;
import com.azure.search.models.IndexingParameters;
import org.junit.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class IndexersManagementSyncTests extends IndexersManagementTestBase {
private SearchServiceClient client;

@Override
public void createIndexerReturnsCorrectDefinition() {
client = getSearchServiceClientBuilder().buildClient();
Indexer expectedIndexer =
createTestIndexer("indexer")
.setIsDisabled(true)
.setParameters(
new IndexingParameters()
.setBatchSize(50)
.setMaxFailedItems(10)
.setMaxFailedItemsPerBatch(10));

Indexer actualIndexer = client.createOrUpdateIndexer(expectedIndexer);

IndexingParameters ip = new IndexingParameters();
Map<String, Object> config = new HashMap<>();
ip.setConfiguration(config);
expectedIndexer.setParameters(ip); // Get returns empty dictionary.
expectSameStartTime(expectedIndexer, actualIndexer);

assertIndexersEqual(expectedIndexer, actualIndexer);
}

@Override
public void canCreateAndListIndexers() {
client = getSearchServiceClientBuilder().buildClient();

// Create the data source, note it a valid DS with actual
// connection string
DataSource datasource = createTestSqlDataSource();
client.createOrUpdateDataSource(datasource);

// Create an index
Index index = createTestIndexForLiveDatasource();
client.createOrUpdateIndex(index);


// Create two indexers
Indexer indexer1 = createTestIndexer("i1");
Indexer indexer2 = createTestIndexer("i2");
client.createOrUpdateIndexer(indexer1);
client.createOrUpdateIndexer(indexer2);

List<Indexer> indexers = client.listIndexers().stream().collect(Collectors.toList());
Assert.assertEquals(2, indexers.size());

Assert.assertTrue(
indexers.stream()
.anyMatch(item -> indexer1.getName().equals(item.getName())));
Assert.assertTrue(
indexers.stream()
.anyMatch(item -> indexer2.getName().equals(item.getName())));
}

@Override
public void createIndexerFailsWithUsefulMessageOnUserError() {
client = getSearchServiceClientBuilder().buildClient();
Indexer indexer = createTestIndexer("indexer");
indexer.setDataSourceName("thisdatasourcedoesnotexist");

assertException(
() -> client.createOrUpdateIndexer(indexer),
HttpResponseException.class,
"This indexer refers to a data source 'thisdatasourcedoesnotexist' that doesn't exist");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search;

import com.azure.search.models.DataType;
import com.azure.search.models.Field;
import com.azure.search.models.Index;
import com.azure.search.models.Indexer;
import com.azure.search.models.IndexingSchedule;
import org.junit.Assert;
import org.junit.Test;

import java.time.Duration;
import java.util.Arrays;

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

public abstract class IndexersManagementTestBase extends SearchServiceTestBase {

@Test
public abstract void createIndexerReturnsCorrectDefinition();

@Test
public abstract void canCreateAndListIndexers();

@Test
public abstract void createIndexerFailsWithUsefulMessageOnUserError();

protected void assertIndexersEqual(Indexer expected, Indexer actual) {
expected.setETag("none");
actual.setETag("none");

// we ignore defaults as when properties are not set they are returned from the service with
// default values
assertReflectionEquals(expected, actual, IGNORE_DEFAULTS);
}

protected Indexer createTestIndexer(String indexerName) {
return new Indexer()
.setName(indexerName)
.setDataSourceName("azs-java-test-sql")
.setTargetIndexName("indexforindexers")
.setSchedule(new IndexingSchedule().setInterval(Duration.ofDays(1)));
}

protected static void expectSameStartTime(Indexer expected, Indexer actual) {
// There ought to be a start time in the response; We just can't know what it is because it would
// make the test timing-dependent.
expected.getSchedule().setStartTime(actual.getSchedule().getStartTime());
}

/**
* This index contains fields that are declared on the live datasource
* we use to test the indexers
*
* @return the newly created Index object
*/
protected Index createTestIndexForLiveDatasource() {
return new Index()
.setName("indexforindexers")
.setFields(Arrays.asList(
new Field()
.setName("county_name")
.setType(DataType.EDM_STRING)
.setSearchable(Boolean.FALSE)
.setFilterable(Boolean.TRUE),
new Field()
.setName("state")
.setType(DataType.EDM_STRING)
.setSearchable(Boolean.TRUE)
.setFilterable(Boolean.TRUE),
new Field()
.setName("feature_id")
.setType(DataType.EDM_STRING)
.setKey(Boolean.TRUE)
.setSearchable(Boolean.TRUE)
.setFilterable(Boolean.FALSE)));
}

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));
}
}
}
Loading

0 comments on commit 978b95e

Please sign in to comment.