From bdbd4d5302a870625205b3013dbae82354735ace Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 16 Nov 2024 15:08:34 +0800 Subject: [PATCH] add National Bank of Romania exchange rates data source --- conf/ezbookkeeping.ini | 1 + .../exchange_rates_datasource_container.go | 3 + .../national_bank_of_romania_datasource.go | 182 ++++++++++++++ ...ational_bank_of_romania_datasource_test.go | 236 ++++++++++++++++++ pkg/settings/setting.go | 3 + 5 files changed, 425 insertions(+) create mode 100644 pkg/exchangerates/national_bank_of_romania_datasource.go create mode 100644 pkg/exchangerates/national_bank_of_romania_datasource_test.go diff --git a/conf/ezbookkeeping.ini b/conf/ezbookkeeping.ini index b3f639a9..d61a4784 100644 --- a/conf/ezbookkeeping.ini +++ b/conf/ezbookkeeping.ini @@ -348,6 +348,7 @@ custom_map_tile_server_default_zoom_level = 14 # "bank_of_israel": https://www.boi.org.il/en/economic-roles/financial-markets/exchange-rates/ # "swiss_national_bank": https://www.snb.ch/en/the-snb/mandates-goals/statistics/statistics-pub/current_interest_exchange_rates # "danmarks_national_bank": https://www.nationalbanken.dk/en/what-we-do/stable-prices-monetary-policy-and-the-danish-economy/exchange-rates +# "national_bank_of_romania": https://www.bnr.ro/Exchange-rates-1224.aspx # "international_monetary_fund": https://www.imf.org/external/np/fin/data/param_rms_mth.aspx data_source = euro_central_bank diff --git a/pkg/exchangerates/exchange_rates_datasource_container.go b/pkg/exchangerates/exchange_rates_datasource_container.go index c3f3b88c..51b24476 100644 --- a/pkg/exchangerates/exchange_rates_datasource_container.go +++ b/pkg/exchangerates/exchange_rates_datasource_container.go @@ -41,6 +41,9 @@ func InitializeExchangeRatesDataSource(config *settings.Config) error { } else if config.ExchangeRatesDataSource == settings.DanmarksNationalbankDataSource { Container.Current = &DanmarksNationalbankDataSource{} return nil + } else if config.ExchangeRatesDataSource == settings.NationalBankOfRomaniaDataSource { + Container.Current = &NationalBankOfRomaniaDataSource{} + return nil } else if config.ExchangeRatesDataSource == settings.InternationalMonetaryFundDataSource { Container.Current = &InternationalMonetaryFundDataSource{} return nil diff --git a/pkg/exchangerates/national_bank_of_romania_datasource.go b/pkg/exchangerates/national_bank_of_romania_datasource.go new file mode 100644 index 00000000..fee4b62d --- /dev/null +++ b/pkg/exchangerates/national_bank_of_romania_datasource.go @@ -0,0 +1,182 @@ +package exchangerates + +import ( + "encoding/xml" + "math" + "time" + + "github.com/mayswind/ezbookkeeping/pkg/core" + "github.com/mayswind/ezbookkeeping/pkg/errs" + "github.com/mayswind/ezbookkeeping/pkg/log" + "github.com/mayswind/ezbookkeeping/pkg/models" + "github.com/mayswind/ezbookkeeping/pkg/utils" + "github.com/mayswind/ezbookkeeping/pkg/validators" +) + +const nationalBankOfRomaniaExchangeRateUrl = "https://www.bnr.ro/nbrfxrates.xml" +const nationalBankOfRomaniaExchangeRateReferenceUrl = "https://www.bnr.ro/Exchange-rates-1224.aspx" +const nationalBankOfRomaniaDataSource = "National Bank of Romania" + +const nationalBankOfRomaniaUpdateDateFormat = "2006-01-02 15" +const nationalBankOfRomaniaUpdateDateTimezone = "Europe/Bucharest" + +// NationalBankOfRomaniaDataSource defines the structure of exchange rates data source of national bank of Romania +type NationalBankOfRomaniaDataSource struct { + ExchangeRatesDataSource +} + +// NationalBankOfRomaniaExchangeRateData represents the whole data from national bank of Romania +type NationalBankOfRomaniaExchangeRateData struct { + XMLName xml.Name `xml:"DataSet"` + Header *NationalBankOfRomaniaExchangeRateDataHeader `xml:"Header"` + Body *NationalBankOfRomaniaExchangeRateDataBody `xml:"Body"` +} + +// NationalBankOfRomaniaExchangeRateDataHeader represents the header for exchange rates data of national bank of Romania +type NationalBankOfRomaniaExchangeRateDataHeader struct { + PublishingDate string `xml:"PublishingDate"` +} + +// NationalBankOfRomaniaExchangeRateDataBody represents the body for exchange rates data of national bank of Romania +type NationalBankOfRomaniaExchangeRateDataBody struct { + OrigCurrency string `xml:"OrigCurrency"` + AllExchangeRates []*NationalBankOfRomaniaExchangeRates `xml:"Cube"` +} + +// NationalBankOfRomaniaExchangeRates represents the exchange rates data from national bank of Romania +type NationalBankOfRomaniaExchangeRates struct { + Date string `xml:"date,attr"` + ExchangeRates []*NationalBankOfRomaniaExchangeRate `xml:"Rate"` +} + +// NationalBankOfRomaniaExchangeRate represents the exchange rate data from national bank of Romania +type NationalBankOfRomaniaExchangeRate struct { + Currency string `xml:"currency,attr"` + Multiplier string `xml:"multiplier,attr"` + Rate string `xml:",chardata"` +} + +// ToLatestExchangeRateResponse returns a view-object according to original data from national bank of Romania +func (e *NationalBankOfRomaniaExchangeRateData) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse { + if e.Header == nil || e.Body == nil { + log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] header or body is empty") + return nil + } + + if len(e.Body.AllExchangeRates) < 1 { + log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] all exchange rates is empty") + return nil + } + + latestNationalBankOfRomaniaExchangeRate := e.Body.AllExchangeRates[0] + + if len(latestNationalBankOfRomaniaExchangeRate.ExchangeRates) < 1 { + log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] exchange rates is empty") + return nil + } + + exchangeRates := make(models.LatestExchangeRateSlice, 0, len(latestNationalBankOfRomaniaExchangeRate.ExchangeRates)) + + for i := 0; i < len(latestNationalBankOfRomaniaExchangeRate.ExchangeRates); i++ { + exchangeRate := latestNationalBankOfRomaniaExchangeRate.ExchangeRates[i] + + if _, exists := validators.AllCurrencyNames[exchangeRate.Currency]; !exists { + continue + } + + finalExchangeRate := exchangeRate.ToLatestExchangeRate(c) + + if finalExchangeRate == nil { + continue + } + + exchangeRates = append(exchangeRates, finalExchangeRate) + } + + timezone, err := time.LoadLocation(nationalBankOfRomaniaUpdateDateTimezone) + + if err != nil { + log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", nationalBankOfRomaniaUpdateDateTimezone) + return nil + } + + updateDateTime := e.Header.PublishingDate + " 13" // The data are updated in real time, shortly after 13:00, every banking day. + updateTime, err := time.ParseInLocation(nationalBankOfRomaniaUpdateDateFormat, updateDateTime, timezone) + + if err != nil { + log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime) + return nil + } + + latestExchangeRateResp := &models.LatestExchangeRateResponse{ + DataSource: nationalBankOfRomaniaDataSource, + ReferenceUrl: nationalBankOfRomaniaExchangeRateReferenceUrl, + UpdateTime: updateTime.Unix(), + BaseCurrency: e.Body.OrigCurrency, + ExchangeRates: exchangeRates, + } + + return latestExchangeRateResp +} + +// ToLatestExchangeRate returns a data pair according to original data from national bank of Romania +func (e *NationalBankOfRomaniaExchangeRate) ToLatestExchangeRate(c core.Context) *models.LatestExchangeRate { + rate, err := utils.StringToFloat64(e.Rate) + + if err != nil { + log.Warnf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRate] failed to parse rate, currency is %s, rate is %s", e.Currency, e.Rate) + return nil + } + + if rate <= 0 { + log.Warnf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRate] rate is invalid, currency is %s, rate is %s", e.Currency, e.Rate) + return nil + } + + unit := float64(1) + + if e.Multiplier != "" { + unit, err = utils.StringToFloat64(e.Multiplier) + + if err != nil || unit <= 0 { + log.Warnf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRate] failed to parse unit, currency is %s, unit is %s", e.Currency, e.Multiplier) + return nil + } + } + + finalRate := unit / rate + + if math.IsInf(finalRate, 0) { + return nil + } + + return &models.LatestExchangeRate{ + Currency: e.Currency, + Rate: utils.Float64ToString(finalRate), + } +} + +// GetRequestUrls returns the national bank of Romania data source urls +func (e *NationalBankOfRomaniaDataSource) GetRequestUrls() []string { + return []string{nationalBankOfRomaniaExchangeRateUrl} +} + +// Parse returns the common response entity according to the national bank of Romania data source raw response +func (e *NationalBankOfRomaniaDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) { + nationalBankOfRomaniaData := &NationalBankOfRomaniaExchangeRateData{} + err := xml.Unmarshal(content, nationalBankOfRomaniaData) + + if err != nil { + log.Errorf(c, "[national_bank_of_romania_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error()) + return nil, errs.ErrFailedToRequestRemoteApi + } + + latestExchangeRateResponse := nationalBankOfRomaniaData.ToLatestExchangeRateResponse(c) + + if latestExchangeRateResponse == nil { + log.Errorf(c, "[national_bank_of_romania_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content)) + return nil, errs.ErrFailedToRequestRemoteApi + } + + return latestExchangeRateResponse, nil +} diff --git a/pkg/exchangerates/national_bank_of_romania_datasource_test.go b/pkg/exchangerates/national_bank_of_romania_datasource_test.go new file mode 100644 index 00000000..3f8a143d --- /dev/null +++ b/pkg/exchangerates/national_bank_of_romania_datasource_test.go @@ -0,0 +1,236 @@ +package exchangerates + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/mayswind/ezbookkeeping/pkg/core" + "github.com/mayswind/ezbookkeeping/pkg/models" +) + +const nationalBankOfRomaniaMinimumRequiredContent = "\n" + + "\n" + + "
\n" + + " 2024-11-15\n" + + "
\n" + + " \n" + + " RON\n" + + " \n" + + " 3.0303\n" + + " 4.7057\n" + + " \n" + + " \n" + + "
" + +func TestNationalBankOfRomaniaDataSource_StandardDataExtractBaseCurrency(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfRomaniaMinimumRequiredContent)) + assert.Equal(t, nil, err) + assert.Equal(t, "RON", actualLatestExchangeRateResponse.BaseCurrency) +} + +func TestNationalBankOfRomaniaDataSource_StandardDataExtractUpdateTime(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfRomaniaMinimumRequiredContent)) + assert.Equal(t, nil, err) + assert.Equal(t, int64(1731668400), actualLatestExchangeRateResponse.UpdateTime) +} + +func TestNationalBankOfRomaniaDataSource_StandardDataExtractExchangeRates(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfRomaniaMinimumRequiredContent)) + assert.Equal(t, nil, err) + assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{ + Currency: "JPY", + Rate: "33.000033000033", + }) + assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{ + Currency: "USD", + Rate: "0.21250823469409438", + }) +} + +func TestNationalBankOfRomaniaDataSource_BlankContent(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte("")) + assert.NotEqual(t, nil, err) +} + +func TestNationalBankOfRomaniaDataSource_OnlyXMLHeader(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte("")) + assert.NotEqual(t, nil, err) +} + +func TestNationalBankOfRomaniaDataSource_EmptyExchangeRatesDataset(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte(""+ + ""+ + "")) + assert.NotEqual(t, nil, err) +} + +func TestNationalBankOfRomaniaDataSource_NoDailyRatesHeader(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + "
")) + assert.NotEqual(t, nil, err) +} + +func TestNationalBankOfRomaniaDataSource_NoDailyRatesBody(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + "
")) + assert.NotEqual(t, nil, err) +} + +func TestNationalBankOfRomaniaDataSource_NoDailyRatesCube(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + _, err := dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + " \n"+ + " RON\n"+ + " \n"+ + "
")) + assert.NotEqual(t, nil, err) +} + +func TestNationalBankOfRomaniaDataSource_InvalidCurrency(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + " \n"+ + " RON\n"+ + " \n"+ + " 1\n"+ + " \n"+ + " \n"+ + "
")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) +} + +func TestNationalBankOfRomaniaDataSource_EmptyRate(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + " \n"+ + " RON\n"+ + " \n"+ + " \n"+ + " \n"+ + " \n"+ + "
")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) +} + +func TestNationalBankOfRomaniaDataSource_InvalidRate(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + " \n"+ + " RON\n"+ + " \n"+ + " null\n"+ + " \n"+ + " \n"+ + "
")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) + + actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + " \n"+ + " RON\n"+ + " \n"+ + " 0\n"+ + " \n"+ + " \n"+ + "
")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) +} + +func TestNationalBankOfRomaniaDataSource_InvalidMultiplier(t *testing.T) { + dataSource := &NationalBankOfRomaniaDataSource{} + context := core.NewNullContext() + + actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + " \n"+ + " RON\n"+ + " \n"+ + " 3.0303\n"+ + " \n"+ + " \n"+ + "
")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) + + actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte(""+ + ""+ + "
\n"+ + " 2024-11-15\n"+ + "
\n"+ + " \n"+ + " RON\n"+ + " \n"+ + " 3.0303\n"+ + " \n"+ + " \n"+ + "
")) + assert.Equal(t, nil, err) + assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0) +} diff --git a/pkg/settings/setting.go b/pkg/settings/setting.go index 863ebd66..8f743fd6 100644 --- a/pkg/settings/setting.go +++ b/pkg/settings/setting.go @@ -108,6 +108,7 @@ const ( BankOfIsraelDataSource string = "bank_of_israel" SwissNationalBankDataSource string = "swiss_national_bank" DanmarksNationalbankDataSource string = "danmarks_national_bank" + NationalBankOfRomaniaDataSource string = "national_bank_of_romania" InternationalMonetaryFundDataSource string = "international_monetary_fund" ) @@ -896,6 +897,8 @@ func loadExchangeRatesConfiguration(config *Config, configFile *ini.File, sectio config.ExchangeRatesDataSource = SwissNationalBankDataSource } else if dataSource == DanmarksNationalbankDataSource { config.ExchangeRatesDataSource = DanmarksNationalbankDataSource + } else if dataSource == NationalBankOfRomaniaDataSource { + config.ExchangeRatesDataSource = NationalBankOfRomaniaDataSource } else if dataSource == InternationalMonetaryFundDataSource { config.ExchangeRatesDataSource = InternationalMonetaryFundDataSource } else {