/// <summary> /// Creates test server with identity base startup file. /// </summary> /// <param name="emailServiceMock"></param> /// <param name="configBuild"></param> /// <returns></returns> public static TestServer CreateServer( Mock <IEmailService> emailServiceMock = null, Action <TestConfigurationBuilder> configBuild = null) { TestServer testServer = null; Func <HttpMessageHandler> messageHandlerFactory = () => { return(testServer.CreateHandler()); }; testServer = new TestServerBuilder() .UseEnvironment("Test") .UseContentRoot() .AddStartup((environment) => { var builder = new TestConfigurationBuilder() .UseDefaultConfiguration(); if (configBuild != null) { configBuild.Invoke(builder); } var startup = new IdentityBase.Startup( builder.Build(), environment, new NullLoggerFactory(), messageHandlerFactory ); if (emailServiceMock != null) { startup.OverrideServices = (services => { services.Replace(emailServiceMock.Object); }); } return(startup); }) .Build(); return(testServer); }
public void AddDockerSecrets(string configKey, string expected) { var configurationBuilder = new TestConfigurationBuilder(); Assert.Empty(configurationBuilder.Sources); configurationBuilder .AddDockerSecrets(); Assert.NotEmpty(configurationBuilder.Sources); var configuration = configurationBuilder.Build(); var actual = configuration.GetValue <string>(configKey); Assert.NotNull(actual); Assert.Equal(expected, actual); }
private PrepareHandlerMockResult BuildPrepareHandler(Action <TestConfigurationBuilder> configBuilderAct) { var factory = new FakeConnectionFactory(MockConnection); // create config var configBuilder = new TestConfigurationBuilder { ControlConnectionFactory = new FakeControlConnectionFactory(), ConnectionFactory = factory, Policies = new Cassandra.Policies(new RoundRobinPolicy(), new ConstantReconnectionPolicy(100), new DefaultRetryPolicy()) }; configBuilderAct(configBuilder); var config = configBuilder.Build(); var initializerMock = Mock.Of <IInitializer>(); Mock.Get(initializerMock).Setup(i => i.ContactPoints).Returns(new List <IPEndPoint> { new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042), new IPEndPoint(IPAddress.Parse("127.0.0.2"), 9042), new IPEndPoint(IPAddress.Parse("127.0.0.3"), 9042) }); Mock.Get(initializerMock).Setup(i => i.GetConfiguration()).Returns(config); // create cluster var cluster = Cluster.BuildFrom(initializerMock, new List <string>()); cluster.Connect(); factory.CreatedConnections.Clear(); // create session var session = new Session(cluster, config, null, SerializerManager.Default, null); // create prepare handler var prepareHandler = new PrepareHandler(new SerializerManager(ProtocolVersion.V3).GetCurrentSerializer(), cluster); // create mock result object var mockResult = new PrepareHandlerMockResult(prepareHandler, session, factory); return(mockResult); }
public static TestServer CreateServer( Mock <IEmailService> emailServiceMock = null, Action <TestConfigurationBuilder> configBuild = null, HttpMessageHandler messageHandler = null) { return(new TestServerBuilder() .UseEnvironment("Test") .UseContentRoot() .AddServices(services => { if (emailServiceMock != null) { services.AddSingleton(emailServiceMock.Object); } }) .AddStartup((environment) => { var builder = new TestConfigurationBuilder() .UseDefaultConfiguration(); if (emailServiceMock != null) { builder.RemoveDebugEmailModule(); } if (configBuild != null) { configBuild.Invoke(builder); } return new Startup( builder.Build(), environment, new NullLogger <Startup>(), messageHandler ); }) .Build()); }
private RequestHandlerMockResult BuildRequestHandler( IStatement statement, Action <TestConfigurationBuilder> configBuilderAct, IExecutionProfile profile) { var cts = new CancellationTokenSource(); var connection = Mock.Of <IConnection>(); // create config var configBuilder = new TestConfigurationBuilder { ControlConnectionFactory = new FakeControlConnectionFactory(), ConnectionFactory = new FakeConnectionFactory(() => connection), Policies = new Cassandra.Policies(new RoundRobinPolicy(), new ConstantReconnectionPolicy(100), new DefaultRetryPolicy()) }; configBuilderAct(configBuilder); var config = configBuilder.Build(); var initializerMock = Mock.Of <IInitializer>(); Mock.Get(initializerMock).Setup(i => i.ContactPoints).Returns(new List <IPEndPoint> { new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042), new IPEndPoint(IPAddress.Parse("127.0.0.2"), 9042) }); Mock.Get(initializerMock).Setup(i => i.GetConfiguration()).Returns(config); // create cluster var cluster = Cluster.BuildFrom(initializerMock, new List <string>()); cluster.Connect(); // create session var session = new Session(cluster, config, null, SerializerManager.Default, null); // create request handler var options = profile != null ? new RequestOptions(profile, null, config.Policies, config.SocketOptions, config.QueryOptions, config.ClientOptions) : config.DefaultRequestOptions; var requestHandler = new RequestHandler( session, new SerializerManager(ProtocolVersion.V3).GetCurrentSerializer(), statement, options); // create mock result object var mockResult = new RequestHandlerMockResult(requestHandler); // mock connection send Mock.Get(connection) .Setup(c => c.Send(It.IsAny <IRequest>(), It.IsAny <Action <IRequestError, Response> >(), It.IsAny <int>())) .Returns <IRequest, Action <IRequestError, Response>, int>((req, act, timeout) => { mockResult.SendResults.Enqueue(new ConnectionSendResult { Request = req, TimeoutMillis = timeout }); Task.Run(async() => { var rp = (FakeRetryPolicy)(statement.RetryPolicy ?? options.RetryPolicy); var sep = (FakeSpeculativeExecutionPolicy)options.SpeculativeExecutionPolicy; if (Interlocked.Read(ref rp.Count) > 0 && Interlocked.Read(ref sep.Count) > 0) { await Task.Delay(1, cts.Token).ConfigureAwait(false); act(null, new ProxyResultResponse(ResultResponse.ResultResponseKind.Void)); cts.Cancel(); } else { try { await Task.Delay(10, cts.Token).ConfigureAwait(false); } finally { act(RequestError.CreateServerError(new OverloadedException(string.Empty)), null); } } }); return(new OperationState(act, req, timeout, NullOperationObserver.Instance)); }); Mock.Get(connection) .SetupGet(c => c.EndPoint) .Returns(new ConnectionEndPoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042), config.ServerNameResolver, null)); return(mockResult); }
private ControlConnectionCreateResult NewInstance( IDictionary <IPEndPoint, IRow> rows = null, IInternalCluster cluster = null, Configuration config = null, Metadata metadata = null, Action <TestConfigurationBuilder> configBuilderAct = null) { if (rows == null) { rows = new Dictionary <IPEndPoint, IRow> { { new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9042), TestHelper.CreateRow(new Dictionary <string, object> { { "cluster_name", "ut-cluster" }, { "data_center", "ut-dc" }, { "rack", "ut-rack" }, { "tokens", null }, { "release_version", "2.2.1-SNAPSHOT" } }) } }; } if (cluster == null) { cluster = Mock.Of <IInternalCluster>(); } var connectionFactory = new FakeConnectionFactory(); if (config == null) { var builder = new TestConfigurationBuilder { ConnectionFactory = connectionFactory, TopologyRefresherFactory = new FakeTopologyRefresherFactory(rows), SchemaParserFactory = new FakeSchemaParserFactory(), SupportedOptionsInitializerFactory = new FakeSupportedOptionsInitializerFactory(), ProtocolVersionNegotiator = new FakeProtocolVersionNegotiator(), ServerEventsSubscriber = new FakeServerEventsSubscriber() }; configBuilderAct?.Invoke(builder); config = builder.Build(); } if (metadata == null) { metadata = new Metadata(config); } return(new ControlConnectionCreateResult { ConnectionFactory = connectionFactory, Metadata = metadata, Cluster = cluster, Config = config, ControlConnection = new ControlConnection( cluster, GetEventDebouncer(config), ProtocolVersion.MaxSupported, config, metadata, new List <IContactPoint> { new IpLiteralContactPoint( IPAddress.Parse("127.0.0.1"), config.ProtocolOptions, config.ServerNameResolver) }) }); }