33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .assertj .core .api .Assertions .assertThatThrownBy ;
55import static org .mockito .ArgumentMatchers .any ;
6- import static org .mockito .Mockito .lenient ;
76import static org .mockito .Mockito .when ;
8-
97import static uk .nhs .adaptors .gp2gp .utils .IdUtil .buildReference ;
108
119import 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}
0 commit comments