Skip to content

Commit

Permalink
Merge pull request Azure#64 from navalev/roundtrip
Browse files Browse the repository at this point in the history
Dynamic & static doc datetime roundtrip UTC tests
  • Loading branch information
chenmliu authored Sep 11, 2019
2 parents 8b98c8b + a61b598 commit acc5a0d
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search.data.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;

public class Author {
@JsonProperty(value = "FirstName")
private String firstName;

@JsonProperty(value = "LastName")
private String lastName;

public String firstName() {
return this.firstName;
}

public Author firstName(String firstName) {
this.firstName = firstName;
return this;
}

public String lastName() {
return this.lastName;
}

public Author lastName(String lastName) {
this.lastName = lastName;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Author)) return false;
Author author = (Author) o;
return Objects.equals(firstName, author.firstName) &&
Objects.equals(lastName, author.lastName);
}

@Override
public int hashCode() {
return Objects.hash(firstName, lastName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.search.data.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import java.util.Objects;

public class Book {
@JsonProperty(value = "ISBN")
private String ISBN;

@JsonProperty(value = "Title")
private String title;

@JsonProperty(value = "Author")
private Author author;

@JsonProperty(value = "PublishDate")
private Date publishDate;

public String ISBN() {
return this.ISBN;
}

public Book ISBN(String ISBN) {
this.ISBN = ISBN;
return this;
}

public String title() {
return this.title;
}

public Book title(String title) {
this.title = title;
return this;
}

public Author author() {
return this.author;
}

public Book author(Author author) {
this.author = author;
return this;
}

public Date publishDate() {
return this.publishDate;
}

public Book publishDate(Date publishDate) {
this.publishDate = publishDate;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Book)) return false;
Book book = (Book) o;
return Objects.equals(ISBN, book.ISBN) &&
Objects.equals(title, book.title) &&
Objects.equals(author, book.author) &&
Objects.equals(publishDate, book.publishDate);
}

@Override
public int hashCode() {
return Objects.hash(ISBN, title, author, publishDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@
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.Book;
import com.azure.search.service.models.Index;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.azure.search.data.models.Hotel;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.junit.Assert;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -81,6 +92,92 @@ public void canRoundtripBoundaryValues() throws Exception {
}
}

@Override
public void dynamicDocumentDateTimesRoundTripAsUtc() throws IOException {
// Book 1's publish date is in UTC format, and book 2's is unspecified.
List<HashMap<String, Object>> books = Arrays.asList(
new HashMap<String, Object>() {
{
put(ISBN_FIELD, ISBN1);
put(PUBLISH_DATE_FIELD, DATE_UTC);
}
},
new HashMap<String, Object>() {
{
put(ISBN_FIELD, ISBN2);
put(PUBLISH_DATE_FIELD, "2010-06-27T00:00:00-00:00");
}
}
);

// Create 'books' index
Reader indexData = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(BOOKS_INDEX_JSON));
Index index = new ObjectMapper().readValue(indexData, Index.class);
if (!interceptorManager.isPlaybackMode()) {
searchServiceClient.indexes().create(index);
}

// Upload and retrieve book documents
uploadDocuments(client, BOOKS_INDEX_NAME, books);
Mono<Document> actualBook1 = client.getDocument(ISBN1);
Mono<Document> actualBook2 = client.getDocument(ISBN2);

// Verify
StepVerifier
.create(actualBook1)
.assertNext(res -> {
Assert.assertEquals(DATE_UTC, res.get(PUBLISH_DATE_FIELD));
})
.verifyComplete();
StepVerifier
.create(actualBook2)
.assertNext(res -> {
Assert.assertEquals(DATE_UTC, res.get(PUBLISH_DATE_FIELD));
})
.verifyComplete();
}

@Override
public void staticallyTypedDateTimesRoundTripAsUtc() throws Exception {
// Book 1's publish date is in UTC format, and book 2's is unspecified.
DateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat dateFormatUnspecifiedTimezone = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Book> books = Arrays.asList(
new Book()
.ISBN(ISBN1)
.publishDate(dateFormatUtc.parse(DATE_UTC)),
new Book()
.ISBN(ISBN2)
.publishDate(dateFormatUnspecifiedTimezone.parse("2010-06-27 00:00:00"))
);

// Create 'books' index
Reader indexData = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(BOOKS_INDEX_JSON));
Index index = new ObjectMapper().readValue(indexData, Index.class);
if (!interceptorManager.isPlaybackMode()) {
searchServiceClient.indexes().create(index);
}

// Upload and retrieve book documents
uploadDocuments(client, BOOKS_INDEX_NAME, books);
Mono<Document> actualBook1 = client.getDocument(ISBN1);
Mono<Document> actualBook2 = client.getDocument(ISBN2);

// Verify
StepVerifier
.create(actualBook1)
.assertNext(res -> {
Assert.assertEquals(books.get(0).publishDate(), res.as(Book.class).publishDate());
})
.verifyComplete();
StepVerifier
.create(actualBook2)
.assertNext(res -> {
Assert.assertEquals(books.get(1).publishDate(), res.as(Book.class).publishDate());
})
.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 @@ -11,11 +11,20 @@
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.Book;
import com.azure.search.service.models.Index;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.azure.search.data.models.Hotel;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.io.InputStreamReader;
import java.io.Reader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -72,6 +81,72 @@ public void canRoundtripBoundaryValues() throws Exception {
}
}

@Override
public void dynamicDocumentDateTimesRoundTripAsUtc() throws Exception {
// Book 1's publish date is in UTC format, and book 2's is unspecified.
List<HashMap<String, Object>> books = Arrays.asList(
new HashMap<String, Object>() {
{
put(ISBN_FIELD, ISBN1);
put(PUBLISH_DATE_FIELD, DATE_UTC);
}
},
new HashMap<String, Object>() {
{
put(ISBN_FIELD, ISBN2);
put(PUBLISH_DATE_FIELD, "2010-06-27T00:00:00-00:00");
}
}
);

// Create 'books' index
Reader indexData = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(BOOKS_INDEX_JSON));
Index index = new ObjectMapper().readValue(indexData, Index.class);
if (!interceptorManager.isPlaybackMode()) {
searchServiceClient.indexes().create(index);
}

// Upload and retrieve book documents
uploadDocuments(client, BOOKS_INDEX_NAME, books);
Document actualBook1 = client.getDocument(ISBN1);
Document actualBook2 = client.getDocument(ISBN2);

// Verify
Assert.assertEquals(DATE_UTC, actualBook1.get(PUBLISH_DATE_FIELD));
Assert.assertEquals(DATE_UTC, actualBook2.get(PUBLISH_DATE_FIELD));
}

@Override
public void staticallyTypedDateTimesRoundTripAsUtc() throws Exception {
// Book 1's publish date is in UTC format, and book 2's is unspecified.
DateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat dateFormatUnspecifiedTimezone = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Book> books = Arrays.asList(
new Book()
.ISBN(ISBN1)
.publishDate(dateFormatUtc.parse(DATE_UTC)),
new Book()
.ISBN(ISBN2)
.publishDate(dateFormatUnspecifiedTimezone.parse("2010-06-27 00:00:00"))
);

// Create 'books' index
Reader indexData = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(BOOKS_INDEX_JSON));
Index index = new ObjectMapper().readValue(indexData, Index.class);
if (!interceptorManager.isPlaybackMode()) {
searchServiceClient.indexes().create(index);
}

// Upload and retrieve book documents
uploadDocuments(client, BOOKS_INDEX_NAME, books);
Document actualBook1 = client.getDocument(ISBN1);
Document actualBook2 = client.getDocument(ISBN2);

// Verify
Assert.assertEquals(books.get(0).publishDate(), actualBook1.as(Book.class).publishDate());
Assert.assertEquals(books.get(1).publishDate(), actualBook2.as(Book.class).publishDate());
}

@Override
protected void initializeClient() {
client = builderSetup().indexName(INDEX_NAME).buildClient();
Expand Down
Loading

0 comments on commit acc5a0d

Please sign in to comment.