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

Throw KapuaNotFoundException if device does not exist #2735

Merged
Merged
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 @@ -25,6 +25,7 @@
import org.eclipse.kapua.model.query.predicate.AndPredicate;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.device.registry.Device;
import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
import org.eclipse.kapua.service.device.registry.event.DeviceEvent;
import org.eclipse.kapua.service.device.registry.event.DeviceEventAttributes;
import org.eclipse.kapua.service.device.registry.event.DeviceEventFactory;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class DeviceEvents extends AbstractKapuaResource {
private final DeviceEventService deviceEventService = locator.getService(DeviceEventService.class);
private final DeviceEventFactory deviceEventFactory = locator.getFactory(DeviceEventFactory.class);

private final DeviceRegistryService deviceRegistryService = locator.getService(DeviceRegistryService.class);

/**
* Gets the {@link DeviceEvent} list in the scope.
*
Expand All @@ -75,6 +78,10 @@ public DeviceEventListResult simpleQuery(
@ApiParam(value = "The result set limit.", defaultValue = "50") @QueryParam("limit") @DefaultValue("50") int limit) throws Exception {
DeviceEventQuery query = deviceEventFactory.newQuery(scopeId);

if (deviceRegistryService.find(scopeId, deviceId) == null) {
throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
}

AndPredicate andPredicate = query.andPredicate(query.attributePredicate(DeviceEventAttributes.DEVICE_ID, deviceId));

if (!Strings.isNullOrEmpty(resource)) {
Expand Down Expand Up @@ -107,8 +114,13 @@ public DeviceEventListResult query(
@ApiParam(value = "The ScopeId in which to search results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The id of the Device in which to search results") @PathParam("deviceId") EntityId deviceId,
@ApiParam(value = "The DeviceEventQuery to use to filter results.", required = true) DeviceEventQuery query) throws Exception {

query.setScopeId(scopeId);

if (deviceRegistryService.find(scopeId, deviceId) == null) {
throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
}

AndPredicate andPredicate = query.andPredicate(
query.attributePredicate(DeviceEventAttributes.DEVICE_ID, deviceId),
query.getPredicate()
Expand Down Expand Up @@ -138,6 +150,11 @@ public CountResult count(
@ApiParam(value = "The ScopeId in which to count results.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The id of the Device in which to count results") @PathParam("deviceId") EntityId deviceId,
@ApiParam(value = "The DeviceEventQuery to use to filter count results", required = true) DeviceEventQuery query) throws Exception {

if (deviceRegistryService.find(scopeId, deviceId) == null) {
throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
}

query.setScopeId(scopeId);
query.setPredicate(query.attributePredicate(DeviceEventAttributes.DEVICE_ID, deviceId));

Expand All @@ -162,6 +179,11 @@ public DeviceEvent find(
@ApiParam(value = "The ScopeId of the requested DeviceEvent.", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The id of the requested Device", required = true) @PathParam("deviceId") EntityId deviceId,
@ApiParam(value = "The id of the requested DeviceEvent", required = true) @PathParam("deviceEventId") EntityId deviceEventId) throws Exception {

if (deviceRegistryService.find(scopeId, deviceId) == null) {
throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
}

DeviceEventQuery query = deviceEventFactory.newQuery(scopeId);

AndPredicate andPredicate = query.andPredicate(
Expand Down Expand Up @@ -197,6 +219,11 @@ public DeviceEvent find(
public Response deleteDeviceEvent(@PathParam("scopeId") ScopeId scopeId,
@ApiParam(value = "The id of the Device in which to delete the event.", required = true) @PathParam("deviceId") EntityId deviceId,
@ApiParam(value = "The id of the DeviceEvent to be deleted", required = true) @PathParam("deviceEventId") EntityId deviceEventId) throws Exception {

if (deviceRegistryService.find(scopeId, deviceId) == null) {
throw new KapuaEntityNotFoundException(Device.TYPE, deviceId);
}

deviceEventService.delete(scopeId, deviceEventId);

return returnOk();
Expand Down