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    @Override
57    protected NamespacesInstanceModel buildTestModel() {
58      return buildTestModel(NAMESPACE_NAME, NAMESPACE_PROPERTIES);
59    }
60  
61    public NamespacesInstanceModel buildTestModel(String namespace, Map<String,String> properties) {
62      NamespacesInstanceModel model = new NamespacesInstanceModel();
63      for(String key: properties.keySet()){
64        model.addProperty(key, properties.get(key));
65      }
66      return model;
67    }
68  
69    @Override
70    protected void checkModel(NamespacesInstanceModel model) {
71      checkModel(model, NAMESPACE_NAME, NAMESPACE_PROPERTIES);
72    }
73  
74    public void checkModel(NamespacesInstanceModel model, String namespace,
75        Map<String,String> properties) {
76      Map<String,String> modProperties = model.getProperties();
77      assertEquals(properties.size(), modProperties.size());
78      // Namespace name comes from REST URI, not properties.
79      assertNotSame(namespace, model.getNamespaceName());
80      for(String property: properties.keySet()){
81        assertEquals(properties.get(property), modProperties.get(property));
82      }
83    }
84  
85    @Test
86    @Override
87    public void testBuildModel() throws Exception {
88      checkModel(buildTestModel());
89    }
90  
91    @Test
92    @Override
93    public void testFromXML() throws Exception {
94      checkModel(fromXML(AS_XML));
95    }
96  
97    @Test
98    @Override
99    public void testFromPB() throws Exception {
100     checkModel(fromPB(AS_PB));
101   }
102 }