Browse Source

After review: update logic for method invoc, update parametrized test

pull/9534/head
Andrii Landiak 3 years ago
parent
commit
cb2d95cdf6
  1. 13
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java
  2. 37
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbHttpClientTest.java

13
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java

@ -194,7 +194,7 @@ public class TbHttpClient {
config.isIgnoreRequestBody()) {
entity = new HttpEntity<>(headers);
} else {
entity = new HttpEntity<>(getData(msg), headers);
entity = new HttpEntity<>(getData(msg, config.isIgnoreRequestBody(), config.isParseToPlainText()), headers);
}
URI uri = buildEncodedUri(endpointUrl);
@ -242,12 +242,15 @@ public class TbHttpClient {
return uri;
}
private String getData(TbMsg tbMsg) {
return parseJsonStringToPlainText(tbMsg.getData(), config.isParseToPlainText());
private String getData(TbMsg tbMsg, boolean ignoreBody, boolean parseToPlainText) {
if (!ignoreBody && parseToPlainText) {
return parseJsonStringToPlainText(tbMsg.getData());
}
return tbMsg.getData();
}
protected String parseJsonStringToPlainText(String data, boolean parseToJson) {
if (parseToJson && data.startsWith("\"") && data.endsWith("\"") && data.length() >= 2) {
protected String parseJsonStringToPlainText(String data) {
if (data.startsWith("\"") && data.endsWith("\"") && data.length() >= 2) {
final String dataBefore = data;
try {
data = JacksonUtil.fromString(data, String.class);

37
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbHttpClientTest.java

@ -24,8 +24,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockserver.integration.ClientAndServer;
@ -43,14 +42,12 @@ import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.willCallRealMethod;
@ -231,35 +228,15 @@ public class TbHttpClientTest {
}
@ParameterizedTest
@MethodSource("provideParameters")
@ValueSource(strings = { "false", "\"", "\"\"", "\"This is a string with double quotes\"", "Path: /home/developer/test.txt",
"First line\nSecond line\n\nFourth line", "Before\rAfter", "Tab\tSeparated\tValues", "Test\bbackspace", "[]",
"[1, 2, 3]", "{\"key\": \"value\"}", "{\n\"temperature\": 25.5,\n\"humidity\": 50.2\n\"}", "Expression: (a + b) * c",
"世界", "Україна", "\u1F1FA\u1F1E6", "🇺🇦"})
public void testParseJsonStringToPlainText(String original) {
Mockito.when(client.parseJsonStringToPlainText(anyString(), anyBoolean())).thenCallRealMethod();
Mockito.when(client.parseJsonStringToPlainText(anyString())).thenCallRealMethod();
String serialized = JacksonUtil.toString(original);
Assertions.assertNotNull(serialized);
Assertions.assertEquals(original, client.parseJsonStringToPlainText(serialized, true));
}
private static Stream<Arguments> provideParameters() {
return Stream.of(Arguments.of("false"),
Arguments.of("\""),
Arguments.of("\"\""),
Arguments.of("\"\"\""),
Arguments.of("\"This is a string with double quotes\""),
Arguments.of("Path: /home/developer/test.txt"),
Arguments.of("First line\nSecond line\n\nFourth line"),
Arguments.of("Before\rAfter"),
Arguments.of("Tab\tSeparated\tValues"),
Arguments.of("Test\bbackspace"),
Arguments.of("[]"),
Arguments.of("[1, 2, 3]"),
Arguments.of("{\"key\": \"value\"}"),
Arguments.of("{\n\"temperature\": 25.5,\n\"humidity\": 50.2\n\"}"),
Arguments.of("Expression: (a + b) * c"),
Arguments.of("世界"),
Arguments.of("Україна"),
Arguments.of("\u1F1FA\u1F1E6"),
Arguments.of("🇺🇦")
);
Assertions.assertEquals(original, client.parseJsonStringToPlainText(serialized));
}
}

Loading…
Cancel
Save