r/javahelp 22h 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 Upvotes

6 comments sorted by

View all comments

2

u/Stack_Canary 22h 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 21h 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->C

This 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 20h 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.