Search in sources :

Example 1 with MockField

use of org.apache.plc4x.java.mock.field.MockField in project plc4x by apache.

the class PlcConnectionAdapterTest method testNewPlcWriteRequest.

/*
     * Verify the adapter yields the appropriate PlcWriteRequest for each type and that it works.
     */
@Test
@Category(FastTests.class)
public void testNewPlcWriteRequest() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    /*{
            TypeSafePlcWriteRequest<Boolean> request = PlcConnectionAdapter.newPlcWriteRequest(address, true);
            PlcWriteRequestItem<Boolean> requestItem = request.getCheckedRequestItems().get(0);
            Class<Boolean> dataType = requestItem.getDatatype();
            assertThat(dataType, typeCompatibleWith(Boolean.class));
            assertThat(address, sameInstance(requestItem.getField()));
            checkWrite(connection, request, true);
        }
        {
            TypeSafePlcWriteRequest<Byte> request = PlcConnectionAdapter.newPlcWriteRequest(address, (byte) 0x113);
            PlcWriteRequestItem<Byte> requestItem = request.getCheckedRequestItems().get(0);
            Class<Byte> dataType = requestItem.getDatatype();
            assertThat(dataType, typeCompatibleWith(Byte.class));
            assertThat(address, sameInstance(requestItem.getField()));
            checkWrite(connection, request, (byte) 0x113);
        }
        {
            TypeSafePlcWriteRequest<Short> request = PlcConnectionAdapter.newPlcWriteRequest(address, (short) 113);
            PlcWriteRequestItem<Short> requestItem = request.getCheckedRequestItems().get(0);
            Class<Short> dataType = requestItem.getDatatype();
            assertThat(dataType, typeCompatibleWith(Short.class));
            assertThat(address, sameInstance(requestItem.getField()));
            checkWrite(connection, request, (short) 113);
        }
        {
            TypeSafePlcWriteRequest<Integer> request = PlcConnectionAdapter.newPlcWriteRequest(address, 1033);
            PlcWriteRequestItem<Integer> requestItem = request.getCheckedRequestItems().get(0);
            Class<Integer> dataType = requestItem.getDatatype();
            assertThat(dataType, typeCompatibleWith(Integer.class));
            assertThat(address, sameInstance(requestItem.getField()));
            checkWrite(connection, request, 1033);
        }
        {
            TypeSafePlcWriteRequest<Float> request = PlcConnectionAdapter.newPlcWriteRequest(address, 1043.5f);
            PlcWriteRequestItem<Float> requestItem = request.getCheckedRequestItems().get(0);
            Class<Float> dataType = requestItem.getDatatype();
            assertThat(dataType, typeCompatibleWith(Float.class));
            assertThat(address, sameInstance(requestItem.getField()));
            checkWrite(connection, request, 1043.5f);
        }
        {
            TypeSafePlcWriteRequest<String> request = PlcConnectionAdapter.newPlcWriteRequest(address, "A written value");
            PlcWriteRequestItem<String> requestItem = request.getCheckedRequestItems().get(0);
            Class<String> dataType = requestItem.getDatatype();
            assertThat(dataType, typeCompatibleWith(String.class));
            assertThat(address, sameInstance(requestItem.getField()));
            checkWrite(connection, request, "A written value");
        }
        {
            Calendar calValue = Calendar.getInstance();
            TypeSafePlcWriteRequest<Calendar> request = PlcConnectionAdapter.newPlcWriteRequest(address, calValue);
            PlcWriteRequestItem<Calendar> requestItem = request.getCheckedRequestItems().get(0);
            Class<Calendar> dataType = requestItem.getDatatype();
            assertThat(dataType, typeCompatibleWith(Calendar.class));
            assertThat(address, sameInstance(requestItem.getField()));
            checkWrite(connection, request, calValue);
        }*/
    adapter.close();
}
Also used : MockField(org.apache.plc4x.java.mock.field.MockField) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection)

Example 2 with MockField

use of org.apache.plc4x.java.mock.field.MockField in project plc4x by apache.

the class PlcConnectionAdapterTest method testNewConsumer1Neg.

/*
     * test PlcConnectionAdapter.newJsonConsumer(address) with write exception
     */
@Test
@Category(FastTests.class)
public void testNewConsumer1Neg() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    Consumer<?> consumer;
    /*consumer = adapter.newConsumer(String.class, addressStr);
        checkConsumer(2, connection, address, consumer, "one", "two", "three");*/
    adapter.close();
}
Also used : MockField(org.apache.plc4x.java.mock.field.MockField) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection)

Example 3 with MockField

use of org.apache.plc4x.java.mock.field.MockField in project plc4x by apache.

the class PlcConnectionAdapterTest method testNewConsumer1.

/*
     * test PlcConnectionAdapter.newJsonConsumer(address)
     */
@Test
@Category(FastTests.class)
public void testNewConsumer1() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    Consumer<?> consumer;
    /*consumer = adapter.newConsumer(Boolean.class, addressStr);
        assertThat(consumer, not(sameInstance(adapter.newConsumer(Boolean.class, addressStr))));
        checkConsumer(connection, address, consumer, true, false);

        consumer = adapter.newConsumer(Byte.class, addressStr);
        checkConsumer(connection, address, consumer, (byte) 0x1, (byte) 0x2, (byte) 0x3);

        consumer = adapter.newConsumer(Short.class, addressStr);
        checkConsumer(connection, address, consumer, (short) 1, (short) 2, (short) 3);

        consumer = adapter.newConsumer(Integer.class, addressStr);
        checkConsumer(connection, address, consumer, 1000, 1001, 1002);

        consumer = adapter.newConsumer(Float.class, addressStr);
        checkConsumer(connection, address, consumer, 1000.5f, 1001.5f, 1002.5f);

        consumer = adapter.newConsumer(String.class, addressStr);
        checkConsumer(connection, address, consumer, "one", "two", "three");*/
    adapter.close();
}
Also used : MockField(org.apache.plc4x.java.mock.field.MockField) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection)

Example 4 with MockField

use of org.apache.plc4x.java.mock.field.MockField in project plc4x by apache.

the class PlcFunctionsTest method testSupplier.

/*
     * test PlcConnectionAdapter.newSupplier
     */
@SuppressWarnings("unchecked")
@Test
@Category(FastTests.class)
public void testSupplier() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    Supplier<?> supplier;
    supplier = PlcFunctions.booleanSupplier(adapter, addressStr);
    PlcConnectionAdapterTest.checkSupplier(connection, address, supplier, true, false);
    supplier = PlcFunctions.byteSupplier(adapter, addressStr);
    PlcConnectionAdapterTest.checkSupplier(connection, address, supplier, (byte) 0x1, (byte) 0x2, (byte) 0x3);
    supplier = PlcFunctions.shortSupplier(adapter, addressStr);
    PlcConnectionAdapterTest.checkSupplier(connection, address, supplier, (short) 1, (short) 2, (short) 3);
    supplier = PlcFunctions.integerSupplier(adapter, addressStr);
    PlcConnectionAdapterTest.checkSupplier(connection, address, supplier, 1000, 1001, 1002);
    supplier = PlcFunctions.floatSupplier(adapter, addressStr);
    PlcConnectionAdapterTest.checkSupplier(connection, address, supplier, 1000.5f, 1001.5f, 1002.5f);
    supplier = PlcFunctions.stringSupplier(adapter, addressStr);
    PlcConnectionAdapterTest.checkSupplier(connection, address, supplier, "one", "two", "three");
    adapter.close();
}
Also used : MockField(org.apache.plc4x.java.mock.field.MockField) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 5 with MockField

use of org.apache.plc4x.java.mock.field.MockField in project plc4x by apache.

the class PlcFunctionsTest method testNewConsumer1.

/*
     * test PlcConnectionAdapter.newJsonConsumer(address)
     */
@Test
@Category(FastTests.class)
public void testNewConsumer1() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    Consumer<?> consumer;
    consumer = PlcFunctions.booleanConsumer(adapter, addressStr);
    PlcConnectionAdapterTest.checkConsumer(connection, address, consumer, true, false);
    consumer = PlcFunctions.byteConsumer(adapter, addressStr);
    PlcConnectionAdapterTest.checkConsumer(connection, address, consumer, (byte) 0x1, (byte) 0x2, (byte) 0x3);
    consumer = PlcFunctions.shortConsumer(adapter, addressStr);
    PlcConnectionAdapterTest.checkConsumer(connection, address, consumer, (short) 1, (short) 2, (short) 3);
    consumer = PlcFunctions.integerConsumer(adapter, addressStr);
    PlcConnectionAdapterTest.checkConsumer(connection, address, consumer, 1000, 1001, 1002);
    consumer = PlcFunctions.floatConsumer(adapter, addressStr);
    PlcConnectionAdapterTest.checkConsumer(connection, address, consumer, 1000.5f, 1001.5f, 1002.5f);
    consumer = PlcFunctions.stringConsumer(adapter, addressStr);
    PlcConnectionAdapterTest.checkConsumer(connection, address, consumer, "one", "two", "three");
    adapter.close();
}
Also used : MockField(org.apache.plc4x.java.mock.field.MockField) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

MockConnection (org.apache.plc4x.java.mock.connection.MockConnection)11 MockField (org.apache.plc4x.java.mock.field.MockField)11 JsonObject (com.google.gson.JsonObject)3 Consumer (org.apache.edgent.function.Consumer)3 Function (org.apache.edgent.function.Function)3 Supplier (org.apache.edgent.function.Supplier)3 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)3 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)3 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)3 FastTests (org.apache.plc4x.test.FastTests)3 Test (org.junit.Test)3 Category (org.junit.experimental.categories.Category)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)2 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)2 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)2 PlcField (org.apache.plc4x.java.api.model.PlcField)2 Matchers.is (org.hamcrest.Matchers.is)2 IsEqual.equalTo (org.hamcrest.core.IsEqual.equalTo)2