Skip to content

Commit

Permalink
[69] robo4j-socket-http next chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
mirage22 committed Oct 8, 2024
1 parent 6f7d407 commit 7928ac7
Show file tree
Hide file tree
Showing 14 changed files with 673 additions and 675 deletions.
17 changes: 9 additions & 8 deletions robo4j-hw-rpi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@
<groupId>com.robo4j</groupId>
<artifactId>robo4j-math</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<!-- Pi4j -->
<dependency>
<groupId>com.pi4j</groupId>
Expand All @@ -60,8 +52,17 @@
<artifactId>pi4j-plugin-linuxfs</artifactId>
<version>${pi4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<!-- TEST -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
10 changes: 9 additions & 1 deletion robo4j-socket-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@
<groupId>com.robo4j</groupId>
<artifactId>robo4j-core</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- TEST -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,40 +170,20 @@ void nestedObjectToJson() {
@Test
void nestedJsonToObject() {

TestPerson testPerson2 = new TestPerson();
testPerson2.setName("name2");
testPerson2.setValue(5);

TestPerson testPerson111 = new TestPerson();
testPerson111.setName("name111");
testPerson111.setValue(42);

TestPerson testPerson11 = new TestPerson();
testPerson11.setName("name11");
testPerson11.setValue(0);
testPerson11.setChild(testPerson111);

TestPerson testPerson1 = new TestPerson();
testPerson1.setName("name1");
testPerson1.setValue(22);
testPerson1.setChild(testPerson11);

Map<String, TestPerson> personMap = new LinkedHashMap<>();
personMap.put("person1", testPerson1);
personMap.put("person2", testPerson2);
Map<String, TestPerson> personMap = getStringTestPersonMap();

long start = System.nanoTime();
NSBWithSimpleCollectionsTypesMessage obj1 = collectionsTypesMessageCodec.decode(testJson);
TimeUtils.printTimeDiffNano("decodeFromJson", start);

LOGGER.info("Obj: {}", obj1);

assertEquals(Integer.valueOf(42), obj1.getNumber());
assertEquals("no message", obj1.getMessage());
assertTrue(!obj1.getActive());
assertFalse(obj1.getActive());
assertArrayEquals(new String[]{"one", "two"}, obj1.getArray());
assertTrue(obj1.getList().containsAll(Arrays.asList("text1", "text2")));
assertEquals(personMap, obj1.getPersonMap());

System.out.println("Obj: " + obj1);
}

@Test
Expand Down Expand Up @@ -247,4 +227,29 @@ private static void printJson(String resultJson) {
LOGGER.debug("resultJson:{}", resultJson);
}

private static Map<String, TestPerson> getStringTestPersonMap() {
TestPerson testPerson2 = new TestPerson();
testPerson2.setName("name2");
testPerson2.setValue(5);

TestPerson testPerson111 = new TestPerson();
testPerson111.setName("name111");
testPerson111.setValue(42);

TestPerson testPerson11 = new TestPerson();
testPerson11.setName("name11");
testPerson11.setValue(0);
testPerson11.setChild(testPerson111);

TestPerson testPerson1 = new TestPerson();
testPerson1.setName("name1");
testPerson1.setValue(22);
testPerson1.setChild(testPerson11);

Map<String, TestPerson> personMap = new LinkedHashMap<>();
personMap.put("person1", testPerson1);
personMap.put("person2", testPerson2);
return personMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
import com.robo4j.socket.http.util.JsonUtil;
import com.robo4j.util.StringConstants;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

/**
* test for Http Server Unit configuration
Expand All @@ -41,100 +37,96 @@
* @author Miroslav Wengner (@miragemiko)
*/
class HttpServerConfigTests {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpServerConfigTests.class);

@Test
void serverConfigurationNullTest() {
@Test
void serverConfigurationNullTest() {
Throwable exception = assertThrows(NullPointerException.class, () -> {
HttpPathMethodDTO serverUnitPathDTO = HttpPathUtils.readServerPathDTO(null);
assertNull(serverUnitPathDTO);
});

Throwable exception = assertThrows(NullPointerException.class, () -> {
HttpPathMethodDTO serverUnitPathDTO = HttpPathUtils.readServerPathDTO(null);
assertNull(serverUnitPathDTO);
});
assertNotNull(exception.getMessage());
}

assertNotNull(exception.getMessage());
}
@Test
void serverConfigurationEmptyTest() {
Throwable exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
HttpPathUtils.readServerPathDTO(StringConstants.EMPTY);
});

@Test
void serverConfigurationEmptyTest() {
Throwable exception = assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
HttpPathUtils.readServerPathDTO(StringConstants.EMPTY);
});
assertEquals("Index 0 out of bounds for length 0", exception.getMessage());
}

assertEquals("Index 0 out of bounds for length 0", exception.getMessage());
}
@Test
void serverConfigurationWithoutPropertiesDTOTest() {
var configurationJson = "{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\"}";
var serverUnitPathDTO = HttpPathUtils.readServerPathDTO(configurationJson);

@Test
void serverConfigurationWithoutPropertiesDTOTest() {
LOGGER.info("serverUnitPathDTO: {}", serverUnitPathDTO);
assertEquals("roboUnit1", serverUnitPathDTO.getRoboUnit());
assertEquals(HttpMethod.GET, serverUnitPathDTO.getMethod());
assertTrue(serverUnitPathDTO.getCallbacks().isEmpty());

String configurationJson = "{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\"}";
HttpPathMethodDTO serverUnitPathDTO = HttpPathUtils.readServerPathDTO(configurationJson);
}

System.out.println("serverUnitPathDTO: " + serverUnitPathDTO);
assertEquals("roboUnit1", serverUnitPathDTO.getRoboUnit());
assertEquals(HttpMethod.GET, serverUnitPathDTO.getMethod());
assertTrue(serverUnitPathDTO.getCallbacks().isEmpty());
@Test
void serverConfigurationWithPropertiesParsingDTOTest() {
var configurationJson = "{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\",\"callbacks\":[\"filter1\",\"filter2\"]}";
var serverUnitPathDTO = HttpPathUtils.readServerPathDTO(configurationJson);

}
assertEquals("roboUnit1", serverUnitPathDTO.getRoboUnit());
assertEquals(HttpMethod.GET, serverUnitPathDTO.getMethod());
assertArrayEquals(Arrays.asList("filter1", "filter2").toArray(), serverUnitPathDTO.getCallbacks().toArray());
}

@Test
void serverConfigurationWithPropertiesParsingDTOTest() {
@Test
void serverConfigurationNullPathTest() {
var paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, null);

String configurationJson = "{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\",\"callbacks\":[\"filter1\",\"filter2\"]}";
HttpPathMethodDTO serverUnitPathDTO = HttpPathUtils.readServerPathDTO(configurationJson);
assertNotNull(paths);
assertTrue(paths.isEmpty());

assertEquals("roboUnit1", serverUnitPathDTO.getRoboUnit());
assertEquals(HttpMethod.GET, serverUnitPathDTO.getMethod());
assertArrayEquals(Arrays.asList("filter1", "filter2").toArray(), serverUnitPathDTO.getCallbacks().toArray());
}

System.out.println("serverUnitPathDTO: " + serverUnitPathDTO);
@Test
void serverConfigurationEmptyPathTest() {
var paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, StringConstants.EMPTY);

}
assertNotNull(paths);
assertTrue(paths.isEmpty());
}

@Test
void serverConfigurationNullPathTest() {
List<HttpPathMethodDTO> paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, null);
assertNotNull(paths);
assertTrue(paths.isEmpty());
@Test
void serverConfigurationWithMultiplePathsWithoutPropertiesTest() {
var configurationJson = "[{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\"},"
+ "{\"roboUnit\":\"roboUnit2\",\"method\":\"POST\"}]";

}
var expectedPathList = Arrays.asList(new HttpPathMethodDTO("roboUnit1", HttpMethod.GET),
new HttpPathMethodDTO("roboUnit2", HttpMethod.POST));

@Test
void serverConfigurationEmptyPathTest() {
List<HttpPathMethodDTO> paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, StringConstants.EMPTY);
assertNotNull(paths);
assertTrue(paths.isEmpty());
var paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, configurationJson);

}
assertEquals(expectedPathList.size(), paths.size());
assertArrayEquals(expectedPathList.toArray(), paths.toArray());
}

@Test
void serverConfigurationWithMultiplePathsWithoutPropertiesTest() {
String configurationJson = "[{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\"},"
+ "{\"roboUnit\":\"roboUnit2\",\"method\":\"POST\"}]";
@Test
void serverConfigurationWithMultiplePathsWithPropertiesTest() {
var configurationJson = "[{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\" , \"callbacks\":[\"filter1\",\"filter2\"]},"
+ "{\"roboUnit\":\"roboUnit2\",\"method\":\"POST\"}, {\"roboUnit\":\"roboUnit3\",\"method\":\"GET\",\"callbacks\":[]}]";

List<HttpPathMethodDTO> expectedPathList = Arrays.asList(new HttpPathMethodDTO("roboUnit1", HttpMethod.GET),
new HttpPathMethodDTO("roboUnit2", HttpMethod.POST));
var expectedPathList = Arrays.asList(
new HttpPathMethodDTO("roboUnit1", HttpMethod.GET, Arrays.asList("filter1", "filter2")),
new HttpPathMethodDTO("roboUnit2", HttpMethod.POST),
new HttpPathMethodDTO("roboUnit3", HttpMethod.GET, Collections.emptyList()));

List<HttpPathMethodDTO> paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, configurationJson);
var paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, configurationJson);

assertEquals(expectedPathList.size(), paths.size());
assertArrayEquals(expectedPathList.toArray(), paths.toArray());
}

@Test
void serverConfigurationWithMultiplePathsWithPropertiesTest() {
String configurationJson = "[{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\" , \"callbacks\":[\"filter1\",\"filter2\"]},"
+ "{\"roboUnit\":\"roboUnit2\",\"method\":\"POST\"}, {\"roboUnit\":\"roboUnit3\",\"method\":\"GET\",\"callbacks\":[]}]";

List<HttpPathMethodDTO> expectedPathList = Arrays.asList(
new HttpPathMethodDTO("roboUnit1", HttpMethod.GET, Arrays.asList("filter1", "filter2")),
new HttpPathMethodDTO("roboUnit2", HttpMethod.POST),
new HttpPathMethodDTO("roboUnit3", HttpMethod.GET, Collections.emptyList()));

List<HttpPathMethodDTO> paths = JsonUtil.readPathConfig(HttpPathMethodDTO.class, configurationJson);
System.out.println("paths: " + paths);

assertNotNull(paths);
assertEquals(expectedPathList.size(), paths.size());
assertArrayEquals(expectedPathList.toArray(), paths.toArray());
}
LOGGER.info("paths: {}", paths);
assertNotNull(paths);
assertEquals(expectedPathList.size(), paths.size());
assertArrayEquals(expectedPathList.toArray(), paths.toArray());
}

}
Loading

0 comments on commit 7928ac7

Please sign in to comment.