From bc0c5f854873aab90e87fec87efe2a573314989d Mon Sep 17 00:00:00 2001 From: LakshanWeerasinghe Date: Mon, 8 Apr 2024 11:34:31 +0530 Subject: [PATCH] Remove unreachable setters and add tests --- .../XMLToRecordRequest.java | 32 +++++------------ .../XMLToRecordConverterTests.java | 21 +++++++++++- .../test/resources/ballerina/sample_25.bal | 34 +++++++++++++++++++ .../src/test/resources/xml/sample_25.xml | 11 ++++++ 4 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 misc/xml-to-record-converter/src/test/resources/ballerina/sample_25.bal create mode 100644 misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml diff --git a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java index 9ff65e0a6ebb..be28d7cadfb4 100644 --- a/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java +++ b/misc/xml-to-record-converter/src/main/java/io/ballerina/xmltorecordconverter/XMLToRecordRequest.java @@ -25,53 +25,39 @@ */ public class XMLToRecordRequest { - private String xmlValue; - private boolean isRecordTypeDesc; - private boolean isClosed; - private boolean forceFormatRecordFields; - private String textFieldName; - private boolean withNameSpace; + private final String xmlValue; + private final boolean isRecordTypeDesc; + private final boolean isClosed; + private final boolean forceFormatRecordFields; + private final String textFieldName; + private final boolean withNameSpace; public XMLToRecordRequest(String xmlValue, boolean isRecordTypeDesc, boolean isClosed, - boolean forceFormatRecordFields) { + boolean forceFormatRecordFields, String textFieldName, boolean withNameSpace) { this.xmlValue = xmlValue; this.isRecordTypeDesc = isRecordTypeDesc; this.isClosed = isClosed; this.forceFormatRecordFields = forceFormatRecordFields; + this.textFieldName = textFieldName; + this.withNameSpace = withNameSpace; } public String getXmlValue() { return xmlValue; } - public void setXmlValue(String xmlValue) { - this.xmlValue = xmlValue; - } - public boolean getIsRecordTypeDesc() { return isRecordTypeDesc; } - public void setIsRecordTypeDesc(boolean isRecordTypeDesc) { - this.isRecordTypeDesc = isRecordTypeDesc; - } - public boolean getIsClosed() { return isClosed; } - public void setIsClosed(boolean isClosed) { - this.isClosed = isClosed; - } - public boolean getForceFormatRecordFields() { return forceFormatRecordFields; } - public void setForceFormatRecordFields(boolean forceFormatRecordFields) { - this.forceFormatRecordFields = forceFormatRecordFields; - } - public String getTextFieldName() { return textFieldName; } 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 fb01aeb065a6..d85b701008a0 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 @@ -164,6 +164,11 @@ public class XMLToRecordConverterTests { private final Path sample24Bal = RES_DIR.resolve(BAL_DIR) .resolve("sample_24.bal"); + private final Path sample25XML = RES_DIR.resolve(XML_DIR) + .resolve("sample_25.xml"); + private final Path sample25Bal = RES_DIR.resolve(BAL_DIR) + .resolve("sample_25.bal"); + private static final String XMLToRecordServiceEP = "xmlToRecord/convert"; @@ -397,11 +402,25 @@ public void testXMLToRecordService() throws IOException, ExecutionException, Int Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String xmlValue = Files.readString(sample0XML); - XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false); + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true); CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); XMLToRecordResponse response = (XMLToRecordResponse) result.get(); String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample0Bal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } + + @Test(description = "Test xml record request with text field name and without namespace") + public void testXMLToRecordServiceWithFieldNameAndWithoutNamespace() + throws IOException, ExecutionException, InterruptedException { + Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); + String xmlValue = Files.readString(sample25XML); + + XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", false); + CompletableFuture result = serviceEndpoint.request(XMLToRecordServiceEP, request); + XMLToRecordResponse response = (XMLToRecordResponse) result.get(); + String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(sample25Bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); + } } diff --git a/misc/xml-to-record-converter/src/test/resources/ballerina/sample_25.bal b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_25.bal new file mode 100644 index 000000000000..bcec66c509fb --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/ballerina/sample_25.bal @@ -0,0 +1,34 @@ +type Book_Title record { + string __text; + @xmldata:Attribute + string edition; + @xmldata:Attribute + string lang; +}; + +type Book_Author record { + string __text; + @xmldata:Attribute + string gender; + @xmldata:Attribute + string nationality; +}; + +type Book_Book record { + Book_Title title; + Book_Author author; + string publish_date; + string description; +}; + +type Books record { + string genre; + Book_Book book; +}; + +@xmldata:Name {value: "library"} +type Library record { + Books books; + @xmldata:Attribute + string genre; +}; diff --git a/misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml b/misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml new file mode 100644 index 000000000000..37f730ac3fe6 --- /dev/null +++ b/misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml @@ -0,0 +1,11 @@ + + + Programming + + XML Developer's Guide + Gambardella, Matthew + 2000-10-01 + An in-depth look at creating applications with XML. + + +