2727import static org .assertj .core .api .Assertions .assertThat ;
2828import static org .assertj .core .api .Assertions .assertThatThrownBy ;
2929import static org .mockito .ArgumentMatchers .anyString ;
30- import static org .mockito .Mockito .lenient ;
3130import static org .mockito .Mockito .when ;
3231import static uk .nhs .adaptors .gp2gp .utils .XmlAssertion .assertThatXml ;
3332
@@ -100,18 +99,10 @@ public class DocumentReferenceToNarrativeStatementMapperTest {
10099
101100 @ BeforeEach
102101 public void setUp () {
103- lenient ().when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
104- lenient ().when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
105-
106102 final String bundleInput = ResourceTestFileUtils .getFileContent (INPUT_JSON_BUNDLE );
107103 final Bundle bundle = new FhirParseService ().parseResource (bundleInput , Bundle .class );
108104 messageContext = new MessageContext (randomIdGeneratorService );
109105 messageContext .initialize (bundle );
110-
111- lenient ().when (supportedContentTypes .isContentTypeSupported ("text/richtext" )).thenReturn (true );
112- lenient ().when (supportedContentTypes .isContentTypeSupported ("application/octet-stream" )).thenReturn (false );
113- lenient ().when (timestampService .now ()).thenReturn (Instant .parse ("2021-08-18T12:00:00.00Z" ));
114-
115106 confidentialityService = new ConfidentialityService (redactionsContext );
116107
117108 mapper = new DocumentReferenceToNarrativeStatementMapper (
@@ -125,6 +116,9 @@ public void tearDown() {
125116
126117 @ Test
127118 void When_DocReferenceMetaSecurityAndSecurityLabelPopulatedWithoutNoPat_Expect_NarrativeStatementPopulatesReferredToExternalDocument () {
119+ when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
120+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
121+ when (redactionsContext .isRedactionMessage ()).thenReturn (false );
128122
129123 final String jsonInput
130124 = ResourceTestFileUtils .getFileContent (INPUT_JSON_OPTIONAL_DATA_WITH_NOPAT_WITH_SECURITY_AND_SECURITY_LABEL_BUT_WITHOUT_NOPAT );
@@ -142,6 +136,10 @@ void When_DocReferenceSecurityLabelPopulatedWithNoPat_Expect_NarrativeStatementP
142136 final String jsonInput = ResourceTestFileUtils .getFileContent (INPUT_JSON_OPTIONAL_DATA_WITH_NOPAT_WITH_SECURITY_LABEL );
143137 final DocumentReference parsedDocumentReference = new FhirParseService ().parseResource (jsonInput , DocumentReference .class );
144138 when (redactionsContext .isRedactionMessage ()).thenReturn (true );
139+ when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
140+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
141+ when (supportedContentTypes .isContentTypeSupported ("text/richtext" )).thenReturn (true );
142+
145143
146144 final String outputMessage = mapper .mapDocumentReferenceToNarrativeStatement (parsedDocumentReference );
147145
@@ -154,6 +152,9 @@ void When_DocReferenceSecurityLabelPopulatedWithNoPatAndNotReduction_Expect_Narr
154152 final String jsonInput = ResourceTestFileUtils .getFileContent (INPUT_JSON_OPTIONAL_DATA_WITH_NOPAT_WITH_SECURITY_LABEL );
155153 final DocumentReference parsedDocumentReference = new FhirParseService ().parseResource (jsonInput , DocumentReference .class );
156154 when (redactionsContext .isRedactionMessage ()).thenReturn (false );
155+ when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
156+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
157+ when (supportedContentTypes .isContentTypeSupported ("text/richtext" )).thenReturn (true );
157158
158159 final String outputMessage = mapper .mapDocumentReferenceToNarrativeStatement (parsedDocumentReference );
159160
@@ -162,6 +163,9 @@ void When_DocReferenceSecurityLabelPopulatedWithNoPatAndNotReduction_Expect_Narr
162163
163164 @ Test
164165 void When_DocReferenceJsonPopulatedWithNoPat_Expect_NarrativeStatementPopulatesReferredToExternalDocument () {
166+ when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
167+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
168+ when (redactionsContext .isRedactionMessage ()).thenReturn (false );
165169
166170 final String jsonInput = ResourceTestFileUtils .getFileContent (INPUT_JSON_OPTIONAL_DATA_WITH_NOPAT_WITH_SECURITY );
167171 final DocumentReference parsedDocumentReference = new FhirParseService ().parseResource (jsonInput , DocumentReference .class );
@@ -174,22 +178,25 @@ void When_DocReferenceJsonPopulatedWithNoPat_Expect_NarrativeStatementPopulatesR
174178
175179 @ Test
176180 void When_DocReferenceJsonPopulatedWithNoPatAndNotReduction_Expect_NarrativeStatementDoesNotPopulateReferredToExternalDoc () {
181+ when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
182+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
183+ when (redactionsContext .isRedactionMessage ()).thenReturn (false );
177184
178185 final String jsonInput = ResourceTestFileUtils .getFileContent (INPUT_JSON_OPTIONAL_DATA_WITH_NOPAT_WITH_SECURITY );
179186 final DocumentReference parsedDocumentReference = new FhirParseService ().parseResource (jsonInput , DocumentReference .class );
180- when (redactionsContext .isRedactionMessage ()).thenReturn (false );
181-
182187 final String outputMessage = mapper .mapDocumentReferenceToNarrativeStatement (parsedDocumentReference );
183188
184189 assertThatXml (outputMessage ).doesNotContainXPath (NARRATIVE_STATEMENT_REFERENCE_CONFIDENTIALITY_CODE_XPATH );
185190 }
186191
187192 @ Test
188193 void When_DocReferenceJsonNotPopulatedWithNoPat_Expect_NarrativeStatementDoesNotPopulateReferredToExternalDocumentWithNopat () {
194+ when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
195+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
196+ when (redactionsContext .isRedactionMessage ()).thenReturn (false );
189197
190198 final String jsonInput = ResourceTestFileUtils .getFileContent (INPUT_JSON_OPTIONAL_DATA );
191199 final DocumentReference parsedDocumentReference = new FhirParseService ().parseResource (jsonInput , DocumentReference .class );
192-
193200 final String outputMessage = mapper .mapDocumentReferenceToNarrativeStatement (parsedDocumentReference );
194201
195202 assertThatXml (outputMessage ).doesNotContainXPath (NARRATIVE_STATEMENT_REFERENCE_CONFIDENTIALITY_CODE_XPATH );
@@ -198,6 +205,10 @@ void When_DocReferenceJsonNotPopulatedWithNoPat_Expect_NarrativeStatementDoesNot
198205 @ ParameterizedTest
199206 @ MethodSource ("documentReferenceResourceFileParams" )
200207 void When_MappingDocReferenceJson_Expect_NarrativeStatementXmlOutput (String inputJson , String outputXml ) {
208+ when (redactionsContext .isRedactionMessage ()).thenReturn (true );
209+ when (redactionsContext .isRedactionMessage ()).thenReturn (true );
210+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
211+ when (supportedContentTypes .isContentTypeSupported ("text/richtext" )).thenReturn (true );
201212
202213 final CharSequence expectedOutputMessage = ResourceTestFileUtils .getFileContent (outputXml );
203214 final String jsonInput = ResourceTestFileUtils .getFileContent (inputJson );
@@ -209,22 +220,66 @@ void When_MappingDocReferenceJson_Expect_NarrativeStatementXmlOutput(String inpu
209220 assertThat (outputMessage ).isEqualTo (expectedOutputMessage );
210221 }
211222
223+ @ ParameterizedTest
224+ @ MethodSource ("documentReferenceResourceFileParamsWhenIdNeeded" )
225+ void When_MappingDocReferenceJson_Expect_NarrativeStatementXmlOutput_WithIdNeeded (String inputJson , String outputXml ) {
226+ when (randomIdGeneratorService .createNewId ()).thenReturn (TEST_ID );
227+ when (redactionsContext .isRedactionMessage ()).thenReturn (true );
228+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
229+ when (supportedContentTypes .isContentTypeSupported (anyString ())).thenReturn (true );
230+
231+
232+ final CharSequence expectedOutputMessage = ResourceTestFileUtils .getFileContent (outputXml );
233+ final String jsonInput = ResourceTestFileUtils .getFileContent (inputJson );
234+ final DocumentReference parsedDocumentReference =
235+ new FhirParseService ().parseResource (jsonInput , DocumentReference .class );
236+
237+ final String outputMessage = mapper .mapDocumentReferenceToNarrativeStatement (parsedDocumentReference );
238+
239+ assertThat (outputMessage ).isEqualTo (expectedOutputMessage );
240+ }
241+
242+ @ Test
243+ void When_MappingDocReferenceJson_Expect_NarrativeStatementXmlOutput_WithIdNeeded2 () {
244+ when (redactionsContext .isRedactionMessage ()).thenReturn (true );
245+ when (randomIdGeneratorService .createNewOrUseExistingUUID (anyString ())).thenReturn (TEST_ID );
246+ when (supportedContentTypes .isContentTypeSupported (anyString ()))
247+ .thenAnswer (invocation -> {
248+ String contentType = invocation .getArgument (0 , String .class );
249+ return !"application/octet-stream" .equals (contentType );
250+ });
251+
252+ final String inputJson = ResourceTestFileUtils .getFileContent (INPUT_JSON_WITH_NOT_SUPPORTED_CONTENT_TYPE );
253+ final String expectedXml = ResourceTestFileUtils .getFileContent (OUTPUT_XML_NOT_SUPPORTED_CONTENT_TYPE );
254+
255+ final DocumentReference parsedDocumentReference =
256+ new FhirParseService ().parseResource (inputJson , DocumentReference .class );
257+ final String outputMessage = mapper .mapDocumentReferenceToNarrativeStatement (parsedDocumentReference );
258+
259+ assertThat (outputMessage ).isEqualTo (expectedXml );
260+ }
261+
262+
212263 private static Stream <Arguments > documentReferenceResourceFileParams () {
213264 return Stream .of (
214- Arguments .of (INPUT_JSON_OPTIONAL_DATA , OUTPUT_XML_OPTIONAL_DATA ),
215265 Arguments .of (INPUT_JSON_WITH_TYPE_TEXT_ONLY , OUTPUT_XML_WITH_TYPE_TEXT_ONLY ),
216266 Arguments .of (INPUT_JSON_WITH_TYPE_DISPLAY_ONLY , OUTPUT_XML_WITH_TYPE_DISPLAY_ONLY ),
217267 Arguments .of (INPUT_JSON_WITH_AVAILABILITY_TIME_CREATED , OUTPUT_XML_WITH_AVAILABILITY_TIME_CREATED ),
218- Arguments .of (INPUT_JSON_WITH_AUTHOR_ORGANISATION , OUTPUT_XML_WITH_AUTHOR_ORGANISATION ),
219268 Arguments .of (INPUT_JSON_WITH_CUSTODIAN_AND_ORG_NAME , OUTPUT_XML_WITH_CUSTODIAN_ORG_NAME ),
220269 Arguments .of (INPUT_JSON_WITH_DESCRIPTION , OUTPUT_XML_WITH_DESCRIPTION ),
221270 Arguments .of (INPUT_JSON_WITH_PRACTICE_SETTING_TEXT_ONLY , OUTPUT_XML_WITH_PRACTICE_SETTING_TEXT_ONLY ),
222271 Arguments .of (INPUT_JSON_WITH_PRACTICE_SETTING_DISPLAY_ONLY , OUTPUT_XML_WITH_PRACTICE_SETTING_DISPLAY_ONLY ),
223272 Arguments .of (INPUT_JSON_WITH_ATTACHMENT_TITLE , OUTPUT_XML_WITH_ABSENT_ATTACHMENT_TITLE ),
224273 Arguments .of (INPUT_JSON_REQUIRED_DATA , OUTPUT_XML_REQUIRED_DATA ),
225274 Arguments .of (INPUT_JSON_WITH_CUSTODIAN_AND_NO_ORG_NAME , OUTPUT_XML_REQUIRED_DATA ),
226- Arguments .of (INPUT_JSON_WITH_AUTHOR_PRACTITIONER , OUTPUT_XML_REQUIRED_DATA ),
227- Arguments .of (INPUT_JSON_WITH_NOT_SUPPORTED_CONTENT_TYPE , OUTPUT_XML_NOT_SUPPORTED_CONTENT_TYPE )
275+ Arguments .of (INPUT_JSON_WITH_AUTHOR_PRACTITIONER , OUTPUT_XML_REQUIRED_DATA )
276+ );
277+ }
278+
279+ private static Stream <Arguments > documentReferenceResourceFileParamsWhenIdNeeded () {
280+ return Stream .of (
281+ Arguments .of (INPUT_JSON_OPTIONAL_DATA , OUTPUT_XML_OPTIONAL_DATA ),
282+ Arguments .of (INPUT_JSON_WITH_AUTHOR_ORGANISATION , OUTPUT_XML_WITH_AUTHOR_ORGANISATION )
228283 );
229284 }
230285
0 commit comments