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.rest.model;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotSame;
022
023import java.util.HashMap;
024import java.util.Map;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.testclassification.RestTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({RestTests.class, SmallTests.class})
033public class TestNamespacesInstanceModel extends TestModelBase<NamespacesInstanceModel> {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037      HBaseClassTestRule.forClass(TestNamespacesInstanceModel.class);
038
039  public static final Map<String,String> NAMESPACE_PROPERTIES = new HashMap<>();
040  public static final String NAMESPACE_NAME = "namespaceName";
041
042  public TestNamespacesInstanceModel() throws Exception {
043    super(NamespacesInstanceModel.class);
044
045    NAMESPACE_PROPERTIES.put("KEY_1","VALUE_1");
046    NAMESPACE_PROPERTIES.put("KEY_2","VALUE_2");
047    NAMESPACE_PROPERTIES.put("NAME","testNamespace");
048
049    AS_XML =
050      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
051      "<NamespaceProperties><properties><entry><key>NAME</key><value>testNamespace" +
052      "</value></entry><entry><key>KEY_2</key><value>VALUE_2" +
053      "</value></entry><entry><key>KEY_1</key><value>VALUE_1</value></entry>" +
054      "</properties></NamespaceProperties>";
055
056    AS_PB = "ChUKBE5BTUUSDXRlc3ROYW1lc3BhY2UKEAoFS0VZXzESB1ZBTFVFXzEKEAoFS0VZXzISB1ZBTFVFXzI=";
057
058    AS_JSON = "{\"properties\":{\"NAME\":\"testNamespace\"," +
059      "\"KEY_1\":\"VALUE_1\",\"KEY_2\":\"VALUE_2\"}}";
060  }
061
062  @Override
063  protected NamespacesInstanceModel buildTestModel() {
064    return buildTestModel(NAMESPACE_NAME, NAMESPACE_PROPERTIES);
065  }
066
067  public NamespacesInstanceModel buildTestModel(String namespace, Map<String,String> properties) {
068    NamespacesInstanceModel model = new NamespacesInstanceModel();
069    for(String key: properties.keySet()){
070      model.addProperty(key, properties.get(key));
071    }
072    return model;
073  }
074
075  @Override
076  protected void checkModel(NamespacesInstanceModel model) {
077    checkModel(model, NAMESPACE_NAME, NAMESPACE_PROPERTIES);
078  }
079
080  public void checkModel(NamespacesInstanceModel model, String namespace,
081      Map<String,String> properties) {
082    Map<String,String> modProperties = model.getProperties();
083    assertEquals(properties.size(), modProperties.size());
084    // Namespace name comes from REST URI, not properties.
085    assertNotSame(namespace, model.getNamespaceName());
086    for(String property: properties.keySet()){
087      assertEquals(properties.get(property), modProperties.get(property));
088    }
089  }
090
091  @Override
092  @Test
093  public void testBuildModel() throws Exception {
094    checkModel(buildTestModel());
095  }
096
097  @Override
098  @Test
099  public void testFromXML() throws Exception {
100    checkModel(fromXML(AS_XML));
101  }
102
103  @Override
104  @Test
105  public void testFromPB() throws Exception {
106    checkModel(fromPB(AS_PB));
107  }
108}