package ch.ethz.globis.distindex.mapping;

import ch.ethz.globis.distindex.util.SerializerUtil;
import ch.ethz.globis.pht.PVEntry;
import ch.ethz.globis.pht.PhMapper;
import ch.ethz.globis.pht.PhMapperKey;
import ch.ethz.globis.pht.PhPredicate;
import org.junit.Test;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class SerializerUtilTest {

    @Test
    public void testSerializeDeserializePhPredicate() throws IOException, ClassNotFoundException {
        PhPredicate pred = getTestPredicate();
        Map<long[], Boolean> argumentResultMap = getTestPredicateResultMap();
        checkResults(argumentResultMap, pred);

        PhPredicate deserializedPredicate = serializeDeserialize(pred);
        checkResults(argumentResultMap, deserializedPredicate);
    }

    @Test
    public void testSerializeNull() throws IOException, ClassNotFoundException {
        SerializerUtil serializer = SerializerUtil.getInstance();
        byte[] data = serializer.serializeDefault(null);
        PhPredicate deserializedPredicate = serializer.deserializeDefault(data);
        assertEquals(null, deserializedPredicate);
    }

    @Test
    public void testSerializeDeserializePhMapper() throws IOException, ClassNotFoundException {
        PVEntry<Object> e = new PVEntry<>(new long[] {1, 2, 3}, "Hello, world");
        assertEquals(e, serializeDeserialize(PhMapper.PVENTRY()).map(e));
        assertEquals(e.getKey(), serializeDeserialize(PhMapper.LONG_ARRAY()).map(e));
    }

    @Test
    public void testSerializeDeserializePhMapperKey() throws IOException, ClassNotFoundException {
        long[] key = {1, 2, 3};
        assertEquals(key, serializeDeserialize(PhMapperKey.LONG_ARRAY()).map(key));
    }

    private void checkResults(Map<long[], Boolean> argumentResultMap, PhPredicate predicate) {
        long[] key;
        boolean result;
        for (Map.Entry<long[], Boolean> entry : argumentResultMap.entrySet()) {
            key = entry.getKey();
            result = entry.getValue();
            assertEquals(result, predicate.test(key));
        }
    }

    private PhPredicate getTestPredicate() {
        return p -> p.length < 2;
    }

    private <T extends Serializable> T serializeDeserialize(T object) throws IOException, ClassNotFoundException {
        SerializerUtil serializer = SerializerUtil.getInstance();
        byte[] data = serializer.serializeDefault(object);
        return serializer.deserializeDefault(data);
    }

    private Map<long[], Boolean> getTestPredicateResultMap() {
        Map<long[], Boolean> argumentResultMap = new HashMap<>();
        argumentResultMap.put(new long[] {}, true);
        argumentResultMap.put(new long[] {1}, true);
        argumentResultMap.put(new long[] {1, 2}, false);
        argumentResultMap.put(new long[] {1, 2, 3}, false);
        argumentResultMap.put(new long[] {1, 2, 3, 4}, false);
        return argumentResultMap;
    }
}
@@ -412,16 +412,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertNull;
 %0A%0Apublic
@@ -523,32 +523,639 @@
 id testSerialize
+Null() %7B%0A        SerializerUtil serializer = SerializerUtil.getInstance();%0A        Object obj = null;%0A        byte%5B%5D data = serializer.serialize(obj);%0A        assertNull(serializer.deserialize(data));%0A    %7D%0A%0A    @Test%0A    public void testSerializeDefaultNull() throws IOException, ClassNotFoundException %7B%0A        SerializerUtil serializer = SerializerUtil.getInstance();%0A        byte%5B%5D data = serializer.serializeDefault(null);%0A        PhPredicate deserializedPredicate = serializer.deserializeDefault(data);%0A        assertEquals(null, deserializedPredicate);%0A    %7D%0A%0A    @Test%0A    public void testSerialize
 DeserializePhPre
@@ -1495,361 +1495,8 @@
 tMap
-, deserializedPredicate);%0A    %7D%0A%0A    @Test%0A    public void testSerializeNull() throws IOException, ClassNotFoundException %7B%0A        SerializerUtil serializer = SerializerUtil.getInstance();%0A        byte%5B%5D data = serializer.serializeDefault(null);%0A        PhPredicate deserializedPredicate = serializer.deserializeDefault(data);%0A        assertEquals(null
 , de

Index distribution Added test for null serialization.