1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
61 protected abstract T buildTestModel();
62
63 @SuppressWarnings("unused")
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
70 protected String toJSON(T model) throws JAXBException, IOException {
71 StringWriter writer = new StringWriter();
72 mapper.writeValue(writer, model);
73
74
75 return writer.toString();
76 }
77
78 public T fromJSON(String json) throws JAXBException, IOException {
79 return (T)
80 mapper.readValue(json, clazz);
81 }
82
83 public T fromXML(String xml) throws JAXBException {
84 return (T)
85 context.createUnmarshaller().unmarshal(new StringReader(xml));
86 }
87
88 @SuppressWarnings("unused")
89 protected byte[] toPB(ProtobufMessageHandler model) {
90 return model.createProtobufOutput();
91 }
92
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
100 protected abstract void checkModel(T model);
101
102 public void testBuildModel() throws Exception {
103 checkModel(buildTestModel());
104 }
105
106 public void testFromPB() throws Exception {
107 checkModel(fromPB(AS_PB));
108 }
109
110 public void testFromXML() throws Exception {
111 checkModel(fromXML(AS_XML));
112 }
113
114 public void testToXML() throws Exception {
115
116 checkModel(fromXML(toXML(buildTestModel())));
117 }
118
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
129 public void testFromJSON() throws Exception {
130 checkModel(fromJSON(AS_JSON));
131 }
132 }
133