Skip to content

Commit

Permalink
lukas-krecan#405 support date placeholder
Browse files Browse the repository at this point in the history
add a method dateReplace, convert date placeholder in json to date string
  • Loading branch information
rgseyvie committed Apr 24, 2022
1 parent 4a44d07 commit 8dd340a
Showing 1 changed file with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import static net.javacrumbs.jsonunit.core.internal.Diff.quoteTextValue;
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.getNode;
Expand Down Expand Up @@ -112,6 +115,8 @@ public JsonAssert and(@NotNull JsonAssertion... assertions) {
@Override
@NotNull
public JsonAssert isEqualTo(@Nullable Object expected) {
//convert date placeholder
expected = dateReplace(expected);
Diff diff = Diff.create(expected, actual, "fullJson", path.asPrefix(), configuration);

String overridingErrorMessage = info.overridingErrorMessage();
Expand All @@ -122,6 +127,98 @@ public JsonAssert isEqualTo(@Nullable Object expected) {
}
return this;
}

/**
* convert date placeholder
* detect keywords like DAYS, HOURS, format, and replace substring
* @param expected original json object
* @return converted json object
*/
public Object dateReplace(Object expected){
String expectedJson = (String)expected;
String dateReplace = "${json-unit.current-date.";
int replaceIndex = expectedJson.indexOf(dateReplace);

//check if date placeholder exists
if (replaceIndex >= 0){
Date date = new Date();
SimpleDateFormat df;
String replaceSub;
String time;
//placeholder end index
int replaceEndIndex = expectedJson.substring(replaceIndex).indexOf("}") + 1 + replaceIndex;
//substring of placeholder
replaceSub = expectedJson.substring(replaceIndex, replaceEndIndex);

if (replaceSub.contains("DAYS#") || replaceSub.contains("HOURS#")){
//end index of modified number
int contentEndIndex = replaceSub.indexOf(".", 25);

if (replaceSub.contains("+") || replaceSub.contains("-")){
int modifiedNum;
//no user-defined date format

if (contentEndIndex == -1){
//end index of modified number
contentEndIndex = replaceSub.indexOf("}");
//transform substring of modified number into integer
modifiedNum = Integer.valueOf(replaceSub.substring(replaceSub.indexOf("#")+2), contentEndIndex);
//default date format
df= new SimpleDateFormat("yyyy-MM-dd: HH");
}
//user-defined date format
else{
//transform substring of modified number into integer
modifiedNum = Integer.valueOf(replaceSub.substring(replaceSub.indexOf("#")+2, contentEndIndex));
int formatIndex = replaceSub.indexOf("format:") + 7;
String format = replaceSub.substring(formatIndex, replaceSub.length()-1);
df= new SimpleDateFormat(format);
}

//set timezone to GMT
df.setTimeZone(TimeZone.getTimeZone("GMT"));
int numSymbolIndex = replaceSub.indexOf("#");
if (replaceSub.contains("HOURS#")){
if (replaceSub.contains("+")){
time = df.format(new Date(date.getTime() + modifiedNum*60*60*1000));
}
else if(replaceSub.substring(numSymbolIndex+1, numSymbolIndex+2).equals("-")){
time = df.format(new Date(date.getTime() - modifiedNum*60*60*1000));
}
else{
return expectedJson;
}
}
else{
if (replaceSub.contains("+")){
time = df.format(new Date(date.getTime() + modifiedNum*24*60*60*1000));
}
else if(replaceSub.substring(numSymbolIndex+1, numSymbolIndex+2).equals("-")){
time = df.format(new Date(date.getTime() - modifiedNum*24*60*60*1000));
}
else{
return expectedJson;
}
}
//to avoid special character, replace substring manually
String beforeReplace = expectedJson.substring(0, replaceIndex);
String afterReplace = expectedJson.substring(replaceEndIndex);
expectedJson = beforeReplace + time + afterReplace;
}
}
else if (replaceSub.contains("format")){
int formatIndex = replaceSub.indexOf("format:") + 7;
String format = replaceSub.substring(formatIndex, replaceSub.length()-1);
df= new SimpleDateFormat(format);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
time = df.format(new Date(date.getTime()));
String beforeReplace = expectedJson.substring(0, replaceIndex);
String afterReplace = expectedJson.substring(replaceEndIndex);
expectedJson = beforeReplace + time + afterReplace;
}
}
return expectedJson;
}

/**
* Asserts that given node is present and is of type object.
Expand Down

0 comments on commit 8dd340a

Please sign in to comment.