use of de.fraunhofer.iosb.ilt.faaast.service.model.api.response.SetSubmodelElementValueByPathResponse in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testSetSubmodelElementValueByPathRequest.
@Test
public void testSetSubmodelElementValueByPathRequest() throws ResourceNotFoundException, AssetConnectionException {
when(persistence.get((Reference) any(), any())).thenReturn(environment.getSubmodels().get(0).getSubmodelElements().get(0));
when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
PropertyValue propertyValue = new PropertyValue.Builder().value(new StringValue("Test")).build();
SetSubmodelElementValueByPathRequest request = new SetSubmodelElementValueByPathRequest.Builder<ElementValue>().id(environment.getSubmodels().get(0).getIdentification()).value(propertyValue).valueParser(new ElementValueParser<ElementValue>() {
@Override
public <U extends ElementValue> U parse(ElementValue raw, Class<U> type) {
return (U) raw;
}
}).path(ReferenceHelper.toKeys(SUBMODEL_ELEMENT_REF)).build();
Response response = manager.execute(request);
SetSubmodelElementValueByPathResponse expected = new SetSubmodelElementValueByPathResponse.Builder().statusCode(StatusCode.Success).build();
Assert.assertEquals(expected, response);
verify(assetValueProvider).setValue(propertyValue);
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.response.SetSubmodelElementValueByPathResponse in project FAAAST-Service by FraunhoferIOSB.
the class SetSubmodelElementValueByPathRequestHandler method process.
@Override
public SetSubmodelElementValueByPathResponse process(SetSubmodelElementValueByPathRequest request) {
SetSubmodelElementValueByPathResponse response = new SetSubmodelElementValueByPathResponse();
try {
Reference reference = ReferenceHelper.toReference(request.getPath(), request.getId(), Submodel.class);
SubmodelElement submodelElement = persistence.get(reference, new OutputModifier.Builder().extend(Extend.WithBLOBValue).build());
ElementValue oldValue = ElementValueMapper.toValue(submodelElement);
if (request.getValueParser() != null) {
ElementValue newValue = request.getValueParser().parse(request.getRawValue(), oldValue.getClass());
ElementValueMapper.setValue(submodelElement, newValue);
writeValueToAssetConnection(reference, newValue);
persistence.put(null, reference, submodelElement);
response.setStatusCode(StatusCode.Success);
publishValueChangeEventMessage(reference, oldValue, newValue);
} else {
throw new RuntimeException("Value parser of request must be non-null");
}
} catch (ResourceNotFoundException ex) {
response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
Aggregations