From 398edb19703e21ae94e0206447bebf430611653d Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Fri, 27 Oct 2023 08:02:36 +0200 Subject: [PATCH] Feature/assure that locations are validated (#26) * Add null validation for device category and location Enhanced the Device class by adding null validation checks for the device category and location fields. These checks ensure the completeness and integrity of data before it is further processed or stored. Now, an exception will be thrown if the device category or location is not specified, ensuring that we maintain robust and reliable device data. * Update pom.xml version to 6.2.0 Bumped up the application version in pom.xml from 6.1.0 to 6.2.0. This new version includes added null validation checks for device category and location fields in the Device class. This ensures completeness and integrity of device data. * Add location data in DeviceIntegrationService tests Modified the DeviceIntegrationService integration tests to include the location data in Device builder. This change ensures that the tests alignments with the latest version of the Device model that includes the 'location' field. --- pom.xml | 2 +- .../de/app/fivegla/fiware/model/Device.java | 7 +++ .../fiware/DeviceIntegrationServiceIT.java | 51 +++++++++++++++++-- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index c3e2e75..988aca8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.app.5gla fiware-integration-layer - 6.1.0 + 6.2.0 5gLa FIWARE integration layer https://github.com/vitrum-connect/5gla-fiware-integration-layer diff --git a/src/main/java/de/app/fivegla/fiware/model/Device.java b/src/main/java/de/app/fivegla/fiware/model/Device.java index 530230f..9dd61f7 100644 --- a/src/main/java/de/app/fivegla/fiware/model/Device.java +++ b/src/main/java/de/app/fivegla/fiware/model/Device.java @@ -44,5 +44,12 @@ public void validate() { if (StringUtils.isBlank(id)) { throw new IllegalArgumentException("The id of the device must not be null."); } + if (deviceCategory == null) { + throw new IllegalArgumentException("The device category of the device must not be null."); + } + if (location == null) { + throw new IllegalArgumentException("The location of the device must not be null."); + } + location.validate(); } } diff --git a/src/test/java/de/app/fivegla/fiware/DeviceIntegrationServiceIT.java b/src/test/java/de/app/fivegla/fiware/DeviceIntegrationServiceIT.java index 5c52c6d..d4af3f1 100644 --- a/src/test/java/de/app/fivegla/fiware/DeviceIntegrationServiceIT.java +++ b/src/test/java/de/app/fivegla/fiware/DeviceIntegrationServiceIT.java @@ -4,6 +4,7 @@ import de.app.fivegla.fiware.api.enums.DeviceCategoryValues; import de.app.fivegla.fiware.model.Device; import de.app.fivegla.fiware.model.DeviceCategory; +import de.app.fivegla.fiware.model.Location; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -15,7 +16,15 @@ class DeviceIntegrationServiceIT extends AbstractIT { @Test void givenExistingPackagePropertiesWhenFetchingTheVersionTheServiceShouldReturnTheCurrentVersion() { var fiwareIntegrationService = new DeviceIntegrationService(contextBrokerUrl, tenant); - var device = Device.builder().id("integration-test:" + UUID.randomUUID()).deviceCategory(DeviceCategory.builder().value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())).build()).build(); + var device = Device.builder() + .id("integration-test:" + UUID.randomUUID()) + .deviceCategory(DeviceCategory.builder() + .value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())) + .build()) + .location(Location.builder() + .coordinates(List.of(2.0, 4.0)) + .build()) + .build(); fiwareIntegrationService.persist(device); Assertions.assertTrue(fiwareIntegrationService.exists(device.getId())); } @@ -24,7 +33,15 @@ void givenExistingPackagePropertiesWhenFetchingTheVersionTheServiceShouldReturnT void givenAlreadyExistingDeviceWhenCreatingNewDevicesTheServiceShouldNotThrowAnException() { var fiwareIntegrationService = new DeviceIntegrationService(contextBrokerUrl, tenant); var id = "integration-test:" + UUID.randomUUID(); - var device = Device.builder().id(id).deviceCategory(DeviceCategory.builder().value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())).build()).build(); + var device = Device.builder() + .id(id) + .deviceCategory(DeviceCategory.builder() + .value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())) + .build()) + .location(Location.builder() + .coordinates(List.of(2.0, 4.0)) + .build()) + .build(); fiwareIntegrationService.persist(device); Assertions.assertTrue(fiwareIntegrationService.exists(id)); fiwareIntegrationService.persist(device); @@ -35,7 +52,15 @@ void givenAlreadyExistingDeviceWhenCreatingNewDevicesTheServiceShouldNotThrowAnE void givenAlreadyExistingDeviceWhenUpdatingTheDeviceTheServiceShouldUpdateTheValuesForTheDevice() { var fiwareIntegrationService = new DeviceIntegrationService(contextBrokerUrl, tenant); var id = "integration-test:" + UUID.randomUUID(); - var device = Device.builder().id(id).deviceCategory(DeviceCategory.builder().value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())).build()).build(); + var device = Device.builder() + .id(id) + .deviceCategory(DeviceCategory.builder() + .value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())) + .build()) + .location(Location.builder() + .coordinates(List.of(2.0, 4.0)) + .build()) + .build(); fiwareIntegrationService.persist(device); Assertions.assertTrue(fiwareIntegrationService.exists(id)); var persistedDevice = fiwareIntegrationService.read(id); @@ -54,7 +79,15 @@ void givenAlreadyExistingDeviceWhenUpdatingTheDeviceTheServiceShouldUpdateTheVal void givenExistingDeviceWhenCheckingIfTheDeviceDoesExistTheServiceShouldReturnTrue() { var fiwareIntegrationService = new DeviceIntegrationService(contextBrokerUrl, tenant); var id = "integration-test:" + UUID.randomUUID(); - var device = Device.builder().id(id).deviceCategory(DeviceCategory.builder().value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())).build()).build(); + var device = Device.builder() + .id(id) + .deviceCategory(DeviceCategory.builder() + .value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())) + .build()) + .location(Location.builder() + .coordinates(List.of(2.0, 4.0)) + .build()) + .build(); fiwareIntegrationService.persist(device); Assertions.assertTrue(fiwareIntegrationService.exists(id)); Assertions.assertFalse(fiwareIntegrationService.exists("integration-test:does-not-exist")); @@ -64,7 +97,15 @@ void givenExistingDeviceWhenCheckingIfTheDeviceDoesExistTheServiceShouldReturnTr void givenExistingDeviceWhenDeletingIfTheDeviceDoesExistTheServiceShouldReturnTrue() { var fiwareIntegrationService = new DeviceIntegrationService(contextBrokerUrl, tenant); var id = "integration-test:" + UUID.randomUUID(); - var device = Device.builder().id(id).deviceCategory(DeviceCategory.builder().value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())).build()).build(); + var device = Device.builder() + .id(id) + .deviceCategory(DeviceCategory.builder() + .value(List.of(DeviceCategoryValues.SoilScoutSensor.getKey())) + .build()) + .location(Location.builder() + .coordinates(List.of(2.0, 4.0)) + .build()) + .build(); fiwareIntegrationService.persist(device); Assertions.assertTrue(fiwareIntegrationService.exists(id)); Assertions.assertTrue(fiwareIntegrationService.delete(id));