Esempio n. 1
0
            public async Task GivenValidLicense_CreatesLicense()
            {
                // Arrange
                var userId = Guid.NewGuid();

                var createLicense = new CreateLicenseDto
                {
                    UserId = userId
                };

                var user = new UserEntity
                {
                    Id = userId
                };

                _mediator
                .SetupHandler <GetUserByIdQuery, UserEntity>()
                .ReturnsAsync(user);

                // Act
                await _classUnderTest.CreateAsync(createLicense, CancellationToken.None);

                // Assert
                _mediator.VerifyHandler <InsertLicenseCommand>(command => command.UserId == userId, Times.Once());
            }
Esempio n. 2
0
            public void GivenInvalidUserId_ThrowsEntityNotFoundException()
            {
                // Arrange
                var createLicense = new CreateLicenseDto
                {
                    UserId = Guid.NewGuid()
                };

                // Act & Assert
                Assert.Multiple(() =>
                {
                    var exception = Assert.ThrowsAsync <EntityNotFoundException>(
                        () => _classUnderTest.CreateAsync(createLicense, CancellationToken.None));

                    Assert.That(exception.ExceptionCode, Is.EqualTo((int)ExceptionCode.EntityNotFound));
                    Assert.That(exception.TechnicalMessage, Is.Not.Null);
                    Assert.That(exception.UserMessage, Is.EqualTo(ExceptionMessages.User.UserNotFound));
                });
            }
        public async Task <CreatedLicenseDto> CreateAsync(CreateLicenseDto createLicense, CancellationToken token)
        {
            await ValidateUserExistsAsync(createLicense.UserId, token);

            var licenseId  = Guid.NewGuid();
            var licenseKey = await GenerateLicenseKey(createLicense.UserId, token);

            var insertLicenseCommandBuilder = new InsertLicenseCommandBuilder()
                                              .WithId(licenseId)
                                              .WithKey(licenseKey)
                                              .WithCreated(DateTime.UtcNow)
                                              .WithUserId(createLicense.UserId);

            var insertLicenseCommand = insertLicenseCommandBuilder.Build();

            await _mediator.Send(insertLicenseCommand, token);

            return(new CreatedLicenseDto {
                LicenseId = licenseId
            });
        }