1
+ using CarbonAware . Configuration ;
1
2
using CarbonAware . DataSources . ElectricityMaps . Client ;
2
3
using CarbonAware . DataSources . ElectricityMaps . Model ;
3
4
using CarbonAware . Exceptions ;
4
5
using CarbonAware . Interfaces ;
5
6
using CarbonAware . Model ;
7
+ using Microsoft . Extensions . Configuration ;
8
+ using Microsoft . Extensions . DependencyInjection ;
6
9
using Microsoft . Extensions . Logging ;
7
10
using Moq ;
8
11
@@ -23,6 +26,14 @@ class ElectricityMapsDataSourceTests
23
26
private static string _defaultLongitude => _defaultLocation . Longitude . ToString ( ) ?? "" ;
24
27
private static DateTimeOffset _defaultDataStartTime = new DateTimeOffset ( 2022 , 4 , 18 , 12 , 32 , 42 , TimeSpan . FromHours ( - 6 ) ) ;
25
28
29
+ private readonly string TypeKey = $ "Configurations:ElectricityMaps:Type";
30
+ private readonly string UsernameKey = $ "Configurations:ElectricityMaps:Username";
31
+ private readonly string PasswordKey = $ "Configurations:ElectricityMaps:Password";
32
+ private readonly string UseProxyKey = $ "Configurations:ElectricityMaps:Proxy:UseProxy";
33
+ private readonly string ProxyUrlKey = $ "Configurations:ElectricityMaps:Proxy:Url";
34
+ private readonly string ProxyUsernameKey = $ "Configurations:ElectricityMaps:Proxy:Username";
35
+ private readonly string ProxyPasswordKey = $ "Configurations:ElectricityMaps:Proxy:Password";
36
+
26
37
[ SetUp ]
27
38
public void Setup ( )
28
39
{
@@ -324,4 +335,79 @@ public async Task GetDurationBetweenHistoryDataPoints_WhenMultipleDataPoints_Ret
324
335
Assert . IsNotNull ( second ) ;
325
336
Assert . That ( second . Duration , Is . EqualTo ( expectedDuration ) ) ;
326
337
}
338
+
339
+ [ Test ]
340
+ public void ConfigureDI_ClientProxyTest_With_Missing_ProxyURL_ThrowsException ( )
341
+ {
342
+ // Arrange
343
+ var inMemorySettings = new Dictionary < string , string > {
344
+ { TypeKey , "ElectricityMaps" } ,
345
+ { UsernameKey , "testUsername" } ,
346
+ { PasswordKey , "testPassword123!" } ,
347
+ { UseProxyKey , "true" } ,
348
+ } ;
349
+
350
+ var configuration = new ConfigurationBuilder ( )
351
+ . AddInMemoryCollection ( inMemorySettings )
352
+ . Build ( ) ;
353
+
354
+ var dataSourceConfig = new DataSourcesConfiguration ( )
355
+ {
356
+ EmissionsDataSource = "ElectricityMaps" ,
357
+ ForecastDataSource = "ElectricityMaps" ,
358
+ Section = configuration . GetSection ( "Configurations" )
359
+ } ;
360
+
361
+ var serviceCollection = new ServiceCollection ( ) ;
362
+
363
+ // Act & Assert
364
+ Assert . Throws < ConfigurationException > ( ( ) => ElectricityMapsDataSource . ConfigureDI < IEmissionsDataSource > ( serviceCollection , dataSourceConfig ) ) ;
365
+ Assert . Throws < ConfigurationException > ( ( ) => ElectricityMapsDataSource . ConfigureDI < IForecastDataSource > ( serviceCollection , dataSourceConfig ) ) ;
366
+ }
367
+
368
+ [ TestCase ( true , TestName = "ClientProxyTest, successful: denotes adding ElectricityMaps data sources using proxy." ) ]
369
+ [ TestCase ( false , TestName = "ClientProxyTest, successful: denotes adding ElectricityMaps data sources without using proxy." ) ]
370
+ public void ConfigureDI_ClientProxyTest_AddsDataSource ( bool withProxyUrl )
371
+ {
372
+ // Arrange
373
+ var inMemorySettings = new Dictionary < string , string > {
374
+ { TypeKey , "ElectricityMaps" } ,
375
+ { UsernameKey , "testUsername" } ,
376
+ { PasswordKey , "testPassword123!" } ,
377
+ { UseProxyKey , withProxyUrl . ToString ( ) } ,
378
+ } ;
379
+
380
+ if ( withProxyUrl )
381
+ {
382
+ inMemorySettings . Add ( ProxyUrlKey , "http://10.10.10.1" ) ;
383
+ inMemorySettings . Add ( ProxyUsernameKey , "proxyUsername" ) ;
384
+ inMemorySettings . Add ( ProxyPasswordKey , "proxyPassword" ) ;
385
+ }
386
+
387
+ var configuration = new ConfigurationBuilder ( )
388
+ . AddInMemoryCollection ( inMemorySettings )
389
+ . Build ( ) ;
390
+
391
+ var dataSourceConfig = new DataSourcesConfiguration ( )
392
+ {
393
+ EmissionsDataSource = "ElectricityMaps" ,
394
+ ForecastDataSource = "ElectricityMaps" ,
395
+ Section = configuration . GetSection ( "Configurations" )
396
+ } ;
397
+
398
+ var serviceCollection = new ServiceCollection ( ) ;
399
+ var emissionsDescriptor = new ServiceDescriptor ( typeof ( IEmissionsDataSource ) , typeof ( ElectricityMapsDataSource ) , ServiceLifetime . Singleton ) ;
400
+ var forecastDescriptor = new ServiceDescriptor ( typeof ( IForecastDataSource ) , typeof ( ElectricityMapsDataSource ) , ServiceLifetime . Singleton ) ;
401
+
402
+ Assert . That ( ! serviceCollection . Any ( i => i . ToString ( ) == emissionsDescriptor . ToString ( ) ) ) ;
403
+ Assert . That ( ! serviceCollection . Any ( i => i . ToString ( ) == forecastDescriptor . ToString ( ) ) ) ;
404
+
405
+ // Act
406
+ ElectricityMapsDataSource . ConfigureDI < IEmissionsDataSource > ( serviceCollection , dataSourceConfig ) ;
407
+ ElectricityMapsDataSource . ConfigureDI < IForecastDataSource > ( serviceCollection , dataSourceConfig ) ;
408
+
409
+ // Assert
410
+ Assert . That ( serviceCollection . Any ( i => i . ToString ( ) == emissionsDescriptor . ToString ( ) ) ) ;
411
+ Assert . That ( serviceCollection . Any ( i => i . ToString ( ) == forecastDescriptor . ToString ( ) ) ) ;
412
+ }
327
413
}
0 commit comments