public async Task HandleShouldFailWhenRecordWithSameNameAlreadyExist()
        {
            // Arrange
            var id          = Guid.NewGuid();
            var name        = "name";
            var image       = "image";
            var description = "description";
            var layout      = "layout";
            var driverWait  = "Yes";
            var operators   = new List <Guid>()
            {
                Guid.NewGuid()
            };
            var filterContent = "{site:123456}";
            var version       = 1;

            var team = new Team(TeamId.With(id), new Name(name));

            team.SetVersion(version);


            var teamWriteRepositoryMock = new Mock <ITeamWriteRepository>();

            teamWriteRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny <Team>())).Throws <UniqueKeyException>();
            var teamWriteRepository = teamWriteRepositoryMock.Object;

            var teamReadRepositoryMock = new Mock <ITeamReadRepository>();

            teamReadRepositoryMock.Setup(x => x.GetAsync(id)).Returns(Task.FromResult(team));
            var teamReadRepository = teamReadRepositoryMock.Object;

            var versionProviderMock = new Mock <IVersionProvider>();

            versionProviderMock.Setup(x => x.Generate()).Returns(version);
            var versionProvider = versionProviderMock.Object;

            var command = new UpdateTeamCommand(id,
                                                name,
                                                image,
                                                description,
                                                driverWait,
                                                layout,
                                                operators,
                                                filterContent,
                                                version);

            var handler = new UpdateTeamCommandHandler(teamWriteRepository, teamReadRepository, versionProvider);

            // Act
            var result = await handler.Handle(command, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.Failures.Should().OnlyContain(x => x.Code == HandlerFaultCode.Conflict.Name &&
                                                 x.Message == HandlerFailures.ConflictTeam &&
                                                 x.Target == "name");
        }
        public async Task HandleShouldReturnOk()
        {
            // Arrange
            var id          = Guid.NewGuid();
            var name        = "name";
            var image       = "image";
            var description = "description";
            var layout      = "layout";
            var driverWait  = "Yes";
            var operators   = new List <Guid>()
            {
                Guid.NewGuid()
            };
            var filterContent = "{site:123456}";
            var version       = 1;

            var team = new Team(TeamId.With(id), new Name(name));

            team.SetVersion(version);


            var teamWriteRepositoryMock = new Mock <ITeamWriteRepository>();

            teamWriteRepositoryMock.Setup(x => x.UpdateAsync(It.IsAny <Team>())).Returns(Task.CompletedTask);
            var teamWriteRepository = teamWriteRepositoryMock.Object;

            var teamReadRepositoryMock = new Mock <ITeamReadRepository>();

            teamReadRepositoryMock.Setup(x => x.GetAsync(id)).Returns(Task.FromResult(team));
            var teamReadRepository = teamReadRepositoryMock.Object;

            var versionProviderMock = new Mock <IVersionProvider>();

            versionProviderMock.Setup(x => x.Generate()).Returns(version);
            var versionProvider = versionProviderMock.Object;

            var command = new UpdateTeamCommand(id,
                                                name,
                                                image,
                                                description,
                                                driverWait,
                                                layout,
                                                operators,
                                                filterContent,
                                                version);

            var handler = new UpdateTeamCommandHandler(teamWriteRepository, teamReadRepository, versionProvider);

            // Act
            var result = await handler.Handle(command, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeFalse();
            result.Should().BeOfType(typeof(Result));
        }
Esempio n. 3
0
        public void CreateTeamIdWithInvalidIdShouldFail(string id)
        {
            // Arrange
            string invalidId = id;

            // Act
            Action action = () => { TeamId.With(invalidId); };

            // Assert
            action.Should().Throw <InvalidIdentityException>();
        }
Esempio n. 4
0
        public void GuidToAndFromTeamIdShouldSucceed()
        {
            // Arrange
            var guid = Guid.NewGuid();

            // Act
            var teamId = TeamId.With(guid);
            var result = teamId.GetGuid();

            // Assert
            result.Should().Be(guid);
        }
        public async Task <Result> Handle(CreateTeamCommand command, CancellationToken cancellationToken)
        {
            var id = _identifierProvider.Generate();

            var teamToCreate = new Team(TeamId.With(id), new Name(command.Name));

            teamToCreate.SetImage(command.Image);
            teamToCreate.SetDescription((Description)command.Description);

            if (Guid.TryParse(command.Layout, out Guid layoutId) && layoutId != default(Guid))
            {
                var layout = new Layout(layoutId);
                teamToCreate.SetLayout(layout);
            }

            teamToCreate.SetVersion(_versionProvider.Generate());

            Result result;

            try
            {
                await _teamWriteRepository.CreateAsync(teamToCreate);

                return(Result.Ok(id, teamToCreate.Version));
            }
            catch (UniqueKeyException)
            {
                result = Result.Fail(new List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.Conflict.Name,
                        Message = HandlerFailures.ConflictTeam,
                        Target  = "name"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.CreateTeamFailure);
            }

            return(result);
        }
Esempio n. 6
0
        public async Task <Team> GetAsync(Guid id)
        {
            try
            {
                var findById = await _collection.FindAsync(doc => doc.Id == id);

                var teamClass = findById.FirstOrDefault();
                if (teamClass == null)
                {
                    throw Error.EntityNotFoundDb();
                }

                var teamId = TeamId.With(teamClass.Id);
                var team   = new Team(teamId, new Name(teamClass.Name));
                team.SetImage(teamClass.Image);
                team.SetDescription((Description)teamClass.Description);
                team.SetImage(teamClass.Image);
                Layout layout = null;
                if (teamClass.Layout != null)
                {
                    layout = new Layout(teamClass.Layout.Id);
                }
                team.SetLayout(layout);
                team.SetFilterContent(teamClass.FilterContent);
                foreach (var member in teamClass.Members)
                {
                    team.AddMember(new Member(member.Id));
                }
                team.SetVersion(teamClass.Version);

                return(team);
            }
            catch (MongoCommandException ex)
            {
                Debug.WriteLine(ex);
                throw Error.GenericDb(ex);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
        }
Esempio n. 7
0
        public async Task <IEnumerable <Team> > ListAsync(string filter, int?skip, int?limit)
        {
            var filterDefinition = _teamOdataProvider.GetFilterDefinition(filter);

            IFindFluent <TeamClass, TeamClass> fluent = _collection.Find(filterDefinition);

            fluent = fluent.Skip(skip).Limit(limit);

            var teamClasses = await fluent.ToListAsync();

            var result = new List <Team>();

            foreach (var teamClass in teamClasses)
            {
                var team = new Team(TeamId.With(teamClass.Id), new Name(teamClass.Name));
                team.SetImage(teamClass.Image);
                team.SetDescription((Description)teamClass.Description);
                team.SetImage(teamClass.Image);
                Layout layout = null;
                if (teamClass.Layout != null)
                {
                    layout = new Layout(teamClass.Layout.Id);
                }
                team.SetLayout(layout);
                team.SetFilterContent(teamClass.FilterContent);
                foreach (var member in teamClass.Members)
                {
                    team.AddMember(new Member(member.Id));
                }
                team.SetVersion(teamClass.Version);

                result.Add(team);
            }

            return(result);
        }
        public async Task HandleShouldReturnOk()
        {
            // Arrange
            var id   = Guid.NewGuid();
            var name = "name";

            var teamReadRepositoryMock = new Mock <ITeamReadRepository>();

            teamReadRepositoryMock.Setup(x => x.GetAsync(id)).Returns(Task.FromResult(new Team(TeamId.With(id), new Name(name))));
            var teamReadRepository     = teamReadRepositoryMock.Object;
            var operatorReadRepository = new Mock <IOperatorReadRepository>().Object;

            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(x => x.Map <TeamModel>(It.IsAny <object>())).Returns(new TeamModel());
            var mapper = mapperMock.Object;

            var query = new GetTeamQuery(id);

            var handler = new GetTeamQueryHandler(mapper, teamReadRepository, operatorReadRepository);

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeFalse();
            result.Should().BeOfType(typeof(Result <TeamModel>));
        }