Skip to content

Commit 1bae07c

Browse files
sandy03934pedja4
authored andcommitted
BAEL-1979 Added examples for SnakeYAML Library (eugenp#4802)
* BAEL-1979 Added examples for SnakeYAML Library * BAEL-1979 Moved the snakeyaml related code to libraries module * BAEL-1979 Removed the System.out.println() statements and converted the assertTrue to assertEquals wherever possible. * BAEL-1979 Removed println statements, small formatting fix in pom.xml
1 parent ca1908a commit 1bae07c

14 files changed

Lines changed: 360 additions & 4 deletions

‎libraries/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66

77
# Packaged files #
88
*.jar
9+
/bin/

‎libraries/pom.xml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,20 @@
771771
<version>${hamcrest-all.version}</version>
772772
<scope>test</scope>
773773
</dependency>
774-
<dependency>
774+
775+
<dependency>
776+
<groupId>org.yaml</groupId>
777+
<artifactId>snakeyaml</artifactId>
778+
<version>${snakeyaml.version}</version>
779+
</dependency>
780+
781+
<dependency>
775782
<groupId>org.eclipse.paho</groupId>
776783
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
777784
<version>1.2.0</version>
778785
</dependency>
779786

787+
780788
</dependencies>
781789

782790
<repositories>
@@ -914,6 +922,7 @@
914922
</build>
915923

916924
<properties>
925+
<snakeyaml.version>1.21</snakeyaml.version>
917926
<googleclient.version>1.23.0</googleclient.version>
918927
<crdt.version>0.1.0</crdt.version>
919928
<multiverse.version>0.7.0</multiverse.version>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.snakeyaml;
2+
3+
public class Address {
4+
private String line;
5+
private String city;
6+
private String state;
7+
private Integer zip;
8+
9+
public String getLine() {
10+
return line;
11+
}
12+
13+
public void setLine(String line) {
14+
this.line = line;
15+
}
16+
17+
public String getCity() {
18+
return city;
19+
}
20+
21+
public void setCity(String city) {
22+
this.city = city;
23+
}
24+
25+
public String getState() {
26+
return state;
27+
}
28+
29+
public void setState(String state) {
30+
this.state = state;
31+
}
32+
33+
public Integer getZip() {
34+
return zip;
35+
}
36+
37+
public void setZip(Integer zip) {
38+
this.zip = zip;
39+
}
40+
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.snakeyaml;
2+
3+
public class Contact {
4+
5+
private String type;
6+
7+
private int number;
8+
9+
10+
public String getType() {
11+
return type;
12+
}
13+
14+
public void setType(String type) {
15+
this.type = type;
16+
}
17+
18+
public int getNumber() {
19+
return number;
20+
}
21+
22+
public void setNumber(int number) {
23+
this.number = number;
24+
}
25+
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.snakeyaml;
2+
3+
import java.util.List;
4+
5+
public class Customer {
6+
7+
private String firstName;
8+
private String lastName;
9+
private int age;
10+
private List<Contact> contactDetails;
11+
private Address homeAddress;
12+
13+
public String getFirstName() {
14+
return firstName;
15+
}
16+
17+
public void setFirstName(String firstName) {
18+
this.firstName = firstName;
19+
}
20+
21+
public String getLastName() {
22+
return lastName;
23+
}
24+
25+
public void setLastName(String lastName) {
26+
this.lastName = lastName;
27+
}
28+
29+
public int getAge() {
30+
return age;
31+
}
32+
33+
public void setAge(int age) {
34+
this.age = age;
35+
}
36+
37+
public List<Contact> getContactDetails() {
38+
return contactDetails;
39+
}
40+
41+
public void setContactDetails(List<Contact> contactDetails) {
42+
this.contactDetails = contactDetails;
43+
}
44+
45+
public Address getHomeAddress() {
46+
return homeAddress;
47+
}
48+
49+
public void setHomeAddress(Address homeAddress) {
50+
this.homeAddress = homeAddress;
51+
}
52+
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.snakeyaml;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.StringWriter;
6+
import java.util.LinkedHashMap;
7+
import java.util.Map;
8+
9+
import org.junit.Test;
10+
import org.yaml.snakeyaml.Yaml;
11+
import org.yaml.snakeyaml.nodes.Tag;
12+
13+
import com.baeldung.snakeyaml.Customer;
14+
15+
public class JavaToYAMLSerializationUnitTest {
16+
17+
@Test
18+
public void whenDumpMap_thenGenerateCorrectYAML() {
19+
Map<String, Object> data = new LinkedHashMap<String, Object>();
20+
data.put("name", "Silenthand Olleander");
21+
data.put("race", "Human");
22+
data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
23+
Yaml yaml = new Yaml();
24+
StringWriter writer = new StringWriter();
25+
yaml.dump(data, writer);
26+
String expectedYaml = "name: Silenthand Olleander\nrace: Human\ntraits: [ONE_HAND, ONE_EYE]\n";
27+
assertEquals(expectedYaml, writer.toString());
28+
}
29+
30+
@Test
31+
public void whenDumpACustomType_thenGenerateCorrectYAML() {
32+
Customer customer = new Customer();
33+
customer.setAge(45);
34+
customer.setFirstName("Greg");
35+
customer.setLastName("McDowell");
36+
Yaml yaml = new Yaml();
37+
StringWriter writer = new StringWriter();
38+
yaml.dump(customer, writer);
39+
String expectedYaml = "!!com.baeldung.snakeyaml.Customer {age: 45, contactDetails: null, firstName: Greg,\n homeAddress: null, lastName: McDowell}\n";
40+
assertEquals(expectedYaml, writer.toString());
41+
}
42+
43+
@Test
44+
public void whenDumpAsCustomType_thenGenerateCorrectYAML() {
45+
Customer customer = new Customer();
46+
customer.setAge(45);
47+
customer.setFirstName("Greg");
48+
customer.setLastName("McDowell");
49+
Yaml yaml = new Yaml();
50+
String expectedYaml = "{age: 45, contactDetails: null, firstName: Greg, homeAddress: null, lastName: McDowell}\n";
51+
assertEquals(expectedYaml, yaml.dumpAs(customer, Tag.MAP, null));
52+
}
53+
54+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.baeldung.snakeyaml;
2+
3+
import org.junit.Test;
4+
import org.yaml.snakeyaml.TypeDescription;
5+
import org.yaml.snakeyaml.Yaml;
6+
import org.yaml.snakeyaml.constructor.Constructor;
7+
8+
import java.io.InputStream;
9+
import java.util.Date;
10+
import java.util.Map;
11+
12+
import static org.junit.Assert.*;
13+
14+
public class YAMLToJavaDeserialisationUnitTest {
15+
16+
@Test
17+
public void whenLoadYAMLDocument_thenLoadCorrectMap() {
18+
Yaml yaml = new Yaml();
19+
InputStream inputStream = this.getClass()
20+
.getClassLoader()
21+
.getResourceAsStream("yaml/customer.yaml");
22+
Map<String, Object> obj = yaml.load(inputStream);
23+
assertEquals("John", obj.get("firstName"));
24+
assertEquals("Doe", obj.get("lastName"));
25+
assertEquals(20, obj.get("age"));
26+
}
27+
28+
@Test
29+
public void whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObject() {
30+
Yaml yaml = new Yaml(new Constructor(Customer.class));
31+
InputStream inputStream = this.getClass()
32+
.getClassLoader()
33+
.getResourceAsStream("yaml/customer.yaml");
34+
Customer customer = yaml.load(inputStream);
35+
assertEquals("John", customer.getFirstName());
36+
assertEquals("Doe", customer.getLastName());
37+
assertEquals(20, customer.getAge());
38+
}
39+
40+
@Test
41+
public void whenLoadYAMLDocumentWithAssumedClass_thenLoadCorrectJavaObject() {
42+
Yaml yaml = new Yaml();
43+
InputStream inputStream = this.getClass()
44+
.getClassLoader()
45+
.getResourceAsStream("yaml/customer_with_type.yaml");
46+
Customer customer = yaml.load(inputStream);
47+
assertEquals("John", customer.getFirstName());
48+
assertEquals("Doe", customer.getLastName());
49+
assertEquals(20, customer.getAge());
50+
}
51+
52+
@Test
53+
public void whenLoadYAML_thenLoadCorrectImplicitTypes() {
54+
Yaml yaml = new Yaml();
55+
Map<Object, Object> document = yaml.load("3.0: 2018-07-22");
56+
assertNotNull(document);
57+
assertEquals(1, document.size());
58+
assertTrue(document.containsKey(3.0d));
59+
assertTrue(document.get(3.0d) instanceof Date);
60+
}
61+
62+
@Test
63+
public void whenLoadYAMLDocumentWithTopLevelClass_thenLoadCorrectJavaObjectWithNestedObjects() {
64+
Yaml yaml = new Yaml(new Constructor(Customer.class));
65+
InputStream inputStream = this.getClass()
66+
.getClassLoader()
67+
.getResourceAsStream("yaml/customer_with_contact_details_and_address.yaml");
68+
Customer customer = yaml.load(inputStream);
69+
assertNotNull(customer);
70+
assertEquals("John", customer.getFirstName());
71+
assertEquals("Doe", customer.getLastName());
72+
assertEquals(31, customer.getAge());
73+
assertNotNull(customer.getContactDetails());
74+
assertEquals(2, customer.getContactDetails().size());
75+
assertEquals("mobile", customer.getContactDetails()
76+
.get(0)
77+
.getType());
78+
assertEquals(123456789,customer.getContactDetails()
79+
.get(0)
80+
.getNumber());
81+
assertEquals("landline", customer.getContactDetails()
82+
.get(1)
83+
.getType());
84+
assertEquals(456786868, customer.getContactDetails()
85+
.get(1)
86+
.getNumber());
87+
assertNotNull(customer.getHomeAddress());
88+
assertEquals("Xyz, DEF Street", customer.getHomeAddress()
89+
.getLine());
90+
}
91+
92+
@Test
93+
public void whenLoadYAMLDocumentWithTypeDescription_thenLoadCorrectJavaObjectWithCorrectGenericType() {
94+
Constructor constructor = new Constructor(Customer.class);
95+
TypeDescription customTypeDescription = new TypeDescription(Customer.class);
96+
customTypeDescription.addPropertyParameters("contactDetails", Contact.class);
97+
constructor.addTypeDescription(customTypeDescription);
98+
Yaml yaml = new Yaml(constructor);
99+
InputStream inputStream = this.getClass()
100+
.getClassLoader()
101+
.getResourceAsStream("yaml/customer_with_contact_details.yaml");
102+
Customer customer = yaml.load(inputStream);
103+
assertNotNull(customer);
104+
assertEquals("John", customer.getFirstName());
105+
assertEquals("Doe", customer.getLastName());
106+
assertEquals(31, customer.getAge());
107+
assertNotNull(customer.getContactDetails());
108+
assertEquals(2, customer.getContactDetails().size());
109+
assertEquals("mobile", customer.getContactDetails()
110+
.get(0)
111+
.getType());
112+
assertEquals("landline", customer.getContactDetails()
113+
.get(1)
114+
.getType());
115+
}
116+
117+
@Test
118+
public void whenLoadMultipleYAMLDocuments_thenLoadCorrectJavaObjects() {
119+
Yaml yaml = new Yaml(new Constructor(Customer.class));
120+
InputStream inputStream = this.getClass()
121+
.getClassLoader()
122+
.getResourceAsStream("yaml/customers.yaml");
123+
int count = 0;
124+
for (Object object : yaml.loadAll(inputStream)) {
125+
count++;
126+
assertTrue(object instanceof Customer);
127+
}
128+
assertEquals(2, count);
129+
}
130+
131+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
firstName: "John"
2+
lastName: "Doe"
3+
age: 20
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
firstName: "John"
2+
lastName: "Doe"
3+
age: 31
4+
contactDetails:
5+
- { type: "mobile", number: 123456789}
6+
- { type: "landline", number: 456786868}
7+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
firstName: "John"
2+
lastName: "Doe"
3+
age: 31
4+
contactDetails:
5+
- type: "mobile"
6+
number: 123456789
7+
- type: "landline"
8+
number: 456786868
9+
homeAddress:
10+
line: "Xyz, DEF Street"
11+
city: "City Y"
12+
state: "State Y"
13+
zip: 345657

0 commit comments

Comments
 (0)