-
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.
* parameterized test configured * checkstyle config * new formatting applied * Util class converted into service * sonar exclusions optimized * sonar exclusions optimized
- Loading branch information
Showing
18 changed files
with
625 additions
and
1,443 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Sonar Scan" type="GradleRunConfiguration" factoryName="Gradle" folderName="00-build"> | ||
<ExternalSystemSettings> | ||
<option name="executionName" /> | ||
<option name="externalProjectPath" value="$PROJECT_DIR$" /> | ||
<option name="externalSystemIdString" value="GRADLE" /> | ||
<option name="scriptParameters" value="-Dsonar.branch.name=local" /> | ||
<option name="taskDescriptions"> | ||
<list /> | ||
</option> | ||
<option name="taskNames"> | ||
<list> | ||
<option value="sonar" /> | ||
</list> | ||
</option> | ||
<option name="vmOptions" /> | ||
</ExternalSystemSettings> | ||
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess> | ||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess> | ||
<DebugAllEnabled>false</DebugAllEnabled> | ||
<RunAsTest>false</RunAsTest> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
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
Large diffs are not rendered by default.
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
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
2 changes: 1 addition & 1 deletion
2
...src/main/java/com/maemresen/k8s/workshop/data/generator/messaging/SensorDataProducer.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
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
52 changes: 0 additions & 52 deletions
52
...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)); | ||
} | ||
|
||
} |
Oops, something went wrong.