public async Task Then_The_Endpoint_Is_Called_And_Find_Data_Returned( OsPlacesApiResponse osPlacesApiResponse, string query, double minMatch, int matchPrecision) { // Arrange var response = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(osPlacesApiResponse)), StatusCode = System.Net.HttpStatusCode.Accepted }; var minMatchBase = Math.Round(minMatch, 1, MidpointRounding.ToZero); var config = new LocationApiConfiguration { OsPlacesApiKey = Guid.NewGuid().ToString() }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.OsPlacesFindUrl, config.OsPlacesApiKey, query, "dpa", minMatchBase, matchPrecision))); var client = new HttpClient(httpMessageHandler.Object); var osPlacesApiService = new OsPlacesApiService(client, config); // Act var actual = await osPlacesApiService.FindFromDpaDataset(query, minMatch); // Assert actual.Should().BeEquivalentTo(osPlacesApiResponse.Results.Select(p => (SuggestedAddress)p.Dpa)); }
public async Task Then_If_NotFound_Result_Then_Service_Returns_Null() { var response = new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound, }; // Arrange var query = "AB1 1AB"; var config = new LocationApiConfiguration { OsPlacesApiKey = Guid.NewGuid().ToString() }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.OsPlacesFindUrl, config.OsPlacesApiKey, query, "dpa", 0.4, 1))); var client = new HttpClient(httpMessageHandler.Object); var osPlacesApiService = new OsPlacesApiService(client, config); // Act var actual = await osPlacesApiService.FindFromDpaDataset(query, 0.4); // Assert actual.Should().BeEmpty(); }
public void Then_The_Dependencies_Are_Correctly_Resolved(Type toResolve) { var config = new LocationApiConfiguration { ConnectionString = "test" }; var serviceCollection = new ServiceCollection(); serviceCollection.AddDatabaseRegistration(config, "local"); var provider = serviceCollection.BuildServiceProvider(); var type = provider.GetService(toResolve); Assert.IsNotNull(type); }
public void Then_The_Context_Is_Resolved_For_Dev() { var config = new LocationApiConfiguration { ConnectionString = "test" }; var serviceCollection = new ServiceCollection(); serviceCollection.AddDatabaseRegistration(config, "Dev"); var provider = serviceCollection.BuildServiceProvider(); var type = provider.GetService <ILocationDataContext>(); Assert.IsNotNull(type); type.GetProviderName().Should().EndWith("InMemory"); }
public void Then_The_Context_Is_Resolved() { var config = new LocationApiConfiguration { ConnectionString = "test" }; var serviceCollection = new ServiceCollection(); serviceCollection.AddDatabaseRegistration(config, "TEST"); var provider = serviceCollection.BuildServiceProvider(); var type = provider.GetService <ILocationDataContext>(); Assert.IsNotNull(type); var tokenProvider = provider.GetService <AzureServiceTokenProvider>(); Assert.IsNotNull(tokenProvider); }
public void Then_If_Arguments_Are_Out_Of_Range_An_Expception_Is_Thrown() { var response = new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, }; // Arrange var query = "AB1 1AB"; var config = new LocationApiConfiguration { OsPlacesApiKey = Guid.NewGuid().ToString() }; var httpMessageHandler = MessageHandler.SetupMessageHandlerMock(response, new Uri(string.Format(Constants.OsPlacesFindUrl, config.OsPlacesApiKey, query, "dpa", 1.1, 1))); var client = new HttpClient(httpMessageHandler.Object); var osPlacesApiService = new OsPlacesApiService(client, config); // Act & Assert Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => osPlacesApiService.FindFromDpaDataset(query, 1.1)); }
public static void AddDatabaseRegistration(this IServiceCollection services, LocationApiConfiguration config, string environmentName) { if (environmentName.Equals("DEV", StringComparison.CurrentCultureIgnoreCase)) { services.AddDbContext <LocationDataContext>(options => options.UseInMemoryDatabase("SFA.DAS.Location"), ServiceLifetime.Transient); } else if (environmentName.Equals("LOCAL", StringComparison.CurrentCultureIgnoreCase)) { services.AddDbContext <LocationDataContext>(options => options.UseSqlServer(config.ConnectionString), ServiceLifetime.Transient); } else { services.AddSingleton(new AzureServiceTokenProvider()); services.AddDbContext <LocationDataContext>(ServiceLifetime.Transient); } services.AddTransient <ILocationDataContext, LocationDataContext>(provider => provider.GetService <LocationDataContext>()); services.AddTransient(provider => new Lazy <LocationDataContext>(provider.GetService <LocationDataContext>())); services.AddTransient <IImportAuditRepository, ImportAuditRepository>(); services.AddTransient <ILocationRepository, LocationRepository>(); services.AddTransient <ILocationImportRepository, LocationImportRepository>(); }
public LocationDataContext(IOptions <LocationApiConfiguration> config, DbContextOptions options, AzureServiceTokenProvider azureServiceTokenProvider) : base(options) { _configuration = config.Value; _azureServiceTokenProvider = azureServiceTokenProvider; }
public OsPlacesApiService(HttpClient client, LocationApiConfiguration config) { _client = client; _config = config; }