Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new options in xml conversion #42440

Merged
merged 13 commits into from
Apr 5, 2024

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,41 @@ public class XMLToRecordConverterTests {
private final Path sample16Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_16.bal");

private final Path sample17XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_17.xml");
private final Path sample17Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_17.bal");

private final Path sample18XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_18.xml");
private final Path sample18Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_18.bal");

private final Path sample19XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_19.xml");
private final Path sample19Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_19.bal");

private final Path sample20XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_20.xml");
private final Path sample20Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_20.bal");

private final Path sample21XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_21.xml");
private final Path sample21Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_21.bal");

private final Path sample22XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_22.xml");
private final Path sample22Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_22.bal");

private final Path sample23XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_23.xml");
private final Path sample23Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_23.bal");

private static final String XMLToRecordServiceEP = "xmlToRecord/convert";


Expand Down Expand Up @@ -280,6 +315,69 @@ public void testComplexXMLWithMultipleNamespacesAndRecurringNodes() throws IOExc
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testWithAttribute")
public void testWithAttribute() throws IOException {
String xmlFileContent = Files.readString(sample17XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false)
.getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample17Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testWithMultipleAttributes")
public void testWithMultipleAttributes() throws IOException {
String xmlFileContent = Files.readString(sample18XML);
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false)
.getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample18Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithNamespacesWithoutNamespaceAnnotation")
public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException {
String xmlFileContent = Files.readString(sample19XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
"amount", false).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample19Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations")
public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throws IOException {
String xmlFileContent = Files.readString(sample20XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
null, false).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample20Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithMultipleAttributesAndNamespacesWithAnnotations")
public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws IOException {
String xmlFileContent = Files.readString(sample21XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample21Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithoutNamespacePrefix")
public void testXMLWithoutNamespacePrefix() throws IOException {
String xmlFileContent = Files.readString(sample22XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample22Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLWithConflictingElementAndAttributeNames")
public void testXMLWithConflictingElementAndAttributeNames() throws IOException {
String xmlFileContent = Files.readString(sample23XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample23Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "testXMLToRecordService")
public void testXMLToRecordService() throws IOException, ExecutionException, InterruptedException {
Endpoint serviceEndpoint = TestUtil.initializeLanguageSever();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
type Main_MainElement record {
string[] child\:childElement;
@xmldata:Namespace {
prefix: "child",
uri:"http://example.com/child"
}
string[] childElement;
};

@xmldata:Name {
value: "root"
}
@xmldata:Namespace {
prefix: "root",
prefix: "root",
uri: "http://example.com/root"
}
type Root_Root record {
Main_MainElement main\:mainElement;
@xmldata:Attribute
string xmlns\:child = "http://example.com/child";
@xmldata:Attribute
string xmlns\:main = "http://example.com/main";
@xmldata:Namespace {
prefix: "main",
uri: "http://example.com/main"
}
Main_MainElement mainElement;
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
type Main_Location record {
string main\:city;
string main\:state;
@xmldata:Namespace {
prefix: "main",
uri: "http://example.com/main"
}
string city;
@xmldata:Namespace {
prefix: "main",
uri: "http://example.com/main"
}
string state;
};

type Main_CompanyInfo record {
string main\:name;
Main_Location main\:location;
@xmldata:Namespace {
prefix: "main",
uri: "http://example.com/main"
}
string name;
@xmldata:Namespace {
prefix: "main",
uri: "http://example.com/main"
}
Main_Location location;
};

type Project record {
Expand Down Expand Up @@ -47,11 +63,13 @@ type Mixed record {
uri: "http://example.com/root"
}
type Root_Root record {
Main_CompanyInfo main\:companyInfo;
@xmldata:Namespace {
prefix: "main",
uri: "http://example.com/main"
}
Main_CompanyInfo companyInfo;
Employees employees;
Mixed mixed;
@xmldata:Attribute
string version;
@xmldata:Attribute
string xmlns\:main = "http://example.com/main";
};
Original file line number Diff line number Diff line change
@@ -1,39 +1,77 @@
type C_Course record {
string u\:name;
int c\:intake?;
string p\:professor?;
@xmldata:Namespace {
prefix: "u",
uri: "http://example.com/university"
}
string name;
@xmldata:Namespace {
prefix: "c",
uri: "http://example.com/course"
}
int intake?;
@xmldata:Namespace {
prefix: "p",
uri: "http://example.com/professor"
}
string professor?;
};

type D_SubDepartment record {
string d\:name;
C_Course[] c\:course?;
D_SubDepartment[] d\:subDepartment?;
@xmldata:Namespace {
prefix: "d",
uri: "http://example.com/department"
}
string name;
@xmldata:Namespace {
prefix: "c",
uri: "http://example.com/course"
}
C_Course[] course?;
@xmldata:Namespace {
prefix: "d",
uri: "http://example.com/department"
}
D_SubDepartment[] subDepartment?;
};

type D_Department record {
string u\:name;
C_Course[] c\:course;
D_SubDepartment d\:subDepartment?;
@xmldata:Namespace {
prefix: "u",
uri: "http://example.com/university"
}
string name;
@xmldata:Namespace {
prefix: "c",
uri: "http://example.com/course"
}
C_Course[] course;
@xmldata:Namespace {
prefix: "d",
uri: "http://example.com/department"
}
D_SubDepartment subDepartment?;
};

type F_Faculty record {
string u\:name;
D_Department[] d\:department;
@xmldata:Namespace {
prefix: "u",
uri: "http://example.com/university"
}
string name;
@xmldata:Namespace {
prefix: "d",
uri: "http://example.com/department"
}
D_Department[] department;
};

@xmldata:Name {
value: "university"
}
type University record {
F_Faculty[] f\:faculty;
@xmldata:Attribute
string xmlns\:c = "http://example.com/course";
@xmldata:Attribute
string xmlns\:d = "http://example.com/department";
@xmldata:Attribute
string xmlns\:f = "http://example.com/faculty";
@xmldata:Attribute
string xmlns\:p = "http://example.com/professor";
@xmldata:Attribute
string xmlns\:u = "http://example.com/university";
@xmldata:Namespace {
prefix: "f",
uri: "http://example.com/faculty"
}
F_Faculty[] faculty;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Title record {
string \#content;
@xmldata:Attribute
string lang;
};

@xmldata:Name {value: "book"}
type Book record {
string author;
Title title;
string genre;
decimal price;
string publish_date;
string description;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type Title record {
string \#content;
@xmldata:Attribute
string lang;
};

type Genre record {
string \#content;
@xmldata:Attribute
string category;
};

@xmldata:Name {value: "book"}
type Book record {
string author;
Title title;
Genre genre;
decimal price;
string publish_date;
string description;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type Book_Author record {
string name;
string country;
};

type Meta_Reviews record {
string[] review;
};

type Book_Book record {
string title;
Book_Author author;
string genre;
string publication_date;
decimal rating;
Meta_Reviews reviews;
};

@xmldata:Name {value: "library"}
type Library record {
Book_Book book;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
type Book_Title record {
string \#content;
@xmldata:Attribute
string edition;
@xmldata:Attribute
string lang;
};

type Book_Author record {
string \#content;
@xmldata:Attribute
string gender;
@xmldata:Attribute
string nationality;
};

type Genre_Genre record {
string \#content;
@xmldata:Attribute
string category;
@xmldata:Attribute
string 'type;
};

type Currency_Price record {
string \#content;
@xmldata:Attribute
string currency;
@xmldata:Attribute
string discounted;
};

type Book_Book record {
Book_Title title;
Book_Author author;
Genre_Genre genre;
Currency_Price price;
string publish_date;
string description;
};

@xmldata:Name {value: "library"}
type Library record {
Book_Book book;
};
Loading
Loading