Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DataFaker instead of our implementation #2

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>io.skodjob</groupId>
<artifactId>load-generator</artifactId>
<artifactId>data-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>

<licenses>
Expand All @@ -15,26 +15,26 @@
</license>
</licenses>

<name>load-generator</name>
<description>Reasonable load generator for Messaging</description>
<url>https://github.com/skodjob/load-generator</url>
<name>data-generator</name>
<description>Reasonable data generator for Messaging</description>
<url>https://github.com/skodjob/data-generator</url>

<scm>
<connection>scm:git:git:/github.com/skodjob/load-generator.git</connection>
<developerConnection>scm:git:ssh://github.com/skodjob/load-generator.git</developerConnection>
<url>https://github.com/skodjob/load-generator</url>
<connection>scm:git:git:/github.com/skodjob/data-generator.git</connection>
<developerConnection>scm:git:ssh://github.com/skodjob/data-generator.git</developerConnection>
<url>https://github.com/skodjob/data-generator</url>
</scm>

<issueManagement>
<system>GitHub</system>
<url>https://github.com/skodjob/load-generator/issues</url>
<url>https://github.com/skodjob/data-generator/issues</url>
</issueManagement>

<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Apache Maven Packages</name>
<url>https://maven.pkg.github.com/skodjob/load-generator</url>
<url>https://maven.pkg.github.com/skodjob/data-generator</url>
</repository>
</distributionManagement>

Expand Down Expand Up @@ -84,6 +84,7 @@
<junit.jupiter.version>5.10.2</junit.jupiter.version>
<junit.platform.version>1.10.2</junit.platform.version>
<maven.surefire.version>3.3.0</maven.surefire.version>
<datafaker.version>2.3.0</datafaker.version>

<jackson-dataformat-yaml.version>2.17.1</jackson-dataformat-yaml.version>

Expand Down Expand Up @@ -164,6 +165,13 @@
<version>${spotbugs.version}</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/net.datafaker/datafaker -->
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>${datafaker.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/io/skodjob/datagenerator/DataGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.datagenerator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.skodjob.datagenerator.enums.ETemplateType;
import io.skodjob.datagenerator.handlers.Flights;
import io.skodjob.datagenerator.handlers.IotDevice;
import io.skodjob.datagenerator.handlers.PaymentFiat;
import io.skodjob.datagenerator.handlers.Payroll;
import io.skodjob.datagenerator.handlers.StarGate;
import io.skodjob.datagenerator.handlers.StarWars;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;

/**
* This class is responsible for generating data based on specified templates.
*/
public class DataGenerator {
private static final Logger LOGGER = LogManager.getLogger(DataGenerator.class);

private final ETemplateType templateType;

/**
* Constructor for DataGenerator.
*
* @param templateType the type of template to be used for data generation
*/
public DataGenerator(ETemplateType templateType) {
this.templateType = Objects.requireNonNull(templateType, "TemplateType cannot be null!");
LOGGER.info("Initialized DataGenerator with template type {}", templateType.getTemplateName());
}

/**
* Generates string data based on the template type.
*
* @return the generated string data
*/
public String generateStringData() {
switch (this.templateType) {
case PAYROLL:
return Payroll.generateData();
case IOT_DEVICE:
return IotDevice.generateData();
case STARGATE:
return StarGate.generateData();
case STARWARS:
return StarWars.generateData();
case PAYMENT_FIAT:
return PaymentFiat.generateData();
case FLIGHTS:
return Flights.generateData();
default:
throw new IllegalArgumentException("Unknown template type: " + this.templateType);
}
}

/**
* Generates JSON data based on the template type.
*
* @return the generated JSON data
*/
public JsonNode generateJsonData() {
try {
return new ObjectMapper().readTree(generateStringData());
} catch (Exception e) {
throw new RuntimeException("Error generating JSON data", e);
}
}

/**
* Return template type ued within the generator
*
* @return templateType
*/
public ETemplateType getTemplateType() {
return templateType;
}
}
67 changes: 67 additions & 0 deletions src/main/java/io/skodjob/datagenerator/enums/ETemplateType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.datagenerator.enums;

import io.skodjob.datagenerator.handlers.Flights;
import io.skodjob.datagenerator.handlers.IotDevice;
import io.skodjob.datagenerator.handlers.PaymentFiat;
import io.skodjob.datagenerator.handlers.Payroll;
import io.skodjob.datagenerator.handlers.StarGate;
import io.skodjob.datagenerator.handlers.StarWars;

/**
* Enum representing different template types for use in the load generator.
*/
public enum ETemplateType {
/**
* Template for People Payrol data
*/
PAYROLL(Payroll.TEMPLATE_NAME),

/**
* Template for IoT device data
*/
IOT_DEVICE(IotDevice.TEMPLATE_NAME),

/**
* Template for StarGate data
*/
STARGATE(StarGate.TEMPLATE_NAME),

/**
* Template for StarWars data
*/
STARWARS(StarWars.TEMPLATE_NAME),

/**
* Template for Payment data
*/
PAYMENT_FIAT(PaymentFiat.TEMPLATE_NAME),

/**
* Template for Flights data
*/
FLIGHTS(Flights.TEMPLATE_NAME);

private final String templateName;

/**
* Constructor for ETemplateType.
*
* @param templateName the name of the template
*/
ETemplateType(String templateName) {
this.templateName = templateName;
}

/**
* Gets the name of the template.
*
* @return the name of the template
*/
public String getTemplateName() {
return templateName;
}
}
77 changes: 77 additions & 0 deletions src/main/java/io/skodjob/datagenerator/handlers/Flights.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Skodjob authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
package io.skodjob.datagenerator.handlers;

import net.datafaker.Faker;

import java.time.ZoneOffset;
import java.util.Locale;

/**
* This class is responsible for generating flight data using the Faker library.
*/
public class Flights {

/**
* Private constructor to make class static
*/
private Flights() {

}

private static final Faker FAKER = new Faker();

/**
* Template name used in the generator
*/
public static final String TEMPLATE_NAME = "flights";

/**
* Generates flight data using the Faker library.
*
* @return the generated flight data as a JSON string
*/
public static String generateData() {
String passengerId = FAKER.idNumber().valid();
String passengerName = FAKER.name().fullName();
String passportNumber = FAKER.regexify("[A-Z]{2}[0-9]{6}");
String nationality = FAKER.nation().nationality();
String flightNumber = FAKER.regexify("[A-Z]{2}[0-9]{4}");
String departureAirport = FAKER.aviation().airport();
String arrivalAirport = FAKER.aviation().airport();
String departureTime = FAKER.timeAndDate().future(1, java.util.concurrent.TimeUnit.DAYS)
.atOffset(ZoneOffset.UTC).toString();
String arrivalTime = FAKER.timeAndDate().future(2, java.util.concurrent.TimeUnit.DAYS)
.atOffset(ZoneOffset.UTC).toString();
String seatNumber = FAKER.regexify("[A-Z][0-9]{2}");
String gate = FAKER.regexify("[A-Z][0-9]{1,2}");
String boardingGroup = FAKER.options().option("A", "B", "C", "D", "E", "F");
String planeModel = FAKER.aviation().aircraft();
String airline = FAKER.aviation().airline();

return String.format(Locale.US,
"{\"passenger\":{" +
"\"id\":\"%s\"," +
"\"name\":\"%s\"," +
"\"passport_number\":\"%s\"," +
"\"nationality\":\"%s\"" +
"}," +
"\"flight\":{" +
"\"number\":\"%s\"," +
"\"departure_airport\":\"%s\"," +
"\"arrival_airport\":\"%s\"," +
"\"departure_time\":\"%s\"," +
"\"arrival_time\":\"%s\"," +
"\"seat_number\":\"%s\"," +
"\"gate\":\"%s\"," +
"\"boarding_group\":\"%s\"," +
"\"plane_model\":\"%s\"," +
"\"airline\":\"%s\"" +
"}}",
passengerId, passengerName, passportNumber, nationality,
flightNumber, departureAirport, arrivalAirport, departureTime, arrivalTime, seatNumber, gate, boardingGroup,
planeModel, airline);
}
}
Loading