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 com.sun.jersey.api.json.JSONJAXBContext;
24  import junit.framework.TestCase;
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
27  import org.apache.hadoop.hbase.rest.provider.JAXBContextResolver;
28  import org.apache.hadoop.hbase.util.Base64;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.codehaus.jackson.JsonGenerationException;
31  import org.codehaus.jackson.JsonParseException;
32  import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
33  import org.codehaus.jackson.map.AnnotationIntrospector;
34  import org.codehaus.jackson.map.ObjectMapper;
35  import org.codehaus.jackson.map.SerializationConfig;
36  import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
37  import org.codehaus.jackson.node.ObjectNode;
38  import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
39  import org.junit.experimental.categories.Category;
40  
41  import javax.ws.rs.core.MediaType;
42  import javax.xml.bind.JAXBContext;
43  import javax.xml.bind.JAXBException;
44  import java.io.IOException;
45  import java.io.StringReader;
46  import java.io.StringWriter;
47  import java.lang.reflect.InvocationTargetException;
48  
49  @Category(SmallTests.class)
50  public abstract class TestModelBase<T> extends TestCase {
51  
52    protected String AS_XML;
53  
54    protected String AS_PB;
55  
56    protected String AS_JSON;
57  
58    protected JAXBContext context;
59  
60    protected Class<?> clazz;
61  
62    protected ObjectMapper mapper;
63  
64    protected TestModelBase(Class<?> clazz) throws Exception {
65      super();
66      this.clazz = clazz;
67      context = new JAXBContextResolver().getContext(clazz);
68      mapper = new JacksonJaxbJsonProvider().locateMapper(clazz,
69          MediaType.APPLICATION_JSON_TYPE);
70    }
71  
72    protected abstract T buildTestModel();
73  
74    @SuppressWarnings("unused")
75    protected String toXML(T model) throws JAXBException {
76      StringWriter writer = new StringWriter();
77      context.createMarshaller().marshal(model, writer);
78      return writer.toString();
79    }
80  
81    protected String toJSON(T model) throws JAXBException, IOException {
82      StringWriter writer = new StringWriter();
83      mapper.writeValue(writer, model);
84  //  original marshaller, uncomment this and comment mapper to verify backward compatibility
85  //  ((JSONJAXBContext)context).createJSONMarshaller().marshallToJSON(model, writer);
86      return writer.toString();
87    }
88  
89    public T fromJSON(String json) throws JAXBException, IOException {
90      return (T)
91        mapper.readValue(json, clazz);
92    }
93  
94    public T fromXML(String xml) throws JAXBException {
95      return (T)
96        context.createUnmarshaller().unmarshal(new StringReader(xml));
97    }
98  
99    @SuppressWarnings("unused")
100   protected byte[] toPB(ProtobufMessageHandler model) {
101     return model.createProtobufOutput();
102   }
103 
104   protected T fromPB(String pb) throws
105       Exception {
106     return (T)clazz.getMethod("getObjectFromMessage", byte[].class).invoke(
107         clazz.newInstance(),
108         Base64.decode(AS_PB));
109   }
110 
111   protected abstract  void checkModel(T model);
112 
113   public void testBuildModel() throws Exception {
114     checkModel(buildTestModel());
115   }
116 
117   public void testFromPB() throws Exception {
118     checkModel(fromPB(AS_PB));
119   }
120 
121   public void testFromXML() throws Exception {
122     checkModel(fromXML(AS_XML));
123   }
124 
125   public void testToXML() throws Exception {
126     assertEquals(AS_XML, toXML(buildTestModel()));
127   }
128 
129   public void testToJSON() throws Exception {
130     try {
131       ObjectNode expObj = mapper.readValue(AS_JSON, ObjectNode.class);
132       ObjectNode actObj = mapper.readValue(toJSON(buildTestModel()), ObjectNode.class);
133       assertEquals(expObj, actObj);
134     } catch(Exception e) {
135       assertEquals(AS_JSON, toJSON(buildTestModel()));
136     }
137   }
138 
139   public void testFromJSON() throws Exception {
140     checkModel(fromJSON(AS_JSON));
141   }
142 }
143