diff --git a/robo4j-core/src/test/java/com/robo4j/RoboBuilderTests.java b/robo4j-core/src/test/java/com/robo4j/RoboBuilderTests.java index 1e0434f6..920fa60f 100644 --- a/robo4j-core/src/test/java/com/robo4j/RoboBuilderTests.java +++ b/robo4j-core/src/test/java/com/robo4j/RoboBuilderTests.java @@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import static com.robo4j.RoboUnitTestUtils.getAttributeOrTimeout; import static org.junit.jupiter.api.Assertions.*; /** @@ -246,8 +247,4 @@ void testProgrammaticConfiguration() throws RoboBuilderException, InterruptedExc system.shutdown(); } - - R getAttributeOrTimeout(RoboReference roboReference, AttributeDescriptor attributeDescriptor) throws InterruptedException, ExecutionException, TimeoutException { - return roboReference.getAttribute(attributeDescriptor).get(TIMEOUT, TimeUnit.MINUTES); - } } diff --git a/robo4j-core/src/test/java/com/robo4j/RoboUnitTestUtils.java b/robo4j-core/src/test/java/com/robo4j/RoboUnitTestUtils.java new file mode 100644 index 00000000..4d325e14 --- /dev/null +++ b/robo4j-core/src/test/java/com/robo4j/RoboUnitTestUtils.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2014, 2024, Marcus Hirt, Miroslav Wengner + * + * Robo4J is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Robo4J is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Robo4J. If not, see . + */ + +package com.robo4j; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +final public class RoboUnitTestUtils { + private static final int TIMEOUT = 5; + + public static R getAttributeOrTimeout(RoboReference roboReference, AttributeDescriptor attributeDescriptor) throws InterruptedException, ExecutionException, TimeoutException { + return roboReference.getAttribute(attributeDescriptor).get(TIMEOUT, TimeUnit.MINUTES); + } + +} diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/RoboHttpDynamicTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/RoboHttpDynamicTests.java index 8efa3dfd..663e256c 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/RoboHttpDynamicTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/RoboHttpDynamicTests.java @@ -16,10 +16,7 @@ */ package com.robo4j.socket.http.test.units; -import com.robo4j.LifecycleState; -import com.robo4j.RoboBuilder; -import com.robo4j.RoboContext; -import com.robo4j.RoboReference; +import com.robo4j.*; import com.robo4j.configuration.Configuration; import com.robo4j.configuration.ConfigurationBuilder; import com.robo4j.socket.http.HttpMethod; @@ -37,17 +34,14 @@ import org.junit.jupiter.api.Test; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; -import static com.robo4j.socket.http.util.RoboHttpUtils.PROPERTY_HOST; -import static com.robo4j.socket.http.util.RoboHttpUtils.PROPERTY_SOCKET_PORT; -import static com.robo4j.socket.http.util.RoboHttpUtils.PROPERTY_TARGET; -import static com.robo4j.socket.http.util.RoboHttpUtils.PROPERTY_UNIT_PATHS_CONFIG; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static com.robo4j.socket.http.util.RoboHttpUtils.*; +import static org.junit.jupiter.api.Assertions.*; /** - * * Dynamic HttpUnit request/method configuration * * @author Marcus Hirt (@hirt) @@ -55,149 +49,152 @@ */ class RoboHttpDynamicTests { - private static final int TIMEOUT = 10; - private static final TimeUnit TIME_UNIT = TimeUnit.HOURS; - private static final String ID_HTTP_SERVER = "http"; - private static final int PORT = 8025; - private static final String ID_CLIENT_UNIT = "httpClient"; - private static final String ID_TARGET_UNIT = "controller"; - private static final int MESSAGES_NUMBER = 42; - private static final String HOST_SYSTEM = "localhost"; - static final String JSON_STRING = "{\"value\":\"stop\"}"; - private static final String DECORATED_PRODUCER = "decoratedProducer"; - - /** - * Motivation Client system is sending messages to the main system over HTTP - * Main System receives desired number of messages. - * - * Values are requested by Attributes - * - * @throws Exception - * exception - */ - @Test - void simpleHttpNonUnitTest() throws Exception { - - /* tested system configuration */ - RoboContext mainSystem = getServerRoboSystem(MESSAGES_NUMBER); - - /* system which is testing main system */ - RoboContext clientSystem = getClientRoboSystem(); - - System.out.println("Client system state after start:"); - System.out.println(SystemUtil.printStateReport(clientSystem)); - - System.out.println("Main system state after start:"); - System.out.println(SystemUtil.printStateReport(mainSystem)); - - /* client system sending a messages to the main system */ - RoboReference decoratedProducer = clientSystem.getReference(DECORATED_PRODUCER); - decoratedProducer.sendMessage(MESSAGES_NUMBER); - CountDownLatch countDownLatchDecoratedProducer = decoratedProducer - .getAttribute(SocketMessageDecoratedProducerUnit.DESCRIPTOR_MESSAGES_LATCH).get(); - countDownLatchDecoratedProducer.await(TIMEOUT, TIME_UNIT); - - final RoboReference stringConsumer = mainSystem.getReference(StringConsumer.NAME); - final CountDownLatch countDownLatch = stringConsumer.getAttribute(StringConsumer.DESCRIPTOR_MESSAGES_LATCH) - .get(); - countDownLatch.await(TIMEOUT, TIME_UNIT); - final int receivedMessages = stringConsumer.getAttribute(StringConsumer.DESCRIPTOR_MESSAGES_TOTAL).get(); - - clientSystem.shutdown(); - mainSystem.shutdown(); - - System.out.println("System is Down!"); - assertNotNull(mainSystem.getUnits()); - assertEquals(MESSAGES_NUMBER, receivedMessages, "wrong received messages"); - } - - /** - * testing ping external system - * - * @throws Exception - * exception - */ - @Disabled("intent to run manual") - @Test - void pingExternalSystem() throws Exception { - RoboBuilder pingSystemBuilder = getHttpClientRobotBuilder("127.0.0.1", 8080); - - pingSystemBuilder.add(StringConsumer.class, StringConsumer.NAME); - - RoboContext pingSystemContext = pingSystemBuilder.build(); - pingSystemContext.start(); - System.out.println("PingSystem state after start:"); - System.out.println(SystemUtil.printStateReport(pingSystemContext)); - - RoboReference httpClient = pingSystemContext.getReference(ID_CLIENT_UNIT); - - Thread.sleep(1000); - for (int i = 0; i < 1; i++) { - HttpRequestDenominator denominator = new HttpRequestDenominator(HttpMethod.GET, "/noparams", - HttpVersion.HTTP_1_1); - HttpDecoratedRequest request = new HttpDecoratedRequest(denominator); - request.addCallback(StringConsumer.NAME); - httpClient.sendMessage(request); - } - Thread.sleep(1000); - pingSystemContext.stop(); - System.out.println("PingSystem state after stop:"); - System.out.println(SystemUtil.printStateReport(pingSystemContext)); - - } - - // Private Methods - private RoboContext getServerRoboSystem(int totalMessageNumber) throws Exception { - /* tested system configuration */ - RoboBuilder builder = new RoboBuilder(); - - Configuration config = new ConfigurationBuilder().addInteger(PROPERTY_SOCKET_PORT, PORT) - .addString("packages", "com.robo4j.socket.http.test.units.config.codec").addString(PROPERTY_UNIT_PATHS_CONFIG, - HttpPathConfigJsonBuilder.Builder().addPath(ID_TARGET_UNIT, HttpMethod.POST).build()) - .build(); - builder.add(HttpServerUnit.class, config, ID_HTTP_SERVER); - - config = new ConfigurationBuilder().addString(PROPERTY_TARGET, StringConsumer.NAME).build(); - builder.add(HttpCommandTestController.class, config, ID_TARGET_UNIT); - - config = new ConfigurationBuilder().addInteger(StringConsumer.PROP_TOTAL_NUMBER_MESSAGES, totalMessageNumber) - .build(); - builder.add(StringConsumer.class, config, StringConsumer.NAME); - - RoboContext result = builder.build(); - assertNotNull(result.getUnits()); - assertEquals(3, result.getUnits().size()); - assertEquals(LifecycleState.INITIALIZED, result.getReference(ID_HTTP_SERVER).getState()); - assertEquals(LifecycleState.INITIALIZED, result.getState()); - - result.start(); - System.out.println(SystemUtil.printSocketEndPoint(result.getReference(ID_HTTP_SERVER), - result.getReference(ID_TARGET_UNIT))); - return result; - } - - private RoboBuilder getHttpClientRobotBuilder(String host, int port) throws Exception { - /* system which is testing main system */ - RoboBuilder result = new RoboBuilder(); - - Configuration config = new ConfigurationBuilder().addString(PROPERTY_HOST, host) - .addInteger(PROPERTY_SOCKET_PORT, port).build(); - result.add(HttpClientUnit.class, config, ID_CLIENT_UNIT); - return result; - } - - private RoboContext getClientRoboSystem() throws Exception { - /* system which is testing main system */ - RoboBuilder builder = getHttpClientRobotBuilder(HOST_SYSTEM, PORT); - - Configuration config = new ConfigurationBuilder().addString(PROPERTY_TARGET, ID_CLIENT_UNIT) - .addString(PROPERTY_UNIT_PATHS_CONFIG, - "[{\"roboUnit\":\"" + ID_TARGET_UNIT + "\",\"method\":\"POST\"}]") - .addString("message", JSON_STRING).build(); - builder.add(SocketMessageDecoratedProducerUnit.class, config, DECORATED_PRODUCER); - - RoboContext result = builder.build(); - result.start(); - return result; - } + private static final int TIMEOUT = 20; + private static final TimeUnit TIME_UNIT = TimeUnit.HOURS; + private static final String ID_HTTP_SERVER = "http"; + private static final int PORT = 8025; + private static final String ID_CLIENT_UNIT = "httpClient"; + private static final String ID_TARGET_UNIT = "controller"; + private static final int MESSAGES_NUMBER = 42; + private static final String HOST_SYSTEM = "localhost"; + static final String JSON_STRING = "{\"value\":\"stop\"}"; + private static final String DECORATED_PRODUCER = "decoratedProducer"; + + /** + * Motivation Client system is sending messages to the main system over HTTP + * Main System receives desired number of messages. + *

+ * Values are requested by Attributes + * + * @throws Exception exception + */ + @Test + void simpleHttpNonUnitTest() throws Exception { + + /* tested system configuration */ + RoboContext mainSystem = getServerRoboSystem(MESSAGES_NUMBER); + + /* system which is testing main system */ + RoboContext clientSystem = getClientRoboSystem(); + + System.out.println("Client system state after start:"); + System.out.println(SystemUtil.printStateReport(clientSystem)); + + System.out.println("Main system state after start:"); + System.out.println(SystemUtil.printStateReport(mainSystem)); + + /* client system sending a messages to the main system */ + RoboReference decoratedProducer = clientSystem.getReference(DECORATED_PRODUCER); + decoratedProducer.sendMessage(MESSAGES_NUMBER); + CountDownLatch countDownLatchDecoratedProducer = getAttributeOrTimeout(decoratedProducer, SocketMessageDecoratedProducerUnit.DESCRIPTOR_MESSAGES_LATCH); + var messagesProduced = countDownLatchDecoratedProducer.await(TIMEOUT, TIME_UNIT); + + final RoboReference stringConsumer = mainSystem.getReference(StringConsumer.NAME); + final CountDownLatch countDownLatch = getAttributeOrTimeout(stringConsumer, StringConsumer.DESCRIPTOR_MESSAGES_LATCH); + var messagesReceived = countDownLatch.await(TIMEOUT, TIME_UNIT); + final int receivedMessages = getAttributeOrTimeout(stringConsumer, StringConsumer.DESCRIPTOR_MESSAGES_TOTAL); + + clientSystem.shutdown(); + mainSystem.shutdown(); + + System.out.println("System is Down!"); + assertTrue(messagesProduced); + assertTrue(messagesReceived); + assertNotNull(mainSystem.getUnits()); + assertEquals(MESSAGES_NUMBER, receivedMessages, "wrong received messages"); + } + + /** + * testing ping external system + * + * @throws Exception exception + */ + @Disabled("intent to run manual") + @Test + void pingExternalSystem() throws Exception { + RoboBuilder pingSystemBuilder = getHttpClientRobotBuilder("127.0.0.1", 8080); + + pingSystemBuilder.add(StringConsumer.class, StringConsumer.NAME); + + RoboContext pingSystemContext = pingSystemBuilder.build(); + pingSystemContext.start(); + System.out.println("PingSystem state after start:"); + System.out.println(SystemUtil.printStateReport(pingSystemContext)); + + RoboReference httpClient = pingSystemContext.getReference(ID_CLIENT_UNIT); + + Thread.sleep(1000); + for (int i = 0; i < 1; i++) { + HttpRequestDenominator denominator = new HttpRequestDenominator(HttpMethod.GET, "/noparams", + HttpVersion.HTTP_1_1); + HttpDecoratedRequest request = new HttpDecoratedRequest(denominator); + request.addCallback(StringConsumer.NAME); + httpClient.sendMessage(request); + } + Thread.sleep(1000); + pingSystemContext.stop(); + System.out.println("PingSystem state after stop:"); + System.out.println(SystemUtil.printStateReport(pingSystemContext)); + + } + + // Private Methods + private RoboContext getServerRoboSystem(int totalMessageNumber) throws Exception { + /* tested system configuration */ + RoboBuilder builder = new RoboBuilder(); + + Configuration config = new ConfigurationBuilder().addInteger(PROPERTY_SOCKET_PORT, PORT) + .addString("packages", "com.robo4j.socket.http.test.units.config.codec").addString(PROPERTY_UNIT_PATHS_CONFIG, + HttpPathConfigJsonBuilder.Builder().addPath(ID_TARGET_UNIT, HttpMethod.POST).build()) + .build(); + builder.add(HttpServerUnit.class, config, ID_HTTP_SERVER); + + config = new ConfigurationBuilder().addString(PROPERTY_TARGET, StringConsumer.NAME).build(); + builder.add(HttpCommandTestController.class, config, ID_TARGET_UNIT); + + config = new ConfigurationBuilder().addInteger(StringConsumer.PROP_TOTAL_NUMBER_MESSAGES, totalMessageNumber) + .build(); + builder.add(StringConsumer.class, config, StringConsumer.NAME); + + RoboContext result = builder.build(); + assertNotNull(result.getUnits()); + assertEquals(3, result.getUnits().size()); + assertEquals(LifecycleState.INITIALIZED, result.getReference(ID_HTTP_SERVER).getState()); + assertEquals(LifecycleState.INITIALIZED, result.getState()); + + result.start(); + System.out.println(SystemUtil.printSocketEndPoint(result.getReference(ID_HTTP_SERVER), + result.getReference(ID_TARGET_UNIT))); + return result; + } + + private RoboBuilder getHttpClientRobotBuilder(String host, int port) throws Exception { + /* system which is testing main system */ + RoboBuilder result = new RoboBuilder(); + + Configuration config = new ConfigurationBuilder().addString(PROPERTY_HOST, host) + .addInteger(PROPERTY_SOCKET_PORT, port).build(); + result.add(HttpClientUnit.class, config, ID_CLIENT_UNIT); + return result; + } + + private RoboContext getClientRoboSystem() throws Exception { + /* system which is testing main system */ + RoboBuilder builder = getHttpClientRobotBuilder(HOST_SYSTEM, PORT); + + Configuration config = new ConfigurationBuilder().addString(PROPERTY_TARGET, ID_CLIENT_UNIT) + .addString(PROPERTY_UNIT_PATHS_CONFIG, + "[{\"roboUnit\":\"" + ID_TARGET_UNIT + "\",\"method\":\"POST\"}]") + .addString("message", JSON_STRING).build(); + builder.add(SocketMessageDecoratedProducerUnit.class, config, DECORATED_PRODUCER); + + RoboContext result = builder.build(); + result.start(); + return result; + } + + private static R getAttributeOrTimeout(RoboReference roboReference, AttributeDescriptor attributeDescriptor) throws InterruptedException, ExecutionException, TimeoutException { + return roboReference.getAttribute(attributeDescriptor).get(TIMEOUT, TimeUnit.MINUTES); + } + }