use of de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testReadValueFromAssetConnection.
@Test
public void testReadValueFromAssetConnection() throws AssetConnectionException {
RequestHandler requestHandler = new DeleteSubmodelByIdRequestHandler(persistence, messageBus, assetConnectionManager);
PropertyValue expected = new PropertyValue.Builder().value(new StringValue("test")).build();
when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
when(assetValueProvider.getValue()).thenReturn(expected);
DataElementValue actual = requestHandler.readDataElementValueFromAssetConnection(new DefaultReference());
Assert.assertEquals(expected, actual);
}
use of de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testReadValueFromAssetConnectionAndUpdatePersistence.
@Test
public void testReadValueFromAssetConnectionAndUpdatePersistence() throws AssetConnectionException, ResourceNotFoundException {
RequestHandler requestHandler = new DeleteSubmodelByIdRequestHandler(persistence, messageBus, assetConnectionManager);
Reference parentRef = new DefaultReference.Builder().key(new DefaultKey.Builder().value("sub").idType(KeyType.IRI).type(KeyElements.SUBMODEL).build()).build();
SubmodelElement prop1 = new DefaultProperty.Builder().idShort("prop1").value("test").valueType("string").build();
SubmodelElement range = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
SubmodelElement prop2 = new DefaultProperty.Builder().idShort("prop2").value("test").valueType("string").build();
SubmodelElementCollection collection = new DefaultSubmodelElementCollection.Builder().idShort("col1").value(prop2).build();
SubmodelElement prop1_new = new DefaultProperty.Builder().idShort("prop1").value("testNew").valueType("string").build();
SubmodelElement range_new = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
SubmodelElement prop2_new = new DefaultProperty.Builder().idShort("prop2").value("testNew").valueType("string").build();
List<SubmodelElement> submodelElements = List.of(prop1, range, collection);
AssetValueProvider provider_prop1 = mock(AssetValueProvider.class);
AssetValueProvider provider_prop2 = mock(AssetValueProvider.class);
AssetValueProvider provider_range1 = mock(AssetValueProvider.class);
when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
// mock value prop1
when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, prop1))).thenReturn(provider_prop1);
when(provider_prop1.getValue()).thenReturn(ElementValueMapper.toValue(prop1_new));
// mock value prop2
when(assetConnectionManager.getValueProvider(AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2))).thenReturn(provider_prop2);
when(provider_prop2.getValue()).thenReturn(ElementValueMapper.toValue(prop2_new));
// mock value range
when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, range))).thenReturn(provider_range1);
when(provider_range1.getValue()).thenReturn(ElementValueMapper.toValue(range_new));
requestHandler.readValueFromAssetConnectionAndUpdatePersistence(parentRef, submodelElements);
verify(persistence).put(null, AasUtils.toReference(parentRef, prop1), prop1_new);
verify(persistence).put(null, AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2), prop2_new);
Assert.assertEquals(prop1_new, prop1);
Assert.assertEquals(prop2_new, prop2);
Assert.assertEquals(range_new, range);
}
use of de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManager method init.
private void init() {
// TODO implement build-time scan to improve performance (see https://github.com/classgraph/classgraph/wiki/Build-Time-Scanning)
final Object[] constructorArgs = new Object[] { persistence, messageBus, assetConnectionManager };
final Class<?>[] constructorArgTypes = RequestHandler.class.getConstructors()[0].getParameterTypes();
try (ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages(getClass().getPackageName()).scan()) {
// TODO change approach for RequestHandler from abstract class to interface
// (either with init method or pass all arguments with handle method)
handlers = scanResult.getSubclasses(RequestHandler.class).loadClasses().stream().map(x -> (Class<? extends RequestHandler>) x).collect(Collectors.toMap(x -> {
return (Class<? extends Request>) TypeToken.of(x).resolveType(RequestHandler.class.getTypeParameters()[0]).getRawType();
}, x -> {
try {
Constructor<? extends RequestHandler> constructor = x.getConstructor(constructorArgTypes);
return constructor.newInstance(constructorArgs);
} catch (NoSuchMethodException | SecurityException ex) {
logger.warn("request handler implementation could not be loaded, " + "reason: missing constructor (implementation class: {}, required constructor signature: {}", x.getName(), constructorArgTypes);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
logger.warn("request handler implementation could not be loaded, " + "reason: calling constructor failed (implementation class: {}, constructor arguments: {}", x.getName(), constructorArgs);
}
return null;
}));
}
// filter out null values from handlers that could not be instantiated so that later we don't need to check for null on each access
// create request handler executor service
requestHandlerExecutorService = Executors.newFixedThreadPool(coreConfig.getRequestHandlerThreadPoolSize(), new BasicThreadFactory.Builder().namingPattern("RequestHandler" + "-%d").build());
}
use of de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testWriteValueToAssetConnection.
@Test
public void testWriteValueToAssetConnection() throws AssetConnectionException {
RequestHandler requestHandler = new DeleteSubmodelByIdRequestHandler(persistence, messageBus, assetConnectionManager);
PropertyValue expected = new PropertyValue.Builder().value(new StringValue("test")).build();
when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
requestHandler.writeValueToAssetConnection(new DefaultReference(), expected);
verify(assetValueProvider).setValue(expected);
}
Aggregations