Skip to content

Commit

Permalink
fix(api): throw KapuaIllegalArgument for non-KapuaConfigurableServi…
Browse files Browse the repository at this point in the history
…ce pid

Update `ServiceConfigurations` methods to throw `KapuaIllegalArgument` when the provided pid is not a `KapuaConfigurableService`.
  • Loading branch information
MDeLuise committed Jun 26, 2024
1 parent 2349705 commit c090c8a
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.app.api.core.model.ScopeId;
import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
import org.eclipse.kapua.commons.configuration.metatype.EmptyTocd;
Expand Down Expand Up @@ -86,10 +87,11 @@ public Response update(
}
for (ServiceComponentConfiguration serviceComponentConfiguration : serviceConfiguration.getComponentConfigurations()) {
Class<KapuaService> configurableServiceClass = (Class<KapuaService>) Class.forName(serviceComponentConfiguration.getId()).asSubclass(KapuaService.class);
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());
if (!KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
throw new KapuaIllegalArgumentException("serviceComponentConfiguration.id", serviceComponentConfiguration.getId());

Check warning on line 91 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#L91

Added line #L91 was not covered by tests
}
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());

Check warning on line 94 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#L93-L94

Added lines #L93 - L94 were not covered by tests
}
return Response.noContent().build();
}
Expand All @@ -102,17 +104,18 @@ public ServiceComponentConfiguration getComponent(
@PathParam("serviceId") String serviceId
) throws KapuaException, ClassNotFoundException {
Class<KapuaService> configurableServiceClass = (Class<KapuaService>) Class.forName(serviceId).asSubclass(KapuaService.class);
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
KapuaTocd metadata = configurableService.getConfigMetadata(scopeId);
Map<String, Object> values = configurableService.getConfigValues(scopeId);
if (metadata != null && !(metadata instanceof EmptyTocd)) {
ServiceComponentConfiguration serviceComponentConfiguration = serviceConfigurationFactory.newComponentConfigurationInstance(metadata.getId());
serviceComponentConfiguration.setDefinition(metadata);
serviceComponentConfiguration.setName(metadata.getName());
serviceComponentConfiguration.setProperties(values);
return serviceComponentConfiguration;
}
if (!KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
throw new KapuaIllegalArgumentException("service.pid", serviceId);

Check warning on line 108 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#L108

Added line #L108 was not covered by tests
}
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
KapuaTocd metadata = configurableService.getConfigMetadata(scopeId);
Map<String, Object> values = configurableService.getConfigValues(scopeId);

Check warning on line 112 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#L110-L112

Added lines #L110 - L112 were not covered by tests
if (metadata != null && !(metadata instanceof EmptyTocd)) {
ServiceComponentConfiguration serviceComponentConfiguration = serviceConfigurationFactory.newComponentConfigurationInstance(metadata.getId());
serviceComponentConfiguration.setDefinition(metadata);
serviceComponentConfiguration.setName(metadata.getName());
serviceComponentConfiguration.setProperties(values);
return serviceComponentConfiguration;

Check warning on line 118 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#L114-L118

Added lines #L114 - L118 were not covered by tests
}
return null;
}
Expand All @@ -131,10 +134,11 @@ public Response updateComponent(
throw new KapuaEntityNotFoundException(Account.TYPE, scopeId);
}
Class<KapuaService> configurableServiceClass = (Class<KapuaService>) Class.forName(serviceId).asSubclass(KapuaService.class);
if (KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());
if (!KapuaConfigurableService.class.isAssignableFrom(configurableServiceClass)) {
throw new KapuaIllegalArgumentException("service.pid", serviceId);

Check warning on line 138 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#L138

Added line #L138 was not covered by tests
}
KapuaConfigurableService configurableService = (KapuaConfigurableService) locator.getService(configurableServiceClass);
configurableService.setConfigValues(scopeId, account.getScopeId(), serviceComponentConfiguration.getProperties());

Check warning on line 141 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#L140-L141

Added lines #L140 - L141 were not covered by tests
return Response.noContent().build();
}

Expand Down

0 comments on commit c090c8a

Please sign in to comment.