Example #1
0
    public void Implements_ServiceConfig()
    {
        // Arrange
        var config = new SeqConfig();

        // Act

        // Assert
        Assert.IsAssignableFrom <IServiceConfig>(config);
    }
Example #2
0
    public void Returns_With_Server_Value()
    {
        // Arrange
        var server = Rnd.Str;
        var config = new SeqConfig {
            Server = server
        };

        // Act
        var result = config.Webhook;

        // Assert
        Assert.Equal($"{server}/api/events/raw?clef", result);
    }
Example #3
0
    public void Returns_True()
    {
        // Arrange
        var config = new SeqConfig
        {
            Server = "https://news.contoso.com",
            ApiKey = Rnd.Str
        };

        // Act
        var result = config.IsValid;

        // Assert
        Assert.True(result);
    }
Example #4
0
    public void Returns_False(string server, string apiKey)
    {
        // Arrange
        var config = new SeqConfig
        {
            Server = server,
            ApiKey = apiKey,
        };

        // Act
        var result = config.IsValid;

        // Assert
        Assert.False(result);
    }
Example #5
0
    public void Returns_ServiceConfig()
    {
        // Arrange
        var config  = new ServicesConfig();
        var name    = Rnd.Str;
        var service = new SeqConfig
        {
            Server = "https://www.contoso.com",
            ApiKey = Rnd.Str
        };

        config.Seq.Add(name, service);

        // Act
        var result = config.GetServiceConfig(x => x.Seq, name);

        // Assert
        Assert.Equal(service, result);
    }
Example #6
0
    public void Splits_Definition_And_Returns_ServiceConfig()
    {
        // Arrange
        var config  = new ServicesConfig();
        var name    = Rnd.Str;
        var service = new SeqConfig
        {
            Server = "https://www.contoso.com",
            ApiKey = Rnd.Str
        };

        config.Seq.Add(name, service);

        // Act
        var result = config.GetServiceConfig($"seq.{name}");

        // Assert
        Assert.Equal(service, result);
    }
Example #7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //configure snakecase request/response
            services.AddControllers().AddJsonOptions(
                options =>
            {
                options.JsonSerializerOptions.PropertyNamingPolicy =
                    SnakeCaseNamingPolicy.Instance;
            });

            //automapper
            var mapperConfiguration = new MapperConfiguration(
                cfg => { cfg.AddProfile(new CustomerMapper()); }
                );

            //configuration mongo and gateway
            var settings = new ConnectionMongoSettings();

            Configuration.GetSection("ConnectionMongoSettings").Bind(settings);

            var gatewayConfig = new GatewayConfig();

            Configuration.GetSection("GatewayConfig").Bind(gatewayConfig);

            var seqConfig = new SeqConfig();

            Configuration.GetSection("SeqConfig").Bind(seqConfig);

            Console.WriteLine(settings.ConnectionString);
            Console.WriteLine(gatewayConfig.Url);
            Console.WriteLine(gatewayConfig.SecretKey);

            // connection mongo and create request
            services.AddSingleton <IMongoDatabase>(_ =>
            {
                var client = new MongoClient(settings.ConnectionString);
                return(client.GetDatabase(settings.DatabaseName));
            });

            services.AddSingleton <IRestRequest>(_ =>
            {
                var request = new RestRequest();

                request.AddHeader("Content-Type", "application/json");
                return(request);
            });

            services.AddSingleton <IRestClient>(_ =>
            {
                var restClient           = new RestClient(gatewayConfig.Url + "/{endpoint}");
                restClient.Authenticator = new HttpBasicAuthenticator(gatewayConfig.SecretKey, "");
                return(restClient);
            });

            //restSharp
            SimpleJson.CurrentJsonSerializerStrategy = new SnakeJsonSerializerStrategy();

            // seq log
            services.AddSingleton <ILogInfo>(_ =>
            {
                var seq = new LoggerConfiguration()
                          .WriteTo.Console()
                          .WriteTo.Seq(seqConfig.Url)
                          .CreateLogger();

                return(new LogInfo(seq));
            });

            //service core
            services.AddScoped <ICustomerService, CustomerService>();

            //services infra
            services.AddSingleton <IGatewayCustomerService, CustomerServiceInfra>(container =>
            {
                var restRequest = (IRestRequest)container.GetService(typeof(IRestRequest));
                var restClient  = (IRestClient)container.GetService(typeof(IRestClient));
                var logInfo     = (ILogInfo)container.GetService(typeof(ILogInfo));

                return(new CustomerServiceInfra(restRequest, restClient, mapperConfiguration, logInfo));
            });

            //repositories
            services.AddScoped <ICustomerRepository, CustomerRepository>();

            //heathCheck
            services
            .AddHealthChecks()
            .AddMongoDb(settings.ConnectionString, name: "MongoCheck", timeout: TimeSpan.FromSeconds(2))
            .AddUrlGroup(
                options =>
            {
                options.AddUri(new Uri($"{gatewayConfig.Url}/customers"));
                options.UseHttpMethod(Post);
                options.ExpectHttpCodes(200, 401);
            },
                "MundipaggGatewayCheck",
                timeout: TimeSpan.FromSeconds(10)
                );

            //doc swagger
            services.AddSwaggerGen(container =>
            {
                container.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "My API", Version = "v1"
                });
            }
                                   );
        }