View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.junit.Test;
27  import org.junit.experimental.categories.Category;
28  
29  @Category(SmallTests.class)
30  public class TestNamespacesInstanceModel extends TestModelBase<NamespacesInstanceModel> {
31  
32    public static final Map<String,String> NAMESPACE_PROPERTIES = new HashMap<String, String>();
33    public static final String NAMESPACE_NAME = "namespaceName";
34  
35    public TestNamespacesInstanceModel() throws Exception {
36      super(NamespacesInstanceModel.class);
37  
38      NAMESPACE_PROPERTIES.put("KEY_1","VALUE_1");
39      NAMESPACE_PROPERTIES.put("KEY_2","VALUE_2");
40      NAMESPACE_PROPERTIES.put("NAME","testNamespace");
41  
42      AS_XML =
43        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
44        "<NamespaceProperties><properties><entry><key>NAME</key><value>testNamespace" +
45        "</value></entry><entry><key>KEY_2</key><value>VALUE_2" +
46        "</value></entry><entry><key>KEY_1</key><value>VALUE_1</value></entry>" +
47        "</properties></NamespaceProperties>";
48  
49      AS_PB = "ChUKBE5BTUUSDXRlc3ROYW1lc3BhY2UKEAoFS0VZXzESB1ZBTFVFXzEKEAoFS0VZXzISB1ZBTFVFXzI=";
50  
51      AS_JSON = "{\"properties\":{\"NAME\":\"testNamespace\"," +
52        "\"KEY_1\":\"VALUE_1\",\"KEY_2\":\"VALUE_2\"}}";
53    }
54  
55    @Override
56    protected NamespacesInstanceModel buildTestModel() {
57      return buildTestModel(NAMESPACE_NAME, NAMESPACE_PROPERTIES);
58    }
59  
60    public NamespacesInstanceModel buildTestModel(String namespace, Map<String,String> properties) {
61      NamespacesInstanceModel model = new NamespacesInstanceModel();
62      for(String key: properties.keySet()){
63        model.addProperty(key, properties.get(key));
64      }
65      return model;
66    }
67  
68    @Override
69    protected void checkModel(NamespacesInstanceModel model) {
70      checkModel(model, NAMESPACE_NAME, NAMESPACE_PROPERTIES);
71    }
72  
73    public void checkModel(NamespacesInstanceModel model, String namespace,
74        Map<String,String> properties) {
75      Map<String,String> modProperties = model.getProperties();
76      assertEquals(properties.size(), modProperties.size());
77      // Namespace name comes from REST URI, not properties.
78      assertNotSame(namespace, model.getNamespaceName());
79      for(String property: properties.keySet()){
80        assertEquals(properties.get(property), modProperties.get(property));
81      }
82    }
83  
84    @Override
85    @Test
86    public void testBuildModel() throws Exception {
87      checkModel(buildTestModel());
88    }
89  
90    @Test
91    public void testFromXML() throws Exception {
92      checkModel(fromXML(AS_XML));
93    }
94  
95    @Override
96    @Test
97    public void testFromPB() throws Exception {
98      checkModel(fromPB(AS_PB));
99    }
100 }
101