Exemplo n.º 1
0
        public void WhenInformationalEventOccured_EventShouldBeSkipped()
        {
            var processType = new EventSetProcessType {
                Level = (byte)EventLevel.Information
            };

            processTypeManagerMock.Setup(obj =>
                                         obj.GetProcessTypeAsync(
                                             It.Is <int>(i => i == informationalEvent.EventTypeId),
                                             It.Is <EventTypeCategory>(i => i == informationalEvent.Category)))
            .Returns(Task.FromResult(processType))
            .Verifiable();

            var result = storageProcessor.InvokeSync(new[] { informationalEvent });

            result.Should().HaveCount(1);
            result.First().VerifyEventSkipped(informationalEvent);

            identityManagementServiceMock.Verify(obj =>
                                                 obj.GetNextLongIdsAsync(It.IsAny <string>(), It.IsAny <int>()),
                                                 Times.Never);
            repositoryMock.Verify(obj =>
                                  obj.ApplyChangesAsync(It.IsAny <IList <EventSet> >(), It.IsAny <IList <EventSet> >()),
                                  Times.Never);

            VerifyMocks();
        }
Exemplo n.º 2
0
        public void WhenGetNextLongIdsThrowsException_EventShouldBeFailed()
        {
            var processType = new EventSetProcessType {
                Level = (byte)EventLevel.Critical
            };

            processTypeManagerMock.Setup(obj =>
                                         obj.GetProcessTypeAsync(
                                             It.Is <int>(i => i == criticalEvent.EventTypeId),
                                             It.Is <EventTypeCategory>(i => i == criticalEvent.Category)))
            .Returns(Task.FromResult(processType))
            .Verifiable();
            repositoryMock.Setup(obj =>
                                 obj.FindLastEventSetsByTypeCodesAsync(
                                     It.Is <IList <long> >(typeCodes =>
                                                           typeCodes.SequenceEqual(new[] { GetCriticalEventsetTypeCode() }))))
            .Returns(Task.FromResult <IList <EventSet> >(new EventSet[] {}))
            .Verifiable();
            identityManagementServiceMock.Setup(obj =>
                                                obj.GetNextLongIdsAsync(It.IsAny <string>(), It.IsAny <int>()))
            .Throws <Exception>()
            .Verifiable();

            var result = storageProcessor.InvokeSync(new[] { criticalEvent });

            result.Should().HaveCount(1);
            result.First().VerifyEventFailed(criticalEvent);

            repositoryMock.Verify(obj =>
                                  obj.ApplyChangesAsync(It.IsAny <IList <EventSet> >(), It.IsAny <IList <EventSet> >()),
                                  Times.Never);

            VerifyMocks();
        }
Exemplo n.º 3
0
        public void WhenThresholdOccurredBeforeSecondEvent_EventSetShouldBeUpdatedAndNewEventSetsShouldBeCreatedForSecondEvent()
        {
            var processType = new EventSetProcessType
            {
                Level     = (byte)EventLevel.Critical,
                Threshold = TimeSpan.FromMinutes(5)
            };

            processTypeManagerMock.Setup(obj =>
                                         obj.GetProcessTypeAsync(
                                             It.Is <int>(i => i == criticalEvent.EventTypeId),
                                             It.Is <EventTypeCategory>(i => i == criticalEvent.Category)))
            .Returns(Task.FromResult(processType))
            .Verifiable();

            var eventSet = new EventSet
            {
                LastReadTime = currentTime.AddMinutes(-7),
                TypeCode     = GetCriticalEventsetTypeCode()
            };
            var events = new[]
            {
                criticalEvent
                .Clone()
                .WithReadTime(currentTime.AddMinutes(-6)),
                criticalEvent
            };

            repositoryMock.Setup(obj =>
                                 obj.FindLastEventSetsByTypeCodesAsync(
                                     It.Is <IList <long> >(typeCodes =>
                                                           typeCodes.SequenceEqual(new[] { GetCriticalEventsetTypeCode() }))))
            .Returns(Task.FromResult <IList <EventSet> >(new[] { eventSet }))
            .Verifiable();
            repositoryMock.Setup(obj =>
                                 obj.ApplyChangesAsync(
                                     It.IsAny <IList <EventSet> >(),
                                     It.IsAny <IList <EventSet> >()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            long newEventSetId = 15345;

            identityManagementServiceMock.Setup(obj =>
                                                obj.GetNextLongIdsAsync(It.IsAny <string>(), 1))
            .Returns(Task.FromResult <IList <long> >(new[] { newEventSetId }))
            .Verifiable();

            var result = storageProcessor.InvokeSync(events);

            result.Should().HaveCount(2);
            result[0].VerifyEventSetCreated(newEventSetId, events.Last(), EventLevel.Critical, currentTime);
            result[1].VerifyEventSetUpdated(events.First(), 1, currentTime);

            VerifyMocks();
        }
Exemplo n.º 4
0
        public void WhenCurrentAutocompleteTimeOccurredAndStatusIsRejected_NewEventSetShouldBeCreated()
        {
            var processType = new EventSetProcessType
            {
                Level               = (byte)EventLevel.Critical,
                Threshold           = TimeSpan.FromMinutes(10),
                AutoComplete        = true,
                AutoCompleteTimeout = TimeSpan.FromSeconds(9)
            };

            processTypeManagerMock.Setup(obj =>
                                         obj.GetProcessTypeAsync(
                                             It.Is <int>(i => i == criticalEvent.EventTypeId),
                                             It.Is <EventTypeCategory>(i => i == criticalEvent.Category)))
            .Returns(Task.FromResult(processType))
            .Verifiable();

            var eventSet = new EventSet
            {
                LastReadTime = currentTime.AddMinutes(-1),
                AcceptedTime = currentTime.AddMinutes(-1),
                Status       = (byte)EventSetStatus.Rejected,
                TypeCode     = GetCriticalEventsetTypeCode()
            };

            repositoryMock.Setup(obj =>
                                 obj.FindLastEventSetsByTypeCodesAsync(
                                     It.Is <IList <long> >(typeCodes =>
                                                           typeCodes.SequenceEqual(new[] { GetCriticalEventsetTypeCode() }))))
            .Returns(Task.FromResult <IList <EventSet> >(new[] { eventSet }))
            .Verifiable();
            repositoryMock.Setup(obj =>
                                 obj.ApplyChangesAsync(
                                     It.Is <IList <EventSet> >(sets => sets.Count == 1),
                                     It.Is <IList <EventSet> >(sets => sets.Count == 0)))
            .Returns(Task.CompletedTask)
            .Verifiable();

            long newEventSetId = 15345;

            identityManagementServiceMock.Setup(obj =>
                                                obj.GetNextLongIdsAsync(It.IsAny <string>(), 1))
            .Returns(Task.FromResult <IList <long> >(new[] { newEventSetId }))
            .Verifiable();

            var result = storageProcessor.InvokeSync(new[] { criticalEvent });

            result.Should().HaveCount(1);
            result.First().VerifyEventSetCreated(newEventSetId, criticalEvent, EventLevel.Critical, currentTime);

            VerifyMocks();
        }
Exemplo n.º 5
0
        public void WhenEventSetIsOutdated_NewEventSetShouldBeCreated()
        {
            var processType = new EventSetProcessType {
                Level = (byte)EventLevel.Critical
            };

            processTypeManagerMock.Setup(obj =>
                                         obj.GetProcessTypeAsync(
                                             It.Is <int>(i => i == criticalEvent.EventTypeId),
                                             It.Is <EventTypeCategory>(i => i == criticalEvent.Category)))
            .Returns(Task.FromResult(processType))
            .Verifiable();

            var eventSet = new EventSet
            {
                Status   = (byte)EventSetStatus.Completed,
                TypeCode = GetCriticalEventsetTypeCode()
            };

            repositoryMock.Setup(obj =>
                                 obj.FindLastEventSetsByTypeCodesAsync(
                                     It.Is <IList <long> >(typeCodes => typeCodes.SequenceEqual(new[] { GetCriticalEventsetTypeCode() }))))
            .Returns(Task.FromResult <IList <EventSet> >(new[] { eventSet }))
            .Verifiable();
            repositoryMock.Setup(obj =>
                                 obj.ApplyChangesAsync(
                                     It.Is <IList <EventSet> >(sets => sets.Count == 1),
                                     It.Is <IList <EventSet> >(sets => sets.Count == 0)))
            .Returns(Task.CompletedTask)
            .Verifiable();

            long newEventSetId = 15345;

            identityManagementServiceMock.Setup(obj =>
                                                obj.GetNextLongIdsAsync(It.IsAny <string>(), 1))
            .Returns(Task.FromResult <IList <long> >(new[] { newEventSetId }))
            .Verifiable();

            var result = storageProcessor.InvokeSync(new[] { criticalEvent });

            result.Should().HaveCount(1);
            result.First().VerifyEventSetCreated(newEventSetId, criticalEvent, EventLevel.Critical, currentTime);

            VerifyMocks();
        }
Exemplo n.º 6
0
        public void WhenEventSetWasFoundInDb_ItShouldBeUpdated()
        {
            var processType = new EventSetProcessType {
                Level = (byte)EventLevel.Critical, Threshold = TimeSpan.FromHours(1)
            };

            processTypeManagerMock.Setup(obj =>
                                         obj.GetProcessTypeAsync(
                                             It.Is <int>(i => i == criticalEvent.EventTypeId),
                                             It.Is <EventTypeCategory>(i => i == criticalEvent.Category)))
            .Returns(Task.FromResult(processType))
            .Verifiable();

            var eventSet = new EventSet
            {
                LastReadTime = currentTime.AddMinutes(-1),
                Level        = (byte)EventLevel.Critical,
                TypeCode     = GetCriticalEventsetTypeCode()
            };

            repositoryMock.Setup(obj =>
                                 obj.FindLastEventSetsByTypeCodesAsync(
                                     It.Is <IList <long> >(typeCodes =>
                                                           typeCodes.SequenceEqual(new[] { GetCriticalEventsetTypeCode() }))))
            .Returns(Task.FromResult <IList <EventSet> >(new[] { eventSet }))
            .Verifiable();

            repositoryMock.Setup(obj =>
                                 obj.ApplyChangesAsync(
                                     It.Is <IList <EventSet> >(sets => sets.Count == 0),
                                     It.Is <IList <EventSet> >(sets => sets.Count == 1)))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var result = storageProcessor.InvokeSync(new[] { criticalEvent });

            result.Should().HaveCount(1);
            result.First().VerifyEventSetUpdated(criticalEvent, 1, currentTime);

            identityManagementServiceMock.Verify(obj =>
                                                 obj.GetNextLongIdsAsync(It.IsAny <string>(), It.IsAny <int>()),
                                                 Times.Never);

            VerifyMocks();
        }
Exemplo n.º 7
0
        public void WhenThresholdOccurredBetweenEvents_NewEventSetsForEachEventShouldBeCreated()
        {
            var processType = new EventSetProcessType
            {
                Level     = (byte)EventLevel.Critical,
                Threshold = TimeSpan.FromMinutes(5)
            };

            processTypeManagerMock.Setup(obj =>
                                         obj.GetProcessTypeAsync(
                                             It.Is <int>(i => i == criticalEvent.EventTypeId),
                                             It.Is <EventTypeCategory>(i => i == criticalEvent.Category)))
            .Returns(Task.FromResult(processType))
            .Verifiable();

            var eventSet = new EventSet
            {
                LastReadTime = currentTime.AddMinutes(-12),
                TypeCode     = GetCriticalEventsetTypeCode()
            };

            var events = new[]
            {
                criticalEvent
                .Clone()
                .WithReadTime(currentTime.AddMinutes(-6)),
                criticalEvent
            };

            repositoryMock.Setup(obj =>
                                 obj.FindLastEventSetsByTypeCodesAsync(
                                     It.Is <IList <long> >(typeCodes =>
                                                           typeCodes.SequenceEqual(new[] { GetCriticalEventsetTypeCode() }))))
            .Returns(Task.FromResult <IList <EventSet> >(new[] { eventSet }))
            .Verifiable();
            repositoryMock.Setup(obj =>
                                 obj.ApplyChangesAsync(
                                     It.IsAny <IList <EventSet> >(),
                                     It.IsAny <IList <EventSet> >()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            // TODO: Refactoring
            var eventSetIds            = new long[] { 15345, 15346 };
            int currentEventSetIdIndex = 0;

            identityManagementServiceMock.Setup(obj =>
                                                obj.GetNextLongIdsAsync(
                                                    It.IsAny <string>(),
                                                    It.Is <int>(i => i > 0 && i <= eventSetIds.Length)))
            .Returns((string sequenceName, int amount) =>
            {
                var res = eventSetIds
                          .Skip(Interlocked.Add(ref currentEventSetIdIndex, amount) - amount)
                          .Take(amount)
                          .ToList();
                return(Task.FromResult <IList <long> >(res));
            });

            var result = storageProcessor.InvokeSync(events);

            result.VerifyEventSetsCreatedForEachEvent(
                eventSetIds, events, EventLevel.Critical, currentTime);

            VerifyMocks();
        }