1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.rest; |
35 |
|
|
36 |
|
import static org.hamcrest.core.StringContains.containsString; |
37 |
|
import static org.junit.Assert.*; |
38 |
|
|
39 |
|
import java.io.ByteArrayOutputStream; |
40 |
|
import java.io.StringReader; |
41 |
|
import java.io.StringWriter; |
42 |
|
|
43 |
|
import javax.xml.bind.JAXBContext; |
44 |
|
import javax.xml.bind.Marshaller; |
45 |
|
import javax.xml.bind.Unmarshaller; |
46 |
|
|
47 |
|
import org.junit.Before; |
48 |
|
import org.junit.Test; |
49 |
|
|
50 |
|
import com.fasterxml.jackson.databind.ObjectMapper; |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (36) |
Complexity: 5 |
Complexity Density: 0.16 |
|
52 |
|
public class RestErrorTest { |
53 |
|
|
54 |
|
private Marshaller marshaller; |
55 |
|
private Unmarshaller unmarshaller; |
56 |
|
private ObjectMapper mapper; |
57 |
|
private ByteArrayOutputStream baos; |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
59 |
4 |
@Before... |
60 |
|
public void setUp() throws Exception { |
61 |
4 |
JAXBContext context = JAXBContext.newInstance(RestError.class); |
62 |
4 |
marshaller = context.createMarshaller(); |
63 |
4 |
unmarshaller = context.createUnmarshaller(); |
64 |
4 |
mapper = new ObjectMapper(); |
65 |
4 |
baos = new ByteArrayOutputStream(); |
66 |
|
} |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
68 |
1 |
@Test... |
69 |
|
public void marshallObjectIntoXml() throws Exception { |
70 |
|
|
71 |
1 |
String errorCode = "thisIsCode"; |
72 |
1 |
String errorMessage = "thisIsMessage"; |
73 |
1 |
RestError error = new RestError(errorCode, errorMessage); |
74 |
1 |
StringWriter writer = new StringWriter(); |
75 |
|
|
76 |
|
|
77 |
1 |
marshaller.marshal(error, writer); |
78 |
1 |
String errorXml = writer.toString(); |
79 |
|
|
80 |
|
|
81 |
1 |
assertThat(errorXml, containsString(errorCode)); |
82 |
1 |
assertThat(errorXml, containsString(errorMessage)); |
83 |
|
} |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
85 |
1 |
@Test... |
86 |
|
public void unmarshallXmlIntoObject() throws Exception { |
87 |
|
|
88 |
1 |
String errorCode = "thisIsCode"; |
89 |
1 |
String errorMessage = "thisIsMessage"; |
90 |
1 |
String errorXml = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><restError><error><code>%s</code><message>%s</message></error></restError>", errorCode, errorMessage); |
91 |
|
|
92 |
|
|
93 |
1 |
RestError restError = (RestError) unmarshaller.unmarshal(new StringReader(errorXml)); |
94 |
|
|
95 |
|
|
96 |
1 |
assertEquals(errorCode, restError.getErrorContainer().getCode()); |
97 |
1 |
assertEquals(errorMessage, restError.getErrorContainer().getMessage()); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
100 |
1 |
@Test... |
101 |
|
public void writeObjectIntoJson() throws Exception { |
102 |
|
|
103 |
1 |
String errorCode = "thisIsCode"; |
104 |
1 |
String errorMessage = "thisIsMessage"; |
105 |
1 |
RestError restError = new RestError(errorCode, errorMessage); |
106 |
|
|
107 |
|
|
108 |
1 |
String errorJson = mapper.writeValueAsString(restError); |
109 |
|
|
110 |
|
|
111 |
1 |
assertThat(errorJson, containsString(errorCode)); |
112 |
1 |
assertThat(errorJson, containsString(errorMessage)); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
115 |
1 |
@Test... |
116 |
|
public void readJsonIntoObject() throws Exception { |
117 |
|
|
118 |
1 |
String errorCode = "thisIsCode"; |
119 |
1 |
String errorMessage = "thisIsMessage"; |
120 |
1 |
String errorJson = String.format("{\"error\":{\"code\":\"%s\",\"message\":\"%s\"}}", errorCode, errorMessage); |
121 |
|
|
122 |
|
|
123 |
1 |
RestError restError = mapper.readValue(errorJson, RestError.class); |
124 |
|
|
125 |
|
|
126 |
1 |
assertEquals(errorCode, restError.getErrorContainer().getCode()); |
127 |
1 |
assertEquals(errorMessage, restError.getErrorContainer().getMessage()); |
128 |
|
} |
129 |
|
} |