Skip to content

Commit 41cc677

Browse files
NIAD-1136: Removed lenient from tests
1 parent 1d7300a commit 41cc677

File tree

4 files changed

+123
-149
lines changed

4 files changed

+123
-149
lines changed

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/mapper/AgentDirectoryMapperTest.java

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatThrownBy;
55
import static org.mockito.ArgumentMatchers.any;
6-
import static org.mockito.Mockito.lenient;
76
import static org.mockito.Mockito.when;
8-
97
import static uk.nhs.adaptors.gp2gp.utils.IdUtil.buildReference;
108

119
import org.hl7.fhir.dstu3.model.Bundle;
@@ -31,12 +29,12 @@ class AgentDirectoryMapperTest {
3129
private static final String AGENT_DIRECTORY_FOLDER = "/ehr/mapper/agent-directory/";
3230
private static final String INPUT_AGENT_DIRECTORY = AGENT_DIRECTORY_FOLDER + "input-agent-directory-bundle.json";
3331
private static final String INPUT_AGENT_DIRECTORY_WITHOUT_MANAGING_ORGANIZATION_REFERENCE =
34-
AGENT_DIRECTORY_FOLDER + "without-patient-managing-organization-reference.json";
32+
AGENT_DIRECTORY_FOLDER + "without-patient-managing-organization-reference.json";
3533
private static final String INPUT_AGENT_DIRECTORY_WITHOUT_MANAGING_ORGANIZATION_RESOURCE =
36-
AGENT_DIRECTORY_FOLDER + "without-patient-managing-organization-resource.json";
34+
AGENT_DIRECTORY_FOLDER + "without-patient-managing-organization-resource.json";
3735
private static final String EXPECTED_AGENT_DIRECTORY = AGENT_DIRECTORY_FOLDER + "expected-agent-directory.xml";
3836
private static final String EXPECTED_AGENT_DIRECTORY_AGENT_PERSON_AS_ORGANIZATION =
39-
AGENT_DIRECTORY_FOLDER + "expected-with-agent-person-as-organization.xml";
37+
AGENT_DIRECTORY_FOLDER + "expected-with-agent-person-as-organization.xml";
4038

4139
@Mock
4240
private RandomIdGeneratorService randomIdGeneratorService;
@@ -48,99 +46,112 @@ class AgentDirectoryMapperTest {
4846
private MessageContext messageContext;
4947

5048
@BeforeEach
51-
public void setUp() {
52-
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
49+
void setUp() {
5350
messageContext = new MessageContext(randomIdGeneratorService);
54-
lenient().when(agentPersonMapper.mapAgentPerson(any(), any())).thenAnswer(answerWithObjectId());
55-
5651
agentDirectoryMapper = new AgentDirectoryMapper(messageContext, agentPersonMapper);
5752
fhirParseService = new FhirParseService();
5853
}
5954

55+
@AfterEach
56+
void tearDown() {
57+
messageContext.resetMessageContext();
58+
}
59+
6060
private Answer<String> answerWithObjectId() {
6161
return invocation -> {
62-
AgentDirectory.AgentKey agentKey = invocation.getArgument(0);
62+
var agentKey = invocation.getArgument(0, AgentDirectory.AgentKey.class);
6363
return String.format("<!--Mocked agentPerson for: %s %s -->",
64-
agentKey.getPractitionerReference(),
65-
agentKey.getOrganizationReference());
64+
agentKey.getPractitionerReference(),
65+
agentKey.getOrganizationReference());
6666
};
6767
}
6868

69+
private Bundle parseBundle(String path) {
70+
var jsonInput = ResourceTestFileUtils.getFileContent(path);
71+
return fhirParseService.parseResource(jsonInput, Bundle.class);
72+
}
73+
74+
private String readExpectedOutput(String path) {
75+
return ResourceTestFileUtils.getFileContent(path);
76+
}
77+
78+
private void initializeMessageContextWithAgentKeys(Bundle bundle) {
79+
messageContext.initialize(bundle);
80+
messageContext.getAgentDirectory().getAgentRef(
81+
buildReference(ResourceType.Practitioner, "11112222"),
82+
buildReference(ResourceType.Organization, "33334444")
83+
);
84+
messageContext.getAgentDirectory().getAgentRef(
85+
buildReference(ResourceType.Practitioner, "55556666"),
86+
buildReference(ResourceType.Organization, "77778888")
87+
);
88+
}
89+
6990
@Test
7091
void When_MappingAgentDirectory_Expect_CorrectOutputFromMapper() {
71-
var jsonInput = ResourceTestFileUtils.getFileContent(INPUT_AGENT_DIRECTORY);
72-
Bundle bundle = fhirParseService.parseResource(jsonInput, Bundle.class);
92+
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
93+
when(agentPersonMapper.mapAgentPerson(any(), any())).thenAnswer(answerWithObjectId());
94+
95+
var bundle = parseBundle(INPUT_AGENT_DIRECTORY);
7396
initializeMessageContextWithAgentKeys(bundle);
74-
var expectedOutput = ResourceTestFileUtils.getFileContent(EXPECTED_AGENT_DIRECTORY);
7597

98+
var expectedOutput = readExpectedOutput(EXPECTED_AGENT_DIRECTORY);
7699
var mapperOutput = agentDirectoryMapper.mapEHRFolderToAgentDirectory(bundle, NHS_NUMBER);
77100

78101
assertThat(mapperOutput).isEqualTo(expectedOutput);
79102
}
80103

81104
@Test
82105
void When_MappingAgentDirectoryWithoutPatientManagingOrganizationReference_Expect_Exception() {
83-
var jsonInput = ResourceTestFileUtils.getFileContent(INPUT_AGENT_DIRECTORY_WITHOUT_MANAGING_ORGANIZATION_REFERENCE);
84-
Bundle bundle = fhirParseService.parseResource(jsonInput, Bundle.class);
106+
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
107+
108+
var bundle = parseBundle(INPUT_AGENT_DIRECTORY_WITHOUT_MANAGING_ORGANIZATION_REFERENCE);
85109
initializeMessageContextWithAgentKeys(bundle);
86110

87111
assertThatThrownBy(() -> agentDirectoryMapper.mapEHRFolderToAgentDirectory(bundle, NHS_NUMBER))
88-
.isExactlyInstanceOf(EhrMapperException.class)
89-
.hasMessage("The ASR bundle does not contain a Patient resource with the correct identifier and managingOrganization");
112+
.isInstanceOf(EhrMapperException.class)
113+
.hasMessage("The ASR bundle does not contain a Patient resource with the correct identifier and managingOrganization");
90114
}
91115

92116
@Test
93117
void When_MappingAgentDirectoryWithoutPatientManagingOrganizationResource_Expect_Exception() {
94-
var jsonInput = ResourceTestFileUtils.getFileContent(INPUT_AGENT_DIRECTORY_WITHOUT_MANAGING_ORGANIZATION_RESOURCE);
95-
Bundle bundle = fhirParseService.parseResource(jsonInput, Bundle.class);
118+
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
119+
120+
var bundle = parseBundle(INPUT_AGENT_DIRECTORY_WITHOUT_MANAGING_ORGANIZATION_RESOURCE);
96121
initializeMessageContextWithAgentKeys(bundle);
97122

98123
assertThatThrownBy(() -> agentDirectoryMapper.mapEHRFolderToAgentDirectory(bundle, NHS_NUMBER))
99-
.isExactlyInstanceOf(EhrMapperException.class)
100-
.hasMessage("The ASR bundle does not contain a Patient resource with the correct identifier and managingOrganization");
124+
.isInstanceOf(EhrMapperException.class)
125+
.hasMessage("The ASR bundle does not contain a Patient resource with the correct identifier and managingOrganization");
101126
}
102127

103128
@Test
104129
void When_MappingAgentDirectoryWithPatientManagingOrganizationInAgentKeys_Expect_AgentPersonNotDuplicated() {
105-
var jsonInput = ResourceTestFileUtils.getFileContent(INPUT_AGENT_DIRECTORY);
106-
Bundle bundle = fhirParseService.parseResource(jsonInput, Bundle.class);
107-
initializeMessageContextWithAgentKeys(bundle);
108-
messageContext.getAgentDirectory().getAgentId(buildReference(ResourceType.Organization, "5E496953-065B-41F2-9577-BE8F2FBD0757"));
130+
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
131+
when(agentPersonMapper.mapAgentPerson(any(), any())).thenAnswer(answerWithObjectId());
109132

110-
var expectedOutput = ResourceTestFileUtils.getFileContent(EXPECTED_AGENT_DIRECTORY);
133+
var bundle = parseBundle(INPUT_AGENT_DIRECTORY);
134+
initializeMessageContextWithAgentKeys(bundle);
135+
messageContext.getAgentDirectory().getAgentId(
136+
buildReference(ResourceType.Organization, TEST_ID)
137+
);
111138

139+
var expectedOutput = readExpectedOutput(EXPECTED_AGENT_DIRECTORY);
112140
var mapperOutput = agentDirectoryMapper.mapEHRFolderToAgentDirectory(bundle, NHS_NUMBER);
113141

114142
assertThat(mapperOutput).isEqualTo(expectedOutput);
115143
}
116144

117145
@Test
118146
void When_MappingAgentKeysWithoutAgentKeys_Expect_CorrectOutputFromMapper() {
119-
var jsonInput = ResourceTestFileUtils.getFileContent(INPUT_AGENT_DIRECTORY);
120-
Bundle bundle = fhirParseService.parseResource(jsonInput, Bundle.class);
121-
messageContext.initialize(bundle);
147+
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
122148

123-
var expectedOutput = ResourceTestFileUtils.getFileContent(EXPECTED_AGENT_DIRECTORY_AGENT_PERSON_AS_ORGANIZATION);
149+
var bundle = parseBundle(INPUT_AGENT_DIRECTORY);
150+
messageContext.initialize(bundle);
124151

152+
var expectedOutput = readExpectedOutput(EXPECTED_AGENT_DIRECTORY_AGENT_PERSON_AS_ORGANIZATION);
125153
var mapperOutput = agentDirectoryMapper.mapEHRFolderToAgentDirectory(bundle, NHS_NUMBER);
126154

127155
assertThat(mapperOutput).isEqualTo(expectedOutput);
128156
}
129-
130-
private void initializeMessageContextWithAgentKeys(Bundle bundle) {
131-
messageContext.initialize(bundle);
132-
messageContext.getAgentDirectory().getAgentRef(
133-
buildReference(ResourceType.Practitioner, "11112222"),
134-
buildReference(ResourceType.Organization, "33334444")
135-
);
136-
messageContext.getAgentDirectory().getAgentRef(
137-
buildReference(ResourceType.Practitioner, "55556666"),
138-
buildReference(ResourceType.Organization, "77778888")
139-
);
140-
}
141-
142-
@AfterEach
143-
public void tearDown() {
144-
messageContext.resetMessageContext();
145-
}
146157
}

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/utils/AllergyStructureExtractorTest.java

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -36,145 +36,112 @@ class AllergyStructureExtractorTest {
3636
private static final String EXPECTED_ONSET_DATE = "19781231";
3737
private static final String REACTION_START = "Reaction 1 ";
3838
private static final String FULL_REACTION = REACTION_START + "Description: description Exposure Route: exposure route "
39-
+ "Severity: MODERATE Manifestation(s): manifestation 1, manifestation 2";
39+
+ "Severity: MODERATE Manifestation(s): manifestation 1, manifestation 2";
4040

4141
private Extension extension;
4242
private List<Extension> extensionList;
43-
private Extension nestedExtension;
4443

4544
private static Stream<Arguments> reasonEndTextParams() {
4645
return Stream.of(
47-
Arguments.of(REASON_END_URL, REASON_END_TEXT, EXPECTED_REASON_END_TEXT),
48-
Arguments.of(REASON_END_URL, REASON_END_NO_INFO, StringUtils.EMPTY),
49-
Arguments.of(INVALID_URL, REASON_END_TEXT, StringUtils.EMPTY)
46+
Arguments.of(REASON_END_URL, REASON_END_TEXT, EXPECTED_REASON_END_TEXT),
47+
Arguments.of(REASON_END_URL, REASON_END_NO_INFO, StringUtils.EMPTY),
48+
Arguments.of(INVALID_URL, REASON_END_TEXT, StringUtils.EMPTY)
5049
);
5150
}
5251

5352
private static Stream<Arguments> reasonEndDateHL7Params() {
5453
return Stream.of(
55-
Arguments.of(ALLERGY_END_DATE_URL, REASON_END_DATE, EXPECTED_REASON_END_DATE_HL7),
56-
Arguments.of(INVALID_URL, REASON_END_DATE, StringUtils.EMPTY)
54+
Arguments.of(ALLERGY_END_DATE_URL, REASON_END_DATE, EXPECTED_REASON_END_DATE_HL7),
55+
Arguments.of(INVALID_URL, REASON_END_DATE, StringUtils.EMPTY)
5756
);
5857
}
5958

6059
private static Stream<Arguments> reasonEndDateHumanReadableParams() {
6160
return Stream.of(
62-
Arguments.of(ALLERGY_END_DATE_URL, REASON_END_DATE, EXPECTED_REASON_END_DATE_HUMAN_READABLE),
63-
Arguments.of(INVALID_URL, REASON_END_DATE, StringUtils.EMPTY)
61+
Arguments.of(ALLERGY_END_DATE_URL, REASON_END_DATE, EXPECTED_REASON_END_DATE_HUMAN_READABLE),
62+
Arguments.of(INVALID_URL, REASON_END_DATE, StringUtils.EMPTY)
6463
);
6564
}
6665

6766
private static Stream<Arguments> onsetDateParams() {
6867
return Stream.of(
69-
Arguments.of(ONSET_DATE, EXPECTED_ONSET_DATE),
70-
Arguments.of(null, StringUtils.EMPTY)
68+
Arguments.of(ONSET_DATE, EXPECTED_ONSET_DATE),
69+
Arguments.of(null, StringUtils.EMPTY)
7170
);
7271
}
7372

7473
@BeforeEach
75-
public void setUp() {
74+
void setUp() {
7675
extension = new Extension();
7776
extensionList = new ArrayList<>();
78-
nestedExtension = new Extension();
77+
}
78+
79+
private void setupNestedExtension(String url, org.hl7.fhir.dstu3.model.Type value) {
80+
var nested = new Extension(url, value);
81+
extensionList.clear();
82+
extensionList.add(nested);
83+
extension.setExtension(extensionList);
7984
}
8085

8186
@ParameterizedTest
8287
@MethodSource("reasonEndTextParams")
8388
void When_ExtractingReasonEnd_Expect_ReasonOutput(String reasonEndUrl, String reasonEndText, String expectedReasonEnd) {
84-
nestedExtension.setUrl(reasonEndUrl);
85-
nestedExtension.setValue(new StringType(reasonEndText));
86-
extensionList.add(nestedExtension);
87-
extension.setExtension(extensionList);
88-
89-
String outputReasonEnd = AllergyStructureExtractor.extractReasonEnd(extension);
90-
91-
assertEquals(expectedReasonEnd, outputReasonEnd);
89+
setupNestedExtension(reasonEndUrl, new StringType(reasonEndText));
90+
assertEquals(expectedReasonEnd, AllergyStructureExtractor.extractReasonEnd(extension));
9291
}
9392

9493
@ParameterizedTest
9594
@MethodSource("reasonEndDateHL7Params")
9695
void When_ExtractingReasonEndDate_Expect_EndDateOutput(String reasonEndDateUrl, String reasonEndDate, String expectedEndDate) {
97-
nestedExtension.setUrl(reasonEndDateUrl);
98-
nestedExtension.setValue(new DateTimeType(reasonEndDate));
99-
extensionList.add(nestedExtension);
100-
extension.setExtension(extensionList);
101-
102-
String outputEndDate = AllergyStructureExtractor.extractEndDate(extension, DateFormatUtil::toHl7Format);
103-
104-
assertEquals(expectedEndDate, outputEndDate);
96+
setupNestedExtension(reasonEndDateUrl, new DateTimeType(reasonEndDate));
97+
assertEquals(expectedEndDate, AllergyStructureExtractor.extractEndDate(extension, DateFormatUtil::toHl7Format));
10598
}
10699

107100
@ParameterizedTest
108101
@MethodSource("reasonEndDateHumanReadableParams")
109102
void When_ExtractingReasonEndDateHumanReadable_Expect_EndDateOutput(String reasonEndDateUrl, String reasonEndDate,
110-
String expectedEndDate) {
111-
nestedExtension.setUrl(reasonEndDateUrl);
112-
nestedExtension.setValue(new DateTimeType(reasonEndDate));
113-
extensionList.add(nestedExtension);
114-
extension.setExtension(extensionList);
115-
116-
String outputEndDate = AllergyStructureExtractor.extractEndDate(extension, DateFormatUtil::toTextFormat);
117-
118-
assertEquals(expectedEndDate, outputEndDate);
103+
String expectedEndDate) {
104+
setupNestedExtension(reasonEndDateUrl, new DateTimeType(reasonEndDate));
105+
assertEquals(expectedEndDate, AllergyStructureExtractor.extractEndDate(extension, DateFormatUtil::toTextFormat));
119106
}
120107

121108
@ParameterizedTest
122109
@MethodSource("onsetDateParams")
123110
void When_ExtractingOnsetDate_Expect_OnsetDateOutput(String onsetDate, String expectedOnsetDate) {
124111
AllergyIntolerance allergyIntolerance = new AllergyIntolerance();
125112
allergyIntolerance.setOnset(new DateTimeType(onsetDate));
126-
127-
String outputReasonEnd = AllergyStructureExtractor.extractOnsetDate(allergyIntolerance);
128-
129-
assertEquals(expectedOnsetDate, outputReasonEnd);
113+
assertEquals(expectedOnsetDate, AllergyStructureExtractor.extractOnsetDate(allergyIntolerance));
130114
}
131115

132116
@Test
133117
void When_ExtractingNoOnsetDate_Expect_EmptyOutput() {
134118
AllergyIntolerance allergyIntolerance = new AllergyIntolerance();
135-
136-
String outputOnsetDate = AllergyStructureExtractor.extractOnsetDate(allergyIntolerance);
137-
138-
assertEquals(StringUtils.EMPTY, outputOnsetDate);
119+
assertEquals(StringUtils.EMPTY, AllergyStructureExtractor.extractOnsetDate(allergyIntolerance));
139120
}
140121

141122
@Test
142123
void When_ExtractingFullReaction_Expect_Output() {
143124
AtomicInteger atomicInteger = new AtomicInteger(1);
144-
AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent
145-
= new AllergyIntolerance.AllergyIntoleranceReactionComponent();
146-
125+
AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent = new AllergyIntolerance.AllergyIntoleranceReactionComponent();
147126
reactionComponent.setDescription("description");
148127

149128
CodeableConcept exposureRoute = new CodeableConcept();
150129
exposureRoute.setText("exposure route");
151-
152130
reactionComponent.setExposureRoute(exposureRoute);
153-
154131
reactionComponent.setSeverity(MODERATE);
155132

156133
List<CodeableConcept> manifestations = new ArrayList<>();
157-
CodeableConcept manifestation1 = new CodeableConcept();
158-
CodeableConcept manifestation2 = new CodeableConcept();
159-
manifestation1.setText("manifestation 1");
160-
manifestation2.setText("manifestation 2");
161-
manifestations.add(manifestation1);
162-
manifestations.add(manifestation2);
134+
manifestations.add(new CodeableConcept().setText("manifestation 1"));
135+
manifestations.add(new CodeableConcept().setText("manifestation 2"));
163136
reactionComponent.setManifestation(manifestations);
164137

165-
String outputOnsetDate = AllergyStructureExtractor.extractReaction(reactionComponent, atomicInteger);
166-
167-
assertEquals(FULL_REACTION, outputOnsetDate);
138+
assertEquals(FULL_REACTION, AllergyStructureExtractor.extractReaction(reactionComponent, atomicInteger));
168139
}
169140

170141
@Test
171142
void When_ExtractingEmptyReaction_Expect_Output() {
172143
AtomicInteger atomicInteger = new AtomicInteger(1);
173-
AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent =
174-
new AllergyIntolerance.AllergyIntoleranceReactionComponent();
175-
176-
String outputOnsetDate = AllergyStructureExtractor.extractReaction(reactionComponent, atomicInteger);
177-
178-
assertEquals(REACTION_START, outputOnsetDate);
144+
AllergyIntolerance.AllergyIntoleranceReactionComponent reactionComponent = new AllergyIntolerance.AllergyIntoleranceReactionComponent();
145+
assertEquals(REACTION_START, AllergyStructureExtractor.extractReaction(reactionComponent, atomicInteger));
179146
}
180147
}

service/src/test/java/uk/nhs/adaptors/gp2gp/ehr/utils/EncounterExtractorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010
import static org.assertj.core.api.Assertions.assertThat;
1111

12-
public class EncounterExtractorTest {
12+
class EncounterExtractorTest {
1313
private static final String TEST_FILE_DIRECTORY = "/ehr/request/fhir/";
1414
private static final String INPUT_DIRECTORY = "input/";
1515
private static final String FULL_BUNDLE_FILE = "gpc-access-structured.json";
@@ -31,7 +31,7 @@ public class EncounterExtractorTest {
3131
private static Bundle bundleWithNoConsultationList;
3232

3333
@BeforeAll
34-
public static void initialize() {
34+
static void initialize() {
3535
String inputFullBundle = ResourceTestFileUtils.getFileContent(INPUT_PATH
3636
+ FULL_BUNDLE_FILE);
3737
fullBundle = FHIR_PARSE_SERVICE.parseResource(inputFullBundle, Bundle.class);

0 commit comments

Comments
 (0)