|
| 1 | +package io.openapitoools.jackson.dataformat.hal.deser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonView; |
| 4 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import io.openapitools.jackson.dataformat.hal.HALLink; |
| 7 | +import io.openapitools.jackson.dataformat.hal.HALMapper; |
| 8 | +import io.openapitools.jackson.dataformat.hal.annotation.EmbeddedResource; |
| 9 | +import io.openapitools.jackson.dataformat.hal.annotation.Link; |
| 10 | +import io.openapitools.jackson.dataformat.hal.annotation.Resource; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.io.StringReader; |
| 14 | +import java.net.URI; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | + |
| 19 | +public class HALBeanDeserializerJsonViewIT { |
| 20 | + static final String HAL_DOC_VIEW = "{" + |
| 21 | + "\"_links\":{" + |
| 22 | + "\"linkWithView1\":{" + |
| 23 | + "\"href\":\"/link/with/view/1\"," + |
| 24 | + "\"templated\":false" + |
| 25 | + "}}," + |
| 26 | + "\"_embedded\":{" + |
| 27 | + "\"childWithView1\":{" + |
| 28 | + "\"_links\":" + |
| 29 | + "{\"self\":" + |
| 30 | + "{\"href\":\"/top/1/child/childWithView1\"" + |
| 31 | + "}}," + |
| 32 | + "\"id\":\"childWithView1\"" + |
| 33 | + "}}," + |
| 34 | + "\"stateWithView1\":\"stateWithView1\"" + |
| 35 | + "}"; |
| 36 | + |
| 37 | + static final String HAL_DOC_INCLUSIVE_VIEW = "{\"_links\":{" + |
| 38 | + "\"linkWithView1\":{" + |
| 39 | + "\"href\":\"/link/with/view/1\"," + |
| 40 | + "\"templated\":false" + |
| 41 | + "}," + |
| 42 | + "\"linkWithoutView\":{" + |
| 43 | + "\"href\":\"/link/without/view\"," + |
| 44 | + "\"templated\":false" + |
| 45 | + "}}," + |
| 46 | + "\"_embedded\":{" + |
| 47 | + "\"childWithView1\":{" + |
| 48 | + "\"_links\":{" + |
| 49 | + "\"self\":{" + |
| 50 | + "\"href\":\"/top/1/child/childWithView1\"" + |
| 51 | + "}}," + |
| 52 | + "\"id\":\"childWithView1\"" + |
| 53 | + "}," + |
| 54 | + "\"childWithoutView\":{" + |
| 55 | + "\"_links\":{" + |
| 56 | + "\"self\":{" + |
| 57 | + "\"href\":\"/top/1/child/childWithoutView\"" + |
| 58 | + "}}," + |
| 59 | + "\"id\":\"childWithoutView\"" + |
| 60 | + "}}," + |
| 61 | + "\"stateWithoutView\":\"stateWithoutView\"," + |
| 62 | + "\"stateWithView1\":\"stateWithView1\"" + |
| 63 | + "}"; |
| 64 | + |
| 65 | + static final String HAL_DOC_NO_VIEW = "{\"_links\":{" + |
| 66 | + "\"linkWithView1\":{" + |
| 67 | + "\"href\":\"/link/with/view/1\"," + |
| 68 | + "\"templated\":false" + |
| 69 | + "}," + |
| 70 | + "\"linkWithView2\":{" + |
| 71 | + "\"href\":\"/link/with/view/2\",\"templated\":false" + |
| 72 | + "}," + |
| 73 | + "\"linkWithoutView\":{" + |
| 74 | + "\"href\":\"/link/without/view\"," + |
| 75 | + "\"templated\":false" + |
| 76 | + "}}," + |
| 77 | + "\"_embedded\":{" + |
| 78 | + "\"childWithView1\":{" + |
| 79 | + "\"_links\":{" + |
| 80 | + "\"self\":{" + |
| 81 | + "\"href\":\"/top/1/child/childWithView1\"" + |
| 82 | + "}}," + |
| 83 | + "\"id\":\"childWithView1\"" + |
| 84 | + "}," + |
| 85 | + "\"childWithView2\":{" + |
| 86 | + "\"_links\":{" + |
| 87 | + "\"self\":{" + |
| 88 | + "\"href\":\"/top/1/child/childWithView2\"" + |
| 89 | + "}}," + |
| 90 | + "\"id\":\"childWithView2\"" + |
| 91 | + "}," + |
| 92 | + "\"childWithoutView\":{" + |
| 93 | + "\"_links\":{" + |
| 94 | + "\"self\":{" + |
| 95 | + "\"href\":\"/top/1/child/childWithoutView\"" + |
| 96 | + "}}," + |
| 97 | + "\"id\":\"childWithoutView\"" + |
| 98 | + "}}," + |
| 99 | + "\"stateWithoutView\":\"stateWithoutView\"," + |
| 100 | + "\"stateWithView1\":\"stateWithView1\"" + |
| 101 | + ",\"stateWithView2\":\"stateWithView2\"" + |
| 102 | + "}"; |
| 103 | + |
| 104 | + ObjectMapper om = new HALMapper(); |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testDeserializationOfView() throws Exception { |
| 108 | + om.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); |
| 109 | + TopResource tr = om.readerWithView(ResourceView1.class).readValue(new StringReader(HAL_DOC_VIEW), TopResource.class); |
| 110 | + |
| 111 | + assertNull(tr.childWithoutView); |
| 112 | + assertNull(tr.linkWithoutView); |
| 113 | + assertNull(tr.stateWithoutView); |
| 114 | + |
| 115 | + assertNotNull(tr.childWithView1); |
| 116 | + assertNotNull(tr.linkWithView1); |
| 117 | + assertNotNull(tr.stateWithView1); |
| 118 | + |
| 119 | + assertNull(tr.childWithView2); |
| 120 | + assertNull(tr.linkWithView2); |
| 121 | + assertNull(tr.stateWithView2); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testDeserializationOfViewWithInclusion() throws Exception { |
| 127 | + om.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true); |
| 128 | + TopResource tr = om.readerWithView(ResourceView1.class).readValue(new StringReader(HAL_DOC_INCLUSIVE_VIEW), TopResource.class); |
| 129 | + |
| 130 | + assertNotNull(tr.childWithoutView); |
| 131 | + assertNotNull(tr.linkWithoutView); |
| 132 | + assertNotNull(tr.stateWithoutView); |
| 133 | + |
| 134 | + assertNotNull(tr.childWithView1); |
| 135 | + assertNotNull(tr.linkWithView1); |
| 136 | + assertNotNull(tr.stateWithView1); |
| 137 | + |
| 138 | + assertNull(tr.childWithView2); |
| 139 | + assertNull(tr.linkWithView2); |
| 140 | + assertNull(tr.stateWithView2); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testDeserializationOfNoView() throws Exception { |
| 145 | + TopResource tr = om.readValue(new StringReader(HAL_DOC_NO_VIEW), TopResource.class); |
| 146 | + |
| 147 | + assertNotNull(tr.childWithoutView); |
| 148 | + assertNotNull(tr.linkWithoutView); |
| 149 | + assertNotNull(tr.stateWithoutView); |
| 150 | + |
| 151 | + assertNotNull(tr.childWithView1); |
| 152 | + assertNotNull(tr.linkWithView1); |
| 153 | + assertNotNull(tr.stateWithView1); |
| 154 | + |
| 155 | + assertNotNull(tr.childWithView2); |
| 156 | + assertNotNull(tr.linkWithView2); |
| 157 | + assertNotNull(tr.stateWithView2); |
| 158 | + } |
| 159 | + |
| 160 | + @Resource |
| 161 | + public static class TopResource { |
| 162 | + |
| 163 | + // No View |
| 164 | + public String stateWithoutView; |
| 165 | + |
| 166 | + @Link |
| 167 | + public HALLink linkWithoutView; |
| 168 | + |
| 169 | + @EmbeddedResource("childWithoutView") |
| 170 | + public ChildResource childWithoutView; |
| 171 | + |
| 172 | + // Resource View 1 |
| 173 | + @JsonView(HALBeanDeserializerJsonViewIT.ResourceView1.class) |
| 174 | + public String stateWithView1; |
| 175 | + |
| 176 | + @Link("linkWithView1") |
| 177 | + @JsonView(HALBeanDeserializerJsonViewIT.ResourceView1.class) |
| 178 | + public HALLink linkWithView1; |
| 179 | + |
| 180 | + @EmbeddedResource("childWithView1") |
| 181 | + @JsonView(HALBeanDeserializerJsonViewIT.ResourceView1.class) |
| 182 | + public ChildResource childWithView1; |
| 183 | + |
| 184 | + // Resource View 2 |
| 185 | + @JsonView(HALBeanDeserializerJsonViewIT.ResourceView2.class) |
| 186 | + public String stateWithView2; |
| 187 | + |
| 188 | + @Link("linkWithView2") |
| 189 | + @JsonView(HALBeanDeserializerJsonViewIT.ResourceView2.class) |
| 190 | + public HALLink linkWithView2; |
| 191 | + |
| 192 | + @EmbeddedResource("childWithView2") |
| 193 | + @JsonView(HALBeanDeserializerJsonViewIT.ResourceView2.class) |
| 194 | + public ChildResource childWithView2; |
| 195 | + } |
| 196 | + |
| 197 | + @Resource |
| 198 | + @JsonView({ |
| 199 | + HALBeanDeserializerJsonViewIT.ResourceView1.class, |
| 200 | + HALBeanDeserializerJsonViewIT.ResourceView2.class |
| 201 | + }) |
| 202 | + public static class ChildResource { |
| 203 | + |
| 204 | + public String id; |
| 205 | + |
| 206 | + @Link |
| 207 | + public HALLink self; |
| 208 | + } |
| 209 | + |
| 210 | + public interface ResourceView1 { |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + public interface ResourceView2 { |
| 215 | + |
| 216 | + } |
| 217 | +} |
0 commit comments