View Javadoc

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