use of org.apache.plc4x.java.spi.messages.PlcReader in project plc4x by apache.
the class PlcEntityManagerComplexTest method getPlcEntityManager.
private PlcEntityManager getPlcEntityManager(final Map<String, PlcValue> responses) throws PlcConnectionException {
driverManager = Mockito.mock(PlcDriverManager.class);
PlcDriverManager mock = driverManager;
PlcConnection connection = Mockito.mock(PlcConnection.class);
when(mock.getConnection(ArgumentMatchers.anyString())).thenReturn(connection);
when(connection.getMetadata()).thenReturn(new PlcConnectionMetadata() {
@Override
public boolean canRead() {
return true;
}
@Override
public boolean canWrite() {
return true;
}
@Override
public boolean canSubscribe() {
return true;
}
});
PlcReader reader = readRequest -> {
Map<String, ResponseItem<PlcValue>> map = readRequest.getFieldNames().stream().collect(Collectors.toMap(Function.identity(), s -> new ResponseItem<>(PlcResponseCode.OK, Objects.requireNonNull(responses.get(s), s + " not found"))));
return CompletableFuture.completedFuture(new DefaultPlcReadResponse(readRequest, map));
};
when(connection.readRequestBuilder()).then(invocation -> new DefaultPlcReadRequest.Builder(reader, getFieldHandler()));
PlcWriter writer = writeRequest -> {
Map<String, PlcResponseCode> map = writeRequest.getFieldNames().stream().collect(Collectors.toMap(Function.identity(), s -> PlcResponseCode.OK));
return CompletableFuture.completedFuture(new DefaultPlcWriteResponse(writeRequest, map));
};
when(connection.writeRequestBuilder()).then(invocation -> new DefaultPlcWriteRequest.Builder(writer, getFieldHandler(), getValueHandler()));
return new PlcEntityManager(mock);
}
Aggregations