148e1b452c4eae224cb9d6b6611ab6bc094ad186
[so.git] / common / src / test / java / org / onap / so / utils / XMLMarshallerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.onap.so.utils;
22
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 import javax.xml.bind.annotation.XmlRootElement;
27
28 /**
29  * Tests the XMLMarshaller to ensure that it's able to marshal and unmarshall a POJO
30  */
31
32 public class XMLMarshallerTest {
33
34     @Test
35     public void testMarshal() throws Exception {
36         Assert.assertEquals(getXML(), XmlMarshaller.marshal(getPOJO()));
37     }
38
39     @Test
40     public void testUnMarshal() throws Exception {
41         Assert.assertEquals(XmlMarshaller.unMarshal(getXML(), new TestPOJO()), getPOJO());
42     }
43
44     private TestPOJO getPOJO() {
45         TestPOJO testPOJO = new TestPOJO();
46         testPOJO.setFirstName("FN");
47         testPOJO.setLastName("LN");
48         return testPOJO;
49     }
50
51     private String getXML() {
52         return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><testPOJO><firstName>FN</firstName><lastName>LN</lastName></testPOJO>";
53     }
54
55     @XmlRootElement
56     static class TestPOJO {
57         String firstName;
58         String lastName;
59
60         public TestPOJO() {
61         }
62
63
64         public String getFirstName() {
65             return firstName;
66         }
67
68         public void setFirstName(String firstName) {
69             this.firstName = firstName;
70         }
71
72         public String getLastName() {
73             return lastName;
74         }
75
76         public void setLastName(String lastName) {
77             this.lastName = lastName;
78         }
79
80         @Override
81         public boolean equals(Object o) {
82             if (this == o) return true;
83             if (!(o instanceof TestPOJO)) return false;
84
85             TestPOJO testPOJO = (TestPOJO) o;
86
87             if (getFirstName() != null ? !getFirstName().equals(testPOJO.getFirstName()) : testPOJO.getFirstName() != null)
88                 return false;
89             return getLastName() != null ? getLastName().equals(testPOJO.getLastName()) : testPOJO.getLastName() == null;
90         }
91
92         @Override
93         public int hashCode() {
94             int result = getFirstName() != null ? getFirstName().hashCode() : 0;
95             result = 31 * result + (getLastName() != null ? getLastName().hashCode() : 0);
96             return result;
97         }
98     }
99
100
101 }