Skip to content

Commit

Permalink
task 510 + Playback mechanism changes to make it more generic (Azure#21)
Browse files Browse the repository at this point in the history
Task 510 - Adding test methods that verify the returned field types
  • Loading branch information
eladiw authored Aug 13, 2019
1 parent f7cae70 commit e186b82
Show file tree
Hide file tree
Showing 17 changed files with 472 additions and 73 deletions.
Binary file modified search/data-plane/data/lib/service-1.0-SNAPSHOT.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions search/data-plane/data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure.search</groupId>
<artifactId>service</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
public class DocumentResponseConversions {

private static final String ODATA_CONTEXT = "@odata.context";
/**
* Convert a Linked HashMap object to Map object
* @param linkedMapObject object to convert
Expand All @@ -29,6 +30,16 @@ public static Map<String, Object> convertLinkedHashMapToMap(Object linkedMapObje
}

return convertedMap;
}

/**
* Drop fields that shouldn't be in the returned object
* @param map the map to drop items from
* @return the new map
*/
public static Map<String, Object> dropUnnecessaryFields(Map<String, Object> map) {
map.remove(ODATA_CONTEXT);

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public PagedFlux<SearchResult> search(

@Override
public Mono<Map<String, Object>> getDocument(String key) {
return restClient.documents().getAsync(key).map(DocumentResponseConversions::convertLinkedHashMapToMap);
return restClient.documents().getAsync(key).map(DocumentResponseConversions::convertLinkedHashMapToMap)
.map(DocumentResponseConversions::dropUnnecessaryFields);
}

@Override
Expand All @@ -176,7 +177,8 @@ public Mono<Map<String, Object>> getDocument(
return restClient
.documents()
.getAsync(key, selectedFields, searchRequestOptions)
.map(DocumentResponseConversions::convertLinkedHashMapToMap);
.map(DocumentResponseConversions::convertLinkedHashMapToMap)
.map(DocumentResponseConversions::dropUnnecessaryFields);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class AzureSearchResources {
private String resourceGroupName;
private String searchServiceName;
private String searchAdminKey;
private String indexFileName;

private AzureTokenCredentials azureTokenCredentials;
private String subscriptionId;
Expand All @@ -37,6 +38,14 @@ public String getResourceGroupName() {
return resourceGroupName;
}

/**
*
* @return
*/
public String getIndexFileName() {
return indexFileName;
}

/**
*
* @return The created Search service name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class SearchIndexDocs {

private static final String HOTELS_DATA_JSON = "HotelsDataArray.json";
public static final String HOTELS_DATA_JSON = "HotelsDataArray.json";

private String searchServiceName;
private String apiAdminKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

public class SearchIndexService {

private static final String INDEX_DATA_JSON = "IndexData.json";

private String searchServiceName;
private String apiAdminKey;
private String indexName;
private String indexDataFileName;

private SearchServiceClient searchServiceClient;

Expand All @@ -31,7 +30,8 @@ public class SearchIndexService {
* @param searchServiceName the name of Search Service in Azure.
* @param apiAdminKey the Admin Key of Search Service
*/
public SearchIndexService(String searchServiceName, String apiAdminKey) {
public SearchIndexService(String indexDataFileName, String searchServiceName, String apiAdminKey) {
this.indexDataFileName = indexDataFileName;
this.searchServiceName = searchServiceName;
this.apiAdminKey = apiAdminKey;
}
Expand Down Expand Up @@ -62,7 +62,7 @@ private void validate() {
}

private void addIndexes() throws IOException {
Reader indexData = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(INDEX_DATA_JSON));
Reader indexData = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(indexDataFileName));
Index index = new ObjectMapper().readValue(indexData, Index.class);
this.indexName = index.name();
searchServiceClient.indexes().create(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public static void main(String[] args) throws IOException {

String serviceName = azureSearchResources.getSearchServiceName();
String apiAdminKey = azureSearchResources.getSearchAdminKey();
String indexFileName = azureSearchResources.getIndexFileName();

//Creating Index:
SearchIndexService searchIndexService;
try {
searchIndexService = new SearchIndexService(serviceName, apiAdminKey);
searchIndexService = new SearchIndexService(indexFileName, serviceName, apiAdminKey);
searchIndexService.initialize();
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data;
package com.azure.search.data.test.customization;

import com.azure.search.data.SearchIndexAsyncClient;
import com.azure.search.data.env.SearchIndexDocs;
import com.azure.search.data.generated.models.DocumentIndexResult;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
Expand All @@ -22,11 +24,33 @@ public class SearchIndexAsyncClientTest extends SearchIndexClientTestBase {

private SearchIndexAsyncClient searchIndexAsyncClient;
private static final String HOTELS_DATA_JSON = "HotelsDataArray.json";
private static final String INDEX_NAME = "hotels";

private void addDocsData() throws IOException {

Reader docsData = new InputStreamReader(
getClass().getClassLoader().getResourceAsStream(SearchIndexDocs.HOTELS_DATA_JSON));
List<Map> hotels = new ObjectMapper().readValue(docsData, List.class);
DocumentIndexResult documentIndexResult = indexDocuments(searchIndexAsyncClient, hotels);

System.out.println("Indexing Results:");
assert documentIndexResult != null;
documentIndexResult.results().forEach(result ->
System.out.println(
"key:" + result.key() + (result.succeeded() ? " Succeeded" : " Error: " + result.errorMessage()))
);
}

@Override
protected void beforeTest() {
super.beforeTest();
searchIndexAsyncClient = builderSetup().buildAsyncClient();
searchIndexAsyncClient = builderSetup().indexName(INDEX_NAME).buildAsyncClient();

try {
addDocsData();
} catch (IOException exc) {

}
}

@Test
Expand Down Expand Up @@ -55,6 +79,4 @@ private List<Map> loadHotels() throws IOException {

return hotels;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data.test.customization;

import com.azure.search.data.SearchIndexAsyncClient;
import org.junit.Assert;
import org.junit.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

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

import static java.lang.Double.POSITIVE_INFINITY;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.NaN;


public class SearchIndexClientLookupTest extends SearchIndexClientTestBase {
private SearchIndexAsyncClient client;
private static final String INDEX_NAME = "data-types-tests-index";


@Override
protected void beforeTest() {
super.beforeTest();
client = builderSetup().indexName(INDEX_NAME).buildAsyncClient();
}

/**
* This test verify that the same types that were indexed are returned, but some edge cases are not returned as
* expected such as Double.POSITIVE_INFINITY which returns as a string "INF"
* This test verifies that both the 'non edge case' types are returned correctly and that
* the edge cases are behaving as expected
*/
@Test
public void dynamicallyTypedPrimitiveCollectionsDoNotAllRoundtripCorrectly() {
client.setIndexName(INDEX_NAME);
String docKey = "1";

// Creating the document to be indexed
HashMap<String, Object> document = new HashMap<String, Object>();
document.put("Key", docKey);
document.put("Dates", new Object[]{});
document.put("Doubles", new Double[]{0.0, 5.8, POSITIVE_INFINITY, NEGATIVE_INFINITY, NaN});
document.put("Bools", new boolean[]{true, false});
document.put("Longs", new Long[]{9999999999999999L, 832372345832523L});
document.put("Strings", new String[]{"hello", "bye"});
document.put("Ints", new int[]{1, 2, 3, 4, -13, 5, 0});
document.put("Points", new Object[]{});

// This is the expected document when querying the document later
HashMap<String, Object> expectedDocument = new HashMap<String, Object>();
expectedDocument.put("Key", docKey);
expectedDocument.put("Doubles", Arrays.asList(0.0, 5.8, "INF", "-INF", "NaN"));
expectedDocument.put("Bools", Arrays.asList(true, false));
expectedDocument.put("Longs", Arrays.asList(9999999999999999L, 832372345832523L));
expectedDocument.put("Strings", Arrays.asList("hello", "bye"));
expectedDocument.put("Ints", Arrays.asList(1, 2, 3, 4, -13, 5, 0));
// Todo: fix below 2 items
expectedDocument.put("Points", Arrays.asList());
expectedDocument.put("Dates", Arrays.asList());

// Index the document
indexDocument(client, document);

// Get the indexed document
getAndVerifyDoc(client, docKey, expectedDocument);
}

/**
* This test verifies that sometimes the converted type is not the right one and its by design
*/
//@Test
//public void getDynamicDocumentCannotAlwaysDetermineCorrectType() {
// Todo: Uncomment test, when task 574 is done
/*client.setIndexName(INDEX_NAME);
String docKey = "2";
// Creating the document to be indexed
// Set a String field to a valid datetime string
HashMap<String, Object> document = new HashMap<String, Object>();
document.put("Key", docKey);
document.put("Strings", Arrays.asList("2015-02-11T12:58:00Z"));
// This is the expected document when querying the document later
// Expect that the returned String field is actually converted to DateTime... which is wrong, but, by design.
HashMap<String, Object> expectedDocument = new HashMap<String, Object>();
expectedDocument.put("Key", docKey);
expectedDocument.put("Strings", Arrays.asList(DateTime.parse("2015-02-11T12:58:00Z")));
// Index the document
indexDocument(client, document);
getAndVerifyDoc(client, docKey, expectedDocument);*/
//}

@Test
public void emptyDynamicallyTypedPrimitiveCollectionsRoundtripAsObjectArrays() {
client.setIndexName(INDEX_NAME);
String docKey = "3";

// Creating the document to be indexed
HashMap<String, Object> document = new HashMap<String, Object>();
document.put("Key", docKey);
document.put("Dates", new Object[]{});
document.put("Doubles", new Double[]{});
document.put("Bools", new boolean[]{});
document.put("Longs", new Long[]{});
document.put("Strings", new String[]{});
document.put("Ints", new int[]{});
document.put("Points", new Object[]{});

// This is the expected document when querying the document later
HashMap<String, Object> expectedDocument = new HashMap<String, Object>();
expectedDocument.put("Key", docKey);
expectedDocument.put("Doubles", Arrays.asList());
expectedDocument.put("Bools", Arrays.asList());
expectedDocument.put("Longs", Arrays.asList());
expectedDocument.put("Strings", Arrays.asList());
expectedDocument.put("Ints", Arrays.asList());
expectedDocument.put("Points", Arrays.asList());
expectedDocument.put("Dates", Arrays.asList());

// Index the document
indexDocument(client, document);

// Get the indexed document
getAndVerifyDoc(client, docKey, expectedDocument);
}

/**
* Retrieve and verify the document
* @param client
* @param docKey
* @param expectedDocument
*/
private void getAndVerifyDoc(SearchIndexAsyncClient client, String docKey, HashMap<String, Object> expectedDocument) {
// Get the indexed document
Mono<Map<String, Object>> futureDoc = client.getDocument(docKey);
// Verify that for every item we indexed we get the right response
StepVerifier
.create(futureDoc)
.assertNext(result -> {
Assert.assertEquals(expectedDocument, result);
})
.verifyComplete();
}
}

This file was deleted.

Loading

0 comments on commit e186b82

Please sign in to comment.