Merge "Reorder modifiers"
[so.git] / common / src / test / java / org / openecomp / mso / utils / RootIgnoringObjectMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.utils;
22
23 import static org.junit.Assert.assertEquals;
24
25 import org.junit.Test;
26
27 import com.fasterxml.jackson.annotation.JsonProperty;
28 import com.fasterxml.jackson.annotation.JsonRootName;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30
31 public class RootIgnoringObjectMapperTest {
32
33         @Test
34         public void someObjectWithoutRootTest() throws Exception {
35                 ObjectMapper mapper = new RootIgnoringObjectMapper<SomeObject>(SomeObject.class);
36
37                 String content = "{"
38                         + "\"attribute\":\"charm\""
39                         + "}";
40
41                 SomeObject response = mapper.readValue(content, SomeObject.class);
42                 assertEquals("SomeObject[attribute=charm]", response.toString());
43         }
44         
45         @Test
46         public void someObjectWithRootTest() throws Exception {
47                 ObjectMapper mapper = new RootIgnoringObjectMapper<SomeObject>(SomeObject.class);
48
49                 String content = "{\"SomeObject\":{"
50                         + "\"attribute\":\"charm\""
51                         + "}}";
52
53                 SomeObject response = mapper.readValue(content, SomeObject.class);
54                 assertEquals("SomeObject[attribute=charm]", response.toString());
55         }
56         
57         @Test
58         public void annotatedObjectWithoutRootTest() throws Exception {
59                 ObjectMapper mapper = new RootIgnoringObjectMapper<AnnotatedObject>(AnnotatedObject.class);
60
61                 String content = "{"
62                         + "\"attribute\":\"charm\""
63                         + "}";
64
65                 AnnotatedObject response = mapper.readValue(content, AnnotatedObject.class);
66                 assertEquals("AnnotatedObject[attribute=charm]", response.toString());
67         }
68         
69         @Test
70         public void annotatedObjectWithRootTest() throws Exception {
71                 ObjectMapper mapper = new RootIgnoringObjectMapper<AnnotatedObject>(AnnotatedObject.class);
72
73                 String content = "{\"annotated-object\":{"
74                         + "\"attribute\":\"charm\""
75                         + "}}";
76
77                 AnnotatedObject response = mapper.readValue(content, AnnotatedObject.class);
78                 assertEquals("AnnotatedObject[attribute=charm]", response.toString());
79         }
80
81         public static class SomeObject {
82
83                 @JsonProperty("attribute")
84                 private String attribute;
85
86                 public String toString() {
87                         return getClass().getSimpleName() + "[attribute=" + attribute + "]";
88                 }
89         }
90
91         @JsonRootName(value = "annotated-object")
92         public static class AnnotatedObject extends SomeObject {
93         }
94 }