From 9c440d7aa4a776be9051489a40944d779d032d96 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Thu, 1 Apr 2021 19:15:10 +0200 Subject: add tests project, move configuration verification to VippsConfiguration.cs --- .../Models/VippsConfiguration.cs | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/IOL.VippsEcommerce/Models') diff --git a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs index 4a78166..19ca140 100644 --- a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs +++ b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs @@ -122,6 +122,34 @@ namespace IOL.VippsEcommerce.Models } } + /// + /// Ensure that the configuration can be used to issue requests to the vipps api. + /// Throws if a required value is null or whitespace. + /// + public void Verify() { + if (GetValue(VippsConfigurationKeyNames.VIPPS_API_URL).IsNullOrWhiteSpace()) { + throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_API_URL, + "VippsEcommerceService: VIPPS_API_URL is not provided in configuration."); + } + + if (GetValue(VippsConfigurationKeyNames.VIPPS_CLIENT_ID).IsNullOrWhiteSpace()) { + throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_CLIENT_ID, + "VippsEcommerceService: VIPPS_CLIENT_ID is not provided in configuration."); + } + + if (GetValue(VippsConfigurationKeyNames.VIPPS_CLIENT_SECRET).IsNullOrWhiteSpace()) { + throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_CLIENT_SECRET, + "VippsEcommerceService: VIPPS_CLIENT_SECRET is not provided in configuration."); + } + + if (GetValue(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_PRIMARY).IsNullOrWhiteSpace() + && GetValue(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_SECONDARY).IsNullOrWhiteSpace()) { + throw new ArgumentNullException(VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_PRIMARY + + VippsConfigurationKeyNames.VIPPS_SUBSCRIPTION_KEY_SECONDARY, + "VippsEcommerceService: Neither VIPPS_SUBSCRIPTION_KEY_PRIMARY nor VIPPS_SUBSCRIPTION_KEY_SECONDARY was provided in configuration."); + } + } + private string GetValueFromObject(string key) { foreach (var prop in typeof(VippsConfiguration).GetProperties()) { foreach (var attribute in prop.CustomAttributes) { -- cgit v1.3