Search in sources :

Example 21 with Configuration

use of com.robo4j.configuration.Configuration in project robo4j by Robo4J.

the class HttpServerUnitTests method httpServerUnitNoCodecsPackageTest.

@Test
void httpServerUnitNoCodecsPackageTest() throws Exception {
    Throwable exception = assertThrows(RoboBuilderException.class, () -> {
        RoboBuilder builder = new RoboBuilder();
        Configuration config = new ConfigurationBuilder().addInteger(PROPERTY_SOCKET_PORT, PORT).build();
        builder.add(HttpServerUnit.class, config, ID_HTTP_SERVER);
        RoboContext system = builder.build();
        system.start();
        System.out.println("system: State after start:");
        System.out.println(SystemUtil.printStateReport(system));
        RoboReference<HttpServerUnit> systemReference = system.getReference(ID_HTTP_SERVER);
        system.shutdown();
        System.out.println("system: State after shutdown:");
        System.out.println(SystemUtil.printStateReport(system));
        assertEquals(LifecycleState.SHUTDOWN, systemReference.getState());
    });
    assertEquals("Error initializing RoboUnit", exception.getMessage());
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Example 22 with Configuration

use of com.robo4j.configuration.Configuration in project robo4j by Robo4J.

the class MessageServerTest method testClientServerMessagePassing.

@Test
void testClientServerMessagePassing() throws Exception {
    final List<String> messages = new ArrayList<>();
    final CountDownLatch messageLatch = new CountDownLatch(3);
    Configuration serverConfig = new ConfigurationBuilder().addString(PROPERTY_SERVER_NAME, "Server Name").addString(MessageServer.KEY_HOST_NAME, "localhost").build();
    MessageServer server = new MessageServer((uuid, id, message) -> {
        System.out.println("Got uuid: " + uuid + " id:" + id + " message:" + message);
        messages.add(String.valueOf(message));
        messageLatch.countDown();
    }, serverConfig);
    Thread t = new Thread(() -> {
        try {
            server.start();
        } catch (IOException e) {
            exception = e;
            fail(e.getMessage());
        }
    }, "Server Listener");
    t.setDaemon(true);
    t.start();
    for (int i = 0; i < 10; i++) {
        if (server.getListeningPort() == 0) {
            Thread.sleep(250);
        } else {
            break;
        }
    }
    Configuration clientConfig = ConfigurationFactory.createEmptyConfiguration();
    MessageClient client = new MessageClient(server.getListeningURI(), CONST_MYUUID, clientConfig);
    if (exception != null) {
        throw exception;
    }
    List<String> testMessage = getOrderedTestMessage("My First Little Message!", "My Second Little Message!", "My Third Little Message!");
    client.connect();
    for (String message : testMessage) {
        client.sendMessage("test", message);
    }
    messageLatch.await(2, TimeUnit.SECONDS);
    assertEquals(testMessage.size(), messages.size());
    assertArrayEquals(testMessage.toArray(), messages.toArray());
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 23 with Configuration

use of com.robo4j.configuration.Configuration in project robo4j by Robo4J.

the class SimpleTankUnitTests method simpleTankUnitMockTest.

@Test
void simpleTankUnitMockTest() throws Exception {
    RoboBuilder builder = new RoboBuilder();
    Configuration config = ConfigurationFactory.createEmptyConfiguration();
    builder.add(SimpleTankUnitMock.class, config, "tank");
    DefaultAttributeDescriptor<Boolean> descriptor = DefaultAttributeDescriptor.create(Boolean.class, "getStatus");
    RoboContext context = builder.build();
    RoboReference<LegoPlatformMessage> tank = context.getReference("tank");
    tank.sendMessage(new LegoPlatformMessage("right"));
    tank.sendMessage(new LegoPlatformMessage("left"));
    tank.sendMessage(new LegoPlatformMessage("move"));
    tank.sendMessage(new LegoPlatformMessage("back"));
    tank.sendMessage(new LegoPlatformMessage("stop"));
    assertTrue(tank.getAttribute(descriptor).get());
    context.shutdown();
}
Also used : Configuration(com.robo4j.configuration.Configuration) LegoPlatformMessage(com.robo4j.units.lego.platform.LegoPlatformMessage) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) Test(org.junit.jupiter.api.Test)

Aggregations

Configuration (com.robo4j.configuration.Configuration)23 ConfigurationBuilder (com.robo4j.configuration.ConfigurationBuilder)22 RoboBuilder (com.robo4j.RoboBuilder)18 RoboContext (com.robo4j.RoboContext)10 Test (org.junit.jupiter.api.Test)8 HttpPathConfigJsonBuilder (com.robo4j.socket.http.util.HttpPathConfigJsonBuilder)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 StringConsumer (com.robo4j.StringConsumer)1 LegoPlatformMessage (com.robo4j.units.lego.platform.LegoPlatformMessage)1 InputStream (java.io.InputStream)1 Disabled (org.junit.jupiter.api.Disabled)1