|
| 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 | +} |
0 commit comments