@Test public void persistCreateUpdate() throws IOException { mockServer.expect( ExpectedCount.once(), requestTo( "http: .andExpect( content().contentTypeCompatibleWith( MediaType.APPLICATION_JSON ) ) .andExpect( content().json( IOUtils.resourceToString( "/org/dhis2/fhir/adapter/dhis/tracker/program/impl/createEvents.json", StandardCharsets.UTF_8 ) ) ) .andExpect( method( HttpMethod.POST ) ).andRespond( withSuccess( IOUtils.resourceToByteArray( "/org/dhis2/fhir/adapter/dhis/tracker/program/impl/createEvents-response.json" ), MediaType.APPLICATION_JSON ) ); mockServer.expect( ExpectedCount.once(), requestTo( "http: .andExpect( content().contentTypeCompatibleWith( MediaType.APPLICATION_JSON ) ) .andExpect( content().json( IOUtils.resourceToString( "/org/dhis2/fhir/adapter/dhis/tracker/program/impl/updateEvents.json", StandardCharsets.UTF_8 ) ) ) .andExpect( method( HttpMethod.POST ) ).andRespond( withSuccess( IOUtils.resourceToByteArray( "/org/dhis2/fhir/adapter/dhis/tracker/program/impl/updateEvents-response.json" ), MediaType.APPLICATION_JSON ) ); try ( final RequestCacheContext cacheContext = requestCacheService.createRequestCacheContext() ) { final LocalDhisResourceRepositoryContainer repositoryContainer = new LocalDhisResourceRepositoryContainerImpl( Collections.singleton( Event.class ) ); cacheContext.setAttribute( LocalDhisResourceRepositoryTemplate.CONTAINER_REQUEST_CACHE_ATTRIBUTE_NAME, repositoryContainer ); final Event newEvent = new Event( true ); newEvent.setId( "jShdkweusi2" ); newEvent.setEnrollmentId( "Jskdsjeua2s" ); newEvent.setStatus( EventStatus.ACTIVE ); newEvent.setTrackedEntityInstanceId( "jdhshdfj" ); newEvent.setProgramId( "kgdyunhUgg" ); newEvent.setProgramStageId( "gjddShhdfgh" ); newEvent.setOrgUnitId( "jhgtJgrygffg" ); newEvent.setCoordinate( new Location( 64.89767, -86.78866 ) ); newEvent.setDataValues( Collections.emptyList() ); final Event existingEvent = new Event(); existingEvent.setId( "jShdkweusi3" ); existingEvent.setEnrollmentId( "Jskdsjeua2s" ); existingEvent.setStatus( EventStatus.ACTIVE ); existingEvent.setTrackedEntityInstanceId( "jdhshdfj" ); existingEvent.setProgramId( "kgdyunhUgg" ); existingEvent.setProgramStageId( "gjddShhdfgh" ); existingEvent.setOrgUnitId( "jhgtJgrygffg" ); existingEvent.setCoordinate( new Location( 64.89767, -86.78866 ) ); existingEvent.setDataValues( Collections.emptyList() ); final Event createdEvent = service.createOrMinimalUpdate( newEvent ); Assert.assertSame( newEvent, createdEvent ); Assert.assertFalse( createdEvent.isNewResource() ); Assert.assertNotNull( createdEvent.getLastUpdated() ); final Event updatedEvent = service.createOrMinimalUpdate( existingEvent ); Assert.assertSame( existingEvent, updatedEvent ); Assert.assertFalse( updatedEvent.isNewResource() ); final List<String> persistedIds = new ArrayList<>(); repositoryContainer.apply( ( resource, resourceKey, result ) -> persistedIds.add( resource.getId() ) ); Assert.assertThat( persistedIds, Matchers.containsInAnyOrder( "jShdkweusi2", "jShdkweusi3" ) ); } mockServer.verify(); }
@HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Nonnull @Override @CacheEvict( key = "{'find', #a0.programId, #a0.programStageId, #a0.enrollmentId, #a0.trackedEntityInstanceId}", cacheManager = "dhisCacheManager", cacheNames = "events" ) public Event createOrMinimalUpdate( @Nonnull Event event ) { return resourceRepositoryTemplate.save( event ); }
EventServiceImpl implements EventService, LocalDhisRepositoryPersistCallback<Event> { @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Nonnull @Override @CacheEvict( key = "{'find', #a0.programId, #a0.programStageId, #a0.enrollmentId, #a0.trackedEntityInstanceId}", cacheManager = "dhisCacheManager", cacheNames = "events" ) public Event createOrMinimalUpdate( @Nonnull Event event ) { return resourceRepositoryTemplate.save( event ); } }
EventServiceImpl implements EventService, LocalDhisRepositoryPersistCallback<Event> { @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Nonnull @Override @CacheEvict( key = "{'find', #a0.programId, #a0.programStageId, #a0.enrollmentId, #a0.trackedEntityInstanceId}", cacheManager = "dhisCacheManager", cacheNames = "events" ) public Event createOrMinimalUpdate( @Nonnull Event event ) { return resourceRepositoryTemplate.save( event ); } @Autowired  EventServiceImpl( @Nonnull @Qualifier( "userDhis2RestTemplate" ) RestTemplate restTemplate, @Nonnull RequestCacheService requestCacheService, @Nonnull PolledProgramRetriever polledProgramRetriever ); }
EventServiceImpl implements EventService, LocalDhisRepositoryPersistCallback<Event> { @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Nonnull @Override @CacheEvict( key = "{'find', #a0.programId, #a0.programStageId, #a0.enrollmentId, #a0.trackedEntityInstanceId}", cacheManager = "dhisCacheManager", cacheNames = "events" ) public Event createOrMinimalUpdate( @Nonnull Event event ) { return resourceRepositoryTemplate.save( event ); } @Autowired  EventServiceImpl( @Nonnull @Qualifier( "userDhis2RestTemplate" ) RestTemplate restTemplate, @Nonnull RequestCacheService requestCacheService, @Nonnull PolledProgramRetriever polledProgramRetriever ); @Nonnull @Override DhisResourceType getDhisResourceType(); @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Override boolean delete( @Nonnull String eventId ); @Override void persistDeleteById( @Nonnull Collection<String> ids, @Nullable Consumer<LocalDhisRepositoryPersistResult> resultConsumer ); @Override boolean persistDeleteById( @Nonnull String id ); @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Nonnull @Override @CacheEvict( key = "{'find', #a0.programId, #a0.programStageId, #a0.enrollmentId, #a0.trackedEntityInstanceId}", cacheManager = "dhisCacheManager", cacheNames = "events" ) Event createOrMinimalUpdate( @Nonnull Event event ); @Override void persistSave( @Nonnull Collection<Event> resources, boolean create, @Nullable Consumer<LocalDhisRepositoryPersistResult> resultConsumer ); @Override @Nonnull Event persistSave( @Nonnull Event resource ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override @CachePut( key = "{'find', #a0, #a1, #a2, #a3}", condition = "#a4 == false", cacheManager = "dhisCacheManager", cacheNames = "events" ) Collection<Event> findRefreshed( @Nonnull String programId, @Nonnull String programStageId,
        @Nonnull String enrollmentId, @Nonnull String trackedEntityInstanceId, boolean localOnly ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override @Cacheable( key = "{'find', #a0, #a1, #a2, #a3}", condition = "#a4 == false", cacheManager = "dhisCacheManager", cacheNames = "events" ) Collection<Event> find( @Nonnull String programId, @Nonnull String programStageId,
        @Nonnull String enrollmentId, @Nonnull String trackedEntityInstanceId, boolean localOnly ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override Optional<Event> findOneById( @Nonnull String eventId ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override Optional<Event> findOneDeletedById( @Nonnull String eventId ); @Nonnull @Override Instant poll( @Nonnull DhisSyncGroup group, @Nonnull Instant lastUpdated, int toleranceMillis, int maxSearchCount, @Nonnull Set<String> excludedStoredBy, @Nonnull Consumer<Collection<ProcessedItemInfo>> consumer ); @HystrixCommand( ignoreExceptions = { UnauthorizedException.class, DhisFindException.class } ) @Nonnull @Override DhisResourceResult<Event> find( @Nullable String programId, @Nullable String programStageId, @Nonnull UriFilterApplier uriFilterApplier, int from, int max ); }
EventServiceImpl implements EventService, LocalDhisRepositoryPersistCallback<Event> { @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Nonnull @Override @CacheEvict( key = "{'find', #a0.programId, #a0.programStageId, #a0.enrollmentId, #a0.trackedEntityInstanceId}", cacheManager = "dhisCacheManager", cacheNames = "events" ) public Event createOrMinimalUpdate( @Nonnull Event event ) { return resourceRepositoryTemplate.save( event ); } @Autowired  EventServiceImpl( @Nonnull @Qualifier( "userDhis2RestTemplate" ) RestTemplate restTemplate, @Nonnull RequestCacheService requestCacheService, @Nonnull PolledProgramRetriever polledProgramRetriever ); @Nonnull @Override DhisResourceType getDhisResourceType(); @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Override boolean delete( @Nonnull String eventId ); @Override void persistDeleteById( @Nonnull Collection<String> ids, @Nullable Consumer<LocalDhisRepositoryPersistResult> resultConsumer ); @Override boolean persistDeleteById( @Nonnull String id ); @HystrixCommand( ignoreExceptions = { DhisConflictException.class, UnauthorizedException.class } ) @Nonnull @Override @CacheEvict( key = "{'find', #a0.programId, #a0.programStageId, #a0.enrollmentId, #a0.trackedEntityInstanceId}", cacheManager = "dhisCacheManager", cacheNames = "events" ) Event createOrMinimalUpdate( @Nonnull Event event ); @Override void persistSave( @Nonnull Collection<Event> resources, boolean create, @Nullable Consumer<LocalDhisRepositoryPersistResult> resultConsumer ); @Override @Nonnull Event persistSave( @Nonnull Event resource ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override @CachePut( key = "{'find', #a0, #a1, #a2, #a3}", condition = "#a4 == false", cacheManager = "dhisCacheManager", cacheNames = "events" ) Collection<Event> findRefreshed( @Nonnull String programId, @Nonnull String programStageId,
        @Nonnull String enrollmentId, @Nonnull String trackedEntityInstanceId, boolean localOnly ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override @Cacheable( key = "{'find', #a0, #a1, #a2, #a3}", condition = "#a4 == false", cacheManager = "dhisCacheManager", cacheNames = "events" ) Collection<Event> find( @Nonnull String programId, @Nonnull String programStageId,
        @Nonnull String enrollmentId, @Nonnull String trackedEntityInstanceId, boolean localOnly ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override Optional<Event> findOneById( @Nonnull String eventId ); @HystrixCommand( ignoreExceptions = UnauthorizedException.class ) @Nonnull @Override Optional<Event> findOneDeletedById( @Nonnull String eventId ); @Nonnull @Override Instant poll( @Nonnull DhisSyncGroup group, @Nonnull Instant lastUpdated, int toleranceMillis, int maxSearchCount, @Nonnull Set<String> excludedStoredBy, @Nonnull Consumer<Collection<ProcessedItemInfo>> consumer ); @HystrixCommand( ignoreExceptions = { UnauthorizedException.class, DhisFindException.class } ) @Nonnull @Override DhisResourceResult<Event> find( @Nullable String programId, @Nullable String programStageId, @Nonnull UriFilterApplier uriFilterApplier, int from, int max );  }