1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.hbase.rest.model;
20 
21 import junit.framework.TestCase;
22 import org.apache.hadoop.hbase.testclassification.SmallTests;
23 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
24 import org.apache.hadoop.hbase.rest.provider.JAXBContextResolver;
25 import org.apache.hadoop.hbase.util.Base64;
26 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
27 import org.codehaus.jackson.map.ObjectMapper;
28 import org.codehaus.jackson.node.ObjectNode;
29 import org.junit.experimental.categories.Category;
30 
31 import javax.ws.rs.core.MediaType;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.JAXBException;
34 import java.io.IOException;
35 import java.io.StringReader;
36 import java.io.StringWriter;
37 
38 @Category(SmallTests.class)
39 public abstract class TestModelBase<T> extends TestCase {
40 
41   protected String AS_XML;
42 
43   protected String AS_PB;
44 
45   protected String AS_JSON;
46 
47   protected JAXBContext context;
48 
49   protected Class<?> clazz;
50 
51   protected ObjectMapper mapper;
52 
TestModelBase(Class<?> clazz)53   protected TestModelBase(Class<?> clazz) throws Exception {
54     super();
55     this.clazz = clazz;
56     context = new JAXBContextResolver().getContext(clazz);
57     mapper = new JacksonJaxbJsonProvider().locateMapper(clazz,
58         MediaType.APPLICATION_JSON_TYPE);
59   }
60 
buildTestModel()61   protected abstract T buildTestModel();
62 
63   @SuppressWarnings("unused")
toXML(T model)64   protected String toXML(T model) throws JAXBException {
65     StringWriter writer = new StringWriter();
66     context.createMarshaller().marshal(model, writer);
67     return writer.toString();
68   }
69 
toJSON(T model)70   protected String toJSON(T model) throws JAXBException, IOException {
71     StringWriter writer = new StringWriter();
72     mapper.writeValue(writer, model);
73 //  original marshaller, uncomment this and comment mapper to verify backward compatibility
74 //  ((JSONJAXBContext)context).createJSONMarshaller().marshallToJSON(model, writer);
75     return writer.toString();
76   }
77 
fromJSON(String json)78   public T fromJSON(String json) throws JAXBException, IOException {
79     return (T)
80       mapper.readValue(json, clazz);
81   }
82 
fromXML(String xml)83   public T fromXML(String xml) throws JAXBException {
84     return (T)
85       context.createUnmarshaller().unmarshal(new StringReader(xml));
86   }
87 
88   @SuppressWarnings("unused")
toPB(ProtobufMessageHandler model)89   protected byte[] toPB(ProtobufMessageHandler model) {
90     return model.createProtobufOutput();
91   }
92 
fromPB(String pb)93   protected T fromPB(String pb) throws
94       Exception {
95     return (T)clazz.getMethod("getObjectFromMessage", byte[].class).invoke(
96         clazz.newInstance(),
97         Base64.decode(AS_PB));
98   }
99 
checkModel(T model)100   protected abstract  void checkModel(T model);
101 
testBuildModel()102   public void testBuildModel() throws Exception {
103     checkModel(buildTestModel());
104   }
105 
testFromPB()106   public void testFromPB() throws Exception {
107     checkModel(fromPB(AS_PB));
108   }
109 
testFromXML()110   public void testFromXML() throws Exception {
111     checkModel(fromXML(AS_XML));
112   }
113 
testToXML()114   public void testToXML() throws Exception {
115     // Uses fromXML to check model because XML element ordering can be random.
116     checkModel(fromXML(toXML(buildTestModel())));
117   }
118 
testToJSON()119   public void testToJSON() throws Exception {
120     try {
121       ObjectNode expObj = mapper.readValue(AS_JSON, ObjectNode.class);
122       ObjectNode actObj = mapper.readValue(toJSON(buildTestModel()), ObjectNode.class);
123       assertEquals(expObj, actObj);
124     } catch(Exception e) {
125       assertEquals(AS_JSON, toJSON(buildTestModel()));
126     }
127   }
128 
testFromJSON()129   public void testFromJSON() throws Exception {
130     checkModel(fromJSON(AS_JSON));
131   }
132 }
133 
134