001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.util.Arrays;
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.testclassification.ClientTests;
023import org.apache.hadoop.hbase.testclassification.SmallTests;
024import org.apache.hadoop.hbase.util.Bytes;
025import org.junit.Assert;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030@Category({ClientTests.class, SmallTests.class})
031public class TestAttributes {
032
033  @ClassRule
034  public static final HBaseClassTestRule CLASS_RULE =
035      HBaseClassTestRule.forClass(TestAttributes.class);
036
037  private static final byte [] ROW = new byte [] {'r'};
038  @Test
039  public void testPutAttributes() {
040    Put put = new Put(ROW);
041    Assert.assertTrue(put.getAttributesMap().isEmpty());
042    Assert.assertNull(put.getAttribute("absent"));
043
044    put.setAttribute("absent", null);
045    Assert.assertTrue(put.getAttributesMap().isEmpty());
046    Assert.assertNull(put.getAttribute("absent"));
047
048    // adding attribute
049    put.setAttribute("attribute1", Bytes.toBytes("value1"));
050    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), put.getAttribute("attribute1")));
051    Assert.assertEquals(1, put.getAttributesMap().size());
052    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), put.getAttributesMap().get("attribute1")));
053
054    // overriding attribute value
055    put.setAttribute("attribute1", Bytes.toBytes("value12"));
056    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), put.getAttribute("attribute1")));
057    Assert.assertEquals(1, put.getAttributesMap().size());
058    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), put.getAttributesMap().get("attribute1")));
059
060    // adding another attribute
061    put.setAttribute("attribute2", Bytes.toBytes("value2"));
062    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), put.getAttribute("attribute2")));
063    Assert.assertEquals(2, put.getAttributesMap().size());
064    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), put.getAttributesMap().get("attribute2")));
065
066    // removing attribute
067    put.setAttribute("attribute2", null);
068    Assert.assertNull(put.getAttribute("attribute2"));
069    Assert.assertEquals(1, put.getAttributesMap().size());
070    Assert.assertNull(put.getAttributesMap().get("attribute2"));
071
072    // removing non-existed attribute
073    put.setAttribute("attribute2", null);
074    Assert.assertNull(put.getAttribute("attribute2"));
075    Assert.assertEquals(1, put.getAttributesMap().size());
076    Assert.assertNull(put.getAttributesMap().get("attribute2"));
077
078    // removing another attribute
079    put.setAttribute("attribute1", null);
080    Assert.assertNull(put.getAttribute("attribute1"));
081    Assert.assertTrue(put.getAttributesMap().isEmpty());
082    Assert.assertNull(put.getAttributesMap().get("attribute1"));
083  }
084
085
086  @Test
087  public void testDeleteAttributes() {
088    Delete del = new Delete(new byte [] {'r'});
089    Assert.assertTrue(del.getAttributesMap().isEmpty());
090    Assert.assertNull(del.getAttribute("absent"));
091
092    del.setAttribute("absent", null);
093    Assert.assertTrue(del.getAttributesMap().isEmpty());
094    Assert.assertNull(del.getAttribute("absent"));
095
096    // adding attribute
097    del.setAttribute("attribute1", Bytes.toBytes("value1"));
098    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), del.getAttribute("attribute1")));
099    Assert.assertEquals(1, del.getAttributesMap().size());
100    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value1"), del.getAttributesMap().get("attribute1")));
101
102    // overriding attribute value
103    del.setAttribute("attribute1", Bytes.toBytes("value12"));
104    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), del.getAttribute("attribute1")));
105    Assert.assertEquals(1, del.getAttributesMap().size());
106    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value12"), del.getAttributesMap().get("attribute1")));
107
108    // adding another attribute
109    del.setAttribute("attribute2", Bytes.toBytes("value2"));
110    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), del.getAttribute("attribute2")));
111    Assert.assertEquals(2, del.getAttributesMap().size());
112    Assert.assertTrue(Arrays.equals(Bytes.toBytes("value2"), del.getAttributesMap().get("attribute2")));
113
114    // removing attribute
115    del.setAttribute("attribute2", null);
116    Assert.assertNull(del.getAttribute("attribute2"));
117    Assert.assertEquals(1, del.getAttributesMap().size());
118    Assert.assertNull(del.getAttributesMap().get("attribute2"));
119
120    // removing non-existed attribute
121    del.setAttribute("attribute2", null);
122    Assert.assertNull(del.getAttribute("attribute2"));
123    Assert.assertEquals(1, del.getAttributesMap().size());
124    Assert.assertNull(del.getAttributesMap().get("attribute2"));
125
126    // removing another attribute
127    del.setAttribute("attribute1", null);
128    Assert.assertNull(del.getAttribute("attribute1"));
129    Assert.assertTrue(del.getAttributesMap().isEmpty());
130    Assert.assertNull(del.getAttributesMap().get("attribute1"));
131  }
132
133  @Test
134  public void testGetId() {
135    Get get = new Get(ROW);
136    Assert.assertNull("Make sure id is null if unset", get.toMap().get("id"));
137    get.setId("myId");
138    Assert.assertEquals("myId", get.toMap().get("id"));
139  }
140
141  @Test
142  public void testAppendId() {
143    Append append = new Append(ROW);
144    Assert.assertNull("Make sure id is null if unset", append.toMap().get("id"));
145    append.setId("myId");
146    Assert.assertEquals("myId", append.toMap().get("id"));
147  }
148
149  @Test
150  public void testDeleteId() {
151    Delete delete = new Delete(ROW);
152    Assert.assertNull("Make sure id is null if unset", delete.toMap().get("id"));
153    delete.setId("myId");
154    Assert.assertEquals("myId", delete.toMap().get("id"));
155  }
156
157  @Test
158  public void testPutId() {
159    Put put = new Put(ROW);
160    Assert.assertNull("Make sure id is null if unset", put.toMap().get("id"));
161    put.setId("myId");
162    Assert.assertEquals("myId", put.toMap().get("id"));
163  }
164
165  @Test
166  public void testScanId() {
167    Scan scan = new Scan();
168    Assert.assertNull("Make sure id is null if unset", scan.toMap().get("id"));
169    scan.setId("myId");
170    Assert.assertEquals("myId", scan.toMap().get("id"));
171  }
172}