/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.functions;
import static org.apache.jmeter.functions.FunctionTestHelper.makeParams;
import static org.exparity.hamcrest.date.LocalDateMatchers.sameDay;
import static org.exparity.hamcrest.date.LocalDateTimeMatchers.within;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.TimeZone;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.junit.JMeterTestCase;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.junit.Before;
import org.junit.Test;
public class TestTimeShiftFunction extends JMeterTestCase {
private Function function;
private SampleResult result;
private JMeterVariables vars;
private JMeterContext jmctx = null;
private String value;
@Before
public void setUp() {
jmctx = JMeterContextService.getContext();
vars = new JMeterVariables();
jmctx.setVariables(vars);
jmctx.setPreviousResult(result);
result = new SampleResult();
function = new TimeShift();
}
@Test
public void testDatePlusOneDay() throws Exception {
Collection<CompoundVariable> params = makeParams("yyyy-dd-MM", "2017-01-01", "P1D", "");
function.setParameters(params);
value = function.execute(result, null);
assertThat(value, is(equalTo("2017-02-01")));
}
@Test
public void testDatePlusOneDayInVariable() throws Exception {
Collection<CompoundVariable> params = makeParams("yyyy-dd-MM", "2017-01-01", "P1d", "VAR");
function.setParameters(params);
function.execute(result, null);
assertThat(vars.get("VAR"), is(equalTo("2017-02-01")));
}
@Test
public void testDatePlusComplexPeriod() throws Exception {
Collection<CompoundVariable> params = makeParams("yyyy-dd-MM HH:m", "2017-01-01 12:00", "P+32dT-1H-5m", "VAR");
function.setParameters(params);
String value = function.execute(result, null);
assertThat(value, is(equalTo("2017-02-02 10:55")));
}
@Test
public void testDefault() throws Exception {
Collection<CompoundVariable> params = makeParams("", "", "", "");
function.setParameters(params);
value = function.execute(result, null);
long resultat = Long.parseLong(value);
LocalDateTime nowFromFunction = LocalDateTime.ofInstant(Instant.ofEpochMilli(resultat), TimeZone
.getDefault().toZoneId());
assertThat(nowFromFunction, within(5, ChronoUnit.SECONDS, LocalDateTime.now()));
}
@Test
public void testNowPlusOneDay() throws Exception {
Collection<CompoundVariable> params = makeParams("YYYY-MM-dd", "", "P1d", "");
function.setParameters(params);
value = function.execute(result, null);
LocalDate tomorrow = LocalDate.now().plusDays(1);
LocalDate tomorrowFromFunction = LocalDate.parse(value);
assertThat(tomorrowFromFunction, sameDay(tomorrow));
}
@Test
public void testNowWithComplexPeriod() throws Exception {
Collection<CompoundVariable> params = makeParams("YYYY-MM-dd'T'HH:mm:ss", "", "P10DT-1H-5M5S", "");
function.setParameters(params);
value = function.execute(result, null);
LocalDateTime futureDate = LocalDateTime.now().plusDays(10).plusHours(-1).plusMinutes(-5).plusSeconds(5);
LocalDateTime futureDateFromFunction = LocalDateTime.parse(value);
assertThat(futureDateFromFunction, within(1, ChronoUnit.SECONDS, futureDate));
}
@Test
public void testWrongAmountToAdd() throws Exception {
// Nothing is add with wrong value, so check if return is now
Collection<CompoundVariable> params = makeParams("", "", "qefv1Psd", "");
function.setParameters(params);
value = function.execute(result, null);
long resultat = Long.parseLong(value);
LocalDateTime nowFromFunction = LocalDateTime.ofInstant(Instant.ofEpochMilli(resultat), TimeZone
.getDefault().toZoneId());
assertThat(nowFromFunction, within(5, ChronoUnit.SECONDS, LocalDateTime.now()));
}
@Test
public void testWrongFormatDate() throws Exception {
Collection<CompoundVariable> params = makeParams("hjfdjyra:fd", "", "P1D", "");
function.setParameters(params);
value = function.execute(result, null);
assertThat(value, is(equalTo("")));
}
}