package com.github.simonthecat.hom;

import com.github.simonthecat.hom.model.CellDef;
import com.github.simonthecat.hom.model.RowKey;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.hbase.client.Result;
import org.junit.Test;

import static org.apache.hadoop.hbase.util.Bytes.toBytes;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class HBaseObjectMapperTest {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class SimplePojo {

        @RowKey
        private String rowKey;

        @CellDef(family = "f1", qualifier = "q1")
        private String strValue;

        @CellDef(family = "f2", qualifier = "q2")
        private Long longValue;

        @CellDef(family = "f3", qualifier = "q3")
        private Float floatValue;

        @CellDef(family = "f4", qualifier = "q4")
        private Double doubleValue;

        @CellDef(family = "f5", qualifier = "q5")
        private Integer intValue;

    }


    @Test
    public void mapperShouldMapAllFieldsUsingDefaultDeserializers() throws Exception {
        // given
        HBaseObjectMapper mapper = HBaseObjectMapperBuilder.builder()
                .withDefaultDeserializers()
                .build();

        // when
        Result mockResult = mock(Result.class);
        when(mockResult.getRow()).thenReturn(toBytes("rowkey01"));
        when(mockResult.getValue(toBytes("f1"), toBytes("q1"))).thenReturn(toBytes("a string"));
        when(mockResult.getValue(toBytes("f2"), toBytes("q2"))).thenReturn(toBytes(32L));
        when(mockResult.getValue(toBytes("f3"), toBytes("q3"))).thenReturn(toBytes(3.16f));
        when(mockResult.getValue(toBytes("f4"), toBytes("q4"))).thenReturn(toBytes(1.0));
        when(mockResult.getValue(toBytes("f5"), toBytes("q5"))).thenReturn(toBytes(99));

        SimplePojo mapped = mapper.map(mockResult, SimplePojo.class);

        // then
        SimplePojo expected = new SimplePojo("rowkey01", "a string", 32L, 3.16f, 1.0, 99);
        assertThat(mapped).isEqualTo(expected);

    }
}
@@ -275,18 +275,85 @@
 org.
-junit.Test
+apache.hadoop.hbase.util.Bytes;%0Aimport org.junit.Test;%0A%0Aimport java.util.Date
 ;%0A%0Ai
@@ -1160,16 +1160,199 @@
 %0A    %7D%0A%0A
+    @Data%0A    @NoArgsConstructor%0A    @AllArgsConstructor%0A    public static class PojoWithDate %7B%0A%0A        @CellDef(family = %22f1%22, qualifier = %22date%22)%0A        private Date date;%0A%0A    %7D%0A
 %0A    @Te
@@ -2421,10 +2421,1678 @@
 ;%0A%0A    %7D
+%0A%0A    @Test(expected = DeserializationException.class)%0A    public void mapperShouldThrowIfDeserializerNotFound() throws Exception %7B%0A        // given%0A        HBaseObjectMapper mapper = HBaseObjectMapperBuilder.builder()%0A                .withDefaultDeserializers()%0A                .build();%0A%0A        // when%0A        Date date = new Date(1451530800000L);%0A%0A        Result mockResult = mock(Result.class);%0A        when(mockResult.getRow()).thenReturn(toBytes(%22rowkey01%22));%0A        when(mockResult.getValue(toBytes(%22f1%22), toBytes(%22date%22))).thenReturn(toBytes(date.getTime()));%0A%0A        mapper.map(mockResult, PojoWithDate.class);%0A%0A        // then expect DeserializationException%0A    %7D%0A%0A    @Test%0A    public void mapperShouldMakeUseOfCustomDeserializers() throws Exception %7B%0A        // given%0A        HBaseObjectMapper mapper = HBaseObjectMapperBuilder.builder()%0A                .withDefaultDeserializers()%0A                .registerTypeDeserializer(Date.class, new TypeDeserializer%3CDate%3E() %7B%0A                    @Override%0A                    public Date deserialize(byte%5B%5D bytes) throws DeserializationException %7B%0A                        return new Date(Bytes.toLong(bytes));%0A                    %7D%0A                %7D)%0A                .build();%0A%0A        // when%0A        Date date = new Date(1451530800000L);%0A%0A        Result mockResult = mock(Result.class);%0A        when(mockResult.getRow()).thenReturn(toBytes(%22rowkey01%22));%0A        when(mockResult.getValue(toBytes(%22f1%22), toBytes(%22date%22))).thenReturn(toBytes(date.getTime()));%0A%0A        PojoWithDate mapped = mapper.map(mockResult, PojoWithDate.class);%0A%0A        // then%0A        assertThat(mapped.getDate()).isEqualTo(date);%0A    %7D
 %0A%7D

Test custom deserializers