Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mindula committed Aug 13, 2024
1 parent 6f9475f commit 311d04e
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";


Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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+", "");
Expand All @@ -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+", "");
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Book record {
string title;
string author;
int year;
decimal price;
};

@xmldata:Name {value: "bookstore"}
type Bookstore record {
Book book;
};
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J.K. Rowling</author>
<year>2005</year>
<price currency="USD">29.99</price>
</book>
</bookstore>
13 changes: 13 additions & 0 deletions misc/xml-to-record-converter/src/test/resources/xml/sample_36.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<library xmlns:book="http://example.com/books" genre="Programming" location="City Library" established="1985">
<books>
<genre>Programming</genre>
<book:book id="1" shelf="A3" available="true">
<book:title lang="en" edition="1st" pages="500" format="hardcover">XML Developer's Guide</book:title>
<book:author>John Doe</book:author>
<book:publisher>Tech Press</book:publisher>
<book:publish_date>2000-10-01</book:publish_date>
<book:isbn>1234567890</book:isbn>
<book:price currency="USD">49.99</book:price>
</book:book>
</books>
</library>

0 comments on commit 311d04e

Please sign in to comment.