Derived charging - fix nil pointer dereference in case of empty fields on fork cdr, cdrexporter correctly returning now empty file path in case of no cdrs to be exported

This commit is contained in:
DanB
2014-07-02 19:39:19 +02:00
committed by Radu Ioan Fericean
parent e9da9fd565
commit bb102e65c2
4 changed files with 53 additions and 5 deletions

View File

@@ -129,6 +129,10 @@ func (self *ApierV1) ExportCdrsToFile(attr utils.AttrExpFileCdrs, reply *utils.E
if err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
}
if cdrexp.TotalExportedCdrs() == 0 {
*reply = utils.ExportedFileCdrs{ExportedFilePath: ""}
return nil
}
if err := cdrexp.WriteToFile(filePath); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
}

View File

@@ -442,10 +442,6 @@ func (cdre *CdrExporter) writeCsv(csvWriter *csv.Writer) error {
// General method to write the content out to a file
func (cdre *CdrExporter) WriteToFile(filePath string) error {
if cdre.numberOfRecords == 0 { // Not writing the file in case of no CDRs to be exported
engine.Logger.Err("<Cdre> Not writing file out since there are no records to export")
return nil
}
fileOut, err := os.Create(filePath)
if err != nil {
return err
@@ -482,6 +478,10 @@ func (cdre *CdrExporter) TotalCost() float64 {
return cdre.totalCost
}
func (cdre *CdrExporter) TotalExportedCdrs() int {
return cdre.numberOfRecords
}
// Return successfully exported CgrIds
func (cdre *CdrExporter) PositiveExports() []string {
return cdre.positiveExports

View File

@@ -163,34 +163,71 @@ func (storedCdr *StoredCdr) AsHttpForm() url.Values {
// Used in mediation, primaryMandatory marks whether missing field out of request represents error or can be ignored
func (storedCdr *StoredCdr) ForkCdr(runId string, reqTypeFld, directionFld, tenantFld, categFld, accountFld, subjectFld, destFld, setupTimeFld, answerTimeFld, durationFld *RSRField,
extraFlds []*RSRField, primaryMandatory bool) (*StoredCdr, error) {
// MetaDefault will automatically be converted to their standard values
// A more elegant solution for the future to fix
/*for _, fld := range []*RSRField{reqTypeFld, directionFld, tenantFld, categFld, accountFld, subjectFld, destFld, setupTimeFld, answerTimeFld, durationFld} {
if fld == nil {
tmp, _ := NewRSRField(META_DEFAULT)
*fld = *tmp
}
}
*/
if reqTypeFld == nil {
reqTypeFld, _ = NewRSRField(META_DEFAULT)
}
if reqTypeFld.Id == META_DEFAULT {
reqTypeFld.Id = REQTYPE
}
if directionFld == nil {
directionFld, _ = NewRSRField(META_DEFAULT)
}
if directionFld.Id == META_DEFAULT {
directionFld.Id = DIRECTION
}
if tenantFld == nil {
tenantFld, _ = NewRSRField(META_DEFAULT)
}
if tenantFld.Id == META_DEFAULT {
tenantFld.Id = TENANT
}
if categFld == nil {
categFld, _ = NewRSRField(META_DEFAULT)
}
if categFld.Id == META_DEFAULT {
categFld.Id = CATEGORY
}
if accountFld == nil {
accountFld, _ = NewRSRField(META_DEFAULT)
}
if accountFld.Id == META_DEFAULT {
accountFld.Id = ACCOUNT
}
if subjectFld == nil {
subjectFld, _ = NewRSRField(META_DEFAULT)
}
if subjectFld.Id == META_DEFAULT {
subjectFld.Id = SUBJECT
}
if destFld == nil {
destFld, _ = NewRSRField(META_DEFAULT)
}
if destFld.Id == META_DEFAULT {
destFld.Id = DESTINATION
}
if setupTimeFld == nil {
setupTimeFld, _ = NewRSRField(META_DEFAULT)
}
if setupTimeFld.Id == META_DEFAULT {
setupTimeFld.Id = SETUP_TIME
}
if answerTimeFld == nil {
answerTimeFld, _ = NewRSRField(META_DEFAULT)
}
if answerTimeFld.Id == META_DEFAULT {
answerTimeFld.Id = ANSWER_TIME
}
if durationFld == nil {
durationFld, _ = NewRSRField(META_DEFAULT)
}
if durationFld.Id == META_DEFAULT {
durationFld.Id = USAGE
}

View File

@@ -271,6 +271,13 @@ func TestStoredCdrForkCdrFromMetaDefaults(t *testing.T) {
if !reflect.DeepEqual(expctCdr, cdrOut) {
t.Errorf("Expected: %v, received: %v", expctCdr, cdrOut)
}
// Should also accept nil as defaults
if cdrOut, err := storCdr.ForkCdr("wholesale_run", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
[]*RSRField{&RSRField{Id: "field_extr1"}, &RSRField{Id: "fieldextr2"}}, true); err != nil {
t.Fatal("Unexpected error received", err)
} else if !reflect.DeepEqual(expctCdr, cdrOut) {
t.Errorf("Expected: %v, received: %v", expctCdr, cdrOut)
}
}
func TestStoredCdrAsCgrCdrOut(t *testing.T) {