1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest.model;
22
23 import junit.framework.TestCase;
24 import org.apache.hadoop.hbase.SmallTests;
25 import org.apache.hadoop.hbase.rest.ProtobufMessageHandler;
26 import org.apache.hadoop.hbase.rest.provider.JAXBContextResolver;
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
76
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