Skip to content

Commit 6130f70

Browse files
NIAD-1136: Removed lenient from tests
1 parent 1d7300a commit 6130f70

21 files changed

+881
-354
lines changed

service/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ dependencies {
8585
testImplementation 'com.squareup.okhttp3:okhttp:5.1.0'
8686
testImplementation 'com.squareup.okhttp3:mockwebserver3:5.1.0'
8787
testImplementation 'com.adobe.testing:s3mock-testcontainers:4.7.0'
88+
testImplementation 'org.mockito:mockito-inline:4.8.0'
8889

8990
spotbugs 'com.github.spotbugs:spotbugs:4.9.8'
9091
spotbugs 'com.github.spotbugs:spotbugs-annotations:4.9.8'

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/mapper/AgentPersonMapperTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package uk.nhs.adaptors.gp2gp.ehr.mapper;
22

3-
import static org.mockito.Mockito.when;
43
import static org.assertj.core.api.Assertions.assertThat;
54
import java.util.stream.Stream;
65

@@ -18,16 +17,14 @@
1817
import org.junit.jupiter.params.provider.MethodSource;
1918
import org.mockito.Mock;
2019
import org.mockito.junit.jupiter.MockitoExtension;
21-
import org.mockito.junit.jupiter.MockitoSettings;
22-
import org.mockito.quality.Strictness;
20+
2321

2422
import uk.nhs.adaptors.gp2gp.common.service.FhirParseService;
2523
import uk.nhs.adaptors.gp2gp.common.service.RandomIdGeneratorService;
2624
import uk.nhs.adaptors.gp2gp.utils.ResourceTestFileUtils;
2725
import uk.nhs.adaptors.gp2gp.utils.TestArgumentsLoaderUtil;
2826

2927
@ExtendWith(MockitoExtension.class)
30-
@MockitoSettings(strictness = Strictness.LENIENT)
3128
class AgentPersonMapperTest {
3229

3330
private static final String TEST_ID = "6D340A1B-BC15-4D4E-93CF-BBCB5B74DF73";
@@ -47,7 +44,6 @@ class AgentPersonMapperTest {
4744

4845
@BeforeEach
4946
public void setUp() {
50-
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
5147
messageContext = new MessageContext(randomIdGeneratorService);
5248
agentPersonMapper = new AgentPersonMapper(messageContext);
5349
fhirParseService = new FhirParseService();

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

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55
import static org.mockito.ArgumentMatchers.any;
66
import static org.mockito.ArgumentMatchers.anyString;
7-
import static org.mockito.Mockito.lenient;
87
import static org.mockito.Mockito.when;
98

109
import static uk.nhs.adaptors.gp2gp.utils.IdUtil.buildIdType;
@@ -43,6 +42,17 @@ public class AllergyStructureMapperTest {
4342
private static final String TEST_FILE_DIRECTORY = "/ehr/mapper/allergy/";
4443
private static final String INPUT_JSON_BUNDLE = TEST_FILE_DIRECTORY + "fhir-bundle.json";
4544
private static final String INPUT_JSON_WITH_OPTIONAL_TEXT_FIELDS = TEST_FILE_DIRECTORY + "input-with-optional-text-fields.json";
45+
private static final String INPUT_WITH_ENV_CATEGORY = "input-with-environment-category.json";
46+
private static final String EXPECTED_WITH_ENV_CATEGORY = "expected-uses-environment-category.xml";
47+
48+
private static final String INPUT_WITH_CONDITION_ONE_NOTE = "input-with-relation-to-condition-with-one-note.json";
49+
private static final String EXPECTED_WITH_CONDITION_ONE_NOTE = "expected-uses-relation-to-condition-with-one-note.xml";
50+
51+
private static final String INPUT_WITH_CONDITION_TWO_NOTES = "input-with-relation-to-condition-with-two-notes.json";
52+
private static final String EXPECTED_WITH_CONDITION_TWO_NOTES = "expected-uses-relation-to-condition-with-two-notes.xml";
53+
54+
private static final String INPUT_WITH_NO_CONDITION = "input-with-no-relation-to-condition.json";
55+
private static final String EXPECTED_WITH_NO_CONDITION = "expected-uses-no-relation-to-condition.xml";
4656

4757
private static final String COMMON_ID = "6D340A1B-BC15-4D4E-93CF-BBCB5B74DF73";
4858

@@ -71,12 +81,8 @@ private static Stream<Arguments> resourceFileParams() {
7181
Arguments.of("input-with-onset-date-only.json", "expected-uses-onset-date.xml"),
7282
Arguments.of("input-with-reason-end-date-only.json", "expected-uses-end-date.xml"),
7383
Arguments.of("input-with-no-dates.json", "expected-uses-null-flavor-date.xml"),
74-
Arguments.of("input-with-environment-category.json", "expected-uses-environment-category.xml"),
7584
Arguments.of("input-with-medication-category.json", "expected-uses-medication-category.xml"),
7685
Arguments.of("input-with-reaction.json", "expected-uses-reaction.xml"),
77-
Arguments.of("input-with-relation-to-condition-with-one-note.json", "expected-uses-relation-to-condition-with-one-note.xml"),
78-
Arguments.of("input-with-relation-to-condition-with-two-notes.json", "expected-uses-relation-to-condition-with-two-notes.xml"),
79-
Arguments.of("input-with-no-relation-to-condition.json", "expected-uses-no-relation-to-condition.xml"),
8086
Arguments.of("input-with-device-recorder-and-asserter.json", "expected-uses-device-recorder-and-asserter.xml"),
8187
Arguments.of("input-with-related-person-asserter.json", "expected-uses-related-person-asserter.xml"),
8288
Arguments.of("input-with-related-person-asserter-name-text.json", "expected-uses-related-person-asserter.xml"),
@@ -94,22 +100,21 @@ private static Stream<Arguments> resourceFileParams() {
94100
);
95101
}
96102

103+
private static Stream<Arguments> resourceFileParamsWithEnvironmentCategoryAndConditionRelatedData() {
104+
return Stream.of(
105+
Arguments.of(INPUT_WITH_ENV_CATEGORY, EXPECTED_WITH_ENV_CATEGORY),
106+
Arguments.of(INPUT_WITH_CONDITION_ONE_NOTE, EXPECTED_WITH_CONDITION_ONE_NOTE),
107+
Arguments.of(INPUT_WITH_CONDITION_TWO_NOTES, EXPECTED_WITH_CONDITION_TWO_NOTES),
108+
Arguments.of(INPUT_WITH_NO_CONDITION, EXPECTED_WITH_NO_CONDITION)
109+
);
110+
}
111+
97112
@BeforeEach
98113
void setUp() {
99114
when(randomIdGeneratorService.createNewId()).thenReturn(TEST_ID);
100115
when(randomIdGeneratorService.createNewOrUseExistingUUID(anyString())).thenReturn(TEST_ID);
101-
102-
lenient().when(codeableConceptCdMapper.mapToNullFlavorCodeableConcept(any(CodeableConcept.class)))
103-
.thenReturn(CodeableConceptMapperMockUtil.NULL_FLAVOR_CODE);
104-
lenient().when(codeableConceptCdMapper.mapCodeableConceptToCd(any(CodeableConcept.class)))
105-
.thenReturn(CodeableConceptMapperMockUtil.NULL_FLAVOR_CODE);
106-
lenient().when(codeableConceptCdMapper.mapCodeableConceptToCdForAllergy(any(CodeableConcept.class),
107-
any(AllergyIntolerance.AllergyIntoleranceClinicalStatus.class)))
108-
.thenReturn(CodeableConceptMapperMockUtil.NULL_FLAVOR_CODE);
109-
lenient().when(codeableConceptCdMapper.mapToNullFlavorCodeableConceptForAllergy(any(CodeableConcept.class),
110-
any(AllergyIntolerance.AllergyIntoleranceClinicalStatus.class)))
111-
.thenReturn(CodeableConceptMapperMockUtil.NULL_FLAVOR_CODE);
112-
lenient().when(confidentialityService.generateConfidentialityCode(any()))
116+
when(randomIdGeneratorService.createNewOrUseExistingUUID(anyString())).thenReturn(TEST_ID);
117+
when(confidentialityService.generateConfidentialityCode(any()))
113118
.thenReturn(Optional.empty());
114119

115120
var bundleInput = ResourceTestFileUtils.getFileContent(INPUT_JSON_BUNDLE);
@@ -145,6 +150,24 @@ void When_MappingAllergyIntoleranceJson_Expect_AllergyStructureXmlOutput(String
145150
final var expectedMessage = ResourceTestFileUtils.getFileContent(TEST_FILE_DIRECTORY + outputXml);
146151
final var allergyIntolerance = parseAllergyIntoleranceFromJsonFile(TEST_FILE_DIRECTORY + inputJson);
147152

153+
when(codeableConceptCdMapper.mapToNullFlavorCodeableConceptForAllergy(any(CodeableConcept.class),
154+
any(AllergyIntolerance.AllergyIntoleranceClinicalStatus.class)))
155+
.thenReturn(CodeableConceptMapperMockUtil.NULL_FLAVOR_CODE);
156+
157+
String message = allergyStructureMapper.mapAllergyIntoleranceToAllergyStructure(allergyIntolerance);
158+
assertThat(message).contains(expectedMessage);
159+
}
160+
161+
@ParameterizedTest
162+
@MethodSource("resourceFileParamsWithEnvironmentCategoryAndConditionRelatedData")
163+
void When_MappingAllergyIntoleranceJson_Expect_AllergyStructureXmlOutputWithEnvironmentAndData(String inputJson, String outputXml) {
164+
final var expectedMessage = ResourceTestFileUtils.getFileContent(TEST_FILE_DIRECTORY + outputXml);
165+
final var allergyIntolerance = parseAllergyIntoleranceFromJsonFile(TEST_FILE_DIRECTORY + inputJson);
166+
167+
when(codeableConceptCdMapper.mapCodeableConceptToCdForAllergy(any(CodeableConcept.class),
168+
any(AllergyIntolerance.AllergyIntoleranceClinicalStatus.class)))
169+
.thenReturn(CodeableConceptMapperMockUtil.NULL_FLAVOR_CODE);
170+
148171
String message = allergyStructureMapper.mapAllergyIntoleranceToAllergyStructure(allergyIntolerance);
149172
assertThat(message).contains(expectedMessage);
150173
}
@@ -161,11 +184,11 @@ void When_MappingInvalidAllergyIntoleranceJson_Expect_Exception(String inputJson
161184
@Test
162185
void When_ConfidentialityServiceReturnsConfidentialityCode_Expect_MessageContainsConfidentialityCode() {
163186
final var allergyIntolerance = parseAllergyIntoleranceFromJsonFile(INPUT_JSON_WITH_OPTIONAL_TEXT_FIELDS);
164-
when(confidentialityService.generateConfidentialityCode(allergyIntolerance))
165-
.thenReturn(Optional.of(CONFIDENTIALITY_CODE));
166-
167187
final var message = allergyStructureMapper.mapAllergyIntoleranceToAllergyStructure(allergyIntolerance);
168188

189+
when(confidentialityService.generateConfidentialityCode(allergyIntolerance))
190+
.thenReturn(Optional.of(CONFIDENTIALITY_CODE));
191+
169192
assertThat(message).contains(CONFIDENTIALITY_CODE);
170193
assertThat(StringUtils.countOccurrencesOf(message, CONFIDENTIALITY_CODE))
171194
.withFailMessage("<confidentialityCode /> should appear within both the CompoundStatement and ObservationStatement")

0 commit comments

Comments
 (0)