@Test public void create() 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/createEvent.json", StandardCharsets.UTF_8 ) ) ) .andExpect( method( HttpMethod.POST ) ).andRespond( withSuccess( IOUtils.resourceToByteArray( "/org/dhis2/fhir/adapter/dhis/tracker/program/impl/createEvent-response.json" ), MediaType.APPLICATION_JSON ) ); final WritableDataValue dataValue1 = new WritableDataValue(); dataValue1.setDataElementId( "dsf84sfsdf" ); dataValue1.setProvidedElsewhere( true ); dataValue1.setValue( "Test 1" ); final WritableDataValue dataValue2 = new WritableDataValue(); dataValue2.setDataElementId( "dsfdfj98js" ); dataValue2.setProvidedElsewhere( true ); dataValue2.setValue( "Test 2" ); final Event event = new Event( true ); event.setId( "jShdkweusi2" ); event.setEnrollmentId( "Jskdsjeua1s" ); event.setEventDate( ZonedDateTime.of( 2018, 10, 5, 22, 12, 34, 998000000, ZoneId.systemDefault() ) ); event.setDueDate( ZonedDateTime.of( 2018, 10, 6, 22, 12, 34, 998000000, ZoneId.systemDefault() ) ); event.setStatus( EventStatus.ACTIVE ); event.setTrackedEntityInstanceId( "jdhshdfj" ); event.setProgramId( "kgdyunhUgg" ); event.setProgramStageId( "gjddShhdfgh" ); event.setOrgUnitId( "jhgtJgrygffg" ); event.setCoordinate( new Location( 64.89767, -86.78866 ) ); event.setDataValues( Arrays.asList( dataValue1, dataValue2 ) ); final Event createdEvent = service.createOrMinimalUpdate( event ); Assert.assertSame( event, createdEvent ); Assert.assertFalse( createdEvent.isNewResource() ); Assert.assertNotNull( createdEvent.getLastUpdated() ); mockServer.verify(); }
@Nonnull protected Event create( @Nonnull Event event ) { final ResponseEntity<ImportSummariesWebMessage> response; if ( event.getId() == null ) { event.setId( CodeGenerator.generateUid() ); } try { response = restTemplate.exchange( CREATE_URI, HttpMethod.POST, new HttpEntity<>( event ), ImportSummariesWebMessage.class ); } catch ( HttpClientErrorException e ) { if ( HttpStatus.CONFLICT.equals( e.getStatusCode() ) ) { throw new DhisConflictException( "Event could not be created: " + e.getResponseBodyAsString(), e ); } throw e; } final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() ); if ( result.isNotSuccessful() ) { throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful event import." ); } event.setId( result.getResponse().getImportSummaries().get( 0 ).getReference() ); event.resetNewResource(); event.setLocal( false ); return event; }
EventServiceImpl implements EventService, LocalDhisRepositoryPersistCallback<Event> { @Nonnull protected Event create( @Nonnull Event event ) { final ResponseEntity<ImportSummariesWebMessage> response; if ( event.getId() == null ) { event.setId( CodeGenerator.generateUid() ); } try { response = restTemplate.exchange( CREATE_URI, HttpMethod.POST, new HttpEntity<>( event ), ImportSummariesWebMessage.class ); } catch ( HttpClientErrorException e ) { if ( HttpStatus.CONFLICT.equals( e.getStatusCode() ) ) { throw new DhisConflictException( "Event could not be created: " + e.getResponseBodyAsString(), e ); } throw e; } final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() ); if ( result.isNotSuccessful() ) { throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful event import." ); } event.setId( result.getResponse().getImportSummaries().get( 0 ).getReference() ); event.resetNewResource(); event.setLocal( false ); return event; } }
EventServiceImpl implements EventService, LocalDhisRepositoryPersistCallback<Event> { @Nonnull protected Event create( @Nonnull Event event ) { final ResponseEntity<ImportSummariesWebMessage> response; if ( event.getId() == null ) { event.setId( CodeGenerator.generateUid() ); } try { response = restTemplate.exchange( CREATE_URI, HttpMethod.POST, new HttpEntity<>( event ), ImportSummariesWebMessage.class ); } catch ( HttpClientErrorException e ) { if ( HttpStatus.CONFLICT.equals( e.getStatusCode() ) ) { throw new DhisConflictException( "Event could not be created: " + e.getResponseBodyAsString(), e ); } throw e; } final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() ); if ( result.isNotSuccessful() ) { throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful event import." ); } event.setId( result.getResponse().getImportSummaries().get( 0 ).getReference() ); event.resetNewResource(); event.setLocal( false ); return event; } @Autowired  EventServiceImpl( @Nonnull @Qualifier( "userDhis2RestTemplate" ) RestTemplate restTemplate, @Nonnull RequestCacheService requestCacheService, @Nonnull PolledProgramRetriever polledProgramRetriever ); }
EventServiceImpl implements EventService, LocalDhisRepositoryPersistCallback<Event> { @Nonnull protected Event create( @Nonnull Event event ) { final ResponseEntity<ImportSummariesWebMessage> response; if ( event.getId() == null ) { event.setId( CodeGenerator.generateUid() ); } try { response = restTemplate.exchange( CREATE_URI, HttpMethod.POST, new HttpEntity<>( event ), ImportSummariesWebMessage.class ); } catch ( HttpClientErrorException e ) { if ( HttpStatus.CONFLICT.equals( e.getStatusCode() ) ) { throw new DhisConflictException( "Event could not be created: " + e.getResponseBodyAsString(), e ); } throw e; } final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() ); if ( result.isNotSuccessful() ) { throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful event import." ); } event.setId( result.getResponse().getImportSummaries().get( 0 ).getReference() ); event.resetNewResource(); event.setLocal( false ); return 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> { @Nonnull protected Event create( @Nonnull Event event ) { final ResponseEntity<ImportSummariesWebMessage> response; if ( event.getId() == null ) { event.setId( CodeGenerator.generateUid() ); } try { response = restTemplate.exchange( CREATE_URI, HttpMethod.POST, new HttpEntity<>( event ), ImportSummariesWebMessage.class ); } catch ( HttpClientErrorException e ) { if ( HttpStatus.CONFLICT.equals( e.getStatusCode() ) ) { throw new DhisConflictException( "Event could not be created: " + e.getResponseBodyAsString(), e ); } throw e; } final ImportSummariesWebMessage result = Objects.requireNonNull( response.getBody() ); if ( result.isNotSuccessful() ) { throw new DhisImportUnsuccessfulException( "Response indicates an unsuccessful event import." ); } event.setId( result.getResponse().getImportSummaries().get( 0 ).getReference() ); event.resetNewResource(); event.setLocal( false ); return 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 );  }