1 package org.collectionspace.services.structureddate;
3 import java.lang.reflect.Array;
4 import java.lang.reflect.InvocationTargetException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Iterator;
11 import org.apache.commons.beanutils.PropertyUtils;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.testng.Assert;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.Test;
17 import org.yaml.snakeyaml.Yaml;
19 public class StructuredDateEvaluatorTest {
20 public static final String TEST_CASE_FILE = "/test-dates.yaml";
21 public static final List<String> YAML_DATE_SPEC = Arrays.asList("year", "month", "day", "era", "certainty", "qualifierType", "qualifierValue", "qualifierUnit");
23 final Logger logger = LoggerFactory.getLogger(StructuredDateEvaluatorTest.class);
32 Yaml yaml = new Yaml();
33 Map<String, Object> testCases = (Map<String, Object>) yaml.load(getClass().getResourceAsStream(TEST_CASE_FILE));
35 for (String displayDate : testCases.keySet()) {
36 logger.debug("Testing input: " + displayDate);
38 Map<String, Object> expectedStructuredDateFields = (Map<String, Object>) testCases.get(displayDate);
40 StructuredDateInternal expectedStructuredDate = createStructuredDateFromYamlSpec(displayDate, expectedStructuredDateFields);
41 StructuredDateInternal actualStructuredDate = null;
44 actualStructuredDate = StructuredDateInternal.parse(displayDate);
46 catch(StructuredDateFormatException e) {
47 logger.debug(e.getMessage());
50 Assert.assertEquals(actualStructuredDate, expectedStructuredDate);
54 private StructuredDateInternal createStructuredDateFromYamlSpec(String displayDate, Map<String, Object> structuredDateFields) {
55 StructuredDateInternal structuredDate = null;
57 // Can and should we calculate today's date for the "AFTER" dates?
58 if (structuredDateFields != null && structuredDateFields.containsKey("latestDate")) {
59 if (structuredDateFields.get("latestDate").toString().equals("current date")) {
60 ArrayList items = new ArrayList<>();
61 Date currentDate = DateUtils.getCurrentDate();
62 items.add(currentDate.getYear());
63 items.add(currentDate.getMonth());
64 items.add(currentDate.getDay());
65 items.add(Date.DEFAULT_ERA.toString().toUpperCase());
66 structuredDateFields.put("latestDate", items);
70 if (structuredDateFields != null) {
71 structuredDate = new StructuredDateInternal();
73 for (String propertyName : structuredDateFields.keySet()) {
74 Object value = structuredDateFields.get(propertyName);
77 Class propertyType = PropertyUtils.getPropertyType(structuredDate, propertyName);
79 if (propertyType.equals(Date.class)) {
80 value = createDateFromYamlSpec((List<Object>) value);
83 PropertyUtils.setProperty(structuredDate, propertyName, value);
85 catch(NoSuchMethodException e) {
86 logger.warn(propertyName + " is not a property");
88 catch(InvocationTargetException e) {
89 logger.error(propertyName + " accessor threw an exception");
91 catch(IllegalAccessException e) {
92 logger.error("could not access property " + propertyName);
96 if (structuredDate.getDisplayDate() == null) {
97 structuredDate.setDisplayDate(displayDate);
101 return structuredDate;
104 private Date createDateFromYamlSpec(List<Object> dateFields) {
105 Date date = new Date();
106 Iterator<Object> fieldIterator = dateFields.iterator();
108 for (String propertyName : YAML_DATE_SPEC) {
109 Object value = fieldIterator.hasNext() ? fieldIterator.next() : null;
112 Class propertyType = PropertyUtils.getPropertyType(date, propertyName);
114 if (value != null && Enum.class.isAssignableFrom(propertyType)) {
115 value = Enum.valueOf(propertyType, (String) value);
118 PropertyUtils.setProperty(date, propertyName, value);
120 catch(NoSuchMethodException e) {
121 logger.warn(propertyName + " is not a property");
123 catch(InvocationTargetException e) {
124 logger.error(propertyName + " accessor threw an exception");
126 catch(IllegalAccessException e) {
127 logger.error("could not access property " + propertyName);