changeset 1157:cbc27572188d

DateTimeGenerator
author Devel 2
date Mon, 15 Jun 2020 11:53:41 +0200
parents d9dbc633012c
children 539e52cb5d8c
files stress-tester/src/main/java/com/passus/st/config/DateTimeTransformer.java stress-tester/src/main/java/com/passus/st/generator/DateTimeGenerator.java stress-tester/src/main/java/com/passus/st/utils/DateUtils.java stress-tester/src/test/java/com/passus/st/generator/DateTimeGeneratorTest.java
diffstat 4 files changed, 294 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/config/DateTimeTransformer.java	Mon Jun 15 11:53:41 2020 +0200
@@ -0,0 +1,48 @@
+package com.passus.st.config;
+
+import com.passus.config.ConfigurationContext;
+import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
+import org.joda.time.DateTime;
+
+import static com.passus.st.utils.DateUtils.*;
+
+public class DateTimeTransformer implements ValueTransformer {
+
+    @Override
+    public Object transform(Object value, Errors errors, ConfigurationContext context) {
+        if (value == null) {
+            return null;
+        }
+
+        try {
+            if (value instanceof Long) {
+                return new DateTime(((Long) value).longValue());
+            } else if (value instanceof String) {
+                String strVal = value.toString();
+                boolean containsDate = strVal.contains("-");
+                boolean containsTime = strVal.contains(":");
+
+                if (containsDate && containsTime) {
+                    return DateTime.parse(strVal, DEFAULT_DATETIME_FORMATTER);
+                } else if (containsDate) {
+                    return DateTime.parse(strVal, DEFAULT_DATE_FORMATTER);
+                } else if (containsTime) {
+                    return DateTime.parse(strVal, DEFAULT_TIME_FORMATTER);
+                } else {
+                    errors.reject("Invalid date and time format. Long or string in format '" + DATETIME_FORMAT + "' required.");
+                    return null;
+                }
+            }
+        } catch (Exception e) {
+            errors.reject("Invalid date and time format. Long or string in format '" + DATETIME_FORMAT + "' required. " + e.getMessage());
+        }
+
+        return null;
+    }
+
+    @Override
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
+        return null;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/generator/DateTimeGenerator.java	Mon Jun 15 11:53:41 2020 +0200
@@ -0,0 +1,142 @@
+package com.passus.st.generator;
+
+import com.passus.commons.Assert;
+import com.passus.commons.annotations.Plugin;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.config.schema.ValueNodeDefinition;
+import com.passus.config.validation.DoubleValidator;
+import com.passus.st.config.DateTimeTransformer;
+import com.passus.st.plugin.PluginConstants;
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
+import static com.passus.st.utils.DateUtils.DATETIME_FORMAT;
+import static com.passus.st.utils.DateUtils.DEFAULT_DATETIME_FORMATTER;
+
+@NodeDefinitionCreate(DateTimeGenerator.DateTimeNodeDefCreator.class)
+@Plugin(name = DateTimeGenerator.TYPE, category = PluginConstants.CATEGORY_GENERATOR)
+public class DateTimeGenerator implements Generator {
+
+    public static final String TYPE = "dateTime";
+
+    public enum ReturnType {
+        DATETIME, STRING
+    }
+
+    private long startTime;
+
+    private long endTime;
+
+    private String format = DATETIME_FORMAT;
+
+    private DateTimeFormatter formatter = DEFAULT_DATETIME_FORMATTER;
+
+    private ReturnType returnType = ReturnType.STRING;
+
+    @Override
+    public String getType() {
+        return TYPE;
+    }
+
+    public long getStartTime() {
+        return startTime;
+    }
+
+    public void setStartTime(long startTime) {
+        this.startTime = startTime;
+    }
+
+    public void setStartDateTime(DateTime date) {
+        setStartTime(date.getMillis());
+    }
+
+    public long getEndTime() {
+        return endTime;
+    }
+
+    public void setEndTime(long endTime) {
+        this.endTime = endTime;
+    }
+
+    public void setEndDateTime(DateTime date) {
+        setEndTime(date.getMillis());
+    }
+
+    public String getFormat() {
+        return format;
+    }
+
+    public void setFormat(String format) {
+        if (!format.equals(this.format)) {
+            this.format = format;
+            formatter = DateTimeFormat.forPattern(format);
+        }
+    }
+
+    public ReturnType getReturnType() {
+        return returnType;
+    }
+
+    public void setReturnType(ReturnType returnType) {
+        Assert.notNull(returnType, "returnType");
+        this.returnType = returnType;
+    }
+
+    @Override
+    public void configure(Configuration config, ConfigurationContext context) {
+        DateTime startDate = config.get("start", null);
+        DateTime endDate = config.get("end", null);
+        String cfgFormat = config.get("format", DATETIME_FORMAT);
+        returnType = config.get("returnType", ReturnType.STRING);
+
+        if (startDate == null) {
+            startDate = new DateTime();
+            startDate = startDate.withTime(0, 0, 0, 0);
+        }
+
+        if (endDate == null) {
+            if (startDate != null) {
+                endDate = new DateTime(startDate);
+            }
+
+            endDate = endDate.withTime(23, 59, 59, 000);
+        }
+
+        startTime = startDate.getMillis();
+        endTime = endDate.getMillis();
+        this.setFormat(cfgFormat);
+    }
+
+    @Override
+    public Object generate() {
+        long time = startTime + (long) (Math.random() * (endTime - startTime));
+        DateTime dateTime = time > 0 ? new DateTime(time) : new DateTime();
+        if (returnType == ReturnType.DATETIME) {
+            return dateTime;
+        }
+
+        return formatter.print(dateTime);
+    }
+
+    public static final class DateTimeNodeDefCreator implements NodeDefinitionCreator {
+
+        @Override
+        public NodeDefinition create() {
+            ValueNodeDefinition lengthDef = valueDef(Object.class, new DateTimeTransformer())
+                    .addValidator(DoubleValidator.GREATER_EQUAL_ZERO);
+
+            return mapDef(
+                    tupleDef("returnType", enumDef(ReturnType.class)).setRequired(false),
+                    tupleDef("start", lengthDef).setRequired(false),
+                    tupleDef("end", lengthDef).setRequired(false),
+                    tupleDef("format", valueDef()).setRequired(false)
+            ).setCanBeEmpty(true);
+        }
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/utils/DateUtils.java	Mon Jun 15 08:46:19 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/DateUtils.java	Mon Jun 15 11:53:41 2020 +0200
@@ -15,10 +15,12 @@
 
     public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
 
-    public static final String DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
+    public static final String DATETIME_FORMAT = DATE_FORMAT + " " + DEFAULT_TIME_FORMAT;
 
     public static final DateTimeFormatter DEFAULT_TIME_FORMATTER = DateTimeFormat.forPattern(DEFAULT_TIME_FORMAT);
 
+    public static final DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT);
+
     public static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER = DateTimeFormat.forPattern(DATETIME_FORMAT);
 
     private DateUtils() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/generator/DateTimeGeneratorTest.java	Mon Jun 15 11:53:41 2020 +0200
@@ -0,0 +1,101 @@
+package com.passus.st.generator;
+
+import org.joda.time.DateTime;
+import org.testng.annotations.Test;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static com.passus.st.generator.GeneratorUtils.transformToGenerator;
+import static com.passus.st.utils.DateUtils.DEFAULT_DATETIME_FORMATTER;
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertTrue;
+
+public class DateTimeGeneratorTest {
+
+    @Test
+    public void testGenerateString() throws Exception {
+        String configStr = "{type: 'dateTime', start: '2020-02-10 10:00:00', end: '2020-02-10 10:00:01'}";
+        DateTimeGenerator generator = transformToGenerator(configStr);
+
+        Object value = generator.generate();
+        assertTrue(value instanceof String);
+        String strVal = value.toString();
+
+        assertTrue("2020-02-10 10:00:00".equals(strVal) || "2020-02-10 10:00:01".equals(strVal));
+    }
+
+    @Test
+    public void testGenerateStringSetFormat() throws Exception {
+        String configStr = "{type: 'dateTime', start: '2020-02-10 10:00:00', end: '2020-02-10 10:00:01', format: \"dd'%2F'MM'%2F'YYYY HH'%3A'mm\"}";
+        DateTimeGenerator generator = transformToGenerator(configStr);
+
+        Object value = generator.generate();
+        assertTrue(value instanceof String);
+        String strVal = value.toString();
+        
+        assertTrue("10%2F02%2F2020 10%3A00".equals(strVal));
+    }
+
+    @Test
+    public void testGenerateDateTime() throws Exception {
+        Set<Long> values = new HashSet<>();
+        int loop = 10;
+
+        DateTime start = DEFAULT_DATETIME_FORMATTER.parseDateTime("2020-02-01 00:00:00");
+        DateTime end = DEFAULT_DATETIME_FORMATTER.parseDateTime("2020-02-28 23:59:59");
+
+        for (int i = 0; i < loop; i++) {
+            String configStr = "{type: 'dateTime', returnType: 'datetime', start: '2020-02-01 10:00:00', end: '2020-02-28 23:59:59'}";
+            DateTimeGenerator generator = transformToGenerator(configStr);
+
+            Object value = generator.generate();
+            assertTrue(value instanceof DateTime);
+            DateTime dateVal = (DateTime) value;
+
+            dateVal.isAfter(start);
+            dateVal.isBefore(end);
+
+            values.add(dateVal.getMillis());
+        }
+
+        assertEquals(loop, values.size());
+    }
+
+    @Test
+    public void testConfigure() throws Exception {
+        String configStr = "{type: 'dateTime', start: '2020-02-10 10:01:02', end: '2020-02-11 11:01:02'}";
+        DateTimeGenerator generator = transformToGenerator(configStr);
+
+        assertEquals(DateTimeGenerator.ReturnType.STRING, generator.getReturnType());
+        assertEquals(DEFAULT_DATETIME_FORMATTER.parseMillis("2020-02-10 10:01:02"), generator.getStartTime());
+        assertEquals(DEFAULT_DATETIME_FORMATTER.parseMillis("2020-02-11 11:01:02"), generator.getEndTime());
+    }
+
+    @Test
+    public void testConfigure2() throws Exception {
+        String configStr = "{type: 'dateTime', start: '2020-02-10 10:01:02'}";
+        DateTimeGenerator generator = transformToGenerator(configStr);
+
+        assertEquals(DateTimeGenerator.ReturnType.STRING, generator.getReturnType());
+        assertEquals(DEFAULT_DATETIME_FORMATTER.parseMillis("2020-02-10 10:01:02"), generator.getStartTime());
+        assertEquals(DEFAULT_DATETIME_FORMATTER.parseMillis("2020-02-10 23:59:59"), generator.getEndTime());
+    }
+
+    @Test
+    public void testConfigure3() throws Exception {
+        String configStr = "{type: 'dateTime', returnType: 'datetime'}";
+        DateTimeGenerator generator = transformToGenerator(configStr);
+
+        DateTime startDateTime = new DateTime();
+        startDateTime = startDateTime.withTime(0, 0, 0, 0);
+
+        DateTime endDateTime = new DateTime();
+        endDateTime = endDateTime.withTime(23, 59, 59, 0);
+
+        assertEquals(DateTimeGenerator.ReturnType.DATETIME, generator.getReturnType());
+        assertEquals(startDateTime.getMillis(), generator.getStartTime());
+        assertEquals(endDateTime.getMillis(), generator.getEndTime());
+    }
+
+}
\ No newline at end of file