Skip to content

Commit 283ab8f

Browse files
authored
test: Refactoring and solves timezone sensitivity
- Makes them timezone-unaware by removing the TZ-offset from the reference JSON Consent - Makes the tests more self-contained by using the date info from the deserialized reference Consent instead of hard-coded values which are coupled to the JSON content anyway - Uses deep object equality between the target- and created Consent instances instead of String equality on their serialized form.
1 parent e7b5f61 commit 283ab8f

4 files changed

Lines changed: 167 additions & 178 deletions

File tree

src/test/java/de/ukt/mvh/TestConsentMapperParents_1_7_2_from_REDCap.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
package de.ukt.mvh;
22

33
import org.hl7.fhir.r4.model.Consent;
4-
import org.hl7.fhir.r4.model.Resource;
4+
import ca.uhn.fhir.parser.IParser;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77
import org.junit.jupiter.api.BeforeAll;
8-
9-
import java.io.FileReader;
8+
import java.io.InputStreamReader;
109
import java.text.ParseException;
11-
import java.text.SimpleDateFormat;
1210
import java.util.Date;
13-
import java.util.TimeZone;
14-
1511
import static ca.uhn.fhir.context.FhirContext.forR4Cached;
12+
import static org.apache.commons.lang3.time.DateUtils.addYears;
1613
import static org.junit.Assert.assertThrows;
1714

1815
public class TestConsentMapperParents_1_7_2_from_REDCap {
1916

17+
private static Consent targetConsent;
2018
private static Date birthday;
2119

2220
@BeforeAll
23-
private static void init() throws Exception {
24-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
25-
birthday = dateFormat.parse("2020-05-13T00:00:00+02");
26-
TimeZone.setDefault(TimeZone.getTimeZone("GMT+2:00"));
21+
public static void init() throws Exception {
22+
var classLoader = TestConsentMapper_1_7_2.class.getClassLoader();
23+
var jsonParser = forR4Cached().newJsonParser();
24+
targetConsent = jsonParser.parseResource(Consent.class, new InputStreamReader(classLoader.getResourceAsStream("consent_parents.json")));
25+
birthday = addYears(targetConsent.getProvision().getPeriod().getEnd(), -18);
2726
}
2827

2928

@@ -72,15 +71,7 @@ public void testConsentMapper() throws Exception {
7271
]
7372
""";
7473
Consent consent = ConsentMapperParents_1_7_2_from_REDCap.makeConsent(redCapExport,birthday);
75-
var jsonParser = forR4Cached().newJsonParser();
76-
77-
ClassLoader classLoader = getClass().getClassLoader();
78-
var targetConsent = (Resource) jsonParser.parseResource(new FileReader(classLoader.getResource("consent_parents.json").getPath()));
79-
80-
System.out.println(jsonParser.encodeResourceToString(targetConsent));
81-
System.out.println(jsonParser.encodeResourceToString(consent));
82-
83-
Assertions.assertEquals(jsonParser.encodeResourceToString(targetConsent), jsonParser.encodeResourceToString(consent));
74+
Assertions.assertTrue(consent.equalsDeep(targetConsent));
8475
}
8576

8677
@Test

src/test/java/de/ukt/mvh/TestConsentMapper_1_7_2.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,47 @@
55
import de.ukt.mvh.util.ConsentMapper_1_7_2;
66
import de.ukt.mvh.util.ConsentMapper_7_to_11_1_7_2;
77
import org.hl7.fhir.r4.model.Consent;
8-
import org.hl7.fhir.r4.model.Resource;
8+
import ca.uhn.fhir.parser.IParser;
99
import org.junit.jupiter.api.Assertions;
1010
import org.junit.jupiter.api.BeforeAll;
1111
import org.junit.jupiter.api.Test;
1212
import org.junit.jupiter.params.ParameterizedTest;
1313
import org.junit.jupiter.params.provider.CsvSource;
14-
15-
import java.io.FileReader;
16-
import java.text.SimpleDateFormat;
14+
import java.io.InputStreamReader;
1715
import java.util.Date;
18-
import java.util.TimeZone;
19-
2016
import static ca.uhn.fhir.context.FhirContext.forR4Cached;
17+
import static org.apache.commons.lang3.time.DateUtils.addYears;
2118

2219

2320
public class TestConsentMapper_1_7_2 {
21+
22+
private static Consent targetConsent;
2423
private static Date consentDate;
2524
private static Date birthday;
25+
2626
@BeforeAll
2727
public static void init() throws Exception {
28-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
29-
consentDate = dateFormat.parse("2025-06-27T00:00:00+02");
30-
birthday = dateFormat.parse("2020-05-13T00:00:00+02");
31-
TimeZone.setDefault(TimeZone.getTimeZone("GMT+2:00"));
28+
var classLoader = TestConsentMapper_1_7_2.class.getClassLoader();
29+
var jsonParser = forR4Cached().newJsonParser();
30+
targetConsent = jsonParser.parseResource(Consent.class, new InputStreamReader(classLoader.getResourceAsStream("consent.json")));
31+
consentDate = targetConsent.getDateTime();
32+
birthday = addYears(targetConsent.getProvision().getPeriod().getEnd(), -18);
3233
}
3334

34-
3535
@Test
3636
public void testConsentMapper() throws Exception {
3737
ConsentMapper_1_7_2 mapper = new ConsentMapper_1_7_2();
3838
Consent consent = mapper.makeConsent(consentDate);
39-
consent.setProvision(mapper.makeProvisions(
40-
consentDate,
41-
birthday,
42-
true, true, true,true,true,true,true,true,true, null, null));
43-
var jsonParser = forR4Cached().newJsonParser();
44-
45-
ClassLoader classLoader = getClass().getClassLoader();
46-
var targetConsent = (Resource) jsonParser.parseResource(new FileReader(classLoader.getResource("consent.json").getPath()));
47-
Assertions.assertEquals(jsonParser.encodeResourceToString(targetConsent), jsonParser.encodeResourceToString(consent));
39+
consent.setProvision(
40+
mapper.makeProvisions(
41+
consentDate,
42+
birthday,
43+
true,true,true,true,true,true,true,true,true,null,null
44+
)
45+
);
46+
Assertions.assertTrue(consent.equalsDeep(targetConsent));
4847
}
4948

50-
5149
@Test
5250
public void testConsentMapperParents() throws Exception {
5351
ConsentMapperParents_1_7_2 mapper = new ConsentMapperParents_1_7_2();

0 commit comments

Comments
 (0)