2
2
3
3
import ca .uhn .fhir .rest .server .exceptions .InvalidRequestException ;
4
4
import gov .cms .ab2d .bfd .client .BFDClient ;
5
+ import gov .cms .ab2d .common .model .Contract ;
6
+ import gov .cms .ab2d .common .repository .ContractRepository ;
7
+ import gov .cms .ab2d .worker .service .BeneficiaryService ;
5
8
import org .hl7 .fhir .dstu3 .model .Bundle ;
6
9
import org .hl7 .fhir .dstu3 .model .Bundle .BundleEntryComponent ;
7
10
import org .hl7 .fhir .dstu3 .model .Bundle .BundleLinkComponent ;
14
17
import org .mockito .Mock ;
15
18
import org .mockito .Mockito ;
16
19
import org .mockito .junit .jupiter .MockitoExtension ;
20
+ import org .springframework .test .util .ReflectionTestUtils ;
17
21
22
+ import java .time .Instant ;
18
23
import java .time .Month ;
19
24
import java .util .Calendar ;
25
+ import java .util .Optional ;
26
+ import java .util .Set ;
20
27
21
28
import static org .hamcrest .CoreMatchers .endsWith ;
22
29
import static org .hamcrest .CoreMatchers .is ;
23
30
import static org .hamcrest .CoreMatchers .notNullValue ;
24
31
import static org .hamcrest .MatcherAssert .assertThat ;
25
32
import static org .junit .jupiter .api .Assertions .assertThrows ;
26
33
import static org .mockito .ArgumentMatchers .anyInt ;
34
+ import static org .mockito .ArgumentMatchers .anyLong ;
27
35
import static org .mockito .ArgumentMatchers .anyString ;
28
- import static org .mockito .Mockito .never ;
29
- import static org .mockito .Mockito .times ;
30
- import static org .mockito .Mockito .verify ;
31
- import static org .mockito .Mockito .when ;
36
+ import static org .mockito .Mockito .*;
32
37
33
38
34
39
@ ExtendWith (MockitoExtension .class )
35
40
class ContractAdapterTest {
36
41
37
42
private static final String BENEFICIARY_ID = "https://bluebutton.cms.gov/resources/variables/bene_id" ;
38
43
39
- @ Mock
40
- private BFDClient client ;
44
+ @ Mock BFDClient client ;
45
+ @ Mock ContractRepository contractRepository ;
46
+ @ Mock BeneficiaryService beneficiaryService ;
41
47
42
48
private ContractAdapter cut ;
43
49
private String contractNumber = "S0000" ;
@@ -47,9 +53,15 @@ class ContractAdapterTest {
47
53
48
54
@ BeforeEach
49
55
void setUp () {
50
- cut = new ContractAdapterImpl (client );
56
+ cut = new ContractAdapterImpl (client , contractRepository , beneficiaryService );
57
+
51
58
bundle = createBundle ();
52
- when (client .requestPartDEnrolleesFromServer (anyString (), anyInt ())).thenReturn (bundle );
59
+ lenient ().when (client .requestPartDEnrolleesFromServer (anyString (), anyInt ())).thenReturn (bundle );
60
+
61
+ Contract contract = new Contract ();
62
+ contract .setId (Long .valueOf (Instant .now ().getNano ()));
63
+ contract .setContractNumber (contractNumber );
64
+ when (contractRepository .findContractByContractNumber (anyString ())).thenReturn (Optional .of (contract ));
53
65
}
54
66
55
67
@ Test
@@ -277,6 +289,52 @@ void GivenDuplicatePatientRowsFromBFD_ShouldEliminateDuplicates() {
277
289
}
278
290
279
291
292
+ @ Test
293
+ @ DisplayName ("given patientid rows in db for a specific contract & month, should not call BFD contract-2-bene api" )
294
+ void GivenPatientInLocalDb_ShouldNotCallBfdContractToBeneAPI () {
295
+ when (beneficiaryService .findPatientIdsInDb (anyLong (), anyInt ())).thenReturn (Set .of ("ccw_patient_005" ));
296
+ cut .getPatients (contractNumber , Month .JANUARY .getValue ());
297
+
298
+ verify (client , never ()).requestPartDEnrolleesFromServer (anyString (), anyInt ());
299
+ verify (beneficiaryService , never ()).storeBeneficiaries (anyLong (), anySet (), anyInt ());
300
+ }
301
+
302
+
303
+ @ Test
304
+ @ DisplayName ("given patient count > cachingThreshold, should cache beneficiary data" )
305
+ void GivenPatientCountGreaterThanCachingThreshold_ShouldCacheBeneficiaryData () {
306
+ var entries = bundle .getEntry ();
307
+ entries .add (createBundleEntry ("ccw_patient_001" ));
308
+ entries .add (createBundleEntry ("ccw_patient_002" ));
309
+ entries .add (createBundleEntry ("ccw_patient_003" ));
310
+ entries .add (createBundleEntry ("ccw_patient_004" ));
311
+ entries .add (createBundleEntry ("ccw_patient_005" ));
312
+
313
+ ReflectionTestUtils .setField (cut , "cachingThreshold" , 2 );
314
+ cut .getPatients (contractNumber , Month .JANUARY .getValue ());
315
+
316
+ verify (client ).requestPartDEnrolleesFromServer (anyString (), anyInt ());
317
+ verify (beneficiaryService ).storeBeneficiaries (anyLong (), anySet (), anyInt ());
318
+ }
319
+
320
+ @ Test
321
+ @ DisplayName ("given patient count < cachingThreshold, should not cache beneficiary data" )
322
+ void GivenPatientCountLessThanCachingThreshold_ShouldNotCacheBeneficiaryData () {
323
+ var entries = bundle .getEntry ();
324
+ entries .add (createBundleEntry ("ccw_patient_001" ));
325
+ entries .add (createBundleEntry ("ccw_patient_002" ));
326
+ entries .add (createBundleEntry ("ccw_patient_003" ));
327
+ entries .add (createBundleEntry ("ccw_patient_004" ));
328
+ entries .add (createBundleEntry ("ccw_patient_005" ));
329
+
330
+ ReflectionTestUtils .setField (cut , "cachingThreshold" , 10 );
331
+ cut .getPatients (contractNumber , Month .JANUARY .getValue ());
332
+
333
+ verify (client ).requestPartDEnrolleesFromServer (anyString (), anyInt ());
334
+ verify (beneficiaryService , never ()).storeBeneficiaries (anyLong (), anySet (), anyInt ());
335
+ }
336
+
337
+
280
338
@ Test
281
339
@ DisplayName ("when call to BFD API throws Invalid Request exception, throws Exception" )
282
340
void whenBfdCallThrowsInvalidRequestException_ShouldThrowRuntimeException () {
0 commit comments