diff --git a/config/derivedchargers.go b/config/derivedchargers.go new file mode 100644 index 000000000..5e624e1f4 --- /dev/null +++ b/config/derivedchargers.go @@ -0,0 +1,53 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2014 ITsysCOM GmbH + +This program is free software: you can Storagetribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITH*out ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package config + +import ( + "errors" + "github.com/cgrates/cgrates/utils" +) + +type DerivedCharger struct { + RunId string // Unique runId in the chain + ReqTypeField string // Field containing request type info, number in case of csv source, '^' as prefix in case of static values + DirectionField string // Field containing direction info + TenantField string // Field containing tenant info + TorField string // Field containing tor info + AccountField string // Field containing account information + SubjectField string // Field containing subject information + DestinationField string // Field containing destination information + SetupTimeField string // Field containing setup time information + AnswerTimeField string // Field containing answer time information + DurationField string // Field containing duration information +} + +type DerivedChargers []*DerivedCharger + +// Precheck that RunId is unique +func (dcs DerivedChargers) Append(dc *DerivedCharger) (DerivedChargers, error) { + if dc.RunId == utils.DEFAULT_RUNID { + return nil, errors.New("Reserved RunId") + } + for _, dcLocal := range dcs { + if dcLocal.RunId == dc.RunId { + return nil, errors.New("Duplicated RunId") + } + } + return append(dcs, dc), nil +} diff --git a/config/derivedchargers_test.go b/config/derivedchargers_test.go new file mode 100644 index 000000000..a23a5b9b7 --- /dev/null +++ b/config/derivedchargers_test.go @@ -0,0 +1,45 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2014 ITsysCOM GmbH + +This program is free software: you can Storagetribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITH*out ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package config + +import ( + "github.com/cgrates/cgrates/utils" + "testing" +) + +func TestAppendDerivedChargers(t *testing.T) { + var err error + dcs := make(DerivedChargers, 0) + if _, err := dcs.Append(&DerivedCharger{RunId: utils.DEFAULT_RUNID}); err == nil { + t.Error("Failed to detect using of the default runid") + } + if dcs, err = dcs.Append(&DerivedCharger{RunId: "FIRST_RUNID"}); err != nil { + t.Error("Failed to add runid") + } else if len(dcs) != 1 { + t.Error("Unexpected number of items inside DerivedChargers configuration", len(dcs)) + } + if dcs, err = dcs.Append(&DerivedCharger{RunId: "SECOND_RUNID"}); err != nil { + t.Error("Failed to add runid") + } else if len(dcs) != 2 { + t.Error("Unexpected number of items inside DerivedChargers configuration", len(dcs)) + } + if _, err := dcs.Append(&DerivedCharger{RunId: "SECOND_RUNID"}); err == nil { + t.Error("Failed to detect duplicate runid") + } +}