diff --git a/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java b/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java index 387436864..de029a771 100644 --- a/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java +++ b/components/serialization/text/src/main/java/com/microsoft/kiota/serialization/TextParseNode.java @@ -27,13 +27,14 @@ public class TextParseNode implements ParseNode { /** * Initializes a new instance of the {@link TextParseNode} class. + * Enclosing double quotes are stripped when the string length is at least 2. * @param rawText the raw text to parse. */ public TextParseNode(@Nonnull final String rawText) { Objects.requireNonNull(rawText, "parameter node cannot be null"); text = - rawText.startsWith("\"") && rawText.endsWith("\"") - ? rawText.substring(1, rawText.length() - 2) + rawText.length() >= 2 && rawText.startsWith("\"") && rawText.endsWith("\"") + ? rawText.substring(1, rawText.length() - 1) : rawText; } diff --git a/components/serialization/text/src/test/java/com/microsoft/kiota/serialization/TextParseNodeTest.java b/components/serialization/text/src/test/java/com/microsoft/kiota/serialization/TextParseNodeTest.java index 6b88e405d..b65a80ce4 100644 --- a/components/serialization/text/src/test/java/com/microsoft/kiota/serialization/TextParseNodeTest.java +++ b/components/serialization/text/src/test/java/com/microsoft/kiota/serialization/TextParseNodeTest.java @@ -36,4 +36,28 @@ void testInvalidOffsetDateTimeStringThrowsException(final String dateTimeString) assertTrue(ex.getMessage().contains(dateTimeString)); } } + + @Test + void testParsesQuotedString() { + final var node = new TextParseNode("\"hello world\""); + assertEquals("hello world", node.getStringValue()); + } + + @Test + void testParsesEmptyQuotedString() { + final var node = new TextParseNode("\"\""); + assertEquals("", node.getStringValue()); + } + + @Test + void testParsesSingleQuoteString() { + final var node = new TextParseNode("\""); + assertEquals("\"", node.getStringValue()); + } + + @Test + void testParsesUnquotedString() { + final var node = new TextParseNode("hello world"); + assertEquals("hello world", node.getStringValue()); + } } diff --git a/components/serialization/text/src/test/java/com/microsoft/kiota/serialization/TextSerializationWriterTest.java b/components/serialization/text/src/test/java/com/microsoft/kiota/serialization/TextSerializationWriterTest.java new file mode 100644 index 000000000..1a760e085 --- /dev/null +++ b/components/serialization/text/src/test/java/com/microsoft/kiota/serialization/TextSerializationWriterTest.java @@ -0,0 +1,102 @@ +package com.microsoft.kiota.serialization; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.stream.Collectors; + +public class TextSerializationWriterTest { + + private String readStream(final TextSerializationWriter writer) { + try (final var is = writer.getSerializedContent(); + final var isr = new InputStreamReader(is, StandardCharsets.UTF_8); + final var reader = new BufferedReader(isr)) { + return reader.lines().collect(Collectors.joining("\n")); + } catch (final Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + void testWritesStringValue() { + try (final var writer = new TextSerializationWriter()) { + writer.writeStringValue(null, "hello world"); + assertEquals("hello world", readStream(writer)); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + void testWritesBooleanValue() { + try (final var writer = new TextSerializationWriter()) { + writer.writeBooleanValue(null, true); + assertEquals("true", readStream(writer)); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + void testWritesIntegerValue() { + try (final var writer = new TextSerializationWriter()) { + writer.writeIntegerValue(null, 42); + assertEquals("42", readStream(writer)); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + void testWritesByteArrayValue() { + try (final var writer = new TextSerializationWriter()) { + writer.writeByteArrayValue(null, "test".getBytes(StandardCharsets.UTF_8)); + assertEquals("dGVzdA==", readStream(writer)); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + void testThrowsWhenKeyIsProvided() { + try (final var writer = new TextSerializationWriter()) { + assertThrows( + UnsupportedOperationException.class, + () -> writer.writeStringValue("key", "value")); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + void testThrowsWhenWritingMultipleValues() { + try (final var writer = new TextSerializationWriter()) { + writer.writeStringValue(null, "first"); + assertThrows( + UnsupportedOperationException.class, + () -> writer.writeStringValue(null, "second")); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + void testThrowsOnStructuredDataCalls() { + try (final var writer = new TextSerializationWriter()) { + assertThrows( + UnsupportedOperationException.class, + () -> writer.writeCollectionOfPrimitiveValues(null, Collections.emptyList())); + assertThrows( + UnsupportedOperationException.class, + () -> writer.writeAdditionalData(Collections.emptyMap())); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } +}