Bigger picture is I am writing a junit test that uses h2 database. I need to compare the values returned by the endpoint and do assertequals. The values in the response body is linked hash map. Keys are all strings. Values could be date or integer or strings or null.
I have stored the expected values in a string array. I loop through the values in the linked hash map and check if it is null or integer or date and then handle appropriately. Date value 2020-02-03 got converted to Long 1580709600000. I believe date somehow converted to timestamp. I am not able to convert it back to date . or the expected value back to timestamp. I am getting exception in both cases. How to fix this? Thanks for your time.
String values = "205,2020-02-03,Commodi";
ResponseEntity<String> response = testRestTemplate.getForEntity(ResourceUrl, String.class);
@SuppressWarnings("unchecked")
List<Map<String, String>> list = objectMapper.readValue(response.getBody(), List.class);
Iterator<Map.Entry<String, String>> it = list.get(0).entrySet().iterator();
String expected_values[] = values.split(",", -1);
int i = 0;
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
if (expected_values[i].isEmpty())
assertEquals(String.valueOf("null"), String.valueOf(entry.getValue()));
else if (isInteger(expected_values[i],10))
{
assertEquals(Integer.valueOf(expected_values[i]), entry.getValue());
}
else if(isValidDate(expected_values[i]))
{
//long timestamp = Long.valueOf(expected_values[i].trim());
//Long timestamp = Long.valueOf(entry.getValue().trim());
// Timestamp ts=new Timestamp(entry.getValue());
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(Long.valueOf(entry.getValue().trim()));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = format.format(date);
// assertEquals(formatter.format(ts), entry.getValue());
//Date date = Date.from(Instant.ofEpochMilli(Long.valueOf(entry.getValue().trim())));
}
else
{
assertEquals(expected_values[i], entry.getValue());
}
i++;
}