From 311d04ee7054f7dd949a3f8da25fce212eaf86f6 Mon Sep 17 00:00:00 2001 From: mindula Date: Wed, 26 Jun 2024 14:27:17 +0530 Subject: [PATCH] Add tests --- .../XMLToRecordConverterTests.java | 72 ++++++++++++++----- .../test/resources/ballerina/sample_35.bal | 11 +++ .../test/resources/ballerina/sample_36.bal | 22 ++++++ .../test/resources/ballerina/sample_37.bal | 37 ++++++++++ .../src/test/resources/xml/sample_35.xml | 8 +++ .../src/test/resources/xml/sample_36.xml | 13 ++++ 6 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml diff --git a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java index d4a7818a9f21..4e33124984d6 100644 --- a/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java +++ b/misc/xml-to-record-converter/src/test/java/io/ballerina/xmltorecordconverter/XMLToRecordConverterTests.java @@ -214,6 +214,18 @@ public class XMLToRecordConverterTests { private final Path sample34Bal = RES_DIR.resolve(BAL_DIR) .resolve("sample_34.bal"); + private final Path sample35XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_35.xml"); + private final Path sample35Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_35.bal"); + private final Path sample36Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_36.bal"); + + private final Path sample36XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_36.xml"); + private final Path sample37Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_37.bal"); + private static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -392,7 +404,7 @@ public void testWithMultipleAttributes() throws IOException { public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException { String xmlFileContent = Files.readString(sample19XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "amount", false).getCodeBlock().replaceAll("\\s+", ""); + "amount", false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample19Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -401,7 +413,7 @@ public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throws IOException { String xmlFileContent = Files.readString(sample20XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample20Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -410,7 +422,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throw public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws IOException { String xmlFileContent = Files.readString(sample21XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample21Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -419,7 +431,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws I public void testXMLWithoutNamespacePrefix() throws IOException { String xmlFileContent = Files.readString(sample22XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample22Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -428,7 +440,7 @@ public void testXMLWithoutNamespacePrefix() throws IOException { public void testXMLWithConflictingElementAndAttributeNames() throws IOException { String xmlFileContent = Files.readString(sample23XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample23Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -437,7 +449,7 @@ public void testXMLWithConflictingElementAndAttributeNames() throws IOException public void testXMLWithoutNamespaces() throws IOException { String xmlFileContent = Files.readString(sample24XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample24Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -447,7 +459,7 @@ public void testXMLToRecordService() throws IOException, ExecutionException, Int Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample0XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true, false, false); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); @@ -461,7 +473,8 @@ public void testXMLToRecordServiceWithFieldNameAndWithoutNamespace() Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample25XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", false); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", + false, false, false); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); @@ -482,7 +495,7 @@ public void testXMLWithMultipleAttributes() throws IOException { public void testXMLWithoutNamespaceAnnotations() throws IOException { String xmlFileContent = Files.readString(sample27XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample27Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -491,7 +504,7 @@ public void testXMLWithoutNamespaceAnnotations() throws IOException { public void testXMLWithMultipleNamespacesAndSameElement() throws IOException { String xmlFileContent = Files.readString(sample28XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample28Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -500,7 +513,7 @@ public void testXMLWithMultipleNamespacesAndSameElement() throws IOException { public void testXMLWithMultipleNamespaces2() throws IOException { String xmlFileContent = Files.readString(sample29XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample29Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -509,7 +522,7 @@ public void testXMLWithMultipleNamespaces2() throws IOException { public void testXMLWithMultipleNamespaces3() throws IOException { String xmlFileContent = Files.readString(sample30XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample30Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -518,7 +531,7 @@ public void testXMLWithMultipleNamespaces3() throws IOException { public void testXMLWithoutNamespaceAnnotation() throws IOException { String xmlFileContent = Files.readString(sample31XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample31Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -527,7 +540,7 @@ public void testXMLWithoutNamespaceAnnotation() throws IOException { public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOException { String xmlFileContent = Files.readString(sample32XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, false).getCodeBlock().replaceAll("\\s+", ""); + null, false, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample32Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -536,7 +549,7 @@ public void testXMLWithSameElementAndWithoutMultipleNamespaces() throws IOExcept public void textXMLWithDefaultValueNode() throws IOException { String xmlFileContent = Files.readString(sample33XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - null, true).getCodeBlock().replaceAll("\\s+", ""); + null, true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample33Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -545,8 +558,35 @@ public void textXMLWithDefaultValueNode() throws IOException { public void textXMLWithDefaultValueNode2() throws IOException { String xmlFileContent = Files.readString(sample34XML); String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, - "__text", true).getCodeBlock().replaceAll("\\s+", ""); + "__text", true, false, false).getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample34Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } + + @Test(description = "textXMLWithoutAttributes") + public void textXMLWithoutAttributes() throws IOException { + String xmlFileContent = Files.readString(sample35XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + null, true, true, false).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample35Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } + + @Test(description = "textXMLWithoutAttributeAnnotation") + public void textXMLWithoutAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample35XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample36Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } + + @Test(description = "textXMLWithoutMultipleAttributeAnnotation") + public void textXMLWithoutMultipleAttributeAnnotation() throws IOException { + String xmlFileContent = Files.readString(sample36XML); + String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false, + "__text", false, false, true).getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample37Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal new file mode 100644 index 000000000000..7e40d68e6076 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_35.bal @@ -0,0 +1,11 @@ +type Book record { + string title; + string author; + int year; + decimal price; +}; + +@xmldata:Name {value: "bookstore"} +type Bookstore record { + Book book; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal new file mode 100644 index 000000000000..30d9306ba750 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_36.bal @@ -0,0 +1,22 @@ +type Title record { + string __text; + string lang; +}; + +type Price record { + decimal __text; + string currency; +}; + +type Book record { + Title title; + string author; + int year; + Price price; + string category; +}; + +@xmldata:Name {value: "bookstore"} +type Bookstore record { + Book book; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal new file mode 100644 index 000000000000..de7054486b68 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_37.bal @@ -0,0 +1,37 @@ +type Book_Title record { + string __text; + string edition; + string format; + string lang; + string pages; +}; + +type Book_Price record { + decimal __text; + string currency; +}; + +type Book_Book record { + Book_Title title; + string author; + string publisher; + string publish_date; + int isbn; + Book_Price price; + string available; + string id; + string shelf; +}; + +type Books record { + string genre; + Book_Book book; +}; + +@xmldata:Name {value: "library"} +type Library record { + Books books; + string established; + string genre; + string location; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml new file mode 100644 index 000000000000..fb2f40571d64 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_35.xml @@ -0,0 +1,8 @@ + + + Harry Potter + J.K. Rowling + 2005 + 29.99 + + diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml new file mode 100644 index 000000000000..c0f3d434cd84 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml @@ -0,0 +1,13 @@ + + + Programming + + XML Developer's Guide + John Doe + Tech Press + 2000-10-01 + 1234567890 + 49.99 + + +