use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnectionTest method testWriteReadValue.
private void testWriteReadValue(String nodeId, PropertyValue expected) throws AssetConnectionException, InterruptedException {
Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
ServiceContext serviceContext = mock(ServiceContext.class);
doReturn(ElementValueTypeInfo.builder().type(expected.getClass()).datatype(expected.getValue().getDataType()).build()).when(serviceContext).getTypeInfo(reference);
OpcUaAssetConnection connection = new OpcUaAssetConnection(CoreConfig.builder().build(), OpcUaAssetConnectionConfig.builder().valueProvider(reference, OpcUaValueProviderConfig.builder().nodeId(nodeId).build()).host(serverUrl).build(), serviceContext);
connection.getValueProviders().get(reference).setValue(expected);
DataElementValue actual = connection.getValueProviders().get(reference).getValue();
connection.close();
Assert.assertEquals(expected, actual);
}
use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.
the class HttpEndpointTest method init.
@BeforeClass
public static void init() throws Exception {
try (ServerSocket serverSocket = new ServerSocket(0)) {
Assert.assertNotNull(serverSocket);
Assert.assertTrue(serverSocket.getLocalPort() > 0);
port = serverSocket.getLocalPort();
}
deserializer = new HttpJsonDeserializer();
HttpEndpointConfig endpointConfig = new HttpEndpointConfig();
endpointConfig.setPort(port);
serviceContext = mock(ServiceContext.class);
endpoint = new HttpEndpoint();
endpoint.init(CoreConfig.builder().build(), endpointConfig, serviceContext);
endpoint.start();
client = new HttpClient();
client.start();
}
use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.
the class RequestMappingManager method init.
private void init() {
try (ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages(getClass().getPackageName()).scan()) {
mappers = scanResult.getSubclasses(RequestMapper.class.getName()).filter(x -> !x.isAbstract() && !x.isInterface()).loadClasses(RequestMapper.class).stream().map(x -> {
try {
Constructor<RequestMapper> constructor = x.getConstructor(ServiceContext.class);
return constructor.newInstance(serviceContext);
} catch (NoSuchMethodException | SecurityException ex) {
logger.warn("request mapper implementation could not be loaded, " + "reason: missing constructor (implementation class: {}, required constructor signature: {}", x.getName());
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
logger.warn("request mapper implementation could not be loaded, " + "reason: calling constructor failed (implementation class: {}, constructor arguments: {}", x.getName());
}
logger.debug("unable to instantiate class {}", x.getName());
return null;
}).filter(x -> x != null).collect(Collectors.toList());
mappers.stream().filter(x -> x == null).collect(Collectors.toList());
}
}
use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnectionTest method testSubscribe.
private void testSubscribe(String nodeId, PropertyValue expected) throws AssetConnectionException, InterruptedException, ExecutionException, UaException {
Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
long interval = 1000;
ServiceContext serviceContext = mock(ServiceContext.class);
TypeInfo infoExample = ElementValueTypeInfo.builder().type(PropertyValue.class).datatype(expected.getValue().getDataType()).build();
doReturn(infoExample).when(serviceContext).getTypeInfo(reference);
OpcUaAssetConnection connection = new OpcUaAssetConnection();
connection.init(CoreConfig.builder().build(), OpcUaAssetConnectionConfig.builder().host(serverUrl).subscriptionProvider(reference, OpcUaSubscriptionProviderConfig.builder().nodeId(nodeId).interval(interval).build()).valueProvider(reference, OpcUaValueProviderConfig.builder().nodeId(nodeId).build()).build(), serviceContext);
final AtomicReference<DataElementValue> response = new AtomicReference<>();
CountDownLatch condition = new CountDownLatch(1);
connection.getSubscriptionProviders().get(reference).addNewDataListener(new NewDataListener() {
@Override
public void newDataReceived(DataElementValue data) {
response.set(data);
condition.countDown();
}
});
connection.getValueProviders().get(reference).setValue(expected);
long waitTime = 5 * interval;
TimeUnit waitTimeUnit = isDebugging() ? TimeUnit.SECONDS : TimeUnit.MILLISECONDS;
Assert.assertTrue(String.format("test failed because there was no response within defined time (%d %s)", waitTime, waitTimeUnit), condition.await(waitTime, waitTimeUnit));
Assert.assertEquals(expected, response.get());
}
use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.
the class OpcUaAssetConnectionTest method testInvokeOperation.
private void testInvokeOperation(String nodeId, boolean sync, Map<String, PropertyValue> input, Map<String, PropertyValue> inoutput, Map<String, PropertyValue> expectedInoutput, Map<String, PropertyValue> expectedOutput) throws AssetConnectionException, InterruptedException {
Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
OpcUaAssetConnectionConfig config = OpcUaAssetConnectionConfig.builder().host(serverUrl).operationProvider(reference, OpcUaOperationProviderConfig.builder().nodeId(nodeId).build()).build();
OperationVariable[] inputVariables = input == null ? new OperationVariable[0] : input.entrySet().stream().map(x -> {
Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
ElementValueMapper.setValue(property, x.getValue());
return new DefaultOperationVariable.Builder().value(property).build();
}).toArray(OperationVariable[]::new);
OperationVariable[] inoutputVariables = inoutput == null ? new OperationVariable[0] : inoutput.entrySet().stream().map(x -> {
Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
ElementValueMapper.setValue(property, x.getValue());
return new DefaultOperationVariable.Builder().value(property).build();
}).toArray(OperationVariable[]::new);
OperationVariable[] expectedInOut = expectedInoutput == null ? new OperationVariable[0] : expectedInoutput.entrySet().stream().map(x -> {
Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
ElementValueMapper.setValue(property, x.getValue());
return new DefaultOperationVariable.Builder().value(property).build();
}).toArray(OperationVariable[]::new);
OperationVariable[] expectedOut = expectedOutput == null ? new OperationVariable[0] : expectedOutput.entrySet().stream().map(x -> {
Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
ElementValueMapper.setValue(property, x.getValue());
return new DefaultOperationVariable.Builder().value(property).build();
}).toArray(OperationVariable[]::new);
ServiceContext serviceContext = mock(ServiceContext.class);
doReturn(expectedOut).when(serviceContext).getOperationOutputVariables(reference);
OpcUaAssetConnection connection = new OpcUaAssetConnection(CoreConfig.builder().build(), config, serviceContext);
OperationVariable[] actual;
if (sync) {
actual = connection.getOperationProviders().get(reference).invoke(inputVariables, inoutputVariables);
} else {
final AtomicReference<OperationVariable[]> operationResult = new AtomicReference<>();
final AtomicReference<OperationVariable[]> operationInout = new AtomicReference<>();
CountDownLatch condition = new CountDownLatch(1);
connection.getOperationProviders().get(reference).invokeAsync(inputVariables, inoutputVariables, (res, inout) -> {
operationResult.set(res);
operationInout.set(inout);
condition.countDown();
});
condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
actual = operationResult.get();
inoutputVariables = operationInout.get();
}
connection.close();
Assert.assertArrayEquals(expectedOut, actual);
Assert.assertArrayEquals(expectedInOut, inoutputVariables);
}
Aggregations