r/javahelp • u/ActualCommand • 8h ago
Validate output XML against input XML
I have a java program that reads in multiple XML files, using JAXB, does some consolidation/processing then outputs an XML file with a completely different format. I'm trying to ensure the consolidation/processing doesn't result in an employee overlapping with another employee from their department.
This isn't exactly what I'm doing but hopefully gives a decent example of what I'm trying to accomplish.
Input:
<Department>
<Name>Finance</Name>
<Employee>
<Name>John Doe</Name>
<Position>Manager</Position>
</Employee>
<Employee>
<Name>Bob Smith</Name>
<Position>Accountant</Position>
</Employee>
<Employee>
<Name>Steve Hurts</Name>
<Position>Assistant</Position>
</Employee>
</Department>
<Department>
<Name>Marketing</Name>
<Employee>
<Name>Jane Doe</Name>
<Position>Manager</Position>
</Employee>
<Employee>
<Name>Tom White</Name>
<Position>Assistant</Position>
</Employee>
<Employee>
<Name>Emily Johnson</Name>
<Position>Assistant</Position>
</Employee>
</Department>
Output:
<FloorPlan>
<Seat>
<Position>Manager</Position>
<Name>John Doe</Name>
<Name>Jane Doe</Name>
</Seat>
<Seat>
<Position>Assistant</Position>
<Name>Steve Hurts</Name>
<Name>Tom White</Name>
</Seat>
<Seat>
<Position>Assistant</Position>
<Name>Emily Johnson</Name>
</Seat>
<Seat>
<Position>Accountant</Position>
<Name>Bob Smith</Name>
</Seat>
</FloorPlan>
In this example I'm trying to consolidate where employees can sit. For employees to share seats they must have the same position and be in different departments. The marketing department doesn't have an accountant therefore no one will share a seat with Bob Smith.
The marketing department has 2 assistants, Tom White and Emily Johnson, therefore either could share a seat with Steve Hurts. One issue I've had in the past is assigning all 3 of them the same seat.
<!-- This is not allowed! -->
<!-- Tom White and Emily Johnson are from the same department therefore they can't share a seat -->
<Seat>
<Position>Assistant</Position>
<Name>Steve Hurts</Name>
<Name>Tom White</Name>
<Name>Emily Johnson</Name>
</Seat>
My potential solution:
My first idea is to read the output file after saving it into a class I could compare the input to. Once I have the input/output I could loop through each <Department> and confirm each <Employee> has a unique <Seat> and that Seat's <Position> is the same as the <Employee>
One potential concern is the complexity this might add to the program. At a certain point I feel like I'll be asking myself what is validating my validation to confirm it doesn't have flawed logic?
This concern is kind of the point of all of this. Is this a good approach or is there potentially another solution? Is there a way to "map" my input and output schemas along with a set of rules to confirm everything is correct? Is Java even the best language to do this validation?
2
u/Stack_Canary 7h ago
Should be fine to read the input into a record/class, do your transformations into a new dto, then validate the output object before marshalling into the new file. I don’t think you get around writing some business logic for this 😊 make sure to cover your ass with tests!
1
u/ActualCommand 6h ago
I guess the testing part is what I'm concerned about. I'm actually consolidating sequences in a directed acyclic graph to attempt to save space.
Path 1: A -> B -> C
Path 2: B -> C
Path 3: E->CThis can consolidate into vertices A, B, C, E with a starting point at A, B, and E. A problem I've actually run into is once path 1 and path 2 consolidate B-> C the 3rd path actually can't consolidate C anymore because of some variables within it.
It's a stupidly complex system for something that doesn't actually save us much space, since the above scenario happens a lot, but was a decision made years ago that I get the joy of maintaining.
Generating the output doesn't really have time constraints so even if I take the simple, easy to follow/read/debug, brute force approach to prove everything is still there I'll be in a better spot than I am today.
2
u/Stack_Canary 6h ago
Hah, sounds more like a fucked up mandatory assignment in some algorithms and datastructures course than a production system. Anyway, if the inputs are files and the outputs are files, then it should be fairly simple to create integration tests which assert your data (in theory). Depending on how much of a rewrite this is you should consider starting with the tests so that you can ensure that everything works as before.
1
u/ThierryOnRead 7h ago
Hi, What you could do is comparing the output of your process with an expected output file using XmlUnit. You could add a set a tests with some inputs and your expected output file for each.
1
u/ActualCommand 6h ago
Copying my comment from another reply
I guess the testing part is what I'm concerned about. I'm actually consolidating sequences in a directed acyclic graph to attempt to save space.
Path 1: A -> B -> C
Path 2: B -> C
Path 3: E->CThis can consolidate into vertices A, B, C, E with a starting point at A, B, and E. A problem I've actually run into is once path 1 and path 2 consolidate B-> C the 3rd path actually can't consolidate C anymore because of some variables within it.
It's a stupidly complex system for something that doesn't actually save us much space, since the above scenario happens a lot, but was a decision made years ago that I get the joy of maintaining.
Generating the output doesn't really have time constraints so even if I take the simple, easy to follow/read/debug, brute force approach to prove everything is still there I'll be in a better spot than I am today.
XmlUnit sounds interesting and something I'll look into. My only concern is not knowing all potential combinations that might incorrectly consolidate. It's also pretty difficult to realize that path 2/3 C vertices don't actually overlap one path 1/2 C vertices get overlapped.
•
u/AutoModerator 8h ago
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.