ChangeDateFunction extends AbstractDateFunction { public String execute(List<String> parameterList, TestContext context) { if (CollectionUtils.isEmpty(parameterList)) { throw new InvalidFunctionUsageException("Function parameters must not be empty"); } Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat; String result = ""; if (parameterList.size() > 2) { dateFormat = new SimpleDateFormat(parameterList.get(2)); } else { dateFormat = getDefaultDateFormat(); } try { calendar.setTime(dateFormat.parse(parameterList.get(0))); } catch (ParseException e) { throw new CitrusRuntimeException(e); } if (parameterList.size() > 1) { applyDateOffset(calendar, parameterList.get(1)); } try { result = dateFormat.format(calendar.getTime()); } catch (RuntimeException e) { log.error("Error while formatting dateParameter value ", e); throw new CitrusRuntimeException(e); } return result; }  String execute(List<String> parameterList, TestContext context);  }
@Test public void testDefaultDateFormat() { Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$td.%1$tm.%1$tY", Calendar.getInstance()) + "', '+1y'"), context), String.format("%1$td.%1$tm.%1$tY", c)); c = Calendar.getInstance(); c.add(Calendar.MONTH, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$td.%1$tm.%1$tY", Calendar.getInstance()) + "', '+1M'"), context), String.format("%1$td.%1$tm.%1$tY", c)); c = Calendar.getInstance(); c.add(Calendar.DAY_OF_YEAR, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$td.%1$tm.%1$tY", Calendar.getInstance()) + "', '+1d'"), context), String.format("%1$td.%1$tm.%1$tY", c)); }
@Test public void testFunction() { Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1y', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.MONTH, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1M', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.DAY_OF_YEAR, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1d', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.HOUR, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1h', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.MINUTE, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1m', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.SECOND, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1s', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.YEAR, 10); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+10y', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.YEAR, 1); c.add(Calendar.MONTH, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1y+1M', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c.add(Calendar.DAY_OF_YEAR, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1y+1M+1d', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c.add(Calendar.HOUR, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1y+1M+1d+1h', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c.add(Calendar.MINUTE, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1y+1M+1d+1h+1m', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c.add(Calendar.SECOND, 1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1y+1M+1d+1h+1m+1s', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.YEAR, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '-1y', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.MONTH, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '-1M', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.DAY_OF_YEAR, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '-1d', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.HOUR, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '-1h', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.MINUTE, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '-1m', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.SECOND, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '-1s', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.YEAR, -1); c.add(Calendar.MONTH, 1); c.add(Calendar.DAY_OF_YEAR, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '-1y+1M-1d', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); c = Calendar.getInstance(); c.add(Calendar.YEAR, 1); c.add(Calendar.MONTH, -1); c.add(Calendar.DAY_OF_YEAR, -1); Assert.assertEquals(function.execute(FunctionParameterHelper.getParameterList("'" + String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", Calendar.getInstance()) + "', '+1y-1M-1d', 'yyyy-MM-dd HH:mm:ss'"), context), String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", c)); }
@Test(expectedExceptions = {CitrusRuntimeException.class}) public void testWrongDateFormatUsage() { function.execute(FunctionParameterHelper.getParameterList("'1970-01-01', '+1y'"), context); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testNoParameters() { function.execute(Collections.EMPTY_LIST, context); }