Skip to content

Commit

Permalink
Feature/index exception (Azure#58)
Browse files Browse the repository at this point in the history
Added sync and async test indexWithInvalidDocumentThrowsException
  • Loading branch information
Humoiz authored Sep 9, 2019
1 parent 71211e6 commit b2f2179
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
// Licensed under the MIT License.
package com.azure.search.data.tests;

import com.azure.core.exception.HttpResponseException;
import com.azure.search.data.SearchIndexAsyncClient;
import com.azure.search.data.customization.Document;
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 io.netty.handler.codec.http.HttpResponseStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class IndexingAsyncTests extends IndexingTestBase {
private SearchIndexAsyncClient client;

Expand All @@ -17,6 +30,21 @@ public void countingDocsOfNewIndexGivesZero() {
StepVerifier.create(result).expectNext(expected).expectComplete().verify();
}

@Override
public void indexWithInvalidDocumentThrowsException() {
List<IndexAction> indexActions = new LinkedList<>();
addDocumentToIndexActions(indexActions, IndexActionType.UPLOAD, new Document());
Mono<DocumentIndexResult> indexResult = client.index(new IndexBatch().actions(indexActions));

StepVerifier
.create(indexResult)
.verifyErrorSatisfies(error -> {
assertEquals(HttpResponseException.class, error.getClass());
assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ((HttpResponseException) error).response().statusCode());
assertTrue(error.getMessage().contains("The request is invalid. Details: actions : 0: Document key cannot be missing or empty."));
});
}

@Override
protected void initializeClient() {
client = builderSetup().indexName(INDEX_NAME).buildAsyncClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@
// Licensed under the MIT License.
package com.azure.search.data.tests;

import com.azure.core.exception.HttpResponseException;
import com.azure.search.data.SearchIndexClient;
import com.azure.search.data.customization.Document;
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 org.junit.Assert;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.util.LinkedList;
import java.util.List;

public class IndexingSyncTests extends IndexingTestBase {
private SearchIndexClient client;

@Rule
public ExpectedException thrown = ExpectedException.none();

@Override
public void countingDocsOfNewIndexGivesZero() {
Long actual = client.countDocuments();
Expand All @@ -16,6 +29,16 @@ public void countingDocsOfNewIndexGivesZero() {
Assert.assertEquals(expected, actual);
}

@Override
public void indexWithInvalidDocumentThrowsException() {
thrown.expect(HttpResponseException.class);
thrown.expectMessage("The request is invalid. Details: actions : 0: Document key cannot be missing or empty.");

List<IndexAction> indexActions = new LinkedList<>();
addDocumentToIndexActions(indexActions, IndexActionType.UPLOAD, new Document());
client.index(new IndexBatch().actions(indexActions));
}

@Override
protected void initializeClient() {
client = builderSetup().indexName(INDEX_NAME).buildClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
package com.azure.search.data.tests;

import com.azure.search.data.env.SearchIndexClientTestBase;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

public abstract class IndexingTestBase extends SearchIndexClientTestBase {
protected static final String INDEX_NAME = "hotels";

Expand All @@ -17,5 +22,14 @@ protected void beforeTest() {
@Test
public abstract void countingDocsOfNewIndexGivesZero();

@Test
public abstract void indexWithInvalidDocumentThrowsException();

protected abstract void initializeClient();

protected void addDocumentToIndexActions(List<IndexAction> indexActions, IndexActionType indexActionType, HashMap<String, Object> document) {
indexActions.add(new IndexAction()
.actionType(indexActionType)
.additionalProperties(document));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://azs-sdk9cb546626e12.search.windows.net/indexes('hotels')/docs/search.index?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "b2f0825f-a0b3-466f-ad1c-ac82b6280a82",
"StatusCode" : "400",
"Date" : "Sat, 07 Sep 2019 14:34:57 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"elapsed-time" : "88",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "124",
"Body" : "{\"error\":{\"code\":\"\",\"message\":\"The request is invalid. Details: actions : 0: Document key cannot be missing or empty.\\r\\n\"}}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Language" : "en",
"Content-Type" : "application/json; odata.metadata=minimal"
}
} ],
"variables" : [ ]
}

0 comments on commit b2f2179

Please sign in to comment.