-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from openepcis/EDG-39_sensor_report_randomizer
EDG-39: Adding support to random values in SensorReport double fields
- Loading branch information
Showing
16 changed files
with
403 additions
and
152 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
126 changes: 15 additions & 111 deletions
126
...tor-common/src/main/java/io/openepcis/testdata/generator/format/RandomValueGenerator.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 |
---|---|---|
@@ -1,122 +1,26 @@ | ||
/* | ||
* Copyright 2022-2024 benelog GmbH & Co. KG | ||
* | ||
* Licensed 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 io.openepcis.testdata.generator.format; | ||
|
||
import io.openepcis.testdata.generator.constants.RandomizationType; | ||
import io.openepcis.testdata.generator.constants.TestDataGeneratorException; | ||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.SecureRandom; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import io.openepcis.testdata.generator.template.RandomGenerators; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.commons.math3.distribution.TriangularDistribution; | ||
import org.apache.commons.math3.random.MersenneTwister; | ||
import org.apache.commons.math3.random.RandomGenerator; | ||
|
||
import java.util.List; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@RegisterForReflection | ||
public class RandomValueGenerator { | ||
|
||
private SecureRandom random = new SecureRandom(); | ||
|
||
private static final RandomValueGenerator INSTANCE = new RandomValueGenerator(); | ||
|
||
public static final RandomValueGenerator getInstance() { | ||
return INSTANCE; | ||
} | ||
public List<String> randomGenerator( | ||
RandomizationType type, int minLength, int maxLength, int randomCount) { | ||
return switch (type) { | ||
case ALPHA_NUMERIC: | ||
yield alphaNumericGenerator(minLength, maxLength, randomCount); | ||
case URL_SAFE_CHARACTERS: | ||
yield urlSafeGenerator(minLength, maxLength, randomCount); | ||
default: | ||
yield numericGenerator(minLength, maxLength, randomCount); | ||
}; | ||
} | ||
|
||
// If Numeric random values needs to be generated | ||
public List<String> numericGenerator(int minLength, int maxLength, int randomCount) { | ||
try { | ||
final List<String> randomList = new ArrayList<>(); | ||
final var numericRandomSet = "1234567890"; | ||
|
||
for (var id = 0; id < randomCount; id++) { | ||
var randomID = new StringBuilder(); | ||
final int charPicker = minLength + random.nextInt(maxLength - minLength + 1); | ||
|
||
for (var i = 0; i < charPicker; i++) { | ||
randomID.append(numericRandomSet.charAt(random.nextInt(numericRandomSet.length()))); | ||
} | ||
randomList.add(randomID.toString()); | ||
} | ||
return randomList; | ||
} catch (Exception ex) { | ||
throw new TestDataGeneratorException( | ||
"Exception occurred during creation of seral numbers in Random Numeric format, Please check the values provided values for Identifiers random values : " | ||
+ ex.getMessage(), ex); | ||
} | ||
} | ||
|
||
// If Alphanumeric random values needs to be generated | ||
public List<String> alphaNumericGenerator(int minLength, int maxLength, int randomCount) { | ||
try { | ||
final var alphaNumericRandomSet = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
final List<String> randomList = new ArrayList<>(); | ||
|
||
for (var id = 0; id < randomCount; id++) { | ||
var randomID = new StringBuilder(); | ||
final int charPicker = minLength + random.nextInt(maxLength - minLength + 1); | ||
|
||
for (var i = 0; i < charPicker; i++) { | ||
randomID.append( | ||
alphaNumericRandomSet.charAt(random.nextInt(alphaNumericRandomSet.length()))); | ||
} | ||
randomList.add(randomID.toString()); | ||
} | ||
return randomList; | ||
} catch (Exception ex) { | ||
throw new TestDataGeneratorException( | ||
"Exception occurred during creation of seral numbers in Random Alphanumeric format, Please check the values provided values for Identifiers random values : " | ||
+ ex.getMessage(), ex); | ||
} | ||
} | ||
|
||
// If URL Safe random values needs to be generated | ||
public List<String> urlSafeGenerator(int minLength, int maxLength, int randomCount) { | ||
try { | ||
final var urlSafeRandomSet = "abcdefghijklmnopqrstuvwxyz0123456789-_"; | ||
final List<String> randomList = new ArrayList<>(); | ||
|
||
for (var id = 0; id < randomCount; id++) { | ||
var randomID = new StringBuilder(); | ||
final int charPicker = minLength + random.nextInt(maxLength - minLength + 1); | ||
|
||
for (var i = 0; i < charPicker; i++) { | ||
randomID.append(urlSafeRandomSet.charAt(random.nextInt(urlSafeRandomSet.length()))); | ||
//Method to generate the Mersenne Twister instance for each of the RandomGenerators to get subsequent numbers when used multiple times | ||
public static void generateInstance(final List<RandomGenerators> randomGenerators) { | ||
if (randomGenerators != null) { | ||
randomGenerators.forEach(r -> { | ||
// Initialize Mersenne Twister with the seed | ||
final RandomGenerator random = new MersenneTwister(r.getSeedValue()); | ||
final TriangularDistribution triangularDistribution = new TriangularDistribution(random, r.getMinValue(), r.getMeanValue(), r.getMaxValue()); | ||
r.setTriangularDistribution(triangularDistribution); | ||
}); | ||
} | ||
randomList.add(randomID.toString()); | ||
} | ||
return randomList; | ||
} catch (Exception ex) { | ||
throw new TestDataGeneratorException( | ||
"Exception occurred during creation of seral numbers in Random Alphanumeric with special characters, Please check the values provided values for Identifiers random values : " | ||
+ ex.getMessage(), ex); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...enerator-common/src/main/java/io/openepcis/testdata/generator/format/ValueTypeSyntax.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,61 @@ | ||
package io.openepcis.testdata.generator.format; | ||
|
||
import io.openepcis.testdata.generator.constants.TestDataGeneratorException; | ||
import io.openepcis.testdata.generator.template.RandomGenerators; | ||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
@Setter | ||
@Getter | ||
@RegisterForReflection | ||
public class ValueTypeSyntax implements Serializable { | ||
@NotNull(message = "Type cannot be Null for Static/Random value generation") | ||
@Schema(type = SchemaType.STRING, description = "Type of the Static/Random value generation static/random.") | ||
private String type; | ||
|
||
@Schema(type = SchemaType.NUMBER, description = "Static value if the type is static.") | ||
private double staticValue; | ||
|
||
@Schema(type = SchemaType.INTEGER, description = "randomID associated to randomGenerators if the type is random.") | ||
private int randomID; | ||
|
||
// Method to get the value, either static or generated randomly | ||
public double getValue(final List<RandomGenerators> randomGenerators) { | ||
if (this.type.equals("static")) { | ||
return this.staticValue; | ||
} else if (this.type.equals("random") && randomGenerators != null) { | ||
return generateRandomValue(randomGenerators); | ||
} | ||
|
||
throw new TestDataGeneratorException("Unknown type for Value generation : " + this.type); | ||
} | ||
|
||
private double generateRandomValue(final List<RandomGenerators> randomGenerators) { | ||
// Find the corresponding RandomGenerators entry | ||
final RandomGenerators generatorConfig = randomGenerators.stream() | ||
.filter(r -> r.getRandomID() == this.randomID) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("No generator found for randomID: " + randomID)); | ||
|
||
double sample = generatorConfig.getTriangularDistribution().sample(); // Generate and return the random value | ||
|
||
//If user specified format for RandomGenerator then format based on specific format | ||
if(!StringUtils.isBlank(generatorConfig.getFormatValue())){ | ||
try{ | ||
return Double.parseDouble(String.format(generatorConfig.getFormatValue(), sample)); | ||
}catch (Exception ex){ | ||
throw new TestDataGeneratorException("Invalid format value provided for formatting Random value : " + ex.getMessage(), ex); | ||
} | ||
} | ||
|
||
return Double.parseDouble(String.format("%.3f", sample)); | ||
} | ||
} |
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
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
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
Oops, something went wrong.