* BAEL-2727 Example Code

* Example code for BAEL-2879 Jackson YAML
This commit is contained in:
Amy DeGregorio 2019-04-20 11:48:39 -04:00 committed by maibin
parent e41d832038
commit 1f853d5d51
5 changed files with 205 additions and 0 deletions

View File

@ -22,6 +22,20 @@
<version>${jackson.version}</version> <version>${jackson.version}</version>
</dependency> </dependency>
<!-- YAML -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.8</version>
</dependency>
<!-- Allow use of LocalDate -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.8</version>
</dependency>
<!-- test scoped --> <!-- test scoped -->

View File

@ -0,0 +1,68 @@
package com.baeldung.jackson.entities;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class Order {
private String orderNo;
private LocalDate date;
private String customerName;
private List<OrderLine> orderLines;
public Order() {
}
public Order(String orderNo, LocalDate date, String customerName, List<OrderLine> orderLines) {
super();
this.orderNo = orderNo;
this.date = date;
this.customerName = customerName;
this.orderLines = orderLines;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public List<OrderLine> getOrderLines() {
if (orderLines == null) {
orderLines = new ArrayList<>();
}
return orderLines;
}
public void setOrderLines(List<OrderLine> orderLines) {
if (orderLines == null) {
orderLines = new ArrayList<>();
}
this.orderLines = orderLines;
}
@Override
public String toString() {
return "Order [orderNo=" + orderNo + ", date=" + date + ", customerName=" + customerName + ", orderLines=" + orderLines + "]";
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.jackson.entities;
import java.math.BigDecimal;
public class OrderLine {
private String item;
private int quantity;
private BigDecimal unitPrice;
public OrderLine() {
}
public OrderLine(String item, int quantity, BigDecimal unitPrice) {
super();
this.item = item;
this.quantity = quantity;
this.unitPrice = unitPrice;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public BigDecimal getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(BigDecimal unitPrice) {
this.unitPrice = unitPrice;
}
@Override
public String toString() {
return "OrderLine [item=" + item + ", quantity=" + quantity + ", unitPrice=" + unitPrice + "]";
}
}

View File

@ -0,0 +1,11 @@
orderNo: A001
date: 2019-04-17
customerName: Customer, Joe
orderLines:
- item: No. 9 Sprockets
quantity: 12
unitPrice: 1.23
- item: Widget (10mm)
quantity: 4
unitPrice: 3.45

View File

@ -0,0 +1,63 @@
package com.baeldung.jackson.yaml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.jackson.entities.Order;
import com.baeldung.jackson.entities.OrderLine;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
public class YamlUnitTest {
private ObjectMapper mapper;
@Before
public void setup() {
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
mapper.findAndRegisterModules();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
@Test
public void givenYamlInput_ObjectCreated() throws JsonParseException, JsonMappingException, IOException {
Order order = mapper.readValue(new File("src/main/resources/orderInput.yaml"), Order.class);
assertEquals("A001", order.getOrderNo());
assertEquals(LocalDate.parse("2019-04-17", DateTimeFormatter.ISO_DATE), order.getDate());
assertEquals("Customer, Joe", order.getCustomerName());
assertEquals(2, order.getOrderLines()
.size());
}
@Test
public void givenYamlObject_FileWritten() throws JsonGenerationException, JsonMappingException, IOException {
List<OrderLine> lines = new ArrayList<>();
lines.add(new OrderLine("Copper Wire (200ft)", 1, new BigDecimal(50.67).setScale(2, RoundingMode.HALF_UP)));
lines.add(new OrderLine("Washers (1/4\")", 24, new BigDecimal(.15).setScale(2, RoundingMode.HALF_UP)));
Order order = new Order(
"B-9910",
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
"Customer, Jane",
lines);
mapper.writeValue(new File("src/main/resources/orderOutput.yaml"), order);
File outputYaml = new File("src/main/resources/orderOutput.yaml");
assertTrue(outputYaml.exists());
}
}