Search in sources :

Example 1 with RequestHandler

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);
}
Also used : DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) RequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler) DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) StringValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.StringValue) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) Test(org.junit.Test)

Example 2 with RequestHandler

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);
}
Also used : DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) RequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler) DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) DefaultRange(io.adminshell.aas.v3.model.impl.DefaultRange) Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultSubmodelElementCollection(io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) DefaultSubmodelElementCollection(io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) Test(org.junit.Test)

Example 3 with RequestHandler

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());
}
Also used : ScanResult(io.github.classgraph.ScanResult) ClassGraph(io.github.classgraph.ClassGraph) Request(de.fraunhofer.iosb.ilt.faaast.service.model.api.Request) InvocationTargetException(java.lang.reflect.InvocationTargetException) RequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler)

Example 4 with RequestHandler

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);
}
Also used : DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) RequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler) DeleteSubmodelByIdRequestHandler(de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) StringValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.StringValue) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) Test(org.junit.Test)

Aggregations

RequestHandler (de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestHandler)4 DeleteSubmodelByIdRequestHandler (de.fraunhofer.iosb.ilt.faaast.service.request.handler.DeleteSubmodelByIdRequestHandler)3 DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)3 Test (org.junit.Test)3 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)2 StringValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.StringValue)2 AssetValueProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider)1 Request (de.fraunhofer.iosb.ilt.faaast.service.model.api.Request)1 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)1 Reference (io.adminshell.aas.v3.model.Reference)1 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)1 SubmodelElementCollection (io.adminshell.aas.v3.model.SubmodelElementCollection)1 DefaultRange (io.adminshell.aas.v3.model.impl.DefaultRange)1 DefaultSubmodelElementCollection (io.adminshell.aas.v3.model.impl.DefaultSubmodelElementCollection)1 ClassGraph (io.github.classgraph.ClassGraph)1 ScanResult (io.github.classgraph.ScanResult)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1