|
17 | 17 | package com.google.cloud.storage; |
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat; |
20 | | -import static org.mockito.ArgumentMatchers.any; |
21 | | -import static org.mockito.Mockito.when; |
22 | 20 |
|
23 | 21 | import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 22 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 23 | +import com.google.cloud.storage.multipartupload.model.ListPartsResponse; |
| 24 | +import com.google.common.base.MoreObjects; |
24 | 25 | import java.io.ByteArrayInputStream; |
25 | 26 | import java.io.IOException; |
26 | 27 | import java.io.InputStream; |
27 | | -import java.io.Reader; |
| 28 | +import java.io.StringReader; |
28 | 29 | import java.nio.charset.StandardCharsets; |
29 | | -import org.junit.After; |
| 30 | +import java.util.Objects; |
30 | 31 | import org.junit.Before; |
31 | 32 | import org.junit.Test; |
32 | | -import org.mockito.Mock; |
33 | | -import org.mockito.MockitoAnnotations; |
34 | 33 |
|
35 | 34 | public class XmlObjectParserTest { |
36 | 35 |
|
37 | | - @Mock private XmlMapper xmlMapper; |
38 | | - |
39 | | - private AutoCloseable mocks; |
40 | 36 | private XmlObjectParser xmlObjectParser; |
41 | 37 |
|
42 | 38 | @Before |
43 | 39 | public void setUp() { |
44 | | - mocks = MockitoAnnotations.openMocks(this); |
45 | | - xmlObjectParser = new XmlObjectParser(xmlMapper); |
| 40 | + xmlObjectParser = new XmlObjectParser(new XmlMapper()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testParseStringValueEnum() throws IOException { |
| 45 | + // language=xml |
| 46 | + String xml = |
| 47 | + "<TestXmlObject2>\n" + " <storageClass>STANDARD</storageClass>" + "</TestXmlObject2>"; |
| 48 | + InputStream in = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); |
| 49 | + TestXmlObject2 expected = new TestXmlObject2(StorageClass.STANDARD); |
| 50 | + TestXmlObject2 actual = |
| 51 | + xmlObjectParser.parseAndClose(in, StandardCharsets.UTF_8, TestXmlObject2.class); |
| 52 | + assertThat(actual).isEqualTo(expected); |
46 | 53 | } |
47 | 54 |
|
48 | | - @After |
49 | | - public void tearDown() throws Exception { |
50 | | - mocks.close(); |
| 55 | + @Test |
| 56 | + public void testNestedParseStringValueEnum_undefined() throws IOException { |
| 57 | + // language=xml |
| 58 | + String xml = |
| 59 | + "<ListPartsResponse>\n" |
| 60 | + + " <truncated>false</truncated>\n" |
| 61 | + + " <Bucket>bucket</Bucket>\n" |
| 62 | + + " <Key>key</Key>\n" |
| 63 | + + " <UploadId/>\n" |
| 64 | + + " <PartNumberMarker>0</PartNumberMarker>\n" |
| 65 | + + " <NextPartNumberMarker>0</NextPartNumberMarker>\n" |
| 66 | + + " <MaxParts>0</MaxParts>\n" |
| 67 | + + " <IsTruncated>false</IsTruncated>\n" |
| 68 | + + " <Owner/>\n" |
| 69 | + + " <Part>\n" |
| 70 | + + " <PartNumber>1</PartNumber>\n" |
| 71 | + + " <ETag>etag</ETag>\n" |
| 72 | + + " <Size>33</Size>\n" |
| 73 | + + " <LastModified/>\n" |
| 74 | + + " </Part>\n" |
| 75 | + + "</ListPartsResponse>"; |
| 76 | + System.out.println(xml); |
| 77 | + ListPartsResponse listPartsResponse = |
| 78 | + xmlObjectParser.parseAndClose(new StringReader(xml), ListPartsResponse.class); |
| 79 | + assertThat(listPartsResponse.getStorageClass()).isNull(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testNestedParseStringValueEnum_null() throws IOException { |
| 84 | + // language=xml |
| 85 | + String xml = |
| 86 | + "<ListPartsResponse>\n" |
| 87 | + + " <truncated>false</truncated>\n" |
| 88 | + + " <Bucket>bucket</Bucket>\n" |
| 89 | + + " <Key>key</Key>\n" |
| 90 | + + " <UploadId/>\n" |
| 91 | + + " <PartNumberMarker>0</PartNumberMarker>\n" |
| 92 | + + " <NextPartNumberMarker>0</NextPartNumberMarker>\n" |
| 93 | + + " <MaxParts>0</MaxParts>\n" |
| 94 | + + " <IsTruncated>false</IsTruncated>\n" |
| 95 | + + " <Owner/>\n" |
| 96 | + + " <StorageClass/>" |
| 97 | + + " <Part>\n" |
| 98 | + + " <PartNumber>1</PartNumber>\n" |
| 99 | + + " <ETag>etag</ETag>\n" |
| 100 | + + " <Size>33</Size>\n" |
| 101 | + + " <LastModified/>\n" |
| 102 | + + " </Part>\n" |
| 103 | + + "</ListPartsResponse>"; |
| 104 | + System.out.println(xml); |
| 105 | + ListPartsResponse listPartsResponse = |
| 106 | + xmlObjectParser.parseAndClose(new StringReader(xml), ListPartsResponse.class); |
| 107 | + assertThat(listPartsResponse.getStorageClass()).isNull(); |
51 | 108 | } |
52 | 109 |
|
53 | 110 | @Test |
54 | | - public void testParseAndClose() throws IOException { |
55 | | - InputStream in = new ByteArrayInputStream("<Test/>".getBytes(StandardCharsets.UTF_8)); |
56 | | - TestXmlObject expected = new TestXmlObject(); |
57 | | - when(xmlMapper.readValue(any(Reader.class), any(Class.class))).thenReturn(expected); |
58 | | - TestXmlObject actual = |
59 | | - xmlObjectParser.parseAndClose(in, StandardCharsets.UTF_8, TestXmlObject.class); |
60 | | - assertThat(actual).isSameInstanceAs(expected); |
| 111 | + public void testNestedParseStringValueEnum_nonNull() throws IOException { |
| 112 | + // language=xml |
| 113 | + String xml = |
| 114 | + "<ListPartsResponse>\n" |
| 115 | + + " <truncated>false</truncated>\n" |
| 116 | + + " <Bucket>bucket</Bucket>\n" |
| 117 | + + " <Key>key</Key>\n" |
| 118 | + + " <UploadId/>\n" |
| 119 | + + " <PartNumberMarker>0</PartNumberMarker>\n" |
| 120 | + + " <NextPartNumberMarker>0</NextPartNumberMarker>\n" |
| 121 | + + " <MaxParts>0</MaxParts>\n" |
| 122 | + + " <IsTruncated>false</IsTruncated>\n" |
| 123 | + + " <Owner/>\n" |
| 124 | + + " <StorageClass>STANDARD</StorageClass>" |
| 125 | + + " <Part>\n" |
| 126 | + + " <PartNumber>1</PartNumber>\n" |
| 127 | + + " <ETag>etag</ETag>\n" |
| 128 | + + " <Size>33</Size>\n" |
| 129 | + + " <LastModified/>\n" |
| 130 | + + " </Part>\n" |
| 131 | + + "</ListPartsResponse>"; |
| 132 | + System.out.println(xml); |
| 133 | + ListPartsResponse listPartsResponse = |
| 134 | + xmlObjectParser.parseAndClose(new StringReader(xml), ListPartsResponse.class); |
| 135 | + assertThat(listPartsResponse.getStorageClass()).isEqualTo(StorageClass.STANDARD); |
61 | 136 | } |
62 | 137 |
|
63 | 138 | private static class TestXmlObject {} |
| 139 | + |
| 140 | + private static final class TestXmlObject2 { |
| 141 | + @JacksonXmlProperty(localName = "storageClass") |
| 142 | + private StorageClass storageClass; |
| 143 | + |
| 144 | + private TestXmlObject2() {} |
| 145 | + |
| 146 | + public TestXmlObject2(StorageClass storageClass) { |
| 147 | + this.storageClass = storageClass; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public boolean equals(Object o) { |
| 152 | + if (this == o) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + if (!(o instanceof TestXmlObject2)) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + TestXmlObject2 that = (TestXmlObject2) o; |
| 159 | + return Objects.equals(storageClass, that.storageClass); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public int hashCode() { |
| 164 | + return Objects.hashCode(storageClass); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String toString() { |
| 169 | + return MoreObjects.toStringHelper(this).add("storageClass", storageClass).toString(); |
| 170 | + } |
| 171 | + } |
64 | 172 | } |
0 commit comments