View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.quotas;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  
23  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
25  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetQuotaRequest;
26  import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.SpaceLimitRequest;
27  import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.SpaceQuota;
28  import org.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  /**
33   * Test class for {@link SpaceLimitSettings}.
34   */
35  @Category({SmallTests.class})
36  public class TestSpaceLimitSettings {
37  
38    @Test(expected = IllegalArgumentException.class)
39    public void testInvalidTableQuotaSizeLimit() {
40      new SpaceLimitSettings(TableName.valueOf("foo"), -1, SpaceViolationPolicy.NO_INSERTS);
41    }
42  
43    @Test(expected = NullPointerException.class)
44    public void testNullTableName() {
45      TableName tn = null;
46      new SpaceLimitSettings(tn, 1, SpaceViolationPolicy.NO_INSERTS);
47    }
48  
49    @Test(expected = NullPointerException.class)
50    public void testNullTableViolationPolicy() {
51      new SpaceLimitSettings(TableName.valueOf("foo"), 1, null);
52    }
53  
54    @Test(expected = IllegalArgumentException.class)
55    public void testInvalidNamespaceQuotaSizeLimit() {
56      new SpaceLimitSettings("foo_ns", -1, SpaceViolationPolicy.NO_INSERTS);
57    }
58  
59    @Test(expected = NullPointerException.class)
60    public void testNullNamespace() {
61      String ns = null;
62      new SpaceLimitSettings(ns, 1, SpaceViolationPolicy.NO_INSERTS);
63    }
64  
65    @Test(expected = NullPointerException.class)
66    public void testNullNamespaceViolationPolicy() {
67      new SpaceLimitSettings("foo_ns", 1, null);
68    }
69  
70    @Test
71    public void testTableQuota() {
72      final TableName tableName = TableName.valueOf("foo");
73      final long sizeLimit = 1024 * 1024;
74      final SpaceViolationPolicy policy = SpaceViolationPolicy.NO_WRITES;
75      SpaceLimitSettings settings = new SpaceLimitSettings(tableName, sizeLimit, policy);
76      SetQuotaRequest proto = QuotaSettings.buildSetQuotaRequestProto(settings);
77  
78      assertFalse("User should be missing", proto.hasUserName());
79      assertFalse("Namespace should be missing", proto.hasNamespace());
80      assertEquals(ProtobufUtil.toProtoTableName(tableName), proto.getTableName());
81      SpaceLimitRequest spaceLimitReq = proto.getSpaceLimit();
82      assertNotNull("SpaceLimitRequest was null", spaceLimitReq);
83      SpaceQuota spaceQuota = spaceLimitReq.getQuota();
84      assertNotNull("SpaceQuota was null", spaceQuota);
85      assertEquals(sizeLimit, spaceQuota.getSoftLimit());
86      assertEquals(ProtobufUtil.toProtoViolationPolicy(policy), spaceQuota.getViolationPolicy());
87  
88      assertEquals(QuotaType.SPACE, settings.getQuotaType());
89  
90      SpaceLimitSettings copy = new SpaceLimitSettings(tableName, sizeLimit, policy);
91      assertEquals(settings, copy);
92      assertEquals(settings.hashCode(), copy.hashCode());
93    }
94  
95    @Test
96    public void testNamespaceQuota() {
97      final String namespace = "foo_ns";
98      final long sizeLimit = 1024 * 1024;
99      final SpaceViolationPolicy policy = SpaceViolationPolicy.NO_WRITES;
100     SpaceLimitSettings settings = new SpaceLimitSettings(namespace, sizeLimit, policy);
101     SetQuotaRequest proto = QuotaSettings.buildSetQuotaRequestProto(settings);
102 
103     assertFalse("User should be missing", proto.hasUserName());
104     assertFalse("TableName should be missing", proto.hasTableName());
105     assertEquals(namespace, proto.getNamespace());
106     SpaceLimitRequest spaceLimitReq = proto.getSpaceLimit();
107     assertNotNull("SpaceLimitRequest was null", spaceLimitReq);
108     SpaceQuota spaceQuota = spaceLimitReq.getQuota();
109     assertNotNull("SpaceQuota was null", spaceQuota);
110     assertEquals(sizeLimit, spaceQuota.getSoftLimit());
111     assertEquals(ProtobufUtil.toProtoViolationPolicy(policy), spaceQuota.getViolationPolicy());
112 
113     assertEquals(QuotaType.SPACE, settings.getQuotaType());
114 
115     SpaceLimitSettings copy = new SpaceLimitSettings(namespace, sizeLimit, policy);
116     assertEquals(settings, copy);
117     assertEquals(settings.hashCode(), copy.hashCode());
118   }
119 }