Search in sources :

Example 1 with PlcReader

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);
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) org.apache.plc4x.java.api.value(org.apache.plc4x.java.api.value) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) PlcConnection(org.apache.plc4x.java.api.PlcConnection) LocalDateTime(java.time.LocalDateTime) WithAssertions(org.assertj.core.api.WithAssertions) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcFieldHandler(org.apache.plc4x.java.spi.connection.PlcFieldHandler) PlcInvalidFieldException(org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException) PlcDINT(org.apache.plc4x.java.spi.values.PlcDINT) BigDecimal(java.math.BigDecimal) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) Map(java.util.Map) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) LocalTime(java.time.LocalTime) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) BigInteger(java.math.BigInteger) PlcLINT(org.apache.plc4x.java.spi.values.PlcLINT) PlcConnectionMetadata(org.apache.plc4x.java.api.metadata.PlcConnectionMetadata) PlcReader(org.apache.plc4x.java.spi.messages.PlcReader) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) IEC61131ValueHandler(org.apache.plc4x.java.spi.values.IEC61131ValueHandler) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) Mockito(org.mockito.Mockito) PlcWriter(org.apache.plc4x.java.spi.messages.PlcWriter) LocalDate(java.time.LocalDate) Assertions(org.junit.jupiter.api.Assertions) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcWriter(org.apache.plc4x.java.spi.messages.PlcWriter) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) PlcConnection(org.apache.plc4x.java.api.PlcConnection) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) PlcConnectionMetadata(org.apache.plc4x.java.api.metadata.PlcConnectionMetadata) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) HashMap(java.util.HashMap) Map(java.util.Map) PlcReader(org.apache.plc4x.java.spi.messages.PlcReader)

Aggregations

BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 FieldUtils (org.apache.commons.lang3.reflect.FieldUtils)1 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)1 PlcInvalidFieldException (org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException)1 PlcConnectionMetadata (org.apache.plc4x.java.api.metadata.PlcConnectionMetadata)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 org.apache.plc4x.java.api.value (org.apache.plc4x.java.api.value)1