/*
 * Copyright 2012 Mozilla Foundation
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mozilla.bagheera.sink;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.cli.ParseException;
import org.apache.hadoop.hbase.HServerAddress;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Row;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public class HBaseSinkTest {
    SinkConfiguration sinkConfig;
    KeyValueSinkFactory sinkFactory;
    HTablePool hbasePool;
    HTable htable;

    @Before
    public void setup() throws IOException {
        sinkConfig = new SinkConfiguration();
        sinkConfig.setString("hbasesink.hbase.tablename", "test");
        sinkConfig.setString("hbasesink.hbase.column.family", "data");
        sinkConfig.setString("hbasesink.hbase.column.qualifier", "json");
        sinkConfig.setBoolean("hbasesink.hbase.rowkey.prefixdate", false);
        sinkFactory = KeyValueSinkFactory.getInstance(HBaseSink.class, sinkConfig);

        hbasePool = Mockito.mock(HTablePool.class);
        htable = Mockito.mock(HTable.class);

        Mockito.when(hbasePool.getTable("test".getBytes())).thenReturn(htable);

        Mockito.doAnswer(new Answer<Object>() {
            int count = 0;
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                count++;
                // Force code to retry twice.
                if (count <= 2) {
                    throw new RetriesExhaustedWithDetailsException(new ArrayList<Throwable>(), new ArrayList<Row>(), new ArrayList<HServerAddress>());
                }
                return null;
            }
        }).when(htable).put(Mockito.anyListOf(Put.class));
    }

    @Test
    public void testRetry() throws ParseException, IOException {
        HBaseSink sink = (HBaseSink) sinkFactory.getSink("test");
        sink.hbasePool = hbasePool;

        sink.flush();

        Mockito.verify(htable, Mockito.times(2)).clearRegionCache();
    }

}

@@ -928,16 +928,67 @@
 rayList;
+%0Aimport java.util.concurrent.ConcurrentLinkedQueue;
 %0A%0Aimport
@@ -2539,11 +2539,10 @@
 try 
-twi
+on
 ce.%0A
@@ -2570,17 +2570,17 @@
 ount %3C= 
-2
+1
 ) %7B%0A    
@@ -3030,17 +3030,16 @@
 sePool;%0A
-%0A
         
@@ -3052,17 +3052,16 @@
 lush();%0A
-%0A
         
@@ -3097,17 +3097,17 @@
 o.times(
-2
+1
 )).clear
@@ -3128,10 +3128,845 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testLargePut() throws IOException %7B%0A        HBaseSink sink = (HBaseSink) sinkFactory.getSink(%22test%22);%0A%0A        @SuppressWarnings(%22unchecked%22)%0A        ConcurrentLinkedQueue%3CPut%3E putsQueue = Mockito.mock(ConcurrentLinkedQueue.class);%0A%0A        sink.putsQueue = putsQueue;%0A        byte%5B%5D theArray = new byte%5B50*1000*1000%5D; // 50MB%0A        for (int i = 0; i %3C theArray.length; i++) %7B%0A            theArray%5Bi%5D = (byte)(i %25 256 - 128);%0A        %7D%0A%0A        sink.store(%22test1%22, theArray);%0A        Mockito.verify(putsQueue, Mockito.times(0)).add((Put)Mockito.any());%0A        sink.store(%22test2%22, %22acceptable%22.getBytes());%0A        Mockito.verify(putsQueue, Mockito.times(1)).add((Put)Mockito.any());%0A        sink.store(%22test3%22, theArray);%0A        Mockito.verify(putsQueue, Mockito.times(1)).add((Put)Mockito.any());%0A    %7D%0A%0A
 %7D%0A

Add a test for very large HBase Puts