Search in sources :

Example 21 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class EventSecurityTest method testAddEventSimpleUserFullAccess2.

/**
 * program = DATA READ programStage = DATA READ/WRITE orgUnit = Accessible
 * status = ERROR
 */
@Test
void testAddEventSimpleUserFullAccess2() {
    programA.setPublicAccess(AccessStringHelper.DATA_READ);
    programStageA.setPublicAccess(AccessStringHelper.DATA_READ_WRITE);
    manager.update(programA);
    manager.update(programStageA);
    User user = createUser("user1").setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    injectSecurityContext(user);
    Event event = createEvent(programA.getUid(), programStageA.getUid(), organisationUnitA.getUid());
    ImportSummary importSummary = eventService.addEvent(event, ImportOptions.getDefaultImportOptions(), false);
    assertEquals(ImportStatus.ERROR, importSummary.getStatus());
}
Also used : User(org.hisp.dhis.user.User) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Event(org.hisp.dhis.dxf2.events.event.Event) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 22 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class EventSecurityTest method testAddEventSimpleUserFullAccess3.

/**
 * program = DATA READ/WRITE programStage = DATA READ orgUnit = Accessible
 * status = ERROR
 */
@Test
void testAddEventSimpleUserFullAccess3() {
    programA.setPublicAccess(AccessStringHelper.DATA_READ_WRITE);
    programStageA.setPublicAccess(AccessStringHelper.DATA_READ);
    manager.update(programA);
    manager.update(programStageA);
    User user = createUser("user1").setOrganisationUnits(Sets.newHashSet(organisationUnitA));
    injectSecurityContext(user);
    // make sure data is flushed, so event service can access it
    manager.flush();
    Event event = createEvent(programA.getUid(), programStageA.getUid(), organisationUnitA.getUid());
    ImportSummary importSummary = eventService.addEvent(event, ImportOptions.getDefaultImportOptions(), false);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
}
Also used : User(org.hisp.dhis.user.User) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Event(org.hisp.dhis.dxf2.events.event.Event) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 23 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class TrackedEntityAttributeController method reserve.

// Helpers
private List<ReservedValue> reserve(String id, int numberToReserve, int daysToLive) throws WebMessageException {
    if (numberToReserve > 1000 || numberToReserve < 1) {
        throw new WebMessageException(badRequest("You can only reserve between 1 and 1000 values in a single request."));
    }
    Map<String, List<String>> params = context.getParameterValuesMap();
    TrackedEntityAttribute attribute = trackedEntityAttributeService.getTrackedEntityAttribute(id);
    if (attribute == null) {
        throw new WebMessageException(notFound("No attribute found with id " + id));
    }
    if (attribute.getTextPattern() == null) {
        throw new WebMessageException(conflict("This attribute has no pattern"));
    }
    Map<String, String> values = getRequiredValues(attribute, params);
    Date expiration = DateUtils.getDateAfterAddition(new Date(), daysToLive);
    try {
        List<ReservedValue> result = reservedValueService.reserve(attribute, numberToReserve, values, expiration);
        if (result.isEmpty()) {
            throw new WebMessageException(conflict("Unable to reserve id. This may indicate that there are too few available ids left."));
        }
        return result;
    } catch (ReserveValueException ex) {
        throw new WebMessageException(conflict(ex.getMessage()));
    } catch (TextPatternGenerationException ex) {
        throw new WebMessageException(error(ex.getMessage()));
    }
}
Also used : ReservedValue(org.hisp.dhis.reservedvalue.ReservedValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TextPatternGenerationException(org.hisp.dhis.textpattern.TextPatternGenerationException) ReserveValueException(org.hisp.dhis.reservedvalue.ReserveValueException) List(java.util.List) Date(java.util.Date)

Example 24 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class MetadataSyncController method metadataSync.

@PreAuthorize("hasRole('ALL') or hasRole('F_METADATA_MANAGE')")
@GetMapping
public ResponseEntity<? extends WebMessageResponse> metadataSync(HttpServletRequest request, HttpServletResponse response) throws MetadataSyncException, BadRequestException, MetadataImportConflictException, OperationNotAllowedException {
    MetadataSyncParams syncParams;
    MetadataSyncSummary metadataSyncSummary = null;
    synchronized (metadataSyncService) {
        try {
            syncParams = metadataSyncService.getParamsFromMap(contextService.getParameterValuesMap());
        } catch (RemoteServerUnavailableException exception) {
            throw new MetadataSyncException(exception.getMessage(), exception);
        } catch (MetadataSyncServiceException serviceException) {
            throw new BadRequestException("Error in parsing inputParams " + serviceException.getMessage(), serviceException);
        }
        try {
            boolean isSyncRequired = metadataSyncService.isSyncRequired(syncParams);
            if (isSyncRequired) {
                metadataSyncSummary = metadataSyncService.doMetadataSync(syncParams);
                validateSyncSummaryResponse(metadataSyncSummary);
            } else {
                throw new MetadataImportConflictException("Version already exists in system and hence not starting the sync.");
            }
        } catch (MetadataSyncImportException importerException) {
            throw new MetadataSyncException("Runtime exception occurred while doing import: " + importerException.getMessage());
        } catch (MetadataSyncServiceException serviceException) {
            throw new MetadataSyncException("Exception occurred while doing metadata sync: " + serviceException.getMessage());
        } catch (DhisVersionMismatchException versionMismatchException) {
            throw new OperationNotAllowedException("Exception occurred while doing metadata sync: " + versionMismatchException.getMessage());
        }
    }
    return new ResponseEntity<MetadataSyncSummary>(metadataSyncSummary, HttpStatus.OK);
}
Also used : MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) MetadataSyncParams(org.hisp.dhis.dxf2.metadata.sync.MetadataSyncParams) ResponseEntity(org.springframework.http.ResponseEntity) RemoteServerUnavailableException(org.hisp.dhis.dxf2.metadata.sync.exception.RemoteServerUnavailableException) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) MetadataSyncImportException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncImportException) DhisVersionMismatchException(org.hisp.dhis.dxf2.metadata.sync.exception.DhisVersionMismatchException) MetadataSyncSummary(org.hisp.dhis.dxf2.metadata.sync.MetadataSyncSummary) OperationNotAllowedException(org.hisp.dhis.webapi.controller.exception.OperationNotAllowedException) MetadataSyncException(org.hisp.dhis.webapi.controller.exception.MetadataSyncException) MetadataImportConflictException(org.hisp.dhis.webapi.controller.exception.MetadataImportConflictException) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 25 with ERROR

use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.

the class SyncUtils method testServerAvailabilityWithRetries.

/**
 * Checks the availability of remote server. In case of error it tries
 * {@code maxAttempts} of time with a {@code delaybetweenAttempts} delay
 * between retries before giving up.
 *
 * @param systemSettingManager Reference to SystemSettingManager
 * @param restTemplate Reference to RestTemplate
 * @param maxAttempts Specifies how many retries are done in case of error
 * @param delayBetweenAttempts Specifies delay between retries
 * @return AvailabilityStatus that says whether the server is available or
 *         not
 */
private static AvailabilityStatus testServerAvailabilityWithRetries(SystemSettingManager systemSettingManager, RestTemplate restTemplate, int maxAttempts, long delayBetweenAttempts) {
    AvailabilityStatus serverStatus = isRemoteServerAvailable(systemSettingManager, restTemplate);
    for (int i = 1; i < maxAttempts; i++) {
        if (serverStatus.isAvailable()) {
            return serverStatus;
        }
        try {
            log.info("Remote server is not available. Retry #" + i + " in " + delayBetweenAttempts + " ms.");
            Thread.sleep(delayBetweenAttempts);
        } catch (InterruptedException e) {
            log.error("Sleep between sync retries failed.", e);
            Thread.currentThread().interrupt();
        }
        serverStatus = isRemoteServerAvailable(systemSettingManager, restTemplate);
    }
    log.error("Remote server is not available. Details: " + serverStatus);
    return serverStatus;
}
Also used : AvailabilityStatus(org.hisp.dhis.dxf2.synch.AvailabilityStatus)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)33 Test (org.junit.jupiter.api.Test)25 User (org.hisp.dhis.user.User)21 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)20 IOException (java.io.IOException)15 Event (org.hisp.dhis.dxf2.events.event.Event)14 List (java.util.List)12 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)11 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)9 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)8 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)7 GetMapping (org.springframework.web.bind.annotation.GetMapping)7 ArrayList (java.util.ArrayList)6 FileResource (org.hisp.dhis.fileresource.FileResource)6 SchemaService (org.hisp.dhis.schema.SchemaService)6 UserService (org.hisp.dhis.user.UserService)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 InputStream (java.io.InputStream)4