summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@yahoo-inc.com>2017-02-03 14:46:38 +0100
committerTor Brede Vekterli <vekterli@yahoo-inc.com>2017-02-06 13:16:41 +0100
commit3ef1af8be251d1854d9e2829eea915eaaae5e31c (patch)
tree3e1f301293d23136efa80713db17921e69ed87cd /document
parentf4ace23e3314e4dd3708044c38453c1ac15c2a5e (diff)
Add Java/C++ cross-language serialization tests for references
Diffstat (limited to 'document')
-rw-r--r--document/src/test/java/com/yahoo/document/serialization/ReferenceFieldValueSerializationTestCase.java40
-rw-r--r--document/src/test/resources/reference/empty_reference__cppbin0 -> 61 bytes
-rw-r--r--document/src/test/resources/reference/empty_reference__javabin0 -> 61 bytes
-rw-r--r--document/src/test/resources/reference/reference_with_id__cppbin0 -> 84 bytes
-rw-r--r--document/src/test/resources/reference/reference_with_id__javabin0 -> 84 bytes
-rw-r--r--document/src/tests/serialization/vespadocumentserializer_test.cpp32
6 files changed, 63 insertions, 9 deletions
diff --git a/document/src/test/java/com/yahoo/document/serialization/ReferenceFieldValueSerializationTestCase.java b/document/src/test/java/com/yahoo/document/serialization/ReferenceFieldValueSerializationTestCase.java
index bdff2e8e01d..e35326c81c8 100644
--- a/document/src/test/java/com/yahoo/document/serialization/ReferenceFieldValueSerializationTestCase.java
+++ b/document/src/test/java/com/yahoo/document/serialization/ReferenceFieldValueSerializationTestCase.java
@@ -1,6 +1,7 @@
// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.serialization;
+import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
@@ -9,6 +10,8 @@ import com.yahoo.document.ReferenceDataType;
import com.yahoo.document.datatypes.ReferenceFieldValue;
import org.junit.Test;
+import java.io.IOException;
+
/**
* @author vekterli
* @since 6.65
@@ -17,9 +20,12 @@ public class ReferenceFieldValueSerializationTestCase {
static class Fixture {
final TestDocumentFactory documentFactory;
- final static String REF_TARGET_DOC_TYPE_NAME = "ref_target_type";
- final static String REF_SOURCE_DOC_TYPE_NAME = "ref_src_type";
+ // Note: these must match their C++ serialization test counterparts.
+ final static String REF_TARGET_DOC_TYPE_NAME = "my document";
+ final static String REF_SOURCE_DOC_TYPE_NAME = "doc_with_ref";
+ final static int REF_TYPE_ID = 789;
final static String SOURCE_REF_FIELD_NAME = "ref_field";
+ final static String CROSS_LANGUAGE_PATH = "src/test/resources/reference/";
Fixture() {
final DocumentTypeManager typeManager = new DocumentTypeManager();
@@ -30,14 +36,14 @@ public class ReferenceFieldValueSerializationTestCase {
final DocumentType sourceType = createReferenceSourceDocumentType(typeManager, REF_SOURCE_DOC_TYPE_NAME, targetType.getName());
typeManager.register(sourceType);
- this.documentFactory = new TestDocumentFactory(typeManager, sourceType, "id:ns:" + REF_SOURCE_DOC_TYPE_NAME + "::foo");
+ this.documentFactory = new TestDocumentFactory(typeManager, sourceType, "id:test:" + REF_SOURCE_DOC_TYPE_NAME + "::foo");
}
DocumentType createReferenceSourceDocumentType(DocumentTypeManager typeManager, String docTypeName, String targetName) {
final DocumentType type = new DocumentType(docTypeName);
type.addField(new Field(SOURCE_REF_FIELD_NAME, new ReferenceDataType(
typeManager.getDocumentType(targetName),
- targetName.hashCode())));
+ REF_TYPE_ID)));
return type;
}
@@ -51,6 +57,12 @@ public class ReferenceFieldValueSerializationTestCase {
value.setDocumentId(id);
return value;
}
+
+ Document createDocumentWithReference(ReferenceFieldValue refValue) {
+ final Document document = documentFactory.createDocument();
+ document.setFieldValue(Fixture.SOURCE_REF_FIELD_NAME, refValue);
+ return document;
+ }
}
@Test
@@ -68,5 +80,23 @@ public class ReferenceFieldValueSerializationTestCase {
fixture.createReferenceFieldValueWithId(new DocumentId("id:ns:" + Fixture.REF_TARGET_DOC_TYPE_NAME + "::bar")));
}
- // TODO test cross-language serialization once we've got the C++ version of the types
+ @Test
+ public void empty_reference_serialization_matches_cpp() throws IOException {
+ final Fixture fixture = new Fixture();
+ final Document document = fixture.createDocumentWithReference(fixture.createEmptyReferenceFieldValue());
+
+ SerializationTestUtils.assertSerializationMatchesCpp(
+ Fixture.CROSS_LANGUAGE_PATH, "empty_reference", document, fixture.documentFactory);
+ }
+
+ @Test
+ public void reference_with_id_serialization_matches_cpp() throws IOException {
+ final Fixture fixture = new Fixture();
+ final Document document = fixture.createDocumentWithReference(fixture.createReferenceFieldValueWithId(
+ new DocumentId("id:ns:" + Fixture.REF_TARGET_DOC_TYPE_NAME + "::bar")));
+
+ SerializationTestUtils.assertSerializationMatchesCpp(
+ Fixture.CROSS_LANGUAGE_PATH, "reference_with_id", document, fixture.documentFactory);
+ }
+
}
diff --git a/document/src/test/resources/reference/empty_reference__cpp b/document/src/test/resources/reference/empty_reference__cpp
new file mode 100644
index 00000000000..88b3cc00e35
--- /dev/null
+++ b/document/src/test/resources/reference/empty_reference__cpp
Binary files differ
diff --git a/document/src/test/resources/reference/empty_reference__java b/document/src/test/resources/reference/empty_reference__java
new file mode 100644
index 00000000000..88b3cc00e35
--- /dev/null
+++ b/document/src/test/resources/reference/empty_reference__java
Binary files differ
diff --git a/document/src/test/resources/reference/reference_with_id__cpp b/document/src/test/resources/reference/reference_with_id__cpp
new file mode 100644
index 00000000000..d165328676c
--- /dev/null
+++ b/document/src/test/resources/reference/reference_with_id__cpp
Binary files differ
diff --git a/document/src/test/resources/reference/reference_with_id__java b/document/src/test/resources/reference/reference_with_id__java
new file mode 100644
index 00000000000..d165328676c
--- /dev/null
+++ b/document/src/test/resources/reference/reference_with_id__java
Binary files differ
diff --git a/document/src/tests/serialization/vespadocumentserializer_test.cpp b/document/src/tests/serialization/vespadocumentserializer_test.cpp
index a1436b954e7..fe065e563ba 100644
--- a/document/src/tests/serialization/vespadocumentserializer_test.cpp
+++ b/document/src/tests/serialization/vespadocumentserializer_test.cpp
@@ -100,9 +100,9 @@ DocumenttypesConfig getDocTypesConfig() {
Struct("my_type.body")
.addField(predicate_field_name, DataType::T_PREDICATE));
builder.document(doc_with_ref_type_id, doc_with_ref_name,
- Struct(doc_with_ref_name + ".header"),
- Struct(doc_with_ref_name + ".body")
- .addField(ref_field_name, ref_type_id))
+ Struct(doc_with_ref_name + ".header")
+ .addField(ref_field_name, ref_type_id),
+ Struct(doc_with_ref_name + ".body"))
.referenceType(ref_type_id, doc_type_id);
return builder.config();
}
@@ -879,7 +879,8 @@ TEST("Require that tensor deserialization matches Java") {
}
struct RefFixture {
- FixedTypeRepo fixed_repo{doc_repo, *doc_repo.getDocumentType(doc_with_ref_type_id)};
+ const DocumentType* ref_doc_type{doc_repo.getDocumentType(doc_with_ref_type_id)};
+ FixedTypeRepo fixed_repo{doc_repo, *ref_doc_type};
const ReferenceDataType& ref_type() const {
auto* raw_type = fixed_repo.getDataType(ref_type_id);
@@ -895,6 +896,18 @@ struct RefFixture {
VespaDocumentDeserializer deserializer(fixed_repo, stream, serialization_version);
deserializer.read(dest);
}
+
+ void verify_cross_language_serialization(const string& file_base_name,
+ const ReferenceFieldValue& value) {
+ const string data_dir = TEST_PATH("../../test/resources/reference/");
+ serializeToFile(value, data_dir + file_base_name + "__cpp",
+ ref_doc_type, "ref_field");
+
+ deserializeAndCheck(data_dir + file_base_name + "__cpp",
+ value, fixed_repo, "ref_field");
+ deserializeAndCheck(data_dir + file_base_name + "__java",
+ value, fixed_repo, "ref_field");
+ }
};
TEST_F("Empty ReferenceFieldValue can be roundtrip serialized", RefFixture) {
@@ -927,6 +940,17 @@ TEST_F("ReferenceFieldValue with ID has changed-flag cleared after deserializati
EXPECT_FALSE(dest.hasChanged());
}
+TEST_F("Empty ReferenceFieldValue serialization matches Java", RefFixture) {
+ ReferenceFieldValue value(f.ref_type());
+ f.verify_cross_language_serialization("empty_reference", value);
+}
+
+TEST_F("ReferenceFieldValue with ID serialization matches Java", RefFixture) {
+ ReferenceFieldValue value(
+ f.ref_type(), DocumentId("id:ns:" + doc_name + "::bar"));
+ f.verify_cross_language_serialization("reference_with_id", value);
+}
+
} // namespace
TEST_MAIN() { TEST_RUN_ALL(); }