Skip to content

Commit

Permalink
Merge pull request Azure#65 from navalev/feature/1013-boundaryvalues
Browse files Browse the repository at this point in the history
Adds canRoundtripBoundaryValues AB#1013
  • Loading branch information
noelbundick authored Sep 11, 2019
2 parents 5059daa + 541396e commit 8b98c8b
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ public boolean equals(Object o) {
Objects.equals(description, hotel.description) &&
Objects.equals(descriptionFr, hotel.descriptionFr) &&
Objects.equals(category, hotel.category) &&
Objects.equals(tags, hotel.tags) &&
ModelComparer.collectionEquals(tags, hotel.tags) &&
Objects.equals(parkingIncluded, hotel.parkingIncluded) &&
Objects.equals(smokingAllowed, hotel.smokingAllowed) &&
Objects.equals(lastRenovationDate, hotel.lastRenovationDate) &&
Objects.equals(rating, hotel.rating) &&
Objects.equals(location, hotel.location) &&
Objects.equals(address, hotel.address) &&
Objects.equals(rooms, hotel.rooms);
ModelComparer.collectionEquals(rooms, hotel.rooms);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public boolean equals(Object o) {
Objects.equals(bedOptions, hotelRoom.bedOptions) &&
Objects.equals(sleepsCount, hotelRoom.sleepsCount) &&
Objects.equals(smokingAllowed, hotelRoom.smokingAllowed) &&
Objects.equals(tags, hotelRoom.tags);
ModelComparer.collectionEquals(tags, hotelRoom.tags);
}

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

public class ModelComparer {
public static <T> boolean collectionEquals(Collection<T> seq1, Collection<T> seq2) {
if (seq1 == null) {
return seq2 == null || seq2.isEmpty();
} else {
if (seq2 == null) {
seq2 = new ArrayList<>();
}
return Objects.equals(seq1, seq2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@

import com.azure.core.exception.HttpResponseException;
import com.azure.search.data.SearchIndexAsyncClient;
import com.azure.search.data.common.jsonwrapper.JsonWrapper;
import com.azure.search.data.common.jsonwrapper.api.JsonApi;
import com.azure.search.data.common.jsonwrapper.jacksonwrapper.JacksonDeserializer;
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 com.azure.search.data.models.Hotel;
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 java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -45,6 +51,36 @@ public void indexWithInvalidDocumentThrowsException() {
});
}

@Override
public void canRoundtripBoundaryValues() throws Exception {
JsonApi jsonApi = JsonWrapper.newInstance(JacksonDeserializer.class);
jsonApi.configureTimezone();

List<Hotel> boundaryConditionDocs = getBoundaryValues();

List<IndexAction> actions = boundaryConditionDocs.stream()
.map(h -> new IndexAction()
.actionType(IndexActionType.UPLOAD)
.additionalProperties((Map<String, Object>) jsonApi.convertObjectToType(h, Map.class)))
.collect(Collectors.toList());
IndexBatch batch = new IndexBatch()
.actions(actions);

client.index(batch).block();

// Wait 2 secs to allow index request to finish
Thread.sleep(2000);

for (Hotel expected : boundaryConditionDocs) {
StepVerifier.create(client.getDocument(expected.hotelId()))
.expectNextMatches(d -> {
Hotel actual = d.as(Hotel.class);
return actual.equals(expected);
})
.verifyComplete();
}
}

@Override
protected void initializeClient() {
client = builderSetup().indexName(INDEX_NAME).buildAsyncClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

import com.azure.core.exception.HttpResponseException;
import com.azure.search.data.SearchIndexClient;
import com.azure.search.data.common.jsonwrapper.JsonWrapper;
import com.azure.search.data.common.jsonwrapper.api.JsonApi;
import com.azure.search.data.common.jsonwrapper.jacksonwrapper.JacksonDeserializer;
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 com.azure.search.data.models.Hotel;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

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

public class IndexingSyncTests extends IndexingTestBase {
private SearchIndexClient client;
Expand All @@ -39,6 +45,33 @@ public void indexWithInvalidDocumentThrowsException() {
client.index(new IndexBatch().actions(indexActions));
}

@Override
public void canRoundtripBoundaryValues() throws Exception {
JsonApi jsonApi = JsonWrapper.newInstance(JacksonDeserializer.class);
jsonApi.configureTimezone();

List<Hotel> boundaryConditionDocs = getBoundaryValues();

List<IndexAction> actions = boundaryConditionDocs.stream()
.map(h -> new IndexAction()
.actionType(IndexActionType.UPLOAD)
.additionalProperties((Map<String, Object>) jsonApi.convertObjectToType(h, Map.class)))
.collect(Collectors.toList());
IndexBatch batch = new IndexBatch()
.actions(actions);

client.index(batch);

// Wait 2 secs to allow index request to finish
Thread.sleep(2000);

for (Hotel expected : boundaryConditionDocs) {
Document doc = client.getDocument(expected.hotelId());
Hotel actual = doc.as(Hotel.class);
Assert.assertEquals(expected, actual);
}
}

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

import com.azure.search.data.customization.models.GeoPoint;
import com.azure.search.data.env.SearchIndexClientTestBase;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
import com.azure.search.data.models.Hotel;
import com.azure.search.data.models.HotelAddress;
import com.azure.search.data.models.HotelRoom;
import org.junit.Test;

import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

Expand All @@ -25,11 +31,85 @@ protected void beforeTest() {
@Test
public abstract void indexWithInvalidDocumentThrowsException();

@Test
public abstract void canRoundtripBoundaryValues() throws Exception;

protected abstract void initializeClient();

protected void addDocumentToIndexActions(List<IndexAction> indexActions, IndexActionType indexActionType, HashMap<String, Object> document) {
indexActions.add(new IndexAction()
.actionType(indexActionType)
.additionalProperties(document));
}

protected List<Hotel> getBoundaryValues() throws ParseException {
return Arrays.asList(
// Minimum values
new Hotel()
.hotelId("1")
.category("")
.lastRenovationDate(DATE_FORMAT.parse("0001-01-01T00:00:00Z"))
.location(GeoPoint.createWithDefaultCrs(-180, -90)) // South pole, date line from the west
.parkingIncluded(false)
.rating(Integer.MIN_VALUE)
.tags(Arrays.asList())
.address(new HotelAddress())
.rooms(Arrays.asList(
new HotelRoom()
.baseRate(Double.MIN_VALUE)
)),
// Maximimum values
new Hotel()
.hotelId("2")
.category("test") // No meaningful string max since there is no length limit (other than payload size or term length).
.lastRenovationDate(DATE_FORMAT.parse("9999-12-31T11:59:59Z"))
.location(GeoPoint.createWithDefaultCrs(180, 90)) // North pole, date line from the east
.parkingIncluded(true)
.rating(Integer.MAX_VALUE)
.tags(Arrays.asList("test")) // No meaningful string max; see above.
.address(new HotelAddress()
.city("Maximum"))
.rooms(Arrays.asList(
new HotelRoom()
.baseRate(Double.MAX_VALUE)
)),
// Other boundary values #1
new Hotel()
.hotelId("3")
.category(null)
.lastRenovationDate(null)
.location(GeoPoint.createWithDefaultCrs(0, 0)) // Equator, meridian
.parkingIncluded(null)
.rating(null)
.tags(Arrays.asList())
.address(new HotelAddress()
.city("Maximum"))
.rooms(Arrays.asList(
new HotelRoom()
.baseRate(Double.NEGATIVE_INFINITY)
)),
// Other boundary values #2
new Hotel()
.hotelId("4")
.location(null)
.tags(Arrays.asList())
.rooms(Arrays.asList(
new HotelRoom()
.baseRate(Double.POSITIVE_INFINITY)
)),
// Other boundary values #3
new Hotel()
.hotelId("5")
.tags(Arrays.asList())
.rooms(Arrays.asList(
new HotelRoom()
.baseRate(Double.NaN)
)),
// Other boundary values #4
new Hotel()
.hotelId("6")
.category(null)
.tags(Arrays.asList())
.rooms(Arrays.asList()));
}
}
Loading

0 comments on commit 8b98c8b

Please sign in to comment.