From a60393604ee9e0a663922585f2ebef67d3600aa3 Mon Sep 17 00:00:00 2001 From: Miro Wengner Date: Tue, 8 Oct 2024 17:48:06 +0200 Subject: [PATCH] [69] robo4j-socket-http update --- .../socket/http/util/RoboHttpUtils.java | 119 +++--- .../http/test/json/JsonCodecsTests.java | 8 +- .../GenericCodecConversionCyclesTests.java | 361 +++++++++--------- .../http/test/utils/ChannelBufferTests.java | 52 +-- .../test/utils/HttpMessageBuilderTests.java | 58 +-- .../utils/HttpPathConfigJsonBuilderTests.java | 8 +- .../http/test/utils/HttpPathUtilTests.java | 78 ++-- .../http/test/utils/InternalUtilTests.java | 9 +- .../socket/http/test/utils/JsonUtilTests.java | 87 +++-- .../http/test/utils/ReflectUtilTests.java | 259 ++++++------- 10 files changed, 524 insertions(+), 515 deletions(-) diff --git a/robo4j-socket-http/src/main/java/com/robo4j/socket/http/util/RoboHttpUtils.java b/robo4j-socket-http/src/main/java/com/robo4j/socket/http/util/RoboHttpUtils.java index 3b8b7c3a..b04cc6d9 100644 --- a/robo4j-socket-http/src/main/java/com/robo4j/socket/http/util/RoboHttpUtils.java +++ b/robo4j-socket-http/src/main/java/com/robo4j/socket/http/util/RoboHttpUtils.java @@ -18,6 +18,8 @@ import com.robo4j.util.StringConstants; import com.robo4j.util.Utf8Constant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Objects; @@ -28,73 +30,68 @@ * @author Miroslav Wengner (@miragemiko) */ public final class RoboHttpUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(RoboHttpUtils.class); - public static final String NEW_LINE_MAC = "\r"; - public static final String NEW_LINE_UNIX = "\n"; + public static final String NEW_LINE_MAC = "\r"; + public static final String NEW_LINE_UNIX = "\n"; - public static final int DEFAULT_PORT = 8042; - public static final int DEFAULT_UDP_PORT = 9042; - public static final String HTTP_PROPERTY_PROTOCOL = "protocol"; - public static final String PROPERTY_TARGET = "target"; - public static final String PROPERTY_HOST = "host"; - public static final String PROPERTY_SOCKET_PORT = "port"; - public static final String PROPERTY_CODEC_REGISTRY = "codecRegistry"; - public static final String PROPERTY_CODEC_PACKAGES = "packages"; - public static final String PROPERTY_UNIT_PATHS_CONFIG = "unitPathsConfig"; - public static final String PROPERTY_BUFFER_CAPACITY = "bufferCapacity"; - public static final String PROPERTY_BYTE_BUFFER = "byteBuffer"; - public static final String PROPERTY_TIMEOUT = "timeout"; + public static final int DEFAULT_PORT = 8042; + public static final int DEFAULT_UDP_PORT = 9042; + public static final String HTTP_PROPERTY_PROTOCOL = "protocol"; + public static final String PROPERTY_TARGET = "target"; + public static final String PROPERTY_HOST = "host"; + public static final String PROPERTY_SOCKET_PORT = "port"; + public static final String PROPERTY_CODEC_REGISTRY = "codecRegistry"; + public static final String PROPERTY_CODEC_PACKAGES = "packages"; + public static final String PROPERTY_UNIT_PATHS_CONFIG = "unitPathsConfig"; + public static final String PROPERTY_BUFFER_CAPACITY = "bufferCapacity"; + public static final String PROPERTY_BYTE_BUFFER = "byteBuffer"; + public static final String PROPERTY_TIMEOUT = "timeout"; - public static void decorateByNewLine(StringBuilder sb) { - sb.append(NEW_LINE_MAC).append(NEW_LINE_UNIX); - } + public static void decorateByNewLine(StringBuilder sb) { + sb.append(NEW_LINE_MAC).append(NEW_LINE_UNIX); + } - public static String correctLine(String line) { - return line == null ? StringConstants.EMPTY : line; - } + public static String correctLine(String line) { + return line == null ? StringConstants.EMPTY : line; + } - /** - * create host header note - * - * @param host - * desired host - * @param port - * default port is 80 - * @return host string - */ - public static String createHost(String host, Integer port) { - Objects.requireNonNull(host, "host not available"); - Objects.requireNonNull(host, "port not available"); - return new StringBuilder(host).append(Utf8Constant.UTF8_COLON).append(port).toString(); - } + /** + * create host header note + * + * @param host desired host + * @param port default port is 80 + * @return host string + */ + public static String createHost(String host, Integer port) { + Objects.requireNonNull(host, "host not available"); + Objects.requireNonNull(host, "port not available"); + return host + Utf8Constant.UTF8_COLON + port; + } - /** - * report time in moment M from start time - * - * @param clazz - * desired class - * @param message - * message - * @param start - * start time - */ - public static void printMeasuredTime(Class clazz, String message, long start) { - System.out.println(String.format("%s message: %s duration: %d%n", clazz.getSimpleName(), message, - System.currentTimeMillis() - start)); - } + /** + * report time in moment M from start time + * + * @param clazz desired class + * @param message message + * @param start start time + */ + public static void printMeasuredTime(Class clazz, String message, long start) { + LOGGER.info("{} message: {} duration: {}", clazz.getSimpleName(), message, System.currentTimeMillis() - start); + } - // TODO: 1/26/18 (miro - public static boolean validatePackages(String packages) { - if (packages == null) { - return false; - } - for (int i = 0; i < packages.length(); i++) { - char c = packages.charAt(i); - if (Character.isWhitespace(c)) { - return false; - } - } - return true; - } + // TODO: 1/26/18 (miro) review and simplify + public static boolean validatePackages(String packages) { + if (packages == null) { + return false; + } + for (int i = 0; i < packages.length(); i++) { + char c = packages.charAt(i); + if (Character.isWhitespace(c)) { + return false; + } + } + return true; + } } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/json/JsonCodecsTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/json/JsonCodecsTests.java index b1c7475a..6ee92de1 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/json/JsonCodecsTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/json/JsonCodecsTests.java @@ -223,10 +223,6 @@ private static void printDecodedMessage(T decodedMessage) { LOGGER.debug("decodedMessage:{}", decodedMessage); } - private static void printJson(String resultJson) { - LOGGER.debug("resultJson:{}", resultJson); - } - private static Map getStringTestPersonMap() { TestPerson testPerson2 = new TestPerson(); testPerson2.setName("name2"); @@ -252,4 +248,8 @@ private static Map getStringTestPersonMap() { return personMap; } + private static void printJson(String resultJson) { + LOGGER.debug("json:{}", resultJson); + } + } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/config/codec/GenericCodecConversionCyclesTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/config/codec/GenericCodecConversionCyclesTests.java index 598fd6b9..b0be5da0 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/config/codec/GenericCodecConversionCyclesTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/units/config/codec/GenericCodecConversionCyclesTests.java @@ -20,196 +20,195 @@ import com.robo4j.socket.http.test.codec.NSBWithSimpleCollectionsTypesMessageCodec; import org.junit.jupiter.api.BeforeEach; 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.HashMap; import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; /** * @author Marcus Hirt (@hirt) * @author Miroslav Wengner (@miragemiko) */ class GenericCodecConversionCyclesTests { - - private NSBTypesTestMessageCodec fieldTypesMessageCodec; - private NSBWithSimpleCollectionsTypesMessageCodec collectionsTypesMessageCodec; - - @BeforeEach - void setUp() { - fieldTypesMessageCodec = new NSBTypesTestMessageCodec(); - collectionsTypesMessageCodec = new NSBWithSimpleCollectionsTypesMessageCodec(); - } - - @Test - void genericClassCycleFromObjectToJsonWithNullExtractionTest() { - int numberValue = 22; - boolean isActive = true; - String desiredJson = "{\"number\":" + numberValue + ",\"active\":" + isActive + "}"; - - NSBTypesTestMessage obj1 = new NSBTypesTestMessage(numberValue, null, isActive); - - String json = fieldTypesMessageCodec.encode(obj1); - assertEquals(desiredJson, json); - System.out.println("JSON: " + json); - } - - @Test - void genericClassCycleFromObjectToJsonExtractionTest() { - int numberValue = 22; - boolean isActive = true; - String message = "some messge"; - String desiredJson = "{\"number\":" + numberValue + ",\"message\":\"" + message + "\",\"active\":" + isActive - + "}"; - NSBTypesTestMessage obj1 = new NSBTypesTestMessage(numberValue, message, isActive); - - String json = fieldTypesMessageCodec.encode(obj1); - assertEquals(desiredJson, json); - System.out.println("JSON: " + json); - } - - @Test - void genericClassCycleFromObjectToJsonToObjectTest() { - - NSBTypesTestMessage obj1 = new NSBTypesTestMessage(22, "some messge", true); - - String json = fieldTypesMessageCodec.encode(obj1); - System.out.println("json: " + json); - NSBTypesTestMessage createdObj = fieldTypesMessageCodec.decode(json); - - assertEquals(obj1, createdObj); - System.out.println("Result: " + createdObj); - } - - @Test - void genericClassCycleFromObjectToJsonToObjectWithNulTest() { - NSBTypesTestMessage obj1 = new NSBTypesTestMessage(22, null, true); - - String json = fieldTypesMessageCodec.encode(obj1); - NSBTypesTestMessage createdObj = fieldTypesMessageCodec.decode(json); - - assertEquals(obj1, createdObj); - System.out.println("json: " + json); - System.out.println("Result: " + createdObj); - } - - @Test - void genericClassCycleFromObjectToJsonToObjectWithArrayTest() { - String desiredObjectToJson = "{\"number\":42,\"message\":\"no message\",\"active\":false,\"array\":[\"one\",\"two\"],\"list\":[\"text1\",\"text2\"],\"map\":{\"key\":\"value\"}}"; - NSBWithSimpleCollectionsTypesMessage obj1 = new NSBWithSimpleCollectionsTypesMessage(); - obj1.setNumber(42); - obj1.setMessage("no message"); - obj1.setActive(false); - obj1.setArray(new String[] { "one", "two" }); - obj1.setList(Arrays.asList("text1", "text2")); - Map mapStrings = new HashMap<>(); - mapStrings.put("key1", "value1"); - mapStrings.put("key2", "value2"); - mapStrings.put("key3", "value3"); - obj1.setMap(Collections.singletonMap("key", "value")); - - String json = collectionsTypesMessageCodec.encode(obj1); - System.out.println("JSON:" + json); - NSBWithSimpleCollectionsTypesMessage createdObj = collectionsTypesMessageCodec.decode(json); - - assertEquals(desiredObjectToJson, json); - assertEquals("no message", createdObj.getMessage()); - assertTrue(!createdObj.getActive()); - System.out.println("createdObj: " + createdObj); - System.out.println("json: " + json); - - } - - @Test - void testJson() { - - 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 personMap = new LinkedHashMap<>(); - personMap.put("person1", testPerson1); - personMap.put("person2", testPerson2); - - NSBWithSimpleCollectionsTypesMessage obj1 = new NSBWithSimpleCollectionsTypesMessage(); - obj1.setNumber(42); - obj1.setMessage("no message"); - obj1.setActive(false); - obj1.setArray(new String[] { "one", "two" }); - obj1.setList(Arrays.asList("text1", "text2")); - obj1.setMap(Collections.singletonMap("key", "value")); - obj1.setPersons(Arrays.asList(testPerson1, testPerson2)); - obj1.setPersonMap(personMap); - - String json = collectionsTypesMessageCodec.encode(obj1); - System.out.println("JSON: " + json); - } - - @Test - void collectionNSBWithSimpleCollectionsTypesMessageNestedObject() { - String json = "{\"number\":42,\"message\":\"no message\",\"active\":false,\"array\":[\"one\",\"two\"]," - + "\"list\":[\"text1\",\"text2\"],\"map\":{\"key\":\"value\"}, " - + "\"persons\":[{\"name\":\"name1\",\"value\":22, \"child\":{\"name\":\"name11\",\"value\":0, " - + "\"child\":{\"name\":\"name111\",\"value\":42}}},{\"name\":\"name2\",\"value\":5}], " - + "\"personMap\":{\"key1\":\"value1\",\"key2,\":\"value2\"}}"; - - System.out.println("JSON: " + json); - NSBWithSimpleCollectionsTypesMessage createdObj = collectionsTypesMessageCodec.decode(json); - - List expectedList = Arrays.asList("text1", "text2"); - - 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 expectedMap = Collections.singletonMap("key", "value"); - - assertEquals(Integer.valueOf(42), createdObj.getNumber()); - assertTrue(!createdObj.getActive()); - assertArrayEquals(new String[] { "one", "two" }, createdObj.getArray()); - assertEquals(expectedMap, createdObj.getMap()); - assertEquals(expectedList.size(), createdObj.getList().size()); - assertTrue(createdObj.getList().containsAll(expectedList)); - assertEquals("no message", createdObj.getMessage()); - assertEquals(testPerson1, createdObj.getPersons().get(0)); - assertEquals(testPerson2, createdObj.getPersons().get(1)); - assertEquals(testPerson11, createdObj.getPersons().get(0).getChild()); - - System.out.println("createdObj: " + createdObj); - } - + private static final Logger LOGGER = LoggerFactory.getLogger(GenericCodecConversionCyclesTests.class); + private NSBTypesTestMessageCodec fieldTypesMessageCodec; + private NSBWithSimpleCollectionsTypesMessageCodec collectionsTypesMessageCodec; + + @BeforeEach + void setUp() { + fieldTypesMessageCodec = new NSBTypesTestMessageCodec(); + collectionsTypesMessageCodec = new NSBWithSimpleCollectionsTypesMessageCodec(); + } + + @Test + void genericClassCycleFromObjectToJsonWithNullExtractionTest() { + int numberValue = 22; + boolean isActive = true; + String desiredJson = "{\"number\":" + numberValue + ",\"active\":" + isActive + "}"; + + NSBTypesTestMessage obj1 = new NSBTypesTestMessage(numberValue, null, isActive); + + String json = fieldTypesMessageCodec.encode(obj1); + assertEquals(desiredJson, json); + printJson(json); + } + + @Test + void genericClassCycleFromObjectToJsonExtractionTest() { + int numberValue = 22; + boolean isActive = true; + String message = "some messge"; + String desiredJson = "{\"number\":" + numberValue + ",\"message\":\"" + message + "\",\"active\":" + isActive + + "}"; + NSBTypesTestMessage obj1 = new NSBTypesTestMessage(numberValue, message, isActive); + + String json = fieldTypesMessageCodec.encode(obj1); + assertEquals(desiredJson, json); + printJson(json); + } + + @Test + void genericClassCycleFromObjectToJsonToObjectTest() { + + NSBTypesTestMessage obj1 = new NSBTypesTestMessage(22, "some messge", true); + + String json = fieldTypesMessageCodec.encode(obj1); + NSBTypesTestMessage createdObj = fieldTypesMessageCodec.decode(json); + + printJson(json); + LOGGER.info("Result: {}", createdObj); + assertEquals(obj1, createdObj); + } + + @Test + void genericClassCycleFromObjectToJsonToObjectWithNulTest() { + var obj1 = new NSBTypesTestMessage(22, null, true); + var json = fieldTypesMessageCodec.encode(obj1); + var createdObj = fieldTypesMessageCodec.decode(json); + + printJson(json); + printObject(createdObj); + + assertEquals(obj1, createdObj); + } + + @Test + void genericClassCycleFromObjectToJsonToObjectWithArrayTest() { + String desiredObjectToJson = "{\"number\":42,\"message\":\"no message\",\"active\":false,\"array\":[\"one\",\"two\"],\"list\":[\"text1\",\"text2\"],\"map\":{\"key\":\"value\"}}"; + NSBWithSimpleCollectionsTypesMessage obj1 = new NSBWithSimpleCollectionsTypesMessage(); + obj1.setNumber(42); + obj1.setMessage("no message"); + obj1.setActive(false); + obj1.setArray(new String[]{"one", "two"}); + obj1.setList(Arrays.asList("text1", "text2")); + obj1.setMap(Collections.singletonMap("key", "value")); + + String json = collectionsTypesMessageCodec.encode(obj1); + var createdObj = collectionsTypesMessageCodec.decode(json); + + printJson(json); + printObject(createdObj); + + assertEquals(desiredObjectToJson, json); + assertEquals("no message", createdObj.getMessage()); + assertFalse(createdObj.getActive()); + + } + + @Test + void testJson() { + 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); + + var personMap = new LinkedHashMap(); + personMap.put("person1", testPerson1); + personMap.put("person2", testPerson2); + + var obj1 = new NSBWithSimpleCollectionsTypesMessage(); + obj1.setNumber(42); + obj1.setMessage("no message"); + obj1.setActive(false); + obj1.setArray(new String[]{"one", "two"}); + obj1.setList(Arrays.asList("text1", "text2")); + obj1.setMap(Collections.singletonMap("key", "value")); + obj1.setPersons(Arrays.asList(testPerson1, testPerson2)); + obj1.setPersonMap(personMap); + String json = collectionsTypesMessageCodec.encode(obj1); + + printJson(json); + } + + @Test + void collectionNSBWithSimpleCollectionsTypesMessageNestedObject() { + String json = "{\"number\":42,\"message\":\"no message\",\"active\":false,\"array\":[\"one\",\"two\"]," + + "\"list\":[\"text1\",\"text2\"],\"map\":{\"key\":\"value\"}, " + + "\"persons\":[{\"name\":\"name1\",\"value\":22, \"child\":{\"name\":\"name11\",\"value\":0, " + + "\"child\":{\"name\":\"name111\",\"value\":42}}},{\"name\":\"name2\",\"value\":5}], " + + "\"personMap\":{\"key1\":\"value1\",\"key2,\":\"value2\"}}"; + + var createdObj = collectionsTypesMessageCodec.decode(json); + + var expectedList = Arrays.asList("text1", "text2"); + + 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); + + var expectedMap = Collections.singletonMap("key", "value"); + + printJson(json); + printObject(createdObj); + + assertEquals(Integer.valueOf(42), createdObj.getNumber()); + assertFalse(createdObj.getActive()); + assertArrayEquals(new String[]{"one", "two"}, createdObj.getArray()); + assertEquals(expectedMap, createdObj.getMap()); + assertEquals(expectedList.size(), createdObj.getList().size()); + assertTrue(createdObj.getList().containsAll(expectedList)); + assertEquals("no message", createdObj.getMessage()); + assertEquals(testPerson1, createdObj.getPersons().get(0)); + assertEquals(testPerson2, createdObj.getPersons().get(1)); + assertEquals(testPerson11, createdObj.getPersons().get(0).getChild()); + } + + static void printJson(String json) { + LOGGER.info("JSON: {}", json); + } + + private static void printObject(Object obj) { + LOGGER.info("Object: {}", obj); + } } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ChannelBufferTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ChannelBufferTests.java index 09df8272..25c3063b 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ChannelBufferTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ChannelBufferTests.java @@ -17,8 +17,8 @@ package com.robo4j.socket.http.test.utils; import org.junit.jupiter.api.Test; - -import java.util.regex.Matcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static com.robo4j.socket.http.util.ChannelBufferUtils.RESPONSE_SPRING_PATTERN; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -30,22 +30,23 @@ * @author Miroslav Wengner (@miragemiko) */ class ChannelBufferTests { - private static int GROUP_COUNT = 3; + private static final Logger LOGGER = LoggerFactory.getLogger(ChannelBufferTests.class); + private static final int GROUP_COUNT = 3; private static final String JSON_MESSAGE = "{\"content\":\"No Params\",\"number\":11}"; private static final String JSON_ARRAY_MESSAGE = "[{\"id\":\"stringConsumer\",\"state\":\"STARTED\"},{\"id\":\"httpServer\",\"state\":\"STARTED\"}]"; @Test - void springResponsePatternTest(){ - String message = "HTTP/1.1 200 \r\n" + + void springResponsePatternTest() { + var message = "HTTP/1.1 200 \r\n" + "Content-Type: application/json;charset=UTF-8\r\n" + "Transfer-Encoding: chunked\r\n" + "Date: Mon, 05 Mar 2018 21:37:27 GMT\r\n" + "\r\n" + - "23\r\n" + JSON_MESSAGE +"\r\n"; - String[] headerBody = message.split("\r\n\r\n"); - Matcher matcher = RESPONSE_SPRING_PATTERN.matcher(headerBody[1]); - if(matcher.find()){ - System.out.println("json: " + matcher.group(2)); + "23\r\n" + JSON_MESSAGE + "\r\n"; + var headerBody = message.split("\r\n\r\n"); + var matcher = RESPONSE_SPRING_PATTERN.matcher(headerBody[1]); + if (matcher.find()) { + printJson(matcher.group(2)); assertEquals(GROUP_COUNT, matcher.groupCount()); assertEquals(JSON_MESSAGE, matcher.group(2)); } @@ -53,35 +54,38 @@ void springResponsePatternTest(){ @Test - void jsonResponseStandardTest(){ - String message = "HTTP/1.1 200 \r\n" + + void jsonResponseStandardTest() { + var message = "HTTP/1.1 200 \r\n" + "Content-Type: application/json;charset=UTF-8\r\n" + "Transfer-Encoding: chunked\r\n" + "Date: Mon, 05 Mar 2018 21:37:27 GMT\r\n" + - "\r\n" + JSON_MESSAGE +"\r\n"; + "\r\n" + JSON_MESSAGE + "\r\n"; - String[] headerBody = message.split("\r\n\r\n"); - Matcher matcher = RESPONSE_SPRING_PATTERN.matcher(headerBody[1]); - if(matcher.find()){ - System.out.println("json: " + matcher.group(2)); + var headerBody = message.split("\r\n\r\n"); + var matcher = RESPONSE_SPRING_PATTERN.matcher(headerBody[1]); + if (matcher.find()) { + printJson(matcher.group(2)); assertEquals(JSON_MESSAGE, matcher.group(2)); } assertEquals(GROUP_COUNT, matcher.groupCount()); } @Test - void jsonArrayResponseMessage(){ - String message = "HTTP/1.1 200 \r\n" + + void jsonArrayResponseMessage() { + var message = "HTTP/1.1 200 \r\n" + "Date: Mon, 05 Mar 2018 21:37:27 GMT\r\n" + - "\r\n" + JSON_ARRAY_MESSAGE +"\r\n"; + "\r\n" + JSON_ARRAY_MESSAGE + "\r\n"; - String[] headerBody = message.split("\r\n\r\n"); - Matcher matcher = RESPONSE_SPRING_PATTERN.matcher(headerBody[1]); - if(matcher.find()){ - System.out.println("json: " + matcher.group(2)); + var headerBody = message.split("\r\n\r\n"); + var matcher = RESPONSE_SPRING_PATTERN.matcher(headerBody[1]); + if (matcher.find()) { + printJson(matcher.group(2)); assertEquals(JSON_ARRAY_MESSAGE, matcher.group(2)); } assertEquals(GROUP_COUNT, matcher.groupCount()); } + private static void printJson(String resultJson) { + LOGGER.debug("json:{}", resultJson); + } } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpMessageBuilderTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpMessageBuilderTests.java index 74a455a8..ed0adf08 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpMessageBuilderTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpMessageBuilderTests.java @@ -24,6 +24,8 @@ import com.robo4j.socket.http.util.HttpMessageBuilder; import com.robo4j.socket.http.util.RoboHttpUtils; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static com.robo4j.socket.http.provider.DefaultValuesProvider.BASIC_HEADER_MAP; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -34,38 +36,40 @@ * @author Miroslav Wengner (@miragemiko) */ class HttpMessageBuilderTests { + private static final Logger LOGGER = LoggerFactory.getLogger(HttpMessageBuilderTests.class); - @Test - void getRequestTest() { - HttpMessageBuilder getMessageBuilder = getDefaultMessageBuilderByMethod(HttpMethod.GET); + @Test + void getRequestTest() { + var getMessageBuilder = getDefaultMessageBuilderByMethod(HttpMethod.GET); - final String getMessage = getMessageBuilder.build(); - assertNotNull(getMessage); - assertTrue(getMessage.contains("GET / HTTP/1.1")); - assertTrue(getMessage.contains("host: localhost")); - System.out.println("toMessage: " + getMessage); - } + var message = getMessageBuilder.build(); - @Test - void postRequestTest() { - String message = "magic"; - HttpMessageBuilder postMessageBuilder = getDefaultMessageBuilderByMethod(HttpMethod.POST); - postMessageBuilder.addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(message.length())); + LOGGER.info("toMessage: {}", message); + assertNotNull(message); + assertTrue(message.contains("GET / HTTP/1.1")); + assertTrue(message.contains("host: localhost")); + } - final String postRequest = postMessageBuilder.build(message); + @Test + void postRequestTest() { + String message = "magic"; + HttpMessageBuilder postMessageBuilder = getDefaultMessageBuilderByMethod(HttpMethod.POST); + postMessageBuilder.addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(message.length())); - assertNotNull(postRequest); - assertTrue(postRequest.contains("POST / HTTP/1.1")); - assertTrue(postRequest.contains("host: localhost")); - assertTrue(postRequest.contains("content-length: 5")); - assertTrue(postRequest.contains(message)); - } + final String postRequest = postMessageBuilder.build(message); + assertNotNull(postRequest); + assertTrue(postRequest.contains("POST / HTTP/1.1")); + assertTrue(postRequest.contains("host: localhost")); + assertTrue(postRequest.contains("content-length: 5")); + assertTrue(postRequest.contains(message)); + } - private HttpMessageBuilder getDefaultMessageBuilderByMethod(HttpMethod method) { - HttpRequestDenominator getDenominator = new HttpRequestDenominator(method, HttpVersion.HTTP_1_1); - return HttpMessageBuilder.Build().setDenominator(getDenominator) - .addHeaderElement(HttpHeaderFieldNames.HOST, RoboHttpUtils.createHost("localhost", ProtocolType.HTTP.getPort())) - .addHeaderElements(BASIC_HEADER_MAP); - } + + private HttpMessageBuilder getDefaultMessageBuilderByMethod(HttpMethod method) { + HttpRequestDenominator getDenominator = new HttpRequestDenominator(method, HttpVersion.HTTP_1_1); + return HttpMessageBuilder.Build().setDenominator(getDenominator) + .addHeaderElement(HttpHeaderFieldNames.HOST, RoboHttpUtils.createHost("localhost", ProtocolType.HTTP.getPort())) + .addHeaderElements(BASIC_HEADER_MAP); + } } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathConfigJsonBuilderTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathConfigJsonBuilderTests.java index 69f5e561..077b9bbd 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathConfigJsonBuilderTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathConfigJsonBuilderTests.java @@ -19,6 +19,8 @@ import com.robo4j.socket.http.HttpMethod; import com.robo4j.socket.http.util.HttpPathConfigJsonBuilder; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Arrays; import java.util.Collections; @@ -26,14 +28,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /** - * * @author Marcus Hirt (@hirt) * @author Miroslav Wengner (@miragemiko) */ class HttpPathConfigJsonBuilderTests { + private static final Logger LOGGER = LoggerFactory.getLogger(HttpPathConfigJsonBuilderTests.class); @Test - void simpleConfigurationTest(){ + void simpleConfigurationTest() { String expectedJson = "[{\"roboUnit\":\"roboUnit1\",\"method\":\"GET\",\"callbacks\":[\"filter1\",\"filter2\"]}," + "{\"roboUnit\":\"roboUnit2\",\"method\":\"POST\",\"callbacks\":[]},{\"roboUnit\":\"roboUnit3\",\"method\":\"GET\",\"callbacks\":[]}]"; @@ -45,7 +47,7 @@ void simpleConfigurationTest(){ String resultJson = builder.build(); - System.out.println("resultJson: " + resultJson); + LOGGER.info("resultJson: {}", resultJson); assertEquals(expectedJson, resultJson); } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathUtilTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathUtilTests.java index dfc3d89f..74960e57 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathUtilTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/HttpPathUtilTests.java @@ -17,7 +17,6 @@ package com.robo4j.socket.http.test.utils; import com.robo4j.socket.http.HttpMethod; -import com.robo4j.socket.http.dto.ClassGetSetDTO; import com.robo4j.socket.http.dto.HttpPathMethodDTO; import com.robo4j.socket.http.dto.PathAttributeDTO; import com.robo4j.socket.http.test.units.config.PropertyListBuilder; @@ -26,42 +25,32 @@ import com.robo4j.socket.http.util.ReflectUtils; 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.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import static com.robo4j.socket.http.util.HttpPathUtils.ATTRIBUTES_PATH_VALUE; -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.assertTrue; +import static org.junit.jupiter.api.Assertions.*; /** * @author Marcus Hirt (@hirt) * @author Miro Wengner (@miragemiko) */ public class HttpPathUtilTests { + private static final Logger LOGGER = LoggerFactory.getLogger(HttpPathUtilTests.class); - //String roboUnit; - // private HttpMethod method; - // private List callbacks; + @SuppressWarnings("unchecked") + @Test + public void convertPathMethodToJson() { - @SuppressWarnings("unchecked") - @Test - public void convertPathMethodToJson() { - - //@formatter:off - final String expectedResult = "[{\"roboUnit\":\"imageController\",\"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}," + + //@formatter:off + final var expectedResult = "[{\"roboUnit\":\"imageController\",\"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}," + "{\"roboUnit\":\"imageController\",\"method\":\"GET\",\"callbacks\":[\"callbackGETController\"]}," + "{\"roboUnit\":\"cameraController\",\"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}," + "{\"roboUnit\":\"cameraController\",\"method\":\"GET\",\"callbacks\":[\"callbackGETController\"]}," + "{\"roboUnit\":\"emptyController\",\"method\":\"GET\"}]"; - List pathMethodList = PropertyListBuilder.Builder() + final var pathMethodList = PropertyListBuilder.Builder() .add(createPathMethodDTO("imageController", "POST","callbackPOSTController")) .add(createPathMethodDTO("imageController", "GET","callbackGETController")) .add(createPathMethodDTO("cameraController", "POST","callbackPOSTController")) @@ -69,9 +58,9 @@ public void convertPathMethodToJson() { .add(createPathMethodDTO("emptyController", "GET")) .build(); - String result = JsonUtil.getJsonByPathMethodList(pathMethodList); + var result = JsonUtil.getJsonByPathMethodList(pathMethodList); - System.out.println("result: " + result); + printInfo(result); assertNotNull(result); assertEquals(expectedResult, result); @@ -79,18 +68,18 @@ public void convertPathMethodToJson() { @Test void parseJsonArrayPathMethodToList(){ - final int observedElement = 2; - final String jsonArray = "[{\"roboUnit\":\"imageController\", \"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}," + + final var observedElement = 2; + final var jsonArray = "[{\"roboUnit\":\"imageController\", \"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}," + "{\"roboUnit\":\"imageController\",\"method\":\"GET\",\"callbacks\":[\"callbackGETController\"]}," + "{\"roboUnit\":\"cameraController\",\"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}," + "{\"roboUnit\":\"cameraController\",\"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}," + "{\"roboUnit\":\"cameraController\",\"method\":\"GET\",\"callbacks\":[\"callbackGETController\"]}," + "{\"roboUnit\":\"emptyController\",\"method\":\"GET\"}]"; - HttpPathMethodDTO duplicate = new HttpPathMethodDTO("cameraController", HttpMethod.POST, Collections.singletonList("callbackPOSTController")); - List result = JsonUtil.toListFromJsonArray(HttpPathMethodDTO.class, jsonArray); + var duplicate = new HttpPathMethodDTO("cameraController", HttpMethod.POST, Collections.singletonList("callbackPOSTController")); + var result = JsonUtil.toListFromJsonArray(HttpPathMethodDTO.class, jsonArray); - System.out.println("result: " + result); + printInfo(result); assertNotNull(result); assertEquals(6, result.size()); @@ -103,7 +92,7 @@ void parseJsonArrayPathMethodToList(){ @Test void parseJsonPathMethod(){ - List jsonList = Arrays.asList( + final var jsonList = Arrays.asList( "{\"roboUnit\":\"imageController\", \"method\":\"POST\", \"callbacks\":[\"callbackPOSTController\"]}", "{\"roboUnit\":\"imageController\", \"method\" : \"POST\", \"callbacks\" : [ \"callbackPOSTController\" ]}", "{ \"roboUnit\": \"imageController\", \"method\": \"POST\", \"callbacks\" :[\"callbackPOSTController\"] }" @@ -120,7 +109,7 @@ void parseJsonPathMethod(){ @Test void parseFullPathMethod() { - List jsonList = Arrays.asList( + final var jsonList = Arrays.asList( "{\"roboUnit\":\"\", \"method\":\"POST\", \"callbacks\":[\"callbackPOSTController\"]}", "{\"roboUnit\":\"imageController\", \"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}", "{\"roboUnit\":\"imageController\", \"method\":\"POST\",\"callbacks\":[\"callbackPOSTController\"]}", @@ -129,7 +118,7 @@ void parseFullPathMethod() { jsonList.forEach(json -> { HttpPathMethodDTO pathMethod = JsonUtil.getPathMethodByJson(json); - System.out.println("pathMethod: " + pathMethod); + printInfo(pathMethod); assertNotNull(pathMethod, json); assertNotNull(pathMethod.getRoboUnit(), json); assertTrue(pathMethod.getRoboUnit().isEmpty() ? @@ -139,33 +128,38 @@ void parseFullPathMethod() { @Test void parseGetRequestWithAttributes(){ - String path = "/units/controller?attributes=number,counter"; - Map> expectedMap = new HashMap<>(); - Set expectedAttributesValues = new HashSet<>(); - String attributeName = "attributes"; + var path = "/units/controller?attributes=number,counter"; + var expectedMap = new HashMap>(); + var expectedAttributesValues = new HashSet(); + var attributeName = "attributes"; + expectedAttributesValues.add("number"); expectedAttributesValues.add("counter"); expectedMap.put(ATTRIBUTES_PATH_VALUE, expectedAttributesValues); HttpPathUtils.extractAttributesByPath(path); - Map> attributeMap = HttpPathUtils.extractAttributesByPath(path); + var attributeMap = HttpPathUtils.extractAttributesByPath(path); + assertArrayEquals(attributeMap.get(attributeName).toArray(), expectedMap.get(attributeName).toArray()); } @Test void createJsonArrayByList(){ + var attributeDTO = new PathAttributeDTO("number", "42"); - PathAttributeDTO attributeDTO = new PathAttributeDTO("number", "42"); + var descriptorMap = ReflectUtils.getFieldsTypeMap(PathAttributeDTO.class); - Map descriptorMap = ReflectUtils.getFieldsTypeMap(PathAttributeDTO.class); - - System.out.println("result: " + JsonUtil.toJson(descriptorMap, attributeDTO)); + printInfo(JsonUtil.toJson(descriptorMap, attributeDTO)); } private HttpPathMethodDTO createPathMethodDTO(String... args) { - List properties = args.length > 2 ? Collections.singletonList(args[2]) : null; + var properties = args.length > 2 ? Collections.singletonList(args[2]) : null; return new HttpPathMethodDTO(args[0], HttpMethod.getByName(args[1]), properties); } + private static void printInfo(T result) { + LOGGER.info("result: {}",result); + } + } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/InternalUtilTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/InternalUtilTests.java index f248ad91..46530390 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/InternalUtilTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/InternalUtilTests.java @@ -18,6 +18,8 @@ import com.robo4j.socket.http.util.SystemPropertyUtils; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Arrays; @@ -29,12 +31,13 @@ * @author Miro Wengner (@miragemiko) */ class InternalUtilTests { + private static final Logger LOGGER = LoggerFactory.getLogger(InternalUtilTests.class); @Test - void testSeparator(){ - String separator = SystemPropertyUtils.get("line.separator", "\n\n"); - System.out.println("Separator: " + Arrays.asList(separator.toCharArray())); + void testSeparator() { + var separator = SystemPropertyUtils.get("line.separator", "\n\n"); + LOGGER.info("Separator: {}", Arrays.asList(separator.toCharArray())); assertNotNull(separator.toCharArray()); } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/JsonUtilTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/JsonUtilTests.java index 737d083f..2667c614 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/JsonUtilTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/JsonUtilTests.java @@ -20,16 +20,14 @@ import com.robo4j.socket.http.dto.ResponseUnitDTO; import com.robo4j.socket.http.util.JsonUtil; 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.HashMap; -import java.util.List; -import java.util.Map; -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.*; /** * json related utils tests @@ -38,53 +36,58 @@ * @author Miroslav Wengner (@miragemiko) */ class JsonUtilTests { + private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtilTests.class); - @Test - void jsonToListTest() { + @Test + void jsonToListTest() { - String json = "[{\"id\":\"unit1\",\"state\":\"INITIALIZED\"}," + "{\"id\":\"unit2\",\"state\":\"STARTED\"}, " - + "{\"id\":\"unit3\",\"state\":\"FAILED\"}]"; + var json = "[{\"id\":\"unit1\",\"state\":\"INITIALIZED\"}," + "{\"id\":\"unit2\",\"state\":\"STARTED\"}, " + + "{\"id\":\"unit3\",\"state\":\"FAILED\"}]"; - List expectedResult = Arrays.asList(new ResponseUnitDTO("unit1", LifecycleState.INITIALIZED), - new ResponseUnitDTO("unit2", LifecycleState.STARTED), - new ResponseUnitDTO("unit3", LifecycleState.FAILED)); - List result = JsonUtil.jsonToList(ResponseUnitDTO.class, json); + var expectedResult = Arrays.asList(new ResponseUnitDTO("unit1", LifecycleState.INITIALIZED), + new ResponseUnitDTO("unit2", LifecycleState.STARTED), + new ResponseUnitDTO("unit3", LifecycleState.FAILED)); + var result = JsonUtil.jsonToList(ResponseUnitDTO.class, json); - assertNotNull(result); - assertArrayEquals(expectedResult.toArray(), result.toArray()); - } + assertNotNull(result); + assertArrayEquals(expectedResult.toArray(), result.toArray()); + } - @Test - void jsonToListEmptyTest() { - String json = "[]"; - List result = JsonUtil.jsonToList(ResponseUnitDTO.class, json); + @Test + void jsonToListEmptyTest() { + var json = "[]"; + var result = JsonUtil.jsonToList(ResponseUnitDTO.class, json); - assertNotNull(result); - assertArrayEquals(Collections.emptyList().toArray(), result.toArray()); - } + assertNotNull(result); + assertArrayEquals(Collections.emptyList().toArray(), result.toArray()); + } - @Test - void mapToJsonTest() { - String expectedJson = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; - Map testMap = new HashMap<>(); - testMap.put("key1", "value1"); - testMap.put("key2", "value2"); + @Test + void mapToJsonTest() { + var expectedJson = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; + var testMap = new HashMap(); + testMap.put("key1", "value1"); + testMap.put("key2", "value2"); - String result = JsonUtil.toJsonMap(testMap); - System.out.println("result: " + result); + var result = JsonUtil.toJsonMap(testMap); - assertNotNull(result); - assertEquals(result, expectedJson); - } + printInfo(result); + assertNotNull(result); + assertEquals(result, expectedJson); + } - @Test - void mapToJsonEmptyTest() { - String expectedJson = "{}"; + @Test + void mapToJsonEmptyTest() { + var expectedJson = "{}"; - String result = JsonUtil.toJsonMap(new HashMap<>()); - System.out.println("result: " + result); + var result = JsonUtil.toJsonMap(new HashMap<>()); - assertNotNull(result); - assertEquals(result, expectedJson); - } + printInfo(result); + assertNotNull(result); + assertEquals(result, expectedJson); + } + + private static void printInfo(String result) { + LOGGER.info("result: {}", result); + } } diff --git a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ReflectUtilTests.java b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ReflectUtilTests.java index 0862b1e9..e4f213a2 100644 --- a/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ReflectUtilTests.java +++ b/robo4j-socket-http/src/test/java/com/robo4j/socket/http/test/utils/ReflectUtilTests.java @@ -25,18 +25,12 @@ import com.robo4j.socket.http.util.ReflectUtils; import com.robo4j.socket.http.util.RoboReflectException; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; /** @@ -44,162 +38,171 @@ * @author Miroslav Wengner (@miragemiko) */ class ReflectUtilTests { + private static final Logger LOGGER = LoggerFactory.getLogger(ReflectUtilTests.class); - @Test - void objectWithEnumToJson() { - final String expectedJson = "{\"command\":\"MOVE\",\"desc\":\"some description\"}"; - TestCommand command = new TestCommand(); - command.setCommand(TestCommandEnum.MOVE); - command.setDesc("some description"); - - final String result = ReflectUtils.createJson(command); + @Test + void objectWithEnumToJson() { + final var expectedJson = "{\"command\":\"MOVE\",\"desc\":\"some description\"}"; + var command = new TestCommand(); + command.setCommand(TestCommandEnum.MOVE); + command.setDesc("some description"); - System.out.println("result: " + result); - assertNotNull(result); - assertEquals(expectedJson, result); + var result = ReflectUtils.createJson(command); - } + printInfo(result); + assertNotNull(result); + assertEquals(expectedJson, result); + } - @Test - void objectWithEnumListToJson() { + @Test + void objectWithEnumListToJson() { - final String expectedJson = "{\"commands\":[\"MOVE\",\"STOP\",\"BACK\"],\"desc\":\"commands description\"}"; - TestCommandList commands = new TestCommandList(); - commands.setCommands(Arrays.asList(TestCommandEnum.MOVE, TestCommandEnum.STOP, TestCommandEnum.BACK)); - commands.setDesc("commands description"); + final var expectedJson = "{\"commands\":[\"MOVE\",\"STOP\",\"BACK\"],\"desc\":\"commands description\"}"; + var commands = new TestCommandList(); + commands.setCommands(Arrays.asList(TestCommandEnum.MOVE, TestCommandEnum.STOP, TestCommandEnum.BACK)); + commands.setDesc("commands description"); - final String result = ReflectUtils.createJson(commands); - System.out.println("result: " + result); + final String result = ReflectUtils.createJson(commands); - assertNotNull(result); - assertEquals(expectedJson, result); + printInfo(result); + assertNotNull(result); + assertEquals(expectedJson, result); - } + } - @Test - void pathAttributesListToJsonTest() { - final String expectedJson = "{\"attributes\":[{\"name\":\"name\",\"value\":\"java.lang.String\"},{\"name\":\"values\",\"value\":\"java.util.HashMap\"}]}"; - PathAttributeListDTO attributes = new PathAttributeListDTO(); - attributes.addAttribute(new PathAttributeDTO("name", "java.lang.String")); - attributes.addAttribute(new PathAttributeDTO("values", "java.util.HashMap")); - String result = ReflectUtils.createJson(attributes); - System.out.println("result:" + result); - assertEquals(expectedJson, result); - } + @Test + void pathAttributesListToJsonTest() { + final var expectedJson = "{\"attributes\":[{\"name\":\"name\",\"value\":\"java.lang.String\"},{\"name\":\"values\",\"value\":\"java.util.HashMap\"}]}"; + var attributes = new PathAttributeListDTO(); + attributes.addAttribute(new PathAttributeDTO("name", "java.lang.String")); + attributes.addAttribute(new PathAttributeDTO("values", "java.util.HashMap")); - @Test - void serverAttributesResponse(){ - ResponseAttributeListDTO tmpAttr = new ResponseAttributeListDTO(); + String result = ReflectUtils.createJson(attributes); - tmpAttr.setType("Type"); - tmpAttr.setId("ID"); - //String roboUnit, HttpMethod method, List callbacks - tmpAttr.addValue(new HttpPathMethodDTO("testUnit", HttpMethod.GET, Collections.singletonList("test"))); - String result = ReflectUtils.createJson(tmpAttr); - System.out.println("result:" + result); + printInfo(result); + assertEquals(expectedJson, result); + } - } + @Test + void serverAttributesResponse() { + var tmpAttr = new ResponseAttributeListDTO(); + tmpAttr.setType("Type"); + tmpAttr.setId("ID"); + //String roboUnit, HttpMethod method, List callbacks + tmpAttr.addValue(new HttpPathMethodDTO("testUnit", HttpMethod.GET, Collections.singletonList("test"))); + String result = ReflectUtils.createJson(tmpAttr); + + printInfo(result); + } - @Test - void complexObjectToJsonTest(){ - String expectedJson = "{\"name\":\"object\",\"value\":42,\"textList\":[\"one\",\"two\"],\"dictionary\":{\"one\":\"one1\",\"two\":\"two2\"},\"attributes\":{\"one\":{\"name\":\"name\",\"value\":\"test name\"},\"two\":{\"name\":\"value\",\"value\":\"42\"}}}"; - TestListMapValues obj = new TestListMapValues(); + @Test + void complexObjectToJsonTest() { + final var expectedJson = "{\"name\":\"object\",\"value\":42,\"textList\":[\"one\",\"two\"],\"dictionary\":{\"one\":\"one1\",\"two\":\"two2\"},\"attributes\":{\"one\":{\"name\":\"name\",\"value\":\"test name\"},\"two\":{\"name\":\"value\",\"value\":\"42\"}}}"; + var obj = new TestListMapValues(); obj.setName("object"); obj.setValue(42); obj.setTextList(Arrays.asList("one", "two")); - Map textMap = new HashMap<>(); + var textMap = new HashMap(); textMap.put("one", "one1"); textMap.put("two", "two2"); obj.setDictionary(textMap); - Map attributes = new HashMap<>(); + var attributes = new HashMap(); attributes.put("one", new PathAttributeDTO("name", "test name")); attributes.put("two", new PathAttributeDTO("value", "42")); obj.setAttributes(attributes); - String result = ReflectUtils.createJson(obj); - System.out.println("result:"+result); + var result = ReflectUtils.createJson(obj); + + printInfo(result); assertEquals(expectedJson, result); } @Test void createJsonByStringListCollectionTest() { - String expectedJson = "[\"One\",\"Two\"]"; - List list = new ArrayList<>(); + final var expectedJson = "[\"One\",\"Two\"]"; + var list = new ArrayList(); list.add("One"); list.add("Two"); String result = ReflectUtils.createJson(list); - System.out.println("result: " + result); + + printInfo(result); assertEquals(expectedJson, result); + } + + @Test + void createJsonByNumberListCollectionTest() { + final var expectedJson = "[1,2]"; + var list = new ArrayList(); + list.add(1); + list.add(2); + + var result = ReflectUtils.createJson(list); + + printInfo(result); + assertEquals(expectedJson, result); + + } + + @Test + void createJsonByObjectListCollectionTest() { + + Throwable exception = assertThrows(RoboReflectException.class, () -> { + List list = new ArrayList<>(); + list.add(new PathAttributeDTO("one", "1")); + list.add(new PathAttributeDTO("two", "2")); + ReflectUtils.createJson(list); + }); + + assertTrue(exception.getMessage().startsWith("object getter value")); + } + + @Test + void createJsonByStringMapCollection() { + final var expectedString = "{\"one\":\"1\",\"two\":\"2\"}"; + var map = new HashMap(); + map.put("one", "1"); + map.put("two", "2"); + + var result = ReflectUtils.createJson(map); + + printInfo(result); + assertEquals(expectedString, result); + + } + + @Test + void createJsonByNumberMapCollection() { + final var expectedString = "{\"one\":1,\"two\":2}"; + var map = new HashMap(); + map.put("one", 1); + map.put("two", 2); + var result = ReflectUtils.createJson(map); + + printInfo(result); + assertEquals(expectedString, result); } - @Test - void createJsonByNumberListCollectionTest() { - String expectedJson = "[1,2]"; - List list = new ArrayList<>(); - list.add(1); - list.add(2); - - String result = ReflectUtils.createJson(list); - System.out.println("result: " + result); - assertEquals(expectedJson, result); - - } - - @Test - void createJsonByObjectListCollectionTest(){ - - Throwable exception = assertThrows(RoboReflectException.class, () -> { - List list = new ArrayList<>(); - list.add(new PathAttributeDTO("one", "1")); - list.add(new PathAttributeDTO("two", "2")); - ReflectUtils.createJson(list); - }); - - assertTrue(exception.getMessage().startsWith("object getter value")); - } - - @Test - void createJsonByStringMapCollection() { - String expectedString = "{\"one\":\"1\",\"two\":\"2\"}"; - Map map = new HashMap<>(); - map.put("one", "1"); - map.put("two", "2"); - - String result = ReflectUtils.createJson(map); - System.out.println("result: " + result); - assertEquals(expectedString, result); - - } - - @Test - void createJsonByNumberMapCollection() { - String expectedString = "{\"one\":1,\"two\":2}"; - Map map = new HashMap<>(); - map.put("one", 1); - map.put("two", 2); - - String result = ReflectUtils.createJson(map); - System.out.println("result: " + result); - assertEquals(expectedString, result); - } - - - @Test - void createJsonByObjectMapCollection(){ - - Throwable exception = assertThrows(RoboReflectException.class, () -> { - Map map = new HashMap<>(); - map.put("one", new PathAttributeDTO("one1", "1")); - map.put("two", new PathAttributeDTO("two2", "2")); - ReflectUtils.createJson(map); - }); - - assertTrue(exception.getMessage().startsWith("object getter value")); - } + + @Test + void createJsonByObjectMapCollection() { + + Throwable exception = assertThrows(RoboReflectException.class, () -> { + Map map = new HashMap<>(); + map.put("one", new PathAttributeDTO("one1", "1")); + map.put("two", new PathAttributeDTO("two2", "2")); + ReflectUtils.createJson(map); + }); + + assertTrue(exception.getMessage().startsWith("object getter value")); + } + + private static void printInfo(String result) { + LOGGER.info("result: {}", result); + } }