Skip to content

Commit

Permalink
Using TestBase in tests (Azure#20)
Browse files Browse the repository at this point in the history
* Adding http client to client builder

* adding setter

* Adding TestBase for tests

* adding setter

* rename

* session record

* checkstyle fix

* rename variable

* minor naming changes
  • Loading branch information
rabee333 authored and navalev committed Aug 11, 2019
1 parent fcfe970 commit f7cae70
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

package com.azure.search.data.customization;

import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.rest.PagedFlux;
import com.azure.search.data.SearchIndexAsyncClient;
import com.azure.search.data.common.DocumentResponseConversions;
import com.azure.search.data.common.SearchPipelinePolicy;
import com.azure.core.http.rest.PagedFlux;
import com.azure.search.data.generated.SearchIndexRestClient;
import com.azure.search.data.generated.implementation.SearchIndexRestClientBuilder;
import com.azure.search.data.generated.models.AutocompleteParameters;
Expand Down Expand Up @@ -47,20 +48,36 @@ public class SearchIndexAsyncClientImpl extends SearchIndexBaseClient implements
*/
private String indexName;

/**
* The Http Client to be used.
*/
private HttpClient httpClient;

/**
* The Http Client to be used.
*/
private List<HttpPipelinePolicy> policies;


/**
* The underlying REST client to be used to actually interact with the Search service
*/
private SearchIndexRestClient restClient;

/**
* Package private constructor to be used by {@link SearchIndexClientBuilder}
*
* @param searchServiceName
* @param searchDnsSuffix
* @param indexName
* @param apiVersion
* @param policy
* @param httpClient
* @param policies
*/
SearchIndexAsyncClientImpl(String searchServiceName, String searchDnsSuffix, String indexName, String apiVersion, SearchPipelinePolicy policy) {
SearchIndexAsyncClientImpl(
String searchServiceName, String searchDnsSuffix, String indexName, String apiVersion,
HttpClient httpClient,
List<HttpPipelinePolicy> policies) {
if (StringUtils.isBlank(searchServiceName)) {
throw new IllegalArgumentException("Invalid searchServiceName");
}
Expand All @@ -73,25 +90,32 @@ public class SearchIndexAsyncClientImpl extends SearchIndexBaseClient implements
if (StringUtils.isBlank(apiVersion)) {
throw new IllegalArgumentException("Invalid apiVersion");
}
if (policy == null) {
throw new IllegalArgumentException("Invalid policy");
if (httpClient == null) {
throw new IllegalArgumentException("Invalid httpClient");
}
if (policies == null) {
throw new IllegalArgumentException("Invalid policies");
}

this.searchServiceName = searchServiceName;
this.searchDnsSuffix = searchDnsSuffix;
this.indexName = indexName;
this.apiVersion = apiVersion;
this.httpClient = httpClient;
this.policies = policies;

initialize(policy);
initialize();
}

private void initialize(SearchPipelinePolicy policy) {
private void initialize() {
restClient = new SearchIndexRestClientBuilder()
.searchServiceName(searchServiceName)
.indexName(indexName)
.searchDnsSuffix(searchDnsSuffix)
.apiVersion(apiVersion)
.pipeline(new HttpPipelineBuilder().policies(policy).build())
.pipeline(new HttpPipelineBuilder()
.httpClient(httpClient)
.policies(policies.toArray(new HttpPipelinePolicy[0])).build())
.build();
}

Expand Down Expand Up @@ -129,14 +153,15 @@ public Mono<Long> countDocuments() {

@Override
public PagedFlux<SearchResult> search() {
return null; //return restClient.documents().searchGetAsync();
return null; //return restClient.documents().searchPostAsync();
}

@Override
public PagedFlux<SearchResult> search(String searchText,
SearchParameters searchParameters,
SearchRequestOptions searchRequestOptions) {
return null; //return restClient.documents().searchGetAsync(searchText, searchParameters, searchRequestOptions);
public PagedFlux<SearchResult> search(
String searchText,
SearchParameters searchParameters,
SearchRequestOptions searchRequestOptions) {
return null; //return restClient.documents().searchPostAsync(searchText, searchParameters, searchRequestOptions);
}

@Override
Expand All @@ -145,8 +170,9 @@ public Mono<Map<String, Object>> getDocument(String key) {
}

@Override
public Mono<Map<String, Object>> getDocument(String key, List<String> selectedFields,
SearchRequestOptions searchRequestOptions) {
public Mono<Map<String, Object>> getDocument(
String key, List<String> selectedFields,
SearchRequestOptions searchRequestOptions) {
return restClient
.documents()
.getAsync(key, selectedFields, searchRequestOptions)
Expand All @@ -159,10 +185,11 @@ public PagedFlux<SuggestResult> suggest(String searchText, String suggesterName)
}

@Override
public PagedFlux<SuggestResult> suggest(String searchText,
String suggesterName,
SuggestParameters suggestParameters,
SearchRequestOptions searchRequestOptions) {
public PagedFlux<SuggestResult> suggest(
String searchText,
String suggesterName,
SuggestParameters suggestParameters,
SearchRequestOptions searchRequestOptions) {
return null;
}

Expand All @@ -177,13 +204,15 @@ public Mono<AutocompleteResult> autocomplete(String searchText, String suggester
}

@Override
public Mono<AutocompleteResult> autocomplete(String searchText,
String suggesterName,
SearchRequestOptions searchRequestOptions,
AutocompleteParameters autocompleteParameters) {
return restClient.documents().autocompleteGetAsync(searchText,
suggesterName,
searchRequestOptions,
autocompleteParameters);
public Mono<AutocompleteResult> autocomplete(
String searchText,
String suggesterName,
SearchRequestOptions searchRequestOptions,
AutocompleteParameters autocompleteParameters) {
return restClient.documents().autocompleteGetAsync(
searchText,
suggesterName,
searchRequestOptions,
autocompleteParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

package com.azure.search.data.customization;

import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.implementation.annotation.ServiceClientBuilder;
import com.azure.search.data.SearchIndexAsyncClient;
import com.azure.search.data.SearchIndexClient;
import com.azure.search.data.common.SearchPipelinePolicy;

import java.util.ArrayList;
import java.util.List;

/**
* Fluent SearchIndexClientBuilder for instantiating a {@link SearchIndexClientImpl} or a {@link SearchIndexAsyncClientImpl}
Expand All @@ -33,14 +37,17 @@ public class SearchIndexClientBuilder {
private String apiVersion;
private String serviceName;
private String indexName;
private SearchPipelinePolicy policy;
private String searchDnsSuffix;
private HttpClient httpClient;
private List<HttpPipelinePolicy> policies;

/**
* Default Constructor
*/
public SearchIndexClientBuilder() {
searchDnsSuffix = "search.windows.net";
httpClient = HttpClient.createDefault();
policies = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -77,13 +84,24 @@ public SearchIndexClientBuilder indexName(String indexName) {
}

/**
* Set the authentication policy (api-key)
* Set the http client (optional). If this is not set, a default httpClient will be created
*
* @param httpClient value of httpClient
* @return the updated SearchIndexClientBuilder object
*/
public SearchIndexClientBuilder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

/**
* Http Pipeline policy
*
* @param policy value of api-key
* @param policy policy to add to the pipeline
* @return the updated SearchIndexClientBuilder object
*/
public SearchIndexClientBuilder policy(SearchPipelinePolicy policy) {
this.policy = policy;
public SearchIndexClientBuilder addPolicy(HttpPipelinePolicy policy) {
this.policies.add(policy);
return this;
}

Expand All @@ -110,6 +128,6 @@ public SearchIndexClient buildClient() {
* @return a {@link SearchIndexAsyncClient} created from the configurations in this builder.
*/
public SearchIndexAsyncClient buildAsyncClient() {
return new SearchIndexAsyncClientImpl(serviceName, searchDnsSuffix, indexName, apiVersion, policy);
return new SearchIndexAsyncClientImpl(serviceName, searchDnsSuffix, indexName, apiVersion, httpClient, policies);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class SearchIndexClientExample {

/**
* sample
*
* @param args arguments
*/
public static void main(String[] args) {
Expand All @@ -33,12 +34,12 @@ public static void main(String[] args) {


SearchIndexClient searchClient = new SearchIndexClientBuilder()
.serviceName(searchServiceName)
.searchDnsSuffix(dnsSuffix)
.indexName(indexName)
.apiVersion(apiVersion)
.policy(new SearchPipelinePolicy(apiKey))
.buildClient();
.serviceName(searchServiceName)
.searchDnsSuffix(dnsSuffix)
.indexName(indexName)
.apiVersion(apiVersion)
.addPolicy(new SearchPipelinePolicy(apiKey))
.buildClient();

searchForAll(searchClient);

Expand All @@ -50,7 +51,8 @@ private static void searchForAll(SearchIndexClient searchClient) {

for (SearchResult searchResult : result.results()) {
Hotel hotel = getDocument(Hotel.class, searchResult.additionalProperties());
System.out.printf("\t score: %s, id: %s, name: %s\n",
System.out.printf(
"\t score: %s, id: %s, name: %s\n",
searchResult.score(),
hotel.HotelId,
hotel.HotelName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data;

import com.azure.search.data.generated.models.DocumentIndexResult;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
import com.azure.search.data.generated.models.IndexBatch;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchIndexAsyncClientTest extends SearchIndexClientTestBase {

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

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

@Test
public void indexResultSucceeds() throws Exception {

List<Map> hotels = loadHotels();

Map<String, Object> hotel = new HashMap<String, Object>(hotels.get(1));
List<IndexAction> indexActions = new ArrayList<>();
indexActions.add(new IndexAction()
.actionType(IndexActionType.UPLOAD)
.additionalProperties(hotel)
);

DocumentIndexResult result = searchIndexAsyncClient.index(new IndexBatch().actions(indexActions)).block();

assert result.results().get(0).statusCode() == 200;

}

private List<Map> loadHotels() throws IOException {
Reader docsData = new InputStreamReader(
getClass().getClassLoader().getResourceAsStream(HOTELS_DATA_JSON));
List<Map> hotels = new ObjectMapper().readValue(docsData, List.class);
assert hotels != null;

return hotels;
}


}
Loading

0 comments on commit f7cae70

Please sign in to comment.