Exemple #1
0
        public async Task NotifyUpdatedColidEntryToSubscribers_Put_Returns_Ok()
        {
            _seeder.ClearUsers();
            _seeder.SeedMessageTemplates();

            // IMPORTANT: User needs to be new, and not persisted already, even if it doesn't make sense ...
            // thats the reason userEntity is commented. IF you add it before the subscription, another user will be tried
            // so add. Don't ask me why .. Alternatively you can add a new subscription to the user before creation:

            /*
             *  var template = TestData.GenerateSampleMessageTemplateForColidEntrySubscriptionUpdate();
             *  var templateEntity = _seeder.Add(template);
             *  var subscription = TestData.GenerateRandomColidEntrySubscription();
             *  var user = TestData.GenerateRandomUser();
             *  user.ColidEntrySubscriptions = new Collection<ColidEntrySubscription>() { subscription };
             *  var userEntity = _seeder.Add(user);
             */

            var user         = TestData.GenerateRandomUser();
            var subscription = TestData.GenerateRandomColidEntrySubscription();

            subscription.User = user;
            _seeder.Add(subscription);
            var colidEntryDto = new ColidEntryDtoBuilder().WithColidPidUri(subscription.ColidPidUri).WithLabel("Hugo Boss Maximum fragrance").Build();

            var response = await Client.PutAsync($"{PATH}", _api.BuildJsonHttpContent(colidEntryDto));

            var stringResponse = await response.Content.ReadAsStringAsync();

            _output.WriteLine(stringResponse);
            response.EnsureSuccessStatusCode();

            Assert.Contains("1", stringResponse);
        }
        public async Task CreateDeleteMessagesAndRemoveColidEntrySubscriptions_Should_Return0_IfNoUserSubscribedToTheColidPidUri()
        {
            IList <User> subscribedUsers;

            _mockColidEntrySubscriptionService.Setup(x => x.TryGetAllUsers(It.IsAny <Uri>(), out subscribedUsers)).Returns(false);

            var colidEntryDto = new ColidEntryDtoBuilder().WithLabel("ALMIGHTY RESOURCE").WithColidPidUri("http://meh").Build();
            var result        = await _messageService.CreateDeleteMessagesAndRemoveColidEntrySubscriptions(colidEntryDto);

            Assert.Equal(0, result);
        }
        [Fact] // subscription field within the user won't be checked here, because it's part of another service
        public async void CreateDeleteMessagesAndRemoveColidEntrySubscriptions_Should_CreateMessagesForSubscribedUsers()
        {
            // ARRANGE
            IList <User> subscribedUsers = new List <User>()
            {
                TestData.GenerateRandomUser(), TestData.GenerateRandomUser(), TestData.GenerateRandomUser()
            };

            _mockColidEntrySubscriptionService.Setup(x => x.TryGetAllUsers(It.IsAny <Uri>(), out subscribedUsers)).Returns(true);

            var template = TestData.GenerateSampleMessageTemplateForColidEntrySubscriptionDelete();

            _mockMessageTemplateService.Setup(x => x.GetOne(MessageType.ColidEntrySubscriptionDelete)).Returns(template);

            var messageDtoList = new List <MessageDto>(); // To check the generated MessageDto

            _mockUserService.Setup(x => x.AddMessageAsync(It.IsAny <Guid>(), It.IsAny <MessageDto>()))
            .Callback <Guid, MessageDto>((userId, messageDto) => messageDtoList.Add(messageDto));

            var messageConfig = TestData.GenerateSampleMessageConfig();

            _mockUserService.Setup(x => x.GetMessageConfigAsync(It.IsAny <Guid>())).ReturnsAsync(messageConfig);

            // ACT
            var colidEntryDto = new ColidEntryDtoBuilder().WithLabel("ALMIGHTY RESOURCE").WithColidPidUri("http://meh").Build();
            var result        = await _messageService.CreateDeleteMessagesAndRemoveColidEntrySubscriptions(colidEntryDto);

            // ASSERT
            Assert.Equal(3, result);
            Assert.NotNull(messageDtoList);

            var msg = messageDtoList.First();

            Assert.DoesNotContain("%COLID_LABEL%", msg.Subject);
            Assert.DoesNotContain("%COLID_PID_URI%", msg.Subject);
            Assert.DoesNotContain("%COLID_LABEL%", msg.Body);
            Assert.DoesNotContain("%COLID_PID_URI%", msg.Body);
            Assert.Null(msg.ReadOn);
            Assert.NotNull(msg.SendOn);
            Assert.NotNull(msg.DeleteOn);

            _mockColidEntrySubscriptionService.Verify(x => x.TryGetAllUsers(It.IsAny <Uri>(), out subscribedUsers), Times.Once);
            _mockMessageTemplateService.Verify(x => x.GetOne(MessageType.ColidEntrySubscriptionDelete), Times.Once);
            _mockUserService.Verify(x => x.AddMessageAsync(It.IsAny <Guid>(), It.IsAny <MessageDto>()), Times.Exactly(3));
            _mockColidEntrySubscriptionService.Verify(x => x.Delete(It.IsAny <ColidEntrySubscriptionDto>()));
        }