Skip to content

Commit

Permalink
fix(rest-api): handle missing account in ServiceConfigurations to p…
Browse files Browse the repository at this point in the history
…revent NPE

Throw `KapuaEntityNotFoundException` when `Account` is not found for given `scopeId` in `ServiceConfigurations` resource. This prevents `NullPointerException` when `scopeId` does not match any account.
  • Loading branch information
MDeLuise committed Jun 25, 2024
1 parent c2f4542 commit 1eba601
Showing 1 changed file with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
*******************************************************************************/
package org.eclipse.kapua.app.api.resources.v1.resources;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.app.api.core.model.ScopeId;
import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
Expand All @@ -26,21 +42,6 @@
import org.eclipse.kapua.service.config.ServiceConfiguration;
import org.eclipse.kapua.service.config.ServiceConfigurationFactory;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Path("{scopeId}/serviceConfigurations")
public class ServiceConfigurations extends AbstractKapuaResource {

Expand Down Expand Up @@ -84,6 +85,9 @@ public Response update(
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
Account account = accountService.find(scopeId);
if (account == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, scopeId);

Check warning on line 89 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java#L89

Added line #L89 was not covered by tests
}
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());
}
}
Expand Down Expand Up @@ -126,6 +130,9 @@ public Response updateComponent(
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
Account account = accountService.find(scopeId);
if (account == null) {
throw new KapuaEntityNotFoundException(Account.TYPE, scopeId);

Check warning on line 134 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/ServiceConfigurations.java#L134

Added line #L134 was not covered by tests
}
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());
}
return Response.noContent().build();
Expand Down

0 comments on commit 1eba601

Please sign in to comment.