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 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
050      + "<NamespaceProperties><properties><entry><key>NAME</key><value>testNamespace"
051      + "</value></entry><entry><key>KEY_2</key><value>VALUE_2"
052      + "</value></entry><entry><key>KEY_1</key><value>VALUE_1</value></entry>"
053      + "</properties></NamespaceProperties>";
054
055    AS_PB = "ChUKBE5BTUUSDXRlc3ROYW1lc3BhY2UKEAoFS0VZXzESB1ZBTFVFXzEKEAoFS0VZXzISB1ZBTFVFXzI=";
056
057    AS_JSON = "{\"properties\":{\"NAME\":\"testNamespace\","
058      + "\"KEY_1\":\"VALUE_1\",\"KEY_2\":\"VALUE_2\"}}";
059  }
060
061  @Override
062  protected NamespacesInstanceModel buildTestModel() {
063    return buildTestModel(NAMESPACE_NAME, NAMESPACE_PROPERTIES);
064  }
065
066  public NamespacesInstanceModel buildTestModel(String namespace, Map<String, String> properties) {
067    NamespacesInstanceModel model = new NamespacesInstanceModel();
068    for (String key : properties.keySet()) {
069      model.addProperty(key, properties.get(key));
070    }
071    return model;
072  }
073
074  @Override
075  protected void checkModel(NamespacesInstanceModel model) {
076    checkModel(model, NAMESPACE_NAME, NAMESPACE_PROPERTIES);
077  }
078
079  public void checkModel(NamespacesInstanceModel model, String namespace,
080    Map<String, String> properties) {
081    Map<String, String> modProperties = model.getProperties();
082    assertEquals(properties.size(), modProperties.size());
083    // Namespace name comes from REST URI, not properties.
084    assertNotSame(namespace, model.getNamespaceName());
085    for (String property : properties.keySet()) {
086      assertEquals(properties.get(property), modProperties.get(property));
087    }
088  }
089
090  @Override
091  @Test
092  public void testBuildModel() throws Exception {
093    checkModel(buildTestModel());
094  }
095
096  @Override
097  @Test
098  public void testFromXML() throws Exception {
099    checkModel(fromXML(AS_XML));
100  }
101
102  @Override
103  @Test
104  public void testFromPB() throws Exception {
105    checkModel(fromPB(AS_PB));
106  }
107}