summaryrefslogtreecommitdiffstats
path: root/predicate-search/src/test/java/com/yahoo/search/predicate/serialization
diff options
context:
space:
mode:
Diffstat (limited to 'predicate-search/src/test/java/com/yahoo/search/predicate/serialization')
-rw-r--r--predicate-search/src/test/java/com/yahoo/search/predicate/serialization/PredicateQuerySerializerTest.java54
-rw-r--r--predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationHelperTest.java44
-rw-r--r--predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationTestHelper.java48
3 files changed, 146 insertions, 0 deletions
diff --git a/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/PredicateQuerySerializerTest.java b/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/PredicateQuerySerializerTest.java
new file mode 100644
index 00000000000..133834cc3fe
--- /dev/null
+++ b/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/PredicateQuerySerializerTest.java
@@ -0,0 +1,54 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.predicate.serialization;
+
+import com.yahoo.search.predicate.PredicateQuery;
+import com.yahoo.search.predicate.SubqueryBitmap;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author bjorncs
+ */
+public class PredicateQuerySerializerTest {
+
+ @Test
+ public void require_that_query_is_correctly_parsed_and_written_back_to_json() throws Exception {
+ String json =
+ "{\"features\":[" +
+ "{\"k\":\"k1\",\"v\":\"value1\",\"s\":\"0x1\"}," +
+ "{\"k\":\"k2\",\"v\":\"value2\",\"s\":\"0x3\"}" +
+ "],\"rangeFeatures\":[" +
+ "{\"k\":\"range1\",\"v\":123456789123,\"s\":\"0xffff\"}," +
+ "{\"k\":\"range2\",\"v\":0}" +
+ "]}";
+ PredicateQuerySerializer serializer = new PredicateQuerySerializer();
+ PredicateQuery query = serializer.fromJSON(json);
+ List<PredicateQuery.Feature> features = query.getFeatures();
+ PredicateQuery.Feature f1 = features.get(0);
+ PredicateQuery.Feature f2 = features.get(1);
+ List<PredicateQuery.RangeFeature> rangeFeatures = query.getRangeFeatures();
+ PredicateQuery.RangeFeature r1 = rangeFeatures.get(0);
+ PredicateQuery.RangeFeature r2 = rangeFeatures.get(1);
+
+ assertEquals("k1", f1.key);
+ assertEquals("value1", f1.value);
+ assertEquals(0x1, f1.subqueryBitmap);
+
+ assertEquals("k2", f2.key);
+ assertEquals("value2", f2.value);
+ assertEquals(0x3, f2.subqueryBitmap);
+
+ assertEquals("range1", r1.key);
+ assertEquals(123456789123l, r1.value);
+ assertEquals(0xFFFF, r1.subqueryBitmap);
+
+ assertEquals("range2", r2.key);
+ assertEquals(0l, r2.value);
+ assertEquals(SubqueryBitmap.DEFAULT_VALUE, r2.subqueryBitmap);
+
+ assertEquals(json, serializer.toJSON(query));
+ }
+}
diff --git a/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationHelperTest.java b/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationHelperTest.java
new file mode 100644
index 00000000000..4e4d6b40e0d
--- /dev/null
+++ b/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationHelperTest.java
@@ -0,0 +1,44 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.predicate.serialization;
+
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static com.yahoo.search.predicate.serialization.SerializationTestHelper.*;
+
+/**
+ * @author bjorncs
+ */
+public class SerializationHelperTest {
+
+ @Test
+ public void require_that_long_serialization_works() throws IOException {
+ long[] longs = {1, 2, 3, 4};
+ assertSerializationDeserializationMatches(
+ longs, SerializationHelper::writeLongArray, SerializationHelper::readLongArray);
+ }
+
+ @Test
+ public void require_that_int_serialization_works() throws IOException {
+ int[] ints = {1, 2, 3, 4};
+ assertSerializationDeserializationMatches(
+ ints, SerializationHelper::writeIntArray, SerializationHelper::readIntArray);
+ }
+
+ @Test
+ public void require_that_byte_serialization_works() throws IOException {
+ byte[] bytes = {1, 2, 3, 4};
+ assertSerializationDeserializationMatches(
+ bytes, SerializationHelper::writeByteArray, SerializationHelper::readByteArray);
+ }
+
+ @Test
+ public void require_that_short_serialization_works() throws IOException {
+ short[] shorts = {1, 2, 3, 4};
+ assertSerializationDeserializationMatches(
+ shorts, SerializationHelper::writeShortArray, SerializationHelper::readShortArray);
+ }
+
+
+}
diff --git a/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationTestHelper.java b/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationTestHelper.java
new file mode 100644
index 00000000000..47746b15d49
--- /dev/null
+++ b/predicate-search/src/test/java/com/yahoo/search/predicate/serialization/SerializationTestHelper.java
@@ -0,0 +1,48 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.predicate.serialization;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import static org.junit.Assert.assertArrayEquals;
+
+/**
+ * @author bjorncs
+ */
+public class SerializationTestHelper {
+
+ private SerializationTestHelper() {}
+
+ public static <T> void assertSerializationDeserializationMatches
+ (T object, Serializer<T> serializer, Deserializer<T> deserializer) throws IOException {
+
+ ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(4096);
+ DataOutputStream out = new DataOutputStream(byteArrayOut);
+ serializer.serialize(object, out);
+ out.flush();
+
+ byte[] bytes = byteArrayOut.toByteArray();
+ DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
+ T newObject = deserializer.deserialize(in);
+
+ byteArrayOut = new ByteArrayOutputStream(4096);
+ out = new DataOutputStream(byteArrayOut);
+ serializer.serialize(newObject, out);
+ byte[] newBytes = byteArrayOut.toByteArray();
+ assertArrayEquals(bytes, newBytes);
+ }
+
+ @FunctionalInterface
+ public interface Serializer<T> {
+ void serialize(T object, DataOutputStream out) throws IOException;
+ }
+
+ @FunctionalInterface
+ public interface Deserializer<T> {
+ T deserialize(DataInputStream in) throws IOException;
+ }
+
+}