-
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.
- Loading branch information
Showing
8 changed files
with
191 additions
and
110 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...java/com/maemresen/k8s/workshop/data/generator/config/props/GeneratorConstraintProps.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,23 @@ | ||
package com.maemresen.k8s.workshop.data.generator.config.props; | ||
|
||
import com.maemresen.lib.message.dto.SensorType; | ||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
@ConfigurationProperties(prefix = "app.generator.constraint") | ||
@Configuration | ||
public class GeneratorConstraintProps { | ||
private Map<SensorType, Double> minValues; | ||
private Map<SensorType, Double> maxValues; | ||
} |
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
71 changes: 71 additions & 0 deletions
71
...in/java/com/maemresen/k8s/workshop/data/generator/service/SensorDataGeneratorService.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,71 @@ | ||
package com.maemresen.k8s.workshop.data.generator.service; | ||
|
||
import com.maemresen.k8s.workshop.data.generator.config.props.GeneratorConstraintProps; | ||
import com.maemresen.lib.message.dto.Device; | ||
import com.maemresen.lib.message.dto.Sensor; | ||
import com.maemresen.lib.message.dto.SensorData; | ||
import com.maemresen.lib.message.dto.SensorType; | ||
import java.security.SecureRandom; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class SensorDataGeneratorService { | ||
|
||
private final GeneratorConstraintProps generatorConstraintProps; | ||
private final Random random = new SecureRandom(); | ||
|
||
public SensorData generateRandomSensorData( | ||
final String locationName, | ||
final String deviceName, | ||
final String sensorName, | ||
final SensorType sensorType) { | ||
if (sensorType == null) { | ||
throw new IllegalArgumentException("SensorType cannot be null"); | ||
} | ||
|
||
final Device device = Device.newBuilder() | ||
.setDeviceId(deviceName) | ||
.setLocation(locationName) | ||
.build(); | ||
|
||
final Sensor sensor = Sensor.newBuilder() | ||
.setSensorId(sensorName) | ||
.setType(sensorType) | ||
.build(); | ||
|
||
return SensorData.newBuilder() | ||
.setDevice(device) | ||
.setSensor(sensor) | ||
.setTimestamp(System.currentTimeMillis()) | ||
.setValue(getRandomValueBySensorType(sensorType)) | ||
.build(); | ||
} | ||
|
||
private double getRandomValueBySensorType(final SensorType sensorType) { | ||
final double minValue = getMinValueBySensorType(sensorType); | ||
final double maxValue = getMaxValueBySensorType(sensorType); | ||
return random.nextDouble() * (maxValue - minValue) + minValue; | ||
} | ||
|
||
private double getMinValueBySensorType(final SensorType sensorType) { | ||
final Map<SensorType, Double> minValues = generatorConstraintProps.getMinValues(); | ||
if (minValues.containsKey(sensorType)) { | ||
return minValues.get(sensorType); | ||
} | ||
|
||
throw new IllegalArgumentException("Sensor type " + sensorType + " is not supported"); | ||
} | ||
|
||
private double getMaxValueBySensorType(final SensorType sensorType) { | ||
final Map<SensorType, Double> maxValues = generatorConstraintProps.getMaxValues(); | ||
if (maxValues.containsKey(sensorType)) { | ||
return maxValues.get(sensorType); | ||
} | ||
|
||
throw new IllegalArgumentException("Sensor type " + sensorType + " is not supported"); | ||
} | ||
} |
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
51 changes: 0 additions & 51 deletions
51
...src/main/java/com/maemresen/k8s/workshop/data/generator/util/SensorDataGeneratorUtil.java
This file was deleted.
Oops, something went wrong.
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
84 changes: 84 additions & 0 deletions
84
...ava/com/maemresen/k8s/workshop/data/generator/service/SensorDataGeneratorServiceTest.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,84 @@ | ||
package com.maemresen.k8s.workshop.data.generator.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.assertj.core.api.InstanceOfAssertFactories.DOUBLE; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.maemresen.k8s.workshop.data.generator.config.props.GeneratorConstraintProps; | ||
import com.maemresen.lib.message.dto.SensorData; | ||
import com.maemresen.lib.message.dto.SensorType; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SensorDataGeneratorServiceTest { | ||
|
||
private static final SensorType VALID_SENSOR_TYPE = SensorType.TEMP; | ||
private static final double MIN_VALUE = 20.0; | ||
private static final double MAX_VALUE = 35.0; | ||
|
||
private static final Map<SensorType, Double> MIN_VALUES = Map.of(VALID_SENSOR_TYPE, MIN_VALUE); | ||
private static final Map<SensorType, Double> MAX_VALUES = Map.of(VALID_SENSOR_TYPE, MAX_VALUE); | ||
|
||
private static final Map<SensorType, Double> MIN_VALUES_WITHOUT_SENSOR_TYPE = new HashMap<>(); | ||
private static final Map<SensorType, Double> MAX_VALUES_WITHOUT_SENSOR_TYPE = new HashMap<>(); | ||
|
||
private static final String LOCATION_NAME = "Location"; | ||
private static final String DEVICE_NAME = "Device"; | ||
private static final String SENSOR_NAME = "Sensor"; | ||
|
||
@Mock | ||
private GeneratorConstraintProps generatorConstraintProps; | ||
|
||
@InjectMocks | ||
private SensorDataGeneratorService sensorDataGeneratorService; | ||
|
||
@Test | ||
void generateRandomSensorData_shouldGenerateSensorValueWithinRange() { | ||
when(generatorConstraintProps.getMinValues()).thenReturn(MIN_VALUES); | ||
when(generatorConstraintProps.getMaxValues()).thenReturn(MAX_VALUES); | ||
|
||
final SensorData sensorData = | ||
sensorDataGeneratorService.generateRandomSensorData( | ||
LOCATION_NAME, | ||
DEVICE_NAME, | ||
SENSOR_NAME, | ||
VALID_SENSOR_TYPE); | ||
assertThat(sensorData).isNotNull() | ||
.extracting(SensorData::getValue).asInstanceOf(DOUBLE) | ||
.isBetween(MIN_VALUE, MAX_VALUE); | ||
} | ||
|
||
@Test | ||
void generateRandomSensorData_shouldThrowErrorOnNullSensorType() { | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy( | ||
() -> sensorDataGeneratorService.generateRandomSensorData(LOCATION_NAME, DEVICE_NAME, | ||
SENSOR_NAME, null)); | ||
} | ||
|
||
@Test | ||
void generateRandomSensorData_shouldThrowErrorOnInvalidSensorType() { | ||
when(generatorConstraintProps.getMinValues()).thenReturn( | ||
MIN_VALUES_WITHOUT_SENSOR_TYPE); | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy( | ||
() -> sensorDataGeneratorService.generateRandomSensorData(LOCATION_NAME, DEVICE_NAME, | ||
SENSOR_NAME, VALID_SENSOR_TYPE)); | ||
|
||
when(generatorConstraintProps.getMinValues()).thenReturn(MIN_VALUES); | ||
when(generatorConstraintProps.getMaxValues()).thenReturn(MAX_VALUES_WITHOUT_SENSOR_TYPE); | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy( | ||
() -> sensorDataGeneratorService.generateRandomSensorData(LOCATION_NAME, DEVICE_NAME, | ||
SENSOR_NAME, VALID_SENSOR_TYPE)); | ||
} | ||
|
||
} |
54 changes: 0 additions & 54 deletions
54
...test/java/com/maemresen/k8s/workshop/data/generator/util/SensorDataGeneratorUtilTest.java
This file was deleted.
Oops, something went wrong.