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.Ignore;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(SmallTests.class)
31  public class TestNamespacesInstanceModel extends TestModelBase<NamespacesInstanceModel> {
32  
33    public static final Map<String,String> NAMESPACE_PROPERTIES = new HashMap<String, String>();
34    public static final String NAMESPACE_NAME = "namespaceName";
35  
36    public TestNamespacesInstanceModel() throws Exception {
37      super(NamespacesInstanceModel.class);
38  
39      NAMESPACE_PROPERTIES.put("KEY_1","VALUE_1");
40      NAMESPACE_PROPERTIES.put("KEY_2","VALUE_2");
41      NAMESPACE_PROPERTIES.put("NAME","testNamespace");
42  
43      AS_XML =
44        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
45        "<NamespaceProperties><properties><entry><key>NAME</key><value>testNamespace" +
46        "</value></entry><entry><key>KEY_2</key><value>VALUE_2" +
47        "</value></entry><entry><key>KEY_1</key><value>VALUE_1</value></entry>" +
48        "</properties></NamespaceProperties>";
49  
50      AS_PB = "ChUKBE5BTUUSDXRlc3ROYW1lc3BhY2UKEAoFS0VZXzESB1ZBTFVFXzEKEAoFS0VZXzISB1ZBTFVFXzI=";
51  
52      AS_JSON = "{\"properties\":{\"NAME\":\"testNamespace\"," +
53        "\"KEY_1\":\"VALUE_1\",\"KEY_2\":\"VALUE_2\"}}";
54    }
55  
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    protected void checkModel(NamespacesInstanceModel model) {
69      checkModel(model, NAMESPACE_NAME, NAMESPACE_PROPERTIES);
70    }
71  
72    public void checkModel(NamespacesInstanceModel model, String namespace,
73        Map<String,String> properties) {
74      Map<String,String> modProperties = model.getProperties();
75      assertEquals(properties.size(), modProperties.size());
76      // Namespace name comes from REST URI, not properties.
77      assertNotSame(namespace, model.getNamespaceName());
78      for(String property: properties.keySet()){
79        assertEquals(properties.get(property), modProperties.get(property));
80      }
81    }
82  
83    @Test
84    public void testBuildModel() throws Exception {
85      checkModel(buildTestModel());
86    }
87  
88    @Test
89    public void testFromXML() throws Exception {
90      checkModel(fromXML(AS_XML));
91    }
92  
93    @Test
94    public void testFromPB() throws Exception {
95      checkModel(fromPB(AS_PB));
96    }
97  }