Skip to content

Commit

Permalink
Make public types jackson friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Aug 28, 2024
1 parent 9ad8181 commit 99b8293
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 46 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ dependencies {
implementation "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"

testImplementation 'junit:junit:4.13.2'
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
testImplementation 'junit:junit:4.13.2'
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
testImplementation "org.mockito:mockito-junit-jupiter:${mockitoVersion}"
testImplementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonVersion}"

testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
}

mavenPublishing {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/fauna/client/PageIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public Page<E> next() {
if (this.latestResult != null) {
Page<E> page = this.latestResult;
this.latestResult = null;
if (page.after() != null) {
Map<String, Object> args = Map.of("after", page.after());
if (page.getAfter() != null) {
Map<String, Object> args = Map.of("after", page.getAfter());
this.queryFuture = client.asyncQuery(fql(PAGINATE_QUERY, args), pageClass, options);
}
return page;
Expand All @@ -102,7 +102,7 @@ public Page<E> next() {
public Iterator<E> flatten() {
return new Iterator<>() {
private final PageIterator<E> pageIterator = PageIterator.this;
private Iterator<E> thisPage = pageIterator.hasNext() ? pageIterator.next().data().iterator() : null;
private Iterator<E> thisPage = pageIterator.hasNext() ? pageIterator.next().getData().iterator() : null;
@Override
public boolean hasNext() {
return thisPage != null && (thisPage.hasNext() || pageIterator.hasNext());
Expand All @@ -117,7 +117,7 @@ public E next() {
return thisPage.next();
} catch (NoSuchElementException e) {
if (pageIterator.hasNext()) {
this.thisPage = pageIterator.next().data().iterator();
this.thisPage = pageIterator.next().getData().iterator();
return thisPage.next();
} else {
throw e;
Expand Down
29 changes: 10 additions & 19 deletions src/main/java/com/fauna/types/BaseDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,21 @@ public Module getCollection() {
return collection;
}

/**
* Gets a copy of the underlying data as a Map.
*
* @return The data.
*/
public Map<String,Object> getData() {
return Map.copyOf(data);
}

/**
* Gets the count of key-value pairs contained in the document.
*
* @return The number of key-value pairs.
*/
public int getCount() {
public int size() {
return data.size();
}

Expand All @@ -134,22 +143,4 @@ public boolean containsKey(String key) {
public Object get(String key) {
return data.get(key);
}

/**
* Gets a collection containing the keys of the data in the document.
*
* @return A collection containing the keys of the data in the document.
*/
public Enumeration<String> getKeys() {
return data.keys();
}

/**
* Gets a collection containing the values of the data in the document.
*
* @return A collection containing the values of the data in the document.
*/
public Enumeration<Object> getValues() {
return data.elements();
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/fauna/types/NonNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ public NonNull(T val) {
super(val);
}

/**
* Get the wrapped value.
*
* @return The wrapped value
*/
@Override
public T get() {
return val;
}

/**
* Get the wrapped value.
*
* @return The wrapped value
*/
public T getValue() {
// Allows for default serialization without attributes.
return get();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/fauna/types/NullDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public NullDoc(String id, Module coll, String cause) {
this.cause = cause;
}

/**
* Get the cause of the null document.
*
* @return A string describing the cause of the null document.
*/
public String getCause() {
return cause;
}
Expand All @@ -26,11 +31,19 @@ public T get() {
throw new NullDocumentException(id, coll, cause);
}

/**
* Get the ID of the null document.
* @return The document ID.
*/
public String getId() {
return id;
}

public Module getColl() {
/**
* Get the Collection associated with the null document.
* @return A Module representing the collection.
*/
public Module getCollection() {
return coll;
}

Expand All @@ -44,7 +57,7 @@ public boolean equals(Object o) {

var c = (NullDoc<?>) o;
return id.equals(c.getId())
&& coll.equals(c.getColl())
&& coll.equals(c.getCollection())
&& cause.equals(c.getCause());
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fauna/types/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public Page(List<T> data, String after) {
this.after = after;
}

public List<T> data() {
public List<T> getData() {
return data;
}

public String after() {
public String getAfter() {
return after;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/fauna/client/FaunaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ void paginateWithQueryOptions() throws IOException {
PageIterator<Product> iter = client.paginate(fql("Document.all()"), Product.class, options);
assertTrue(iter.hasNext());
Page<Product> firstPage = iter.next();
assertEquals("product-0", firstPage.data().get(0).getName());
assertEquals("product-0", firstPage.getData().get(0).getName());
assertTrue(iter.hasNext());
Page<Product> secondPage = iter.next();
assertEquals("product-1", secondPage.data().get(0).getName());
assertEquals("product-1", secondPage.getData().get(0).getName());
assertFalse(iter.hasNext());

}
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/fauna/client/PageIteratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void test_single_page() throws Exception {
when(client.asyncQuery(any(), any(ParameterizedOf.class), any())).thenReturn(successFuture(false, 0));
PageIterator<String> pageIterator = new PageIterator<>(client, fql("hello"), String.class, null);
assertTrue(pageIterator.hasNext());
assertEquals(pageIterator.next().data(), List.of("0-a", "0-b"));
assertEquals(pageIterator.next().getData(), List.of("0-a", "0-b"));
assertFalse(pageIterator.hasNext());
assertThrows(NoSuchElementException.class, () -> pageIterator.next());
}
Expand All @@ -76,7 +76,7 @@ public void test_single_page_without_calling_hasNext() throws Exception {
when(client.asyncQuery(any(), any(ParameterizedOf.class), any())).thenReturn(successFuture(false, 0));
PageIterator<String> pageIterator = new PageIterator<>(client, fql("hello"), String.class, null);
// No call to hasNext here.
assertEquals(pageIterator.next().data(), List.of("0-a", "0-b"));
assertEquals(pageIterator.next().getData(), List.of("0-a", "0-b"));
assertFalse(pageIterator.hasNext());
assertThrows(NoSuchElementException.class, () -> pageIterator.next());
}
Expand All @@ -87,10 +87,10 @@ public void test_multiple_pages() throws Exception {
successFuture(true, 0), successFuture(false, 1));
PageIterator<String> pageIterator = new PageIterator<>(client, fql("hello"), String.class, null);
assertTrue(pageIterator.hasNext());
assertEquals(List.of("0-a", "0-b"), pageIterator.next().data());
assertEquals(List.of("0-a", "0-b"), pageIterator.next().getData());

assertTrue(pageIterator.hasNext());
assertEquals(List.of("1-a", "1-b"), pageIterator.next().data());
assertEquals(List.of("1-a", "1-b"), pageIterator.next().getData());
assertFalse(pageIterator.hasNext());
assertThrows(NoSuchElementException.class, () -> pageIterator.next());
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public void test_PageIterator_from_Page() throws IOException {
assertEquals(page, pageIterator.next());

assertTrue(pageIterator.hasNext());
assertEquals(List.of("0-a", "0-b"), pageIterator.next().data());
assertEquals(List.of("0-a", "0-b"), pageIterator.next().getData());

assertFalse(pageIterator.hasNext());
assertThrows(NoSuchElementException.class, () -> pageIterator.next());
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/com/fauna/e2e/E2EPaginationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void query_single_item_gets_wrapped_in_page() {
PageIterator<Product> iter = client.paginate(fql("Product.firstWhere(.name == 'product-1')"), Product.class);
assertTrue(iter.hasNext());
Page<Product> page = iter.next();
assertEquals(1, page.data().size());
assertNull(page.after());
assertEquals(1, page.getData().size());
assertNull(page.getAfter());
assertFalse(iter.hasNext());
assertThrows(NoSuchElementException.class, iter::next);
}
Expand All @@ -50,8 +50,8 @@ public void query_single_page_gets_wrapped_in_page() {
PageIterator<Product> iter = client.paginate(fql("Product.where(.quantity < 8)"), Product.class);
assertTrue(iter.hasNext());
Page<Product> page = iter.next();
assertEquals(8, page.data().size());
assertNull(page.after());
assertEquals(8, page.getData().size());
assertNull(page.getAfter());
assertFalse(iter.hasNext());
assertThrows(NoSuchElementException.class, iter::next);
}
Expand All @@ -64,11 +64,11 @@ public void query_all_with_manual_pagination() {
Page<Product> latest = first.getData();
List<List<Product>> pages = new ArrayList<>();

pages.add(latest.data());
while (latest.after() != null) {
QuerySuccess<Page<Product>> paged = client.query(fql("Set.paginate(${after})", Map.of("after", latest.after())), pageOf);
pages.add(latest.getData());
while (latest.getAfter() != null) {
QuerySuccess<Page<Product>> paged = client.query(fql("Set.paginate(${after})", Map.of("after", latest.getAfter())), pageOf);
latest = paged.getData();
pages.add(latest.data());
pages.add(latest.getData());
}
assertEquals(4, pages.size());
assertEquals(2, pages.get(3).size());
Expand All @@ -80,7 +80,7 @@ public void query_all_with_pagination() {
List<Page<Product>> pages = new ArrayList<>();
iter.forEachRemaining(pages::add);
assertEquals(4, pages.size());
List<Product> products = pages.stream().flatMap(p -> p.data().stream()).collect(Collectors.toList());
List<Product> products = pages.stream().flatMap(p -> p.getData().stream()).collect(Collectors.toList());
assertEquals(50, products.size());
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/fauna/e2e/E2EQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void query_pageOfPerson() {
var q = fql("Author.all()");

var qs = c.query(q, pageOf(Author.class));
var actual = qs.getData().data();
var actual = qs.getData().getData();

assertEquals(2, actual.size());
assertEquals("Alice", actual.get(0).getFirstName());
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/fauna/types/DocumentRefTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fauna.types;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DocumentRefTest extends TypeTestBase {

@Test
public void docRef_playsNiceWithJackson() throws JsonProcessingException {
var doc = new DocumentRef(
"123",
new Module("MyColl")
);

var result = mapper.writeValueAsString(doc);
assertEquals("{\"collection\":{\"name\":\"MyColl\"},\"id\":\"123\"}", result);
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/fauna/types/DocumentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.fauna.types;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DocumentTest extends TypeTestBase {

@Test
public void document_playsNiceWithJackson() throws JsonProcessingException {
var doc = new Document(
"123",
new Module("MyColl"),
Instant.parse("2024-01-23T13:33:10.300Z"),
Map.of("some_key", "some_val")
);

var result = mapper.writeValueAsString(doc);
assertEquals("{\"data\":{\"some_key\":\"some_val\"},\"ts\":1706016790.300000000,\"collection\":{\"name\":\"MyColl\"},\"id\":\"123\"}", result);
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/fauna/types/NamedDocumentRefTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.fauna.types;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class NamedDocumentRefTest extends TypeTestBase {

@Test
public void namedDocRef_playsNiceWithJackson() throws JsonProcessingException {
var doc = new NamedDocumentRef(
"AName",
new Module("MyColl")
);

var result = mapper.writeValueAsString(doc);
assertEquals("{\"collection\":{\"name\":\"MyColl\"},\"name\":\"AName\"}", result);
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/fauna/types/NamedDocumentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.fauna.types;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class NamedDocumentTest extends TypeTestBase {

@Test
public void document_playsNiceWithJackson() throws JsonProcessingException {
var doc = new NamedDocument(
"AName",
new Module("MyColl"),
Instant.parse("2024-01-23T13:33:10.300Z"),
Map.of("some_key", "some_val")
);

var result = mapper.writeValueAsString(doc);
assertEquals("{\"data\":{\"some_key\":\"some_val\"},\"ts\":1706016790.300000000,\"collection\":{\"name\":\"MyColl\"},\"name\":\"AName\"}", result);
}
}
Loading

0 comments on commit 99b8293

Please sign in to comment.