Skip to content

Commit

Permalink
[56] test correction
Browse files Browse the repository at this point in the history
  • Loading branch information
mirage22 committed Oct 3, 2024
1 parent 87d3a2e commit 748239f
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 160 deletions.
5 changes: 1 addition & 4 deletions robo4j-core/src/test/java/com/robo4j/RoboBuilderTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

/**
Expand Down Expand Up @@ -246,8 +247,4 @@ void testProgrammaticConfiguration() throws RoboBuilderException, InterruptedExc
system.shutdown();
}


<T, R> R getAttributeOrTimeout(RoboReference<T> roboReference, AttributeDescriptor<R> attributeDescriptor) throws InterruptedException, ExecutionException, TimeoutException {
return roboReference.getAttribute(attributeDescriptor).get(TIMEOUT, TimeUnit.MINUTES);
}
}
31 changes: 31 additions & 0 deletions robo4j-core/src/test/java/com/robo4j/RoboUnitTestUtils.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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 <T, R> R getAttributeOrTimeout(RoboReference<T> roboReference, AttributeDescriptor<R> attributeDescriptor) throws InterruptedException, ExecutionException, TimeoutException {
return roboReference.getAttribute(attributeDescriptor).get(TIMEOUT, TimeUnit.MINUTES);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,167 +34,167 @@
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)
* @author Miro Wengner (@miragemiko)
*/
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<Object> 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<String> 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<HttpDecoratedRequest> 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.
* <p>
* 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<Object> 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<String> 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<HttpDecoratedRequest> 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 <T, R> R getAttributeOrTimeout(RoboReference<T> roboReference, AttributeDescriptor<R> attributeDescriptor) throws InterruptedException, ExecutionException, TimeoutException {
return roboReference.getAttribute(attributeDescriptor).get(TIMEOUT, TimeUnit.MINUTES);
}

}

0 comments on commit 748239f

Please sign in to comment.