diff --git a/cdrexporter/fixedwidth.go b/cdrexporter/fixedwidth.go new file mode 100644 index 000000000..78316f3a0 --- /dev/null +++ b/cdrexporter/fixedwidth.go @@ -0,0 +1,66 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 ITsysCOM + +This program is free software: you can redistribute 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 WITHOUT 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 cdrexporter + +import ( + "fmt" + "strconv" +) + +type DeanCdrWriter struct{} + +func (dcw *DeanCdrWriter) Write(cdr []string) error { + return nil +} + +// Used as generic function logic for various fields + +// Attributes +// source - the base source +// maxLen - the maximum field lenght +// stripAllowed - whether we allow stripping of chars in case of source bigger than the maximum allowed +// lStrip - if true, strip from beginning of the string +// lPadding - if true, add chars at the beginning of the string +// paddingChar - the character wich will be used to fill the existing +func filterField(source string, maxLen int, stripAllowed, lStrip, lPadding, padWithZero bool) (string, error) { + if len(source) == maxLen { // the source is exactly the maximum length + return source, nil + } + if len(source) > maxLen { //the source is bigger than allowed + if !stripAllowed { + return "", fmt.Errorf("source %s is bigger than the maximum allowed length %s", source, maxLen) + } + if !lStrip { + return source[:maxLen], nil + } else { + diffIndx := len(source) - maxLen + return source[diffIndx:], nil + } + } else { //the source is smaller as the maximum allowed + paddingString := "%" + if padWithZero { + paddingString += "0" // it will not work for rPadding but this is not needed + } + if !lPadding { + paddingString += "-" + } + paddingString += strconv.Itoa(maxLen) + "s" + return fmt.Sprintf(paddingString, source), nil + } +} diff --git a/cdrexporter/fixedwidth_test.go b/cdrexporter/fixedwidth_test.go new file mode 100644 index 000000000..9ed650ada --- /dev/null +++ b/cdrexporter/fixedwidth_test.go @@ -0,0 +1,86 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 ITsysCOM + +This program is free software: you can redistribute 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 WITHOUT 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 cdrexporter + +import ( + "testing" +) + +func TestMaxLen(t *testing.T) { + result, err := filterField("test", 4, false, false, false, false) + expected := "test" + if err != nil || result != expected { + t.Errorf("Expected \"test\" was \"%s\"", result) + } +} + +func TestRPadding(t *testing.T) { + result, err := filterField("test", 8, false, false, false, false) + expected := "test " + if err != nil || result != expected { + t.Errorf("Expected \"%s \" was \"%s\"", expected, result) + } +} + +func TestLPadding(t *testing.T) { + result, err := filterField("test", 8, false, false, true, false) + expected := " test" + if err != nil || result != expected { + t.Errorf("Expected \"%s \" was \"%s\"", expected, result) + } +} + +func TestRStrip(t *testing.T) { + result, err := filterField("test", 2, true, false, false, false) + expected := "te" + if err != nil || result != expected { + t.Errorf("Expected \"%s \" was \"%s\"", expected, result) + } +} + +func TestLStrip(t *testing.T) { + result, err := filterField("test", 2, true, true, false, false) + expected := "st" + if err != nil || result != expected { + t.Errorf("Expected \"%s \" was \"%s\"", expected, result) + } +} + +func TestStripNotAllowed(t *testing.T) { + _, err := filterField("test", 2, false, false, false, false) + if err == nil { + t.Error("Expected error") + } +} + +func TestLZeroPadding(t *testing.T) { + result, err := filterField("12", 8, false, false, true, true) + expected := "00000012" + if err != nil || result != expected { + t.Errorf("Expected \"%s \" was \"%s\"", expected, result) + } +} + +func TestRZeroPadding(t *testing.T) { + result, err := filterField("12", 8, false, false, false, true) + expected := "12 " + if err != nil || result != expected { + t.Errorf("Expected \"%s \" was \"%s\"", expected, result) + } +} diff --git a/test_local.sh b/test_local.sh index cf4704926..a39b05e99 100755 --- a/test_local.sh +++ b/test_local.sh @@ -1,42 +1,13 @@ #! /usr/bin/env sh -go test -i github.com/cgrates/cgrates/apier/v1 -go test -i github.com/cgrates/cgrates/engine -go test -i github.com/cgrates/cgrates/sessionmanager -go test -i github.com/cgrates/cgrates/config -go test -i github.com/cgrates/cgrates/cmd/cgr-engine -go test -i github.com/cgrates/cgrates/mediator -go test -i github.com/cgrates/fsock -go test -i github.com/cgrates/cgrates/cache2go -go test -i github.com/cgrates/cgrates/cdrs -go test -i github.com/cgrates/cgrates/utils -go test -i github.com/cgrates/cgrates/history -go test -i github.com/cgrates/cgrates/cdrexporter - +. ./test.sh +gen=$? go test github.com/cgrates/cgrates/apier/v1 -local ap=$? go test github.com/cgrates/cgrates/engine -local en=$? -go test github.com/cgrates/cgrates/sessionmanager -sm=$? -go test github.com/cgrates/cgrates/config -cfg=$? -go test github.com/cgrates/cgrates/cmd/cgr-engine -cr=$? -go test github.com/cgrates/cgrates/mediator -md=$? -go test github.com/cgrates/cgrates/cdrs -cdr=$? -go test github.com/cgrates/cgrates/utils -ut=$? -go test github.com/cgrates/fsock -fs=$? -go test github.com/cgrates/cgrates/history -hs=$? -go test github.com/cgrates/cgrates/cache2go -c2g=$? -go test github.com/cgrates/cgrates/cdrexporter -cdre=$? -exit $ap && $en && $sm && $cfg && $bl && $cr && $md && $cdr && $fs && $ut && $hs && $c2g && $cdre + +exit $gen && $ap && $en +