Skip to content

Commit

Permalink
Added test coverage for new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
basiliskus committed Dec 23, 2024
1 parent c157395 commit c524996
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

/** The HL7Path class represents a path to a specific field in an HL7 message. */
public record HL7Path(String segmentName, int[] indices) {

// Need to override equals, hashCode, and toString to handle array comparison
@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gov.hhs.cdc.trustedintermediary.rse2e.hl7

import spock.lang.Specification

class HL7PathTest extends Specification {

def "should create HL7Path with segment name and indices"() {
when:
def path = new HL7Path("MSH", [1, 2, 3] as int[])

then:
path.segmentName() == "MSH"
path.indices() == [1, 2, 3] as int[]
}

def "equals should compare array contents"() {
given:
def path1 = new HL7Path("MSH", [1, 2] as int[])
def path2 = new HL7Path("MSH", [1, 2] as int[])
def path3 = new HL7Path("MSH", [1, 3] as int[])

expect:
path1 == path2
path1 != path3
path1 != null
path1 == path1
}

def "equals should handle different segment names"() {
given:
def path1 = new HL7Path(segment1, [1, 2] as int[])
def path2 = new HL7Path(segment2, [1, 2] as int[])

expect:
(path1 == path2) == expectedResult

where:
scenario | segment1 | segment2 | expectedResult
"same segment" | "MSH" | "MSH" | true
"both null" | null | null | true
"diff segment" | "MSH" | "PID" | false
"null vs string" | null | "MSH" | false
}

def "hashCode should consider array contents"() {
given:
def path1 = new HL7Path("MSH", [1, 2] as int[])
def path2 = new HL7Path("MSH", [1, 2] as int[])

expect:
path1.hashCode() == path2.hashCode()
}

def "toString should include array contents"() {
given:
def path = new HL7Path("MSH", [1, 2] as int[])

expect:
path.toString() == "HL7Path[segmentName=MSH, indices=[1, 2]]"
}
}

0 comments on commit c524996

Please sign in to comment.