-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use DataFaker instead of our implementation (#2)
Signed-off-by: Jakub Stejskal <[email protected]>
- Loading branch information
Showing
24 changed files
with
912 additions
and
969 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
src/main/java/io/skodjob/datagenerator/enums/ETemplateType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
src/main/java/io/skodjob/datagenerator/handlers/Flights.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.