public void ToComplexEntity_WhenSimpleEntityAndExtendedComplex_ExpectCorrectMap()
        {
            // Arrange
            var mockPropertyMapper = new Mock<IPropertyGetSettersTyped<ExtendedToken>>();
            var mockClaimsMapper = new Mock<IMapper<SimpleClaim, Claim>>();
            var mockClientMapper = new Mock<IMapper<SimpleClient, Client>>();

            mockClaimsMapper.Setup(r => r.ToComplexEntity(It.IsAny<SimpleClaim>())).Returns(new Claim("Val1", "Val2"));
            mockClientMapper.Setup(r => r.ToComplexEntity(It.IsAny<SimpleClient>())).Returns(new Client());

            var typedSetter = new TypedSetter<ExtendedToken>
            {
                OriginalType = typeof(string),
                Setter = typeof(ExtendedToken).GetSetter<ExtendedToken>("CustomDecimal")
            };

            mockPropertyMapper.Setup(r => r.GetSetters(It.IsAny<Type>()))
                .Returns(new Dictionary<string, TypedSetter<ExtendedToken>> { { "CustomDecimal", typedSetter } });

            var tokenMappers = new TokenMapper<ExtendedToken>(mockPropertyMapper.Object, mockClaimsMapper.Object, mockClientMapper.Object);

            var simpleEntity = new SimpleToken
            {
                Claims = new List<SimpleClaim>(),
                Client = new SimpleClient(),
                Type = "Type",
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                Issuer = "Issuer",
                Version = 1,
                Audience = "Audience",
                Lifetime = 1,
                DataBag = new Dictionary<string, object> { { "CustomDecimal", 1.25m } }
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            var complexEntity = tokenMappers.ToComplexEntity(simpleEntity);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(complexEntity, Is.Not.Null);

            Assert.That(complexEntity.Claims, Is.Not.Null);
            Assert.That(complexEntity.Client, Is.Not.Null);
            Assert.That(complexEntity.Type, Is.EqualTo("Type"));
            Assert.That(complexEntity.CreationTime, Is.EqualTo(new DateTimeOffset(new DateTime(2016, 1, 1))));
            Assert.That(complexEntity.Issuer, Is.EqualTo("Issuer"));
            Assert.That(complexEntity.Version, Is.EqualTo(1));
            Assert.That(complexEntity.Audience, Is.EqualTo("Audience"));
            Assert.That(complexEntity.Lifetime, Is.EqualTo(1));

            Assert.That(complexEntity.CustomDecimal, Is.EqualTo(1.25));
        }
        public void ToComplexEntity_WhenSimpleEntityAndExtendedComplex_ExpectCorrectMap()
        {
            // Arrange
            var mockPropertyMapper = new Mock<IPropertyGetSettersTyped<ExtendedRefreshToken>>();
            var mockClaimsPrincipalMapper = new Mock<IMapper<SimpleClaimsPrincipal, ClaimsPrincipal>>();
            var mockTokenMapper = new Mock<IMapper<SimpleToken, Token>>();

            mockClaimsPrincipalMapper.Setup(r => r.ToComplexEntity(It.IsAny<SimpleClaimsPrincipal>()))
                .Returns(new ClaimsPrincipal());

            mockTokenMapper.Setup(r => r.ToComplexEntity(It.IsAny<SimpleToken>())).Returns(new Token());

            var typedSetter = new TypedSetter<ExtendedRefreshToken>
            {
                OriginalType = typeof(int),
                Setter = typeof(ExtendedRefreshToken).GetSetter<ExtendedRefreshToken>("CustomProperty")
            };

            mockPropertyMapper.Setup(r => r.GetSetters(It.IsAny<Type>()))
                .Returns(new Dictionary<string, TypedSetter<ExtendedRefreshToken>> { { "CustomProperty", typedSetter } });

            var refreshTokenMappers = new RefreshTokenMappers<ExtendedRefreshToken>(
                mockPropertyMapper.Object,
                mockClaimsPrincipalMapper.Object,
                mockTokenMapper.Object);

            var simpleEntity = new SimpleRefreshToken
            {
                Subject = new SimpleClaimsPrincipal(),
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                AccessToken = new SimpleToken(),
                LifeTime = 1,
                Version = 1,
                DataBag = new Dictionary<string, object> { { "CustomProperty", new CustomProperty { Name = "Joe", Age = 23 } } }
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            var complexEntity = refreshTokenMappers.ToComplexEntity(simpleEntity);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(complexEntity, Is.Not.Null);

            Assert.That(complexEntity.Subject, Is.Not.Null);
            Assert.That(complexEntity.AccessToken, Is.Not.Null);
            Assert.That(complexEntity.CreationTime, Is.EqualTo(new DateTimeOffset(new DateTime(2016, 1, 1))));
            Assert.That(complexEntity.LifeTime, Is.EqualTo(1));
            Assert.That(complexEntity.Version, Is.EqualTo(1));

            Assert.That(complexEntity.CustomProperty, Is.Not.Null);
            Assert.That(complexEntity.CustomProperty.Name, Is.EqualTo("Joe"));
            Assert.That(complexEntity.CustomProperty.Age, Is.EqualTo(23));
        }
        public void ToComplexEntity_WhenSimpleEntityAndExtendedComplex_ExpectMapSuccess()
        {
            // Arrange
            var mockPropertyMapper = new Mock<IPropertyGetSettersTyped<ExtendedScope>>();

            var typedSetter = new TypedSetter<ExtendedScope>
            {
                OriginalType = typeof(string),
                Setter = typeof(ExtendedScope).GetSetter<ExtendedScope>("CustomString")
            };

            mockPropertyMapper.Setup(r => r.GetSetters(It.IsAny<Type>()))
                .Returns(new Dictionary<string, TypedSetter<ExtendedScope>> { { "CustomString", typedSetter } });

            var scopeMappers = new ScopeMappers<ExtendedScope>(mockPropertyMapper.Object);

            var scopeClaim = new ScopeClaim("Name", true) { Description = "Description" };
            var secret = new Secret("Value", "Description", new DateTimeOffset(new DateTime(2016, 1, 1))) { Type = "Type" };

            var simpleScope = new SimpleScope
            {
                Claims = new List<ScopeClaim> { scopeClaim },
                Type = ScopeType.Identity,
                Enabled = true,
                AllowUnrestrictedIntrospection = true,
                ScopeSecrets = new List<Secret> { secret },
                DisplayName = "DisplayName",
                Emphasize = true,
                ClaimsRule = "ClaimsRule",
                IncludeAllClaimsForUser = true,
                Name = "Name",
                Required = true,
                ShowInDiscoveryDocument = true,
                Description = "Description",
                DataBag = new Dictionary<string, object> { { "CustomString", "customString" } }
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            var complexEntity = scopeMappers.ToComplexEntity(simpleScope);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(complexEntity, Is.Not.Null);

            Assert.That(complexEntity.Claims, Is.Not.Null);
            Assert.That(complexEntity.Claims.Count, Is.EqualTo(1));

            Assert.That(complexEntity.Type, Is.EqualTo(ScopeType.Identity));
            Assert.That(complexEntity.Enabled, Is.True);
            Assert.That(complexEntity.AllowUnrestrictedIntrospection, Is.True);

            Assert.That(complexEntity.ScopeSecrets, Is.Not.Null);
            Assert.That(complexEntity.ScopeSecrets.Count, Is.EqualTo(1));

            Assert.That(complexEntity.DisplayName, Is.EqualTo("DisplayName"));
            Assert.That(complexEntity.Emphasize, Is.True);
            Assert.That(complexEntity.ClaimsRule, Is.EqualTo("ClaimsRule"));
            Assert.That(complexEntity.IncludeAllClaimsForUser, Is.True);
            Assert.That(complexEntity.Name, Is.EqualTo("Name"));
            Assert.That(complexEntity.Required, Is.True);
            Assert.That(complexEntity.ShowInDiscoveryDocument, Is.True);
            Assert.That(complexEntity.Description, Is.EqualTo("Description"));
            Assert.That(complexEntity.CustomString, Is.EqualTo("customString"));
        }
        public void ToComplexEntity_WhenSimpleEntityAndExtendedComplex_ExpectMapSuccess()
        {
            // Arrange
            var mockClaimsMapper = new Mock<IMapper<SimpleClaim, Claim>>();
            var mockPropertyMapper = new Mock<IPropertyGetSettersTyped<ExtendedClient>>();

            mockClaimsMapper.Setup(r => r.ToComplexEntity(It.IsAny<IEnumerable<SimpleClaim>>())).Returns(new List<Claim> { new Claim("DEFAULT", "DEFAULT") });

            var typedSetter = new TypedSetter<ExtendedClient>
            {
                OriginalType = typeof(int),
                Setter = typeof(ExtendedClient).GetSetter<ExtendedClient>("CustomDate")
            };

            mockPropertyMapper.Setup(r => r.GetSetters(It.IsAny<Type>())).Returns(new Dictionary<string, TypedSetter<ExtendedClient>>
            {
                {"CustomDate", typedSetter }
            });

            var clientMappers = new ClientMappers<ExtendedClient>(mockClaimsMapper.Object, mockPropertyMapper.Object);

            var secret = new Secret("Value", "Description", new DateTimeOffset(new DateTime(2016, 1, 1))) { Type = "Type" };

            var simpleClient = new SimpleClient
            {
                Claims = new List<SimpleClaim>(),
                Enabled = true,
                AccessTokenType = AccessTokenType.Jwt,
                AbsoluteRefreshTokenLifetime = 1,
                AccessTokenLifetime = 1,
                AllowAccessToAllCustomGrantTypes = true,
                AllowAccessToAllScopes = true,
                AllowRememberConsent = true,
                EnableLocalLogin = true,
                AllowAccessTokensViaBrowser = true,
                LogoutSessionRequired = true,
                Flow = Flows.AuthorizationCode,
                AlwaysSendClientClaims = true,
                PrefixClientClaims = true,
                ClientSecrets = new List<Secret> { secret },
                RefreshTokenExpiration = TokenExpiration.Absolute,
                RequireSignOutPrompt = true,
                RefreshTokenUsage = TokenUsage.OneTimeOnly,
                IdentityTokenLifetime = 1,
                SlidingRefreshTokenLifetime = 1,
                RequireConsent = true,
                AllowClientCredentialsOnly = true,
                IncludeJwtId = true,
                AuthorizationCodeLifetime = 1,
                UpdateAccessTokenClaimsOnRefresh = true,
                ClientName = "ClientName",
                LogoutUri = "LogoutUri",
                RedirectUris = new List<string>(),
                ClientUri = "ClientUri",
                AllowedCustomGrantTypes = new List<string>(),
                AllowedScopes = new List<string>(),
                ClientId = "ClientId",
                PostLogoutRedirectUris = new List<string>(),
                AllowedCorsOrigins = new List<string>(),
                IdentityProviderRestrictions = new List<string>(),
                LogoUri = "LogoUri",
                DataBag = new Dictionary<string, object> { { "CustomDate", new DateTime(2016, 1, 1) } }
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            var complexEntity = clientMappers.ToComplexEntity(simpleClient);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(complexEntity, Is.Not.Null);

            Assert.That(complexEntity.CustomDate, Is.EqualTo(new DateTime(2016, 1, 1)));
        }
        public void ToComplexEntity_WhenSimpleEntityAndExtendedComplex_ExpectCorrectMap()
        {
            // Arrange
            var mockPropertyMapper = new Mock<IPropertyGetSettersTyped<ExtendedAuthorizationCode>>();
            var mockClaimsPrincipalMapper = new Mock<IMapper<SimpleClaimsPrincipal, ClaimsPrincipal>>();
            var mockClientMapper = new Mock<IMapper<SimpleClient, Client>>();
            var mockScopeMapper = new Mock<IMapper<SimpleScope, Scope>>();

            mockClaimsPrincipalMapper.Setup(r => r.ToComplexEntity(It.IsAny<SimpleClaimsPrincipal>()))
                .Returns(new ClaimsPrincipal());

            var typedSetter = new TypedSetter<ExtendedAuthorizationCode>
            {
                OriginalType = typeof(int),
                Setter = typeof(ExtendedAuthorizationCode).GetSetter<ExtendedAuthorizationCode>("CustomNumber")
            };

            mockPropertyMapper.Setup(r => r.GetSetters(It.IsAny<Type>()))
                .Returns(new Dictionary<string, TypedSetter<ExtendedAuthorizationCode>> { { "CustomNumber", typedSetter } });

            mockClientMapper.Setup(r => r.ToComplexEntity(It.IsAny<SimpleClient>())).Returns(new Client());
            mockScopeMapper.Setup(r => r.ToComplexEntity(It.IsAny<IEnumerable<SimpleScope>>())).Returns(new List<Scope> { new Scope() });

            var authorizationCodeMappers = new AuthorizationCodeMappers<ExtendedAuthorizationCode>(
                mockPropertyMapper.Object,
                mockClaimsPrincipalMapper.Object,
                mockClientMapper.Object,
                mockScopeMapper.Object);

            var simpleEntity = new SimpleAuthorizationCode
            {
                Client = new SimpleClient(),
                Subject = new SimpleClaimsPrincipal(),
                CreationTime = new DateTimeOffset(new DateTime(2016, 1, 1)),
                RedirectUri = "RedirectUri",
                Nonce = "Nonce",
                WasConsentShown = true,
                CodeChallengeMethod = "CodeChallengeMethod",
                IsOpenId = true,
                SessionId = "SessionId",
                CodeChallenge = "CodeChallenge",
                RequestedScopes = new List<SimpleScope>(),
                DataBag = new Dictionary<string, object> { { "CustomNumber", 12 } }
            };

            // Act
            var stopwatch = Stopwatch.StartNew();
            var complexEntity = authorizationCodeMappers.ToComplexEntity(simpleEntity);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(complexEntity, Is.Not.Null);

            Assert.That(complexEntity.Client, Is.Not.Null);
            Assert.That(complexEntity.Subject, Is.Not.Null);
            Assert.That(complexEntity.CreationTime, Is.EqualTo(new DateTimeOffset(new DateTime(2016, 1, 1))));
            Assert.That(complexEntity.RedirectUri, Is.EqualTo("RedirectUri"));
            Assert.That(complexEntity.Nonce, Is.EqualTo("Nonce"));
            Assert.That(complexEntity.WasConsentShown, Is.True);
            Assert.That(complexEntity.CodeChallengeMethod, Is.EqualTo("CodeChallengeMethod"));
            Assert.That(complexEntity.IsOpenId, Is.True);
            Assert.That(complexEntity.SessionId, Is.EqualTo("SessionId"));
            Assert.That(complexEntity.CodeChallenge, Is.EqualTo("CodeChallenge"));
            Assert.That(complexEntity.RequestedScopes, Is.Not.Null);
            Assert.That(complexEntity.RequestedScopes.Any(), Is.True);
            Assert.That(complexEntity.CustomNumber, Is.EqualTo(12));
        }