Skip to content

Commit

Permalink
✨ [Job] Support for JSON-formatted in JobStepProperties.value for com…
Browse files Browse the repository at this point in the history
…plex properties

Signed-off-by: Alberto Codutti <[email protected]>
  • Loading branch information
Coduz committed Dec 19, 2024
1 parent 18b3991 commit 7bf7db6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
import javax.batch.runtime.Metric;
import javax.batch.runtime.context.StepContext;
import javax.xml.bind.DatatypeConverter;
import javax.xml.bind.JAXBException;

import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.KapuaRuntimeException;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.id.KapuaIdFactory;
import org.xml.sax.SAXException;

import com.google.common.base.Strings;

Expand Down Expand Up @@ -96,10 +94,15 @@ public <T, E extends Enum<E>> T getStepProperty(String stepPropertyName, Class<T
throw new KapuaIllegalArgumentException(stepPropertyName, stepPropertyString);
}
} else {
// Try both formats: XML - JSON
try {
stepProperty = xmlUtil.unmarshal(stepPropertyString, type);
} catch (JAXBException | SAXException e) {
throw new KapuaIllegalArgumentException(stepPropertyName, stepPropertyString);
} catch (Exception eXml) {

Check warning on line 100 in job-engine/commons/src/main/java/org/eclipse/kapua/job/engine/commons/wrappers/StepContextWrapper.java

View check run for this annotation

Codecov / codecov/patch

job-engine/commons/src/main/java/org/eclipse/kapua/job/engine/commons/wrappers/StepContextWrapper.java#L100

Added line #L100 was not covered by tests
try {
stepProperty = xmlUtil.unmarshalJson(stepPropertyString, type);
} catch (Exception eJson) {
throw new KapuaIllegalArgumentException(stepPropertyName, stepPropertyString);
}

Check warning on line 105 in job-engine/commons/src/main/java/org/eclipse/kapua/job/engine/commons/wrappers/StepContextWrapper.java

View check run for this annotation

Codecov / codecov/patch

job-engine/commons/src/main/java/org/eclipse/kapua/job/engine/commons/wrappers/StepContextWrapper.java#L102-L105

Added lines #L102 - L105 were not covered by tests
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DeviceConfigurationPutJobStepDefinition() {
Lists.newArrayList(
new JobStepPropertyRecord(
DeviceConfigurationPutPropertyKeys.CONFIGURATION,
"XML string that defines the configuration update sent",
"XML or JSON string that defines the configuration update sent to the target devices",
DeviceConfiguration.class.getName(),
null,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<deviceConfigurations xmlns:ns0=\"http://www.osgi.org/xmlns/metatype/v1.2.0\">\n <configuration>\n <id>org.eclipse.kura.demo.heater.Heater</id>\n <properties>\n <property name=\"temperature.increment\" array=\"false\" encrypted=\"false\" type=\"Float\">\n <value>0.25</value>\n </property>\n <property name=\"publish.rate\" array=\"false\" encrypted=\"false\" type=\"Integer\">\n <value>60</value>\n </property>\n <property name=\"program.stopTime\" array=\"false\" encrypted=\"false\" type=\"String\">\n <value>22:00</value>\n </property>\n <property name=\"publish.retain\" array=\"false\" encrypted=\"false\" type=\"Boolean\">\n <value>false</value>\n </property>\n <property name=\"service.pid\" array=\"false\" encrypted=\"false\" type=\"String\">\n <value>org.eclipse.kura.demo.heater.Heater</value>\n </property>\n <property name=\"kura.service.pid\" array=\"false\" encrypted=\"false\" type=\"String\">\n <value>org.eclipse.kura.demo.heater.Heater</value>\n </property>\n <property name=\"program.startTime\" array=\"false\" encrypted=\"false\" type=\"String\">\n <value>06:00</value>\n </property>\n <property name=\"mode\" array=\"false\" encrypted=\"false\" type=\"String\">\n <value>Program</value>\n </property>\n <property name=\"publish.semanticTopic\" array=\"false\" encrypted=\"false\" type=\"String\">\n <value>data/210</value>\n </property>\n <property name=\"manual.setPoint\" array=\"false\" encrypted=\"false\" type=\"Float\">\n <value>30.0</value>\n </property>\n <property name=\"publish.qos\" array=\"false\" encrypted=\"false\" type=\"Integer\">\n <value>2</value>\n </property>\n <property name=\"temperature.initial\" array=\"false\" encrypted=\"false\" type=\"Float\">\n <value>13.0</value>\n </property>\n <property name=\"program.setPoint\" array=\"false\" encrypted=\"false\" type=\"Float\">\n <value>30.0</value>\n </property>\n </properties>\n </configuration>\n</deviceConfigurations>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public JobStep create(JobStepCreator jobStepCreator) throws KapuaException {
return txManager.execute(tx -> {
// Check job step definition
validateJobStepProperties(tx, jobStepCreator);

// Check duplicate name
if (jobStepRepository.countEntitiesWithNameInScope(
tx,
Expand Down Expand Up @@ -420,9 +421,17 @@ private void validateJobStepProperties(List<JobStepProperty> jobStepProperties,
}

if (jobStepProperty.getPropertyValue() != null) {
ArgumentValidator.areEqual(jobStepProperty.getPropertyType(), jobStepDefinitionProperty.getPropertyType(), "stepProperties[]." + jobStepProperty.getName());
ArgumentValidator.lengthRange(jobStepProperty.getPropertyValue(), jobStepDefinitionProperty.getMinLength(), jobStepDefinitionProperty.getMaxLength(),
"stepProperties[]." + jobStepProperty.getName());
ArgumentValidator.areEqual(
jobStepProperty.getPropertyType(),
jobStepDefinitionProperty.getPropertyType(),
"stepProperties[]." + jobStepProperty.getName()

Check warning on line 427 in service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java#L424-L427

Added lines #L424 - L427 were not covered by tests
);
ArgumentValidator.lengthRange(
jobStepProperty.getPropertyValue(),
jobStepDefinitionProperty.getMinLength(),
jobStepDefinitionProperty.getMaxLength(),
"stepProperties[]." + jobStepProperty.getName()

Check warning on line 433 in service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java#L429-L433

Added lines #L429 - L433 were not covered by tests
);

validateJobStepPropertyValue(jobStepProperty, jobStepDefinitionProperty);
}
Expand Down Expand Up @@ -455,7 +464,12 @@ private <C extends Comparable<C>, E extends Enum<E>> void validateJobStepPropert
Class<E> jobStepDefinitionPropertyClassEnum = (Class<E>) jobStepDefinitionPropertyClass;
Enum.valueOf(jobStepDefinitionPropertyClassEnum, jobStepProperty.getPropertyValue());
} else {
xmlUtil.unmarshal(jobStepProperty.getPropertyValue(), jobStepDefinitionPropertyClass);
// Try both formats: XML - JSON
try {
xmlUtil.unmarshal(jobStepProperty.getPropertyValue(), jobStepDefinitionPropertyClass);
} catch (Exception e) {
xmlUtil.unmarshalJson(jobStepProperty.getPropertyValue(), jobStepDefinitionPropertyClass);
}

Check warning on line 472 in service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java#L469-L472

Added lines #L469 - L472 were not covered by tests
}

} catch (KapuaIllegalArgumentException kiae) {
Expand Down

0 comments on commit 7bf7db6

Please sign in to comment.