Skip to content

Commit

Permalink
fix(deviceManagement): fixed validation of DeviceManagementBundle id …
Browse files Browse the repository at this point in the history
…validation

Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Dec 22, 2023
1 parent b6a4dd5 commit 0a95d37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ components:
properties:
id:
type: string
description: This is the ID of the bundle. Even if type is String, it must be a number!
name:
type: string
version:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.kapua.service.device.management.inventory.internal;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.locator.KapuaProvider;
import org.eclipse.kapua.model.domain.Actions;
Expand Down Expand Up @@ -157,9 +158,12 @@ public void execBundle(KapuaId scopeId, KapuaId deviceId, DeviceInventoryBundle
ArgumentValidator.notNull(scopeId, SCOPE_ID);
ArgumentValidator.notNull(deviceId, DEVICE_ID);
ArgumentValidator.notNull(deviceInventoryBundle, "deviceInventoryBundle");
ArgumentValidator.notNull(deviceInventoryBundle.getId(), "deviceInventoryBundle.id");
ArgumentValidator.notNull(deviceInventoryBundle.getName(), "deviceInventoryBundle.name");
ArgumentValidator.notNull(deviceInventoryBundleAction, "deviceInventoryBundleAction");

performAdditionalValidationOnDeviceInventoryBundleId(deviceInventoryBundle.getId());

//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.write, scopeId));
Expand Down Expand Up @@ -304,4 +308,28 @@ public Class<InventoryPackagesResponseMessage> getResponseClass() {
// Check response
return checkResponseAcceptedOrThrowError(responseMessage, () -> responseMessage.getPayload().getDeviceInventoryPackages());
}


/**
* Performs an additional check on {@link DeviceInventoryBundle#getId()} to verify that it can be converted to a {@link Integer}.
* <p>
* This check is required because initially the property was created as a {@link String} even if in Kura it is a {@link Integer}.
* See Kura documentation on Device Inventory Bundle <a href="https://eclipse.github.io/kura/docs-release-5.4/administration/system-component-inventory/">here</a>
* <p>
* We cannot change the type of {@link DeviceInventoryBundle#getId()} from {@link String} to {@link Integer} because it would be an API breaking change.
* We can add a validation to improve the error returned in case a non-integer value is provided, since the current error returned is {@link NumberFormatException} (at line
* TranslatorAppInventoryBundleExecKapuaKura:74).
*
* @param deviceInventoryBundleId The {@link DeviceInventoryBundle#getId()} to check
* @throws KapuaIllegalArgumentException If {@link DeviceInventoryBundle#getId()} cannot be converted to a {@link Integer}.
* @since 2.0.0
*/
private void performAdditionalValidationOnDeviceInventoryBundleId(String deviceInventoryBundleId) throws KapuaIllegalArgumentException {
try {
Integer.parseInt(deviceInventoryBundleId);
} catch (NumberFormatException nfe) {
throw new KapuaIllegalArgumentException("deviceInventoryBundle.id", deviceInventoryBundleId);
}
}

}

0 comments on commit 0a95d37

Please sign in to comment.