Skip to content

Commit a48e062

Browse files
mkuligowskipedja4
authored andcommitted
BAEL-1204 (eugenp#3508)
* Initialize smooks subproject * Add Smooks dependency * Delete files form badly created submodule * Add domain classes * Create class responsible for converting Orders * Create class responsible for validating messages * Add configuration file * Add integration tests for Smooks converters and validators * ADd en_US locale and fix date format * Fix number format in expected messages * Delete unused mapping * Remove unused conversion to JSON * Add assertion for ruleName in givenIncorrectOrderXML_whenValidate_thenExpectValidationErrors
1 parent f6cfff3 commit a48e062

18 files changed

Lines changed: 453 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,11 @@
710710
<classifier>test</classifier>
711711
<scope>test</scope>
712712
</dependency>
713+
<dependency>
714+
<groupId>org.milyn</groupId>
715+
<artifactId>milyn-smooks-all</artifactId>
716+
<version>${smooks.version}</version>
717+
</dependency>
713718
</dependencies>
714719
<repositories>
715720
<repository>
@@ -789,6 +794,7 @@
789794
<google-api.version>1.23.0</google-api.version>
790795
<google-sheets.version>v4-rev493-1.21.0</google-sheets.version>
791796
<kafka.version>1.0.0</kafka.version>
797+
<smooks.version>1.7.0</smooks.version>
792798
<docker.version>3.0.14</docker.version>
793799
</properties>
794800
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.smooks.converter;
2+
3+
import com.baeldung.smooks.model.Order;
4+
import org.milyn.Smooks;
5+
import org.milyn.payload.JavaResult;
6+
import org.milyn.payload.StringResult;
7+
import org.xml.sax.SAXException;
8+
9+
import javax.xml.transform.stream.StreamSource;
10+
import java.io.IOException;
11+
12+
public class OrderConverter {
13+
14+
public Order convertOrderXMLToOrderObject(String path) throws IOException, SAXException {
15+
Smooks smooks = new Smooks(OrderConverter.class.getResourceAsStream("/smooks/smooks-mapping.xml"));
16+
try {
17+
JavaResult javaResult = new JavaResult();
18+
smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), javaResult);
19+
return (Order) javaResult.getBean("order");
20+
} finally {
21+
smooks.close();
22+
}
23+
}
24+
25+
26+
public String convertOrderXMLtoEDIFACT(String path) throws IOException, SAXException {
27+
return convertDocumentWithTempalte(path, "/smooks/smooks-transform-edi.xml");
28+
}
29+
30+
public String convertOrderXMLtoEmailMessage(String path) throws IOException, SAXException {
31+
return convertDocumentWithTempalte(path, "/smooks/smooks-transform-email.xml");
32+
}
33+
34+
private String convertDocumentWithTempalte(String path, String config) throws IOException, SAXException {
35+
Smooks smooks = new Smooks(config);
36+
37+
try {
38+
StringResult stringResult = new StringResult();
39+
smooks.filterSource(new StreamSource(OrderConverter.class.getResourceAsStream(path)), stringResult);
40+
return stringResult.toString();
41+
} finally {
42+
smooks.close();
43+
}
44+
}
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.smooks.converter;
2+
3+
import org.milyn.Smooks;
4+
import org.milyn.payload.JavaResult;
5+
import org.milyn.payload.StringResult;
6+
import org.milyn.validation.ValidationResult;
7+
import org.xml.sax.SAXException;
8+
9+
import javax.xml.transform.stream.StreamSource;
10+
import java.io.IOException;
11+
12+
public class OrderValidator {
13+
14+
public ValidationResult validate(String path) throws IOException, SAXException {
15+
Smooks smooks = new Smooks(OrderValidator.class.getResourceAsStream("/smooks/smooks-validation.xml"));
16+
17+
try {
18+
StringResult xmlResult = new StringResult();
19+
JavaResult javaResult = new JavaResult();
20+
ValidationResult validationResult = new ValidationResult();
21+
smooks.filterSource(new StreamSource(OrderValidator.class.getResourceAsStream(path)), xmlResult, javaResult, validationResult);
22+
return validationResult;
23+
} finally {
24+
smooks.close();
25+
}
26+
}
27+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.smooks.model;
2+
3+
public class Item {
4+
5+
public Item() {
6+
}
7+
8+
public Item(String code, Double price, Integer quantity) {
9+
this.code = code;
10+
this.price = price;
11+
this.quantity = quantity;
12+
}
13+
14+
private String code;
15+
private Double price;
16+
private Integer quantity;
17+
18+
19+
public String getCode() {
20+
return code;
21+
}
22+
23+
public void setCode(String code) {
24+
this.code = code;
25+
}
26+
27+
public Double getPrice() {
28+
return price;
29+
}
30+
31+
public void setPrice(Double price) {
32+
this.price = price;
33+
}
34+
35+
public Integer getQuantity() {
36+
return quantity;
37+
}
38+
39+
public void setQuantity(Integer quantity) {
40+
this.quantity = quantity;
41+
}
42+
43+
@Override
44+
public boolean equals(Object o) {
45+
if (this == o) return true;
46+
if (o == null || getClass() != o.getClass()) return false;
47+
48+
Item item = (Item) o;
49+
50+
if (code != null ? !code.equals(item.code) : item.code != null) return false;
51+
if (price != null ? !price.equals(item.price) : item.price != null) return false;
52+
return quantity != null ? quantity.equals(item.quantity) : item.quantity == null;
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
int result = code != null ? code.hashCode() : 0;
58+
result = 31 * result + (price != null ? price.hashCode() : 0);
59+
result = 31 * result + (quantity != null ? quantity.hashCode() : 0);
60+
return result;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Item{" +
66+
"code='" + code + '\'' +
67+
", price=" + price +
68+
", quantity=" + quantity +
69+
'}';
70+
}
71+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.smooks.model;
2+
3+
import java.util.Date;
4+
import java.util.List;
5+
6+
public class Order {
7+
private Date creationDate;
8+
private Long number;
9+
private Status status;
10+
private Supplier supplier;
11+
private List<Item> items;
12+
13+
public Date getCreationDate() {
14+
return creationDate;
15+
}
16+
17+
public void setCreationDate(Date creationDate) {
18+
this.creationDate = creationDate;
19+
}
20+
21+
public Long getNumber() {
22+
return number;
23+
}
24+
25+
public void setNumber(Long number) {
26+
this.number = number;
27+
}
28+
29+
public Status getStatus() {
30+
return status;
31+
}
32+
33+
public void setStatus(Status status) {
34+
this.status = status;
35+
}
36+
37+
public Supplier getSupplier() {
38+
return supplier;
39+
}
40+
41+
public void setSupplier(Supplier supplier) {
42+
this.supplier = supplier;
43+
}
44+
45+
public List<Item> getItems() {
46+
return items;
47+
}
48+
49+
public void setItems(List<Item> items) {
50+
this.items = items;
51+
}
52+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.smooks.model;
2+
3+
public enum Status {
4+
NEW, IN_PROGRESS, FINISHED
5+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.smooks.model;
2+
3+
public class Supplier {
4+
5+
private String name;
6+
private String phoneNumber;
7+
8+
public Supplier() {
9+
}
10+
11+
public Supplier(String name, String phoneNumber) {
12+
this.name = name;
13+
this.phoneNumber = phoneNumber;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getPhoneNumber() {
25+
return phoneNumber;
26+
}
27+
28+
public void setPhoneNumber(String phoneNumber) {
29+
this.phoneNumber = phoneNumber;
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) return true;
35+
if (o == null || getClass() != o.getClass()) return false;
36+
37+
Supplier supplier = (Supplier) o;
38+
39+
if (name != null ? !name.equals(supplier.name) : supplier.name != null) return false;
40+
return phoneNumber != null ? phoneNumber.equals(supplier.phoneNumber) : supplier.phoneNumber == null;
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
int result = name != null ? name.hashCode() : 0;
46+
result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0);
47+
return result;
48+
}
49+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<#setting locale="en_US">
2+
Hi,
3+
Order number #${order.number} created on ${order.creationDate?string["yyyy-MM-dd"]} is currently in ${order.status} status.
4+
Consider contact supplier "${supplier.name}" with phone number: "${supplier.phoneNumber}".
5+
Order items:
6+
<#list items as item>
7+
${item.quantity} X ${item.code} (total price ${item.price * item.quantity})
8+
</#list>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"max_total","item.quantity * item.price < 300.00"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<#setting locale="en_US">
2+
UNA:+.? '
3+
UNH+${order.number}+${order.status}+${order.creationDate?string["yyyy-MM-dd"]}'
4+
CTA+${supplier.name}+${supplier.phoneNumber}'
5+
<#list items as item>
6+
LIN+${item.quantity}+${item.code}+${item.price}'
7+
</#list>

0 commit comments

Comments
 (0)