Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deviceManagement): fixed validation of DeviceManagementBundle id validation #3927

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.model.domain.Actions;
import org.eclipse.kapua.model.id.KapuaId;
Expand Down Expand Up @@ -192,8 +193,12 @@
ArgumentValidator.notNull(scopeId, SCOPE_ID);
ArgumentValidator.notNull(deviceId, DEVICE_ID);
ArgumentValidator.notNull(deviceInventoryBundle, "deviceInventoryBundle");
ArgumentValidator.notNull(deviceInventoryBundle.getId(), "deviceInventoryBundle.id");

Check warning on line 196 in service/device/management/inventory/internal/src/main/java/org/eclipse/kapua/service/device/management/inventory/internal/DeviceInventoryManagementServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

service/device/management/inventory/internal/src/main/java/org/eclipse/kapua/service/device/management/inventory/internal/DeviceInventoryManagementServiceImpl.java#L196

Added line #L196 was not covered by tests
ArgumentValidator.notNull(deviceInventoryBundle.getName(), "deviceInventoryBundle.name");
ArgumentValidator.notNull(deviceInventoryBundleAction, "deviceInventoryBundleAction");

performAdditionalValidationOnDeviceInventoryBundleId(deviceInventoryBundle.getId());

Check warning on line 200 in service/device/management/inventory/internal/src/main/java/org/eclipse/kapua/service/device/management/inventory/internal/DeviceInventoryManagementServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

service/device/management/inventory/internal/src/main/java/org/eclipse/kapua/service/device/management/inventory/internal/DeviceInventoryManagementServiceImpl.java#L200

Added line #L200 was not covered by tests

// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(DeviceManagementDomains.DEVICE_MANAGEMENT_DOMAIN, Actions.write, scopeId));
// Prepare the request
Expand Down Expand Up @@ -462,4 +467,28 @@
// 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);
}
}

Check warning on line 492 in service/device/management/inventory/internal/src/main/java/org/eclipse/kapua/service/device/management/inventory/internal/DeviceInventoryManagementServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

service/device/management/inventory/internal/src/main/java/org/eclipse/kapua/service/device/management/inventory/internal/DeviceInventoryManagementServiceImpl.java#L488-L492

Added lines #L488 - L492 were not covered by tests

}