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