use of de.fraunhofer.iosb.ilt.faaast.service.model.request.SetSubmodelElementValueByPathRequest 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.request.SetSubmodelElementValueByPathRequest in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaEndpoint method writeValue.
/**
* Writes the Value of the given SubmodelElement into the service.
*
* @param element The desired SubmodelElement including the new value
* @param submodel The corresponding submodel
* @param refElement The reference to the SubmodelElement
* @return True if the write succeeded, false otherwise
*/
public boolean writeValue(SubmodelElement element, Submodel submodel, Reference refElement) {
boolean retval = false;
if (element == null) {
throw new IllegalArgumentException("element == null");
} else if (submodel == null) {
throw new IllegalArgumentException("submodel == null");
}
try {
SetSubmodelElementValueByPathRequest request = new SetSubmodelElementValueByPathRequest();
List<Key> path = new ArrayList<>();
path.addAll(refElement.getKeys());
request.setId(submodel.getIdentification());
request.setPath(path);
request.setValueParser(new OpcUaElementValueParser());
if (element instanceof MultiLanguageProperty) {
MultiLanguageProperty mlp = (MultiLanguageProperty) element;
if ((mlp.getValues() != null) && (mlp.getValues().size() > 1)) {
for (int i = 0; i < mlp.getValues().size(); i++) {
logger.info("writeValue: MLP " + i + ": " + mlp.getValues().get(i).getValue());
}
}
}
request.setRawValue(ElementValueMapper.toValue(element));
if (request.getRawValue() instanceof MultiLanguagePropertyValue) {
MultiLanguagePropertyValue mlpv = (MultiLanguagePropertyValue) request.getRawValue();
if ((mlpv.getLangStringSet() != null) && (mlpv.getLangStringSet().size() > 1)) {
for (int i = 0; i < mlpv.getLangStringSet().size(); i++) {
logger.info("writeValue: MLPV " + i + ": " + mlpv.getLangStringSet().toArray()[i]);
}
}
}
Response response = service.execute(request);
logger.info("writeValue: Submodel " + submodel.getIdentification().getIdentifier() + "; Element " + element.getIdShort() + "; Status: " + response.getStatusCode());
if (isSuccess(response.getStatusCode())) {
retval = true;
}
} catch (Exception ex) {
logger.error("writeValue error", ex);
}
return retval;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.request.SetSubmodelElementValueByPathRequest in project FAAAST-Service by FraunhoferIOSB.
the class RequestMappingManagerTest method testSetSubmodelElementValueByPath_ContentNormal.
@Test
public void testSetSubmodelElementValueByPath_ContentNormal() throws SerializationException, InvalidRequestException, Exception {
SetSubmodelElementValueByPathRequest expected = SetSubmodelElementValueByPathRequest.<String>builder().id(SUBMODEL.getIdentification()).path(ReferenceHelper.toKeys(SUBMODEL_ELEMENT_REF)).build();
when(serviceContext.getTypeInfo(any())).thenReturn(TypeExtractor.extractTypeInfo(SUBMODEL_ELEMENT));
Request temp = mappingManager.map(HttpRequest.builder().method(HttpMethod.PUT).path("submodels/" + EncodingHelper.base64UrlEncode(SUBMODEL.getIdentification().getIdentifier()) + "/submodel/submodel-elements/" + ElementPathHelper.toElementPath(SUBMODEL_ELEMENT_REF)).query("content=value").body(serializer.write(SUBMODEL_ELEMENT)).build());
SetSubmodelElementValueByPathRequest actual = (SetSubmodelElementValueByPathRequest) temp;
Assert.assertEquals(expected.getId(), actual.getId());
Assert.assertEquals(expected.getPath(), actual.getPath());
Assert.assertEquals(ElementValueMapper.toValue(SUBMODEL_ELEMENT), actual.getValueParser().parse(actual.getRawValue(), SubmodelElement.class));
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.request.SetSubmodelElementValueByPathRequest in project FAAAST-Service by FraunhoferIOSB.
the class RequestMappingManagerTest method testSetSubmodelElementValueByPath_ContentValueOnly.
@Test
public void testSetSubmodelElementValueByPath_ContentValueOnly() throws SerializationException, InvalidRequestException, Exception {
SetSubmodelElementValueByPathRequest expected = SetSubmodelElementValueByPathRequest.<String>builder().id(SUBMODEL.getIdentification()).path(ReferenceHelper.toKeys(SUBMODEL_ELEMENT_REF)).build();
when(serviceContext.getTypeInfo(any())).thenReturn(TypeExtractor.extractTypeInfo(SUBMODEL_ELEMENT));
Request temp = mappingManager.map(HttpRequest.builder().method(HttpMethod.PUT).path("submodels/" + EncodingHelper.base64UrlEncode(SUBMODEL.getIdentification().getIdentifier()) + "/submodel/submodel-elements/" + ElementPathHelper.toElementPath(SUBMODEL_ELEMENT_REF)).query("content=value").body(serializer.write(ElementValueMapper.toValue(SUBMODEL_ELEMENT))).build());
SetSubmodelElementValueByPathRequest actual = (SetSubmodelElementValueByPathRequest) temp;
Assert.assertEquals(expected.getId(), actual.getId());
Assert.assertEquals(expected.getPath(), actual.getPath());
Assert.assertEquals(ElementValueMapper.toValue(SUBMODEL_ELEMENT), actual.getValueParser().parse(actual.getRawValue(), ElementValue.class));
}
Aggregations