jeudi 7 mai 2015

Using Mockito to return a specific value from an Hbase table

This is the method that I am testing. This method gets some Bytes from a Hbase Database based on an specific id, in this case called dtmid. The reason I why I want to return some specific values is because I realized that there is no way to know if an id will always be in Hbase. Also, the column Family and column name could change.

@Override
   public void execute(Tuple tuple, BasicOutputCollector collector) {
      try {
        if (tuple.size() > 0) {
            Long dtmid = tuple.getLong(0);

            byte[] rowKey = HBaseRowKeyDistributor.getDistributedKey(dtmid);
            Get get = new Get(rowKey);
            get.addFamily("a".getBytes());
            Result result = table.get(get);
            byte[] bidUser = result.getValue("a".getBytes(),
                    "co_created_5076".getBytes());
            collector.emit(new Values(dtmid, bidUser));
        }

    } catch (IOException e) {
        e.printStackTrace();

    }

}

On my main class when this method is called I want to return a specific value. The method should return some bytes.

    byte[] bidUser = result.getValue("a".getBytes(),
                    "co_created_5076".getBytes());

This is what I have on my Unit Test.

 @Test
      public void testExecute() throws IOException {
      long dtmId = 350000000770902930L;
       final byte[] COL_FAMILY = "a".getBytes();
       final byte[] COL_QUALIFIER = "co_created_5076".getBytes();

    //setting a key value pair to put in result
    List<KeyValue> kvs = new ArrayList<KeyValue>();
    kvs.add(new KeyValue("--350000000770902930".getBytes(),COL_FAMILY,    COL_QUALIFIER, Bytes
            .toBytes("ExpedtedBytes")));
   // I create an Instance of result
    Result result = new Result(kvs);

    // A mock tuple with a single dtmid
    Tuple tuple = mock(Tuple.class);
    bolt.table = mock(HTable.class);
    Result mcResult = mock(Result.class);
    when(tuple.size()).thenReturn(1);
    when(tuple.getLong(0)).thenReturn(dtmId);
    when(bolt.table.get(any(Get.class))).thenReturn(result);
    when(mcResult.getValue(any(byte[].class), any(byte[].class))).thenReturn(Bytes
            .toBytes("Bytes"));

    BasicOutputCollector collector = mock(BasicOutputCollector.class);

    // Execute the bolt.
    bolt.execute(tuple, collector);


    ArgumentCaptor<Values> valuesArg = ArgumentCaptor
            .forClass(Values.class);
    verify(collector).emit(valuesArg.capture());

    Values d = valuesArg.getValue();
    //casting this object in to a byteArray.
    byte [] i = (byte[])d.get(1);

    assertEquals(dtmId,d.get(0));







}

I am using this down here to return my bytes.For some reason is not working.

  when(mcResult.getValue(any(byte[].class), any(byte[].class))).thenReturn(Bytes
        .toBytes("myBytes"));

For some reason when I capture the values, I still get the bytes that I specified here:

  List<KeyValue> kvs = new ArrayList<KeyValue>();
    kvs.add(new KeyValue("--350000000770902930".getBytes(),COL_FAMILY,       COL_QUALIFIER, Bytes
            .toBytes("ExpedtedBytes")));
    Result result = new Result(kvs);

Aucun commentaire:

Enregistrer un commentaire