Search in sources :

Example 6 with LongPollingSubscriber

use of eu.bcvsolutions.idm.core.rest.LongPollingSubscriber in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManagerTest method testExecuteDeferredResultOnUpdateRoleRequest.

@Test
public void testExecuteDeferredResultOnUpdateRoleRequest() {
    DefaultLongPollingManager defaultPollingManager = (DefaultLongPollingManager) longPollingManager;
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
    IdmIdentityDto identityOne = this.getHelper().createIdentity();
    IdmRoleRequestDto roleRequest = this.getHelper().createRoleRequest(identityOne, this.getHelper().createRole());
    DeferredResult<OperationResultDto> result = new DeferredResult<OperationResultDto>(10000l, new OperationResultDto(OperationState.NOT_EXECUTED));
    DeferredResultWrapper wrapper = new DeferredResultWrapper(identityOne.getId(), identityOne.getClass(), result);
    wrapper.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            checkDeferredRequest(result, subscriber);
        }
    });
    Queue<DeferredResultWrapper> suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    longPollingManager.addSuspendedResult(wrapper);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    // Update of role-request -> must be detected
    roleRequest.setState(RoleRequestState.IN_PROGRESS);
    roleRequestService.save(roleRequest);
    // Check must be called twice, because first detect the change and second remove
    // ended deferred result (from some reason is not invoked method
    // result.onCompleted)
    getHelper().waitForResult(null, 1, 1);
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    getHelper().waitForResult(null, 1, 1);
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
}
Also used : LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Example 7 with LongPollingSubscriber

use of eu.bcvsolutions.idm.core.rest.LongPollingSubscriber in project CzechIdMng by bcvsolutions.

the class DefaultSysSystemServiceIntegrationTest method testExecuteDeferredResultOnCreateSyncItemLog.

@Test
public void testExecuteDeferredResultOnCreateSyncItemLog() {
    DefaultLongPollingManager defaultPollingManager = (DefaultLongPollingManager) longPollingManager;
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
    // create test system and sync
    SysSystemDto system = helper.createTestResourceSystem(true);
    AbstractSysSyncConfigDto syncConfig = createSync(system);
    DeferredResult<OperationResultDto> result = new DeferredResult<OperationResultDto>(1000000l, new OperationResultDto(OperationState.NOT_EXECUTED));
    DeferredResultWrapper wrapper = new DeferredResultWrapper(system.getId(), system.getClass(), result);
    wrapper.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            systemController.checkDeferredRequest(result, subscriber);
        }
    });
    Queue<DeferredResultWrapper> suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    longPollingManager.addSuspendedResult(wrapper);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    longPollingManager.checkDeferredRequests(SysSystemDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    helper.startSynchronization(syncConfig);
    SysSyncConfigFilter logFilter = new SysSyncConfigFilter();
    logFilter.setId(syncConfig.getId());
    logFilter.setIncludeLastLog(Boolean.TRUE);
    List<AbstractSysSyncConfigDto> configs = syncConfigService.find(logFilter, null).getContent();
    Assert.assertEquals(1, configs.size());
    SysSyncLogDto log = new SysSyncLogDto();
    log.setSynchronizationConfig(syncConfig.getId());
    log = syncLogService.save(log);
    SysSyncActionLogDto mockAction = new SysSyncActionLogDto();
    mockAction.setSyncAction(SynchronizationActionType.IGNORE);
    mockAction.setSyncLog(log.getId());
    mockAction.setOperationResult(OperationResultType.IGNORE);
    mockAction = syncActionLogService.save(mockAction);
    // Sync item log created -> must be detected
    SysSyncItemLogDto mockItemLogDto = new SysSyncItemLogDto();
    mockItemLogDto.setSyncActionLog(mockAction.getId());
    mockItemLogDto.setIdentification(helper.createName());
    mockItemLogDto = syncItemLogService.save(mockItemLogDto);
    // Check must be called twice, because first detect the change and second remove
    // ended deferred result (from some reason is not invoked method
    // result.onCompleted)
    longPollingManager.checkDeferredRequests(SysSystemDto.class);
    longPollingManager.checkDeferredRequests(SysSystemDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
}
Also used : LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) SysSyncActionLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncActionLogDto) AbstractSysSyncConfigDto(eu.bcvsolutions.idm.acc.dto.AbstractSysSyncConfigDto) SysSyncItemLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncItemLogDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) DefaultLongPollingManager(eu.bcvsolutions.idm.core.model.service.impl.DefaultLongPollingManager) SysSyncConfigFilter(eu.bcvsolutions.idm.acc.dto.filter.SysSyncConfigFilter) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) SysSyncLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncLogDto) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 8 with LongPollingSubscriber

use of eu.bcvsolutions.idm.core.rest.LongPollingSubscriber in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManager method baseCheckDeferredResult.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void baseCheckDeferredResult(DeferredResult<OperationResultDto> deferredResult, LongPollingSubscriber subscriber, ModifiedFromFilter filter, ReadDtoService service, boolean checkCount) {
    Assert.notNull(deferredResult, "Deferred result is required to check.");
    Assert.notNull(subscriber.getEntityId(), "Subscriber is required to check deferred result.");
    LOG.debug("Start baseCheckDeferredResult for deferred-result [{}] and subscriber [{}]", deferredResult, subscriber);
    if (checkCount) {
        long countOfentities = service.count(filter);
        Long lastNumberOfEntities = subscriber.getLastNumberOfEntities();
        subscriber.setLastNumberOfEntities(countOfentities);
        if (lastNumberOfEntities != null && countOfentities != lastNumberOfEntities) {
            // Notify FE -> Some of an entities were changed (refresh must be executed)
            deferredResult.setResult(new OperationResultDto(OperationState.RUNNING));
            return;
        }
    }
    ZonedDateTime timeStamp = subscriber.getLastTimeStamp();
    if (timeStamp == null) {
        List<AbstractDto> entities = service.find(filter, PageRequest.of(0, 1, new Sort(Direction.DESC, AbstractEntity_.created.getName(), AbstractEntity_.modified.getName()))).getContent();
        if (entities.isEmpty()) {
            subscriber.setLastTimeStamp(ZonedDateTime.now());
            return;
        }
        ZonedDateTime lastModified = this.getLastTimeStamp(entities.get(0));
        subscriber.setLastTimeStamp(lastModified);
        return;
    }
    // Try to find, if some from not finished entities were changed
    // TODO: For search SysSyncLogs the filter by modifiedFrom didn't work properly.
    // I didn't found reason why, but I hope using sort by created and modified fields will be works better.
    // filter.setModifiedFrom(timeStamp);
    List<AbstractDto> changedRequestsFromLastChecks = service.find(filter, PageRequest.of(0, 1, Sort.by(Direction.DESC, AbstractEntity_.created.getName(), AbstractEntity_.modified.getName()))).getContent();
    if (!changedRequestsFromLastChecks.isEmpty()) {
        AbstractDto changedRequestsFromLastCheck = changedRequestsFromLastChecks.get(0);
        ZonedDateTime lastModified = this.getLastTimeStamp(changedRequestsFromLastCheck);
        if (lastModified.isAfter(timeStamp)) {
            // Notify FE -> Some of the entity was changed (refresh must be executed).
            // Notified will be all suspended request for same entity ID!
            this.suspendedRequests.stream().filter(request -> request.getEntityId().equals(subscriber.getEntityId())).forEach(request -> {
                request.getResult().setResult(new OperationResultDto(OperationState.RUNNING));
            });
            subscriber.setLastTimeStamp(lastModified);
            return;
        }
    }
// Nothing was changed
}
Also used : ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) DeferredResult(org.springframework.web.context.request.async.DeferredResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Scheduled(org.springframework.scheduling.annotation.Scheduled) ModifiedFromFilter(eu.bcvsolutions.idm.core.api.dto.filter.ModifiedFromFilter) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) Service(org.springframework.stereotype.Service) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) Map(java.util.Map) LongPollingManager(eu.bcvsolutions.idm.core.model.service.api.LongPollingManager) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) Logger(org.slf4j.Logger) AbstractEntity_(eu.bcvsolutions.idm.core.api.entity.AbstractEntity_) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PageRequest(org.springframework.data.domain.PageRequest) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) UUID(java.util.UUID) LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Lazy(org.springframework.context.annotation.Lazy) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Assert(org.springframework.util.Assert) ZonedDateTime(java.time.ZonedDateTime) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) Sort(org.springframework.data.domain.Sort)

Example 9 with LongPollingSubscriber

use of eu.bcvsolutions.idm.core.rest.LongPollingSubscriber in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManagerTest method testExecuteDeferredResultOnCreateRoleRequest.

@Test
public void testExecuteDeferredResultOnCreateRoleRequest() throws InterruptedException {
    DefaultLongPollingManager defaultPollingManager = (DefaultLongPollingManager) longPollingManager;
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
    IdmIdentityDto identityOne = this.getHelper().createIdentity();
    DeferredResult<OperationResultDto> result = new DeferredResult<OperationResultDto>(10000l, new OperationResultDto(OperationState.NOT_EXECUTED));
    DeferredResultWrapper wrapper = new DeferredResultWrapper(identityOne.getId(), identityOne.getClass(), result);
    wrapper.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            checkDeferredRequest(result, subscriber);
        }
    });
    Queue<DeferredResultWrapper> suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    longPollingManager.addSuspendedResult(wrapper);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    // Create role-request -> must be detected
    this.getHelper().createRoleRequest(identityOne, this.getHelper().createRole());
    // Check must be called twice, because first detect the change and second remove
    // ended deferred result (from some reason is not invoked method
    // result.onCompleted)
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
}
Also used : LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Example 10 with LongPollingSubscriber

use of eu.bcvsolutions.idm.core.rest.LongPollingSubscriber in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManagerTest method testRegisteredSubscribers.

@Test
public void testRegisteredSubscribers() {
    DefaultLongPollingManager defaultPollingManager = (DefaultLongPollingManager) longPollingManager;
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
    IdmIdentityDto identityOne = this.getHelper().createIdentity();
    DeferredResult<OperationResultDto> result = new DeferredResult<OperationResultDto>(10000l, new OperationResultDto(OperationState.NOT_EXECUTED));
    DeferredResultWrapper wrapper = new DeferredResultWrapper(identityOne.getId(), identityOne.getClass(), result);
    wrapper.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            checkDeferredRequest(result, subscriber);
        }
    });
    longPollingManager.addSuspendedResult(wrapper);
    Map<UUID, LongPollingSubscriber> registredSubscirbers = defaultPollingManager.getRegistredSubscribers();
    assertEquals(1, registredSubscirbers.size());
    assertTrue(registredSubscirbers.containsKey(identityOne.getId()));
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    // None subscriber will be cleared ... threshold time stamp is too small
    longPollingManager.clearUnUseSubscribers(ZonedDateTime.now().minusMinutes(1));
    registredSubscirbers = defaultPollingManager.getRegistredSubscribers();
    assertEquals(1, registredSubscirbers.size());
    longPollingManager.clearUnUseSubscribers(ZonedDateTime.now().plusMinutes(1));
    registredSubscirbers = defaultPollingManager.getRegistredSubscribers();
    assertEquals(0, registredSubscirbers.size());
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
}
Also used : LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Aggregations

LongPollingSubscriber (eu.bcvsolutions.idm.core.rest.LongPollingSubscriber)11 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)10 CheckLongPollingResult (eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult)10 DeferredResultWrapper (eu.bcvsolutions.idm.core.rest.DeferredResultWrapper)10 DeferredResult (org.springframework.web.context.request.async.DeferredResult)7 UUID (java.util.UUID)6 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)5 Test (org.junit.Test)5 AbstractCoreWorkflowIntegrationTest (eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest)4 ApiOperation (io.swagger.annotations.ApiOperation)3 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)2 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)2 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)2 IdmRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)2 ModifiedFromFilter (eu.bcvsolutions.idm.core.api.dto.filter.ModifiedFromFilter)2 AbstractEntity_ (eu.bcvsolutions.idm.core.api.entity.AbstractEntity_)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 ConfigurationService (eu.bcvsolutions.idm.core.api.service.ConfigurationService)2 ReadDtoService (eu.bcvsolutions.idm.core.api.service.ReadDtoService)2 LongPollingManager (eu.bcvsolutions.idm.core.model.service.api.LongPollingManager)2