Search in sources :

Example 1 with Function

use of org.apache.edgent.function.Function in project plc4x by apache.

the class PlcConnectionAdapterTest method testNewConsumer2.

/*
     * test PlcConnectionAdapter.newJsonConsumer(addressFn, valueFn)
     */
@Test
@Category(FastTests.class)
public void testNewConsumer2() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    Consumer<JsonObject> consumer;
    Function<JsonObject, String> addressFn = t -> t.get("address").getAsString();
    /*consumer = adapter.newJsonConsumer(Boolean.class, addressFn, t -> t.get("value").getAsBoolean());
        checkConsumerJson(connection, address, consumer, true, false);

        consumer = adapter.newJsonConsumer(Byte.class, addressFn, t -> t.get("value").getAsByte());
        checkConsumerJson(connection, address, consumer, (byte) 0x1, (byte) 0x2, (byte) 0x3);

        consumer = adapter.newJsonConsumer(Short.class, addressFn, t -> t.get("value").getAsShort());
        checkConsumerJson(connection, address, consumer, (short) 1, (short) 2, (short) 3);

        consumer = adapter.newJsonConsumer(Integer.class, addressFn, t -> t.get("value").getAsInt());
        checkConsumerJson(connection, address, consumer, 1000, 1001, 1002);

        consumer = adapter.newJsonConsumer(Float.class, addressFn, t -> t.get("value").getAsFloat());
        checkConsumerJson(connection, address, consumer, 1000.5f, 1001.5f, 1002.5f);

        consumer = adapter.newJsonConsumer(String.class, addressFn, t -> t.get("value").getAsString());
        checkConsumerJson(connection, address, consumer, "one", "two", "three");*/
    adapter.close();
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) JsonObject(com.google.gson.JsonObject) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) MockField(org.apache.plc4x.java.mock.field.MockField) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Function(org.apache.edgent.function.Function) FastTests(org.apache.plc4x.test.FastTests) PlcField(org.apache.plc4x.java.api.model.PlcField) Supplier(org.apache.edgent.function.Supplier) ExecutionException(java.util.concurrent.ExecutionException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) IsNull.notNullValue(org.hamcrest.core.IsNull.notNullValue) IsSame.sameInstance(org.hamcrest.core.IsSame.sameInstance) Consumer(org.apache.edgent.function.Consumer) Matchers.is(org.hamcrest.Matchers.is) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) IsInstanceOf(org.hamcrest.core.IsInstanceOf) IsNot.not(org.hamcrest.core.IsNot.not) MockField(org.apache.plc4x.java.mock.field.MockField) JsonObject(com.google.gson.JsonObject) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection)

Example 2 with Function

use of org.apache.edgent.function.Function in project plc4x by apache.

the class PlcConnectionAdapterTest method testNewConsumer2Neg.

/*
     * test PlcConnectionAdapter.newJsonConsumer(addressFn, valueFn) with write failure
     */
@Test
@Category(FastTests.class)
public void testNewConsumer2Neg() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    Consumer<JsonObject> consumer;
    Function<JsonObject, String> addressFn = t -> t.get("address").getAsString();
    /*consumer = adapter.newJsonConsumer(String.class, addressFn, t -> t.get("value").getAsString());
        checkConsumerJson(2, connection, address, consumer, "one", "two", "three");*/
    adapter.close();
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) JsonObject(com.google.gson.JsonObject) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) MockField(org.apache.plc4x.java.mock.field.MockField) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Function(org.apache.edgent.function.Function) FastTests(org.apache.plc4x.test.FastTests) PlcField(org.apache.plc4x.java.api.model.PlcField) Supplier(org.apache.edgent.function.Supplier) ExecutionException(java.util.concurrent.ExecutionException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) IsNull.notNullValue(org.hamcrest.core.IsNull.notNullValue) IsSame.sameInstance(org.hamcrest.core.IsSame.sameInstance) Consumer(org.apache.edgent.function.Consumer) Matchers.is(org.hamcrest.Matchers.is) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) IsInstanceOf(org.hamcrest.core.IsInstanceOf) IsNot.not(org.hamcrest.core.IsNot.not) MockField(org.apache.plc4x.java.mock.field.MockField) JsonObject(com.google.gson.JsonObject) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection)

Example 3 with Function

use of org.apache.edgent.function.Function in project plc4x by apache.

the class PlcFunctionsTest method testNewConsumer2.

/*
     * test PlcConnectionAdapter.newJsonConsumer(addressFn, valueFn)
     */
@Test
@Category(FastTests.class)
public void testNewConsumer2() throws Exception {
    String addressStr = "MyReadWriteAddress/0";
    MockField address = new MockField(addressStr);
    PlcConnectionAdapter adapter = new PlcConnectionAdapter(getMockConnection());
    MockConnection connection = (MockConnection) adapter.getConnection();
    Consumer<JsonObject> consumer;
    Function<JsonObject, String> addressFn = t -> t.get("address").getAsString();
    consumer = PlcFunctions.booleanConsumer(adapter, addressFn, t -> t.get("value").getAsBoolean());
    PlcConnectionAdapterTest.checkConsumerJson(connection, address, consumer, true, false);
    consumer = PlcFunctions.byteConsumer(adapter, addressFn, t -> t.get("value").getAsByte());
    PlcConnectionAdapterTest.checkConsumerJson(connection, address, consumer, (byte) 0x1, (byte) 0x2, (byte) 0x3);
    consumer = PlcFunctions.shortConsumer(adapter, addressFn, t -> t.get("value").getAsShort());
    PlcConnectionAdapterTest.checkConsumerJson(connection, address, consumer, (short) 1, (short) 2, (short) 3);
    consumer = PlcFunctions.integerConsumer(adapter, addressFn, t -> t.get("value").getAsInt());
    PlcConnectionAdapterTest.checkConsumerJson(connection, address, consumer, 1000, 1001, 1002);
    consumer = PlcFunctions.floatConsumer(adapter, addressFn, t -> t.get("value").getAsFloat());
    PlcConnectionAdapterTest.checkConsumerJson(connection, address, consumer, 1000.5f, 1001.5f, 1002.5f);
    consumer = PlcFunctions.stringConsumer(adapter, addressFn, t -> t.get("value").getAsString());
    PlcConnectionAdapterTest.checkConsumerJson(connection, address, consumer, "one", "two", "three");
    adapter.close();
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) JsonObject(com.google.gson.JsonObject) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) MockField(org.apache.plc4x.java.mock.field.MockField) Consumer(org.apache.edgent.function.Consumer) Test(org.junit.Test) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) Function(org.apache.edgent.function.Function) Category(org.junit.experimental.categories.Category) FastTests(org.apache.plc4x.test.FastTests) Supplier(org.apache.edgent.function.Supplier) MockField(org.apache.plc4x.java.mock.field.MockField) JsonObject(com.google.gson.JsonObject) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

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 MockConnection (org.apache.plc4x.java.mock.connection.MockConnection)3 MockField (org.apache.plc4x.java.mock.field.MockField)3 FastTests (org.apache.plc4x.test.FastTests)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)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 IsInstanceOf (org.hamcrest.core.IsInstanceOf)2 IsNot.not (org.hamcrest.core.IsNot.not)2