Esempio n. 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();

            try
            {
                app.ApplicationServices.GetService <ILogger>()?.TrackEvent(nameof(SuperPoweredPeopleData.SplitSuperPoweredPeople));
                await SuperPoweredPeopleData.SplitSuperPoweredPeople(
                    app.ApplicationServices.GetService <IAllSuperPoweredRepository>(),
                    new List <IIdentifierService>
                {
                    app.ApplicationServices.GetService <SuperHeroIdentifierService>(),
                    app.ApplicationServices.GetService <VillainIdentifierService>()
                });
            }
            catch (Exception ex)
            {
                app.ApplicationServices.GetService <ILogger>()?.TrackError(ex);
                throw;
            }
        }
        public async Task SplitSuperPoweredPeople_Should_Not_Throw_Exception_When_Names_Is_Empty()
        {
            var repoMock = new Mock <IAllSuperPoweredRepository>();

            repoMock.Setup(x => x.GetAsync()).Returns(Task.FromResult(Enumerable.Empty <string>()));
            var identifierService1 = new Mock <IIdentifierService>();

            identifierService1.Setup(x => x.IsMatch(It.IsAny <string>())).Returns(false);
            var identifierService2 = new Mock <IIdentifierService>();

            identifierService2.Setup(x => x.IsMatch(It.IsAny <string>())).Returns(true);
            var identifierServices = new List <IIdentifierService>
            {
                identifierService1.Object, identifierService2.Object
            };

            await SuperPoweredPeopleData.SplitSuperPoweredPeople(repoMock.Object, identifierServices);

            repoMock.Verify(x => x.GetAsync(), Times.Once());
            identifierService1.Verify(x => x.IsMatch(It.IsAny <string>()), Times.Never());
            identifierService1.Verify(x => x.Add(It.IsAny <string>()), Times.Never());
            identifierService1.Verify(x => x.Save(), Times.Never());
            identifierService2.Verify(x => x.IsMatch(It.IsAny <string>()), Times.Never());
            identifierService2.Verify(x => x.Add(It.IsAny <string>()), Times.Never());
            identifierService2.Verify(x => x.Save(), Times.Never());
        }
        public async Task SplitSuperPoweredPeople_Should_Work_As_Expected(IEnumerable <string> names)
        {
            var repoMock = new Mock <IAllSuperPoweredRepository>();

            repoMock.Setup(x => x.GetAsync()).Returns(Task.FromResult(names));
            var identifierService1 = new Mock <IIdentifierService>();

            identifierService1.Setup(x => x.IsMatch(It.IsAny <string>())).Returns(false);
            var identifierService2 = new Mock <IIdentifierService>();

            identifierService2.Setup(x => x.IsMatch(It.IsAny <string>())).Returns(true);
            var identifierServices = new List <IIdentifierService>
            {
                identifierService1.Object, identifierService2.Object
            };

            await SuperPoweredPeopleData.SplitSuperPoweredPeople(repoMock.Object, identifierServices);

            var namesCount = names.Count();

            repoMock.Verify(x => x.GetAsync(), Times.Once());
            identifierService1.Verify(x => x.IsMatch(It.IsAny <string>()), Times.Exactly(namesCount));
            identifierService1.Verify(x => x.Save(), Times.Once());
            identifierService2.Verify(x => x.IsMatch(It.IsAny <string>()), Times.Exactly(namesCount));
            identifierService2.Verify(x => x.Add(It.IsAny <string>()), Times.Exactly(namesCount));
            identifierService2.Verify(x => x.Save(), Times.Once());
        }
        public void SplitSuperPoweredPeople_Should_Throw_ArgumentNullException(
            Mock <IAllSuperPoweredRepository> repoMock,
            IEnumerable <Mock <IIdentifierService> > identifierServicesMock, string paramName)
        {
            Func <Task> act = async() => await SuperPoweredPeopleData.SplitSuperPoweredPeople(repoMock?.Object,
                                                                                              identifierServicesMock?.Select(x => x.Object));

            act.Should().ThrowExactly <ArgumentNullException>().Which.ParamName.Should().Be(paramName);
        }
        public void SplitSuperPoweredPeople_Should_Throw_ArgumentException()
        {
            var         repoMock = new Mock <IAllSuperPoweredRepository>();
            Func <Task> act      = async() => await SuperPoweredPeopleData.SplitSuperPoweredPeople(repoMock.Object,
                                                                                                   Enumerable.Empty <IIdentifierService>());

            var exception = act.Should().ThrowExactly <ArgumentException>().Which;

            exception.Message.Should().Be("Service collection must not be empty.\r\nParameter name: identifierServices");
        }