Skip to content

Commit

Permalink
Use DataFaker instead of our implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Stejskal <[email protected]>
  • Loading branch information
Frawless committed Jul 3, 2024
1 parent 7fcff03 commit 026ed41
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 703 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
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
49 changes: 10 additions & 39 deletions src/main/java/io/skodjob/loadgenerator/DataGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.skodjob.loadgenerator.enums.ETemplateType;
import io.skodjob.loadgenerator.handlers.IotDevice;
import io.skodjob.loadgenerator.handlers.People;
import io.skodjob.loadgenerator.handlers.Payroll;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
Expand All @@ -24,43 +20,16 @@
public class DataGenerator {
private static final Logger LOGGER = LogManager.getLogger(DataGenerator.class);

private final String templateJson;
private final ETemplateType templateType;

/**
* Constructor for DataGenerator.
*
* @param templateType the type of template to be used for data generation
*/
// The suppression is needed due to false positive result
@SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
public DataGenerator(ETemplateType templateType) {
this.templateType = Objects.requireNonNull(templateType, "TemplateType cannot be null!");
try {
this.templateJson = loadTemplate(templateType.getTemplatePath());
} catch (IOException e) {
LOGGER.error("Error loading template", e);
throw new RuntimeException("Failed to load template: " + templateType.getTemplatePath(), e);
}

LOGGER.info("Loaded {} template with location {}", templateType.getTemplateName(),
templateType.getTemplatePath());
}

/**
* Loads the template from the specified path.
*
* @param templateName the name of the template file
* @return the content of the template file as a string
* @throws IOException if an I/O error occurs while loading the template
*/
private String loadTemplate(String templateName) throws IOException {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(templateName)) {
if (inputStream == null) {
throw new IOException("Template not found: " + templateName);
}
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
LOGGER.info("Initialized DataGenerator with template type {}", templateType.getTemplateName());
}

/**
Expand All @@ -71,9 +40,9 @@ private String loadTemplate(String templateName) throws IOException {
public String generateStringData() {
switch (this.templateType) {
case PAYROLL_EMPLOYEE:
return Utils.stripWhitespace(People.fillTemplate(this.templateJson));
return Payroll.generateData();
case IOT_DEVICE:
return Utils.stripWhitespace(IotDevice.fillTemplate(this.templateJson));
return IotDevice.generateData();
default:
throw new IllegalArgumentException("Unknown template type: " + this.templateType);
}
Expand All @@ -83,10 +52,12 @@ public String generateStringData() {
* Generates JSON data based on the template type.
*
* @return the generated JSON data
* @throws IOException if an I/O error occurs while generating JSON data
*/
public JsonNode generateJsonData() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readTree(generateStringData());
public JsonNode generateJsonData() {
try {
return new ObjectMapper().readTree(generateStringData());
} catch (Exception e) {
throw new RuntimeException("Error generating JSON data", e);
}
}
}
64 changes: 0 additions & 64 deletions src/main/java/io/skodjob/loadgenerator/Utils.java

This file was deleted.

18 changes: 3 additions & 15 deletions src/main/java/io/skodjob/loadgenerator/enums/ETemplateType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,22 @@ public enum ETemplateType {
/**
* Template for People Payrol data
*/
PAYROLL_EMPLOYEE("payroll_employee", "templates/payroll_employee.json"),
PAYROLL_EMPLOYEE("payroll_employee"),

/**
* Template for IoT device data
*/
IOT_DEVICE("iot_device", "templates/iot_device.json");
IOT_DEVICE("iot_device");

private final String templateName;
private final String templatePath;

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

/**
Expand All @@ -40,13 +37,4 @@ public enum ETemplateType {
public String getTemplateName() {
return templateName;
}

/**
* Gets the path to the template file.
*
* @return the path to the template file
*/
public String getTemplatePath() {
return templatePath;
}
}
Loading

0 comments on commit 026ed41

Please sign in to comment.