@Test public void processInternal() throws Exception { final AtomicReference<LocalDhisResourceRepositoryContainer> repositoryContainerReference = new AtomicReference<>(); Mockito.when( bundleResourceProvider.getFhirVersion() ).thenReturn( FhirVersion.DSTU3 ); Mockito.when( requestCacheService.createRequestCacheContext() ).thenReturn( requestCacheContext ); Mockito.doAnswer( invocation -> { repositoryContainerReference.set( invocation.getArgument( 1 ) ); return null; } ).when( requestCacheContext ).setAttribute( Mockito.eq( LocalDhisResourceRepositoryTemplate.CONTAINER_REQUEST_CACHE_ATTRIBUTE_NAME ), Mockito.any() ); final FhirOperation invalidDeleteOperation = new FhirOperation( FhirOperationType.DELETE, FhirResourceType.PATIENT, null, null, null, null ); invalidDeleteOperation.getResult().badRequest( "Invalid data included" ); final FhirOperation invalidPostOperation = new FhirOperation( FhirOperationType.POST, FhirResourceType.PATIENT, null, null, null, null ); invalidPostOperation.getResult().badRequest( "Invalid data included" ); final FhirOperation invalidPutOperation = new FhirOperation( FhirOperationType.PUT, FhirResourceType.PATIENT, null, null, null, null ); invalidPutOperation.getResult().badRequest( "Invalid data included" ); final List<FhirOperation> operations = new ArrayList<>(); operations.add( new FhirOperation( FhirOperationType.PUT, FhirResourceType.PATIENT, patientClientResource, "ldXIdLNUNE1", baseResource1, null ) ); operations.add( new FhirOperation( FhirOperationType.DELETE, FhirResourceType.PATIENT, patientClientResource, "ldXIdLNUNE4", null, null ) ); operations.add( new FhirOperation( FhirOperationType.POST, FhirResourceType.OBSERVATION, observationClientResource, null, baseResource2, null ) ); operations.add( new FhirOperation( FhirOperationType.PUT, FhirResourceType.PATIENT, patientClientResource, "ldXIdLNUNE2", baseResource3, new URI( "Patient?identifier=http%3A%2F%2Ftest.com%2Fpatient%7C8972" ) ) ); operations.add( new FhirOperation( FhirOperationType.PUT, FhirResourceType.PATIENT, patientClientResource, null, baseResource4, new URI( "Patient?identifier=http%3A%2F%2Ftest.com%2Fpatient%7C8973" ) ) ); operations.add( new FhirOperation( FhirOperationType.DELETE, FhirResourceType.PATIENT, patientClientResource, "ldXIdLNUNE2", null, null ) ); operations.add( new FhirOperation( FhirOperationType.POST, FhirResourceType.PATIENT, patientClientResource, "ldXIdLNUN12", baseResource5, null ) ); operations.add( new FhirOperation( FhirOperationType.PUT, FhirResourceType.PATIENT, patientClientResource, "ldXIdLNUN22", baseResource6, null ) ); operations.add( invalidDeleteOperation ); operations.add( invalidPostOperation ); operations.add( invalidPutOperation ); operations.add( new FhirOperation( FhirOperationType.PUT, FhirResourceType.PATIENT, patientClientResource, null, baseResource7, null ) ); final FhirBatchRequest batchRequest = new FhirBatchRequest( operations, false ); Mockito.when( requestDetails.getTenantId() ).thenReturn( "default" ); Mockito.when( bundleResourceProvider.createBatchRequest( Mockito.same( baseBundle ) ) ).thenReturn( batchRequest ); Mockito.when( fhirClientSystemRepository.findOneByFhirClientResourceType( Mockito.eq( FHIR_REST_INTERFACE_DSTU3_ID ), Mockito.eq( FhirResourceType.PATIENT ) ) ) .thenReturn( Optional.of( patientClientSystem ) ); Mockito.when( bundleResourceProvider.createBatchResponse( Mockito.same( batchRequest ) ) ).thenReturn( resultBaseBundle ); Mockito.when( fhirRepository.save( Mockito.same( patientClientResource ), Mockito.same( baseResource1 ), Mockito.eq( new FhirRepositoryOperation( FhirRepositoryOperationType.UPDATE ) ) ) ) .thenReturn( new FhirRepositoryOperationOutcome( "ldXIdLNUNE1", false ) ); Mockito.when( fhirRepository.delete( Mockito.same( patientClientResource ), Mockito.eq( DhisFhirResourceId.parse( "ldXIdLNUNE4" ) ) ) ) .thenReturn( true ); Mockito.when( fhirRepository.save( Mockito.same( observationClientResource ), Mockito.same( baseResource2 ), Mockito.eq( new FhirRepositoryOperation( FhirRepositoryOperationType.CREATE ) ) ) ) .thenReturn( new FhirRepositoryOperationOutcome( "ldXIdLNUNE9", true ) ); Mockito.when( dhisRepository.readByIdentifier( Mockito.same( patientClientResource.getFhirClient() ), Mockito.eq( FhirResourceType.PATIENT ), Mockito.eq( "8972" ) ) ) .thenReturn( Optional.empty() ); Mockito.when( fhirRepository.save( Mockito.same( patientClientResource ), Mockito.same( baseResource3 ), Mockito.eq( new FhirRepositoryOperation( FhirRepositoryOperationType.CREATE ) ) ) ) .thenReturn( new FhirRepositoryOperationOutcome( "ldXIdLNUNE8", true ) ); Mockito.when( baseResource4.getIdElement() ).thenReturn( new IdDt( "ldXIdLNUNE7" ) ); Mockito.when( dhisRepository.readByIdentifier( Mockito.same( patientClientResource.getFhirClient() ), Mockito.eq( FhirResourceType.PATIENT ), Mockito.eq( "8973" ) ) ) .thenReturn( Optional.of( baseResource4 ) ); Mockito.when( fhirRepository.save( Mockito.same( patientClientResource ), Mockito.same( baseResource4 ), Mockito.eq( new FhirRepositoryOperation( FhirRepositoryOperationType.UPDATE ) ) ) ) .thenReturn( new FhirRepositoryOperationOutcome( "ldXIdLNUNE4", false ) ); Mockito.when( fhirRepository.save( Mockito.same( patientClientResource ), Mockito.same( baseResource7 ), Mockito.eq( new FhirRepositoryOperation( FhirRepositoryOperationType.CREATE_OR_UPDATE ) ) ) ) .thenReturn( new FhirRepositoryOperationOutcome( "lxXIdLNUNE4", false ) ); Mockito.when( fhirRepository.delete( Mockito.same( patientClientResource ), Mockito.eq( DhisFhirResourceId.parse( "ldXIdLNUNE2" ) ) ) ) .thenReturn( false ); Assert.assertSame( resultBaseBundle, bundleResourceProvider.processInternal( requestDetails, baseBundle ) ); Assert.assertNull( operations.get( 0 ).getResult().getIssue() ); Assert.assertEquals( FhirOperationResult.OK_STATUS_CODE, operations.get( 0 ).getResult().getStatusCode() ); Assert.assertNull( operations.get( 0 ).getResult().getId() ); Assert.assertNull( operations.get( 1 ).getResult().getIssue() ); Assert.assertEquals( FhirOperationResult.NO_CONTENT_STATUS_CODE, operations.get( 1 ).getResult().getStatusCode() ); Assert.assertNull( operations.get( 1 ).getResult().getId() ); Assert.assertNull( operations.get( 2 ).getResult().getIssue() ); Assert.assertEquals( FhirOperationResult.CREATED_STATUS_CODE, operations.get( 2 ).getResult().getStatusCode() ); Assert.assertEquals( new IdDt( "ldXIdLNUNE9" ), operations.get( 2 ).getResult().getId() ); Assert.assertNull( operations.get( 3 ).getResult().getIssue() ); Assert.assertEquals( FhirOperationResult.CREATED_STATUS_CODE, operations.get( 3 ).getResult().getStatusCode() ); Assert.assertEquals( new IdDt( "ldXIdLNUNE8" ), operations.get( 3 ).getResult().getId() ); Assert.assertNull( operations.get( 4 ).getResult().getIssue() ); Assert.assertEquals( FhirOperationResult.OK_STATUS_CODE, operations.get( 4 ).getResult().getStatusCode() ); Assert.assertNull( operations.get( 4 ).getResult().getId() ); Assert.assertNotNull( operations.get( 5 ).getResult().getIssue() ); Assert.assertEquals( FhirOperationResult.NOT_FOUND_STATUS_CODE, operations.get( 5 ).getResult().getStatusCode() ); Assert.assertNull( operations.get( 5 ).getResult().getId() ); Assert.assertNotNull( operations.get( 6 ).getResult().getIssue() ); Assert.assertThat( operations.get( 6 ).getResult().getIssue().toString(), Matchers.containsString( "Could not find a rule that matches" ) ); Assert.assertNotNull( operations.get( 7 ).getResult().getIssue() ); Assert.assertThat( operations.get( 7 ).getResult().getIssue().toString(), Matchers.containsString( "Could not find a rule that matches" ) ); Assert.assertNotNull( operations.get( 8 ).getResult().getIssue() ); Assert.assertThat( operations.get( 8 ).getResult().getIssue().toString(), Matchers.containsString( "Invalid data included" ) ); Assert.assertNotNull( operations.get( 9 ).getResult().getIssue() ); Assert.assertThat( operations.get( 9 ).getResult().getIssue().toString(), Matchers.containsString( "Invalid data included" ) ); Assert.assertNotNull( operations.get( 10 ).getResult().getIssue() ); Assert.assertThat( operations.get( 10 ).getResult().getIssue().toString(), Matchers.containsString( "Invalid data included" ) ); Assert.assertNull( operations.get( 11 ).getResult().getIssue() ); Assert.assertEquals( FhirOperationResult.OK_STATUS_CODE, operations.get( 11 ).getResult().getStatusCode() ); Assert.assertEquals( new IdDt( "lxXIdLNUNE4" ), operations.get( 11 ).getResult().getId() ); Assert.assertNotNull( repositoryContainerReference.get() ); Mockito.verify( requestCacheContext, Mockito.times( 9 ) ).setAttribute( Mockito.eq( LocalDhisResourceRepositoryTemplate.RESOURCE_KEY_REQUEST_CACHE_ATTRIBUTE_NAME ), Mockito.any( FhirOperation.class ) ); }
@Nonnull protected T processInternal( @Nonnull RequestDetails requestDetails, T bundle ) { validateUseCase( requestDetails ); if ( bundle == null ) { throw new InvalidRequestException( "Bundle has not been specified." ); } final FhirBatchRequest batchRequest = createBatchRequest( bundle ); process( batchRequest ); return createBatchResponse( batchRequest ); }
AbstractBundleResourceProvider extends AbstractUntypedResourceProvider { @Nonnull protected T processInternal( @Nonnull RequestDetails requestDetails, T bundle ) { validateUseCase( requestDetails ); if ( bundle == null ) { throw new InvalidRequestException( "Bundle has not been specified." ); } final FhirBatchRequest batchRequest = createBatchRequest( bundle ); process( batchRequest ); return createBatchResponse( batchRequest ); } }
AbstractBundleResourceProvider extends AbstractUntypedResourceProvider { @Nonnull protected T processInternal( @Nonnull RequestDetails requestDetails, T bundle ) { validateUseCase( requestDetails ); if ( bundle == null ) { throw new InvalidRequestException( "Bundle has not been specified." ); } final FhirBatchRequest batchRequest = createBatchRequest( bundle ); process( batchRequest ); return createBatchResponse( batchRequest ); } AbstractBundleResourceProvider(
        @Nonnull FhirClientResourceRepository fhirClientResourceRepository,
        @Nonnull FhirClientSystemRepository fhirClientSystemRepository,
        @Nonnull FhirRepository fhirRepository, @Nonnull DhisRepository dhisRepository,
        @Nonnull RequestCacheService requestCacheService ); }
AbstractBundleResourceProvider extends AbstractUntypedResourceProvider { @Nonnull protected T processInternal( @Nonnull RequestDetails requestDetails, T bundle ) { validateUseCase( requestDetails ); if ( bundle == null ) { throw new InvalidRequestException( "Bundle has not been specified." ); } final FhirBatchRequest batchRequest = createBatchRequest( bundle ); process( batchRequest ); return createBatchResponse( batchRequest ); } AbstractBundleResourceProvider(
        @Nonnull FhirClientResourceRepository fhirClientResourceRepository,
        @Nonnull FhirClientSystemRepository fhirClientSystemRepository,
        @Nonnull FhirRepository fhirRepository, @Nonnull DhisRepository dhisRepository,
        @Nonnull RequestCacheService requestCacheService );  }
AbstractBundleResourceProvider extends AbstractUntypedResourceProvider { @Nonnull protected T processInternal( @Nonnull RequestDetails requestDetails, T bundle ) { validateUseCase( requestDetails ); if ( bundle == null ) { throw new InvalidRequestException( "Bundle has not been specified." ); } final FhirBatchRequest batchRequest = createBatchRequest( bundle ); process( batchRequest ); return createBatchResponse( batchRequest ); } AbstractBundleResourceProvider(
        @Nonnull FhirClientResourceRepository fhirClientResourceRepository,
        @Nonnull FhirClientSystemRepository fhirClientSystemRepository,
        @Nonnull FhirRepository fhirRepository, @Nonnull DhisRepository dhisRepository,
        @Nonnull RequestCacheService requestCacheService );   }