mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-17 14:19:54 +05:00
@@ -21,7 +21,6 @@ package utils
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"encoding/gob"
|
||||
@@ -29,7 +28,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"net/url"
|
||||
@@ -37,7 +35,6 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -402,31 +399,6 @@ func Unzip(src, dest string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ZIPContent compresses src into zipped slice of bytes
|
||||
func GZIPContent(src []byte) (dst []byte, err error) {
|
||||
var b bytes.Buffer
|
||||
gz := gzip.NewWriter(&b)
|
||||
if _, err = gz.Write(src); err != nil {
|
||||
return
|
||||
}
|
||||
if err = gz.Flush(); err != nil {
|
||||
return
|
||||
}
|
||||
if err = gz.Close(); err != nil {
|
||||
return
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func GUnZIPContent(src []byte) (dst []byte, err error) {
|
||||
rdata := bytes.NewReader(src)
|
||||
var r *gzip.Reader
|
||||
if r, err = gzip.NewReader(rdata); err != nil {
|
||||
return
|
||||
}
|
||||
return ioutil.ReadAll(r)
|
||||
}
|
||||
|
||||
// successive Fibonacci numbers.
|
||||
func Fib() func() int {
|
||||
a, b := 0, 1
|
||||
@@ -439,7 +411,7 @@ func Fib() func() int {
|
||||
// Utilities to provide pointers where we need to define ad-hoc
|
||||
func StringPointer(str string) *string {
|
||||
if str == ZERO {
|
||||
str = ""
|
||||
str = EmptyString
|
||||
return &str
|
||||
}
|
||||
return &str
|
||||
@@ -477,12 +449,6 @@ func DurationPointer(d time.Duration) *time.Duration {
|
||||
return &d
|
||||
}
|
||||
|
||||
func ReflectFuncLocation(handler interface{}) (file string, line int) {
|
||||
f := runtime.FuncForPC(reflect.ValueOf(handler).Pointer())
|
||||
entry := f.Entry()
|
||||
return f.FileLine(entry)
|
||||
}
|
||||
|
||||
func ToIJSON(v interface{}) string {
|
||||
b, _ := json.MarshalIndent(v, "", " ")
|
||||
return string(b)
|
||||
@@ -511,11 +477,6 @@ func Clone(a, b interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cloner is an interface for objects to clone themselves into interface
|
||||
type Cloner interface {
|
||||
Clone() (interface{}, error)
|
||||
}
|
||||
|
||||
// Used as generic function logic for various fields
|
||||
|
||||
// Attributes
|
||||
|
||||
@@ -593,6 +593,10 @@ func TestParseZeroRatingSubject(t *testing.T) {
|
||||
if d, err := ParseZeroRatingSubject(MONETARY, EmptyString, dfltRatingSubject); err != nil || d != time.Nanosecond {
|
||||
t.Error("Error parsing rating subject: ", EmptyString, d, err)
|
||||
}
|
||||
expecting := "malformed rating subject: test"
|
||||
if _, err := ParseZeroRatingSubject(MONETARY, "test", dfltRatingSubject); err == nil || err.Error() != expecting {
|
||||
t.Errorf("Expecting: %+v, received: %+v ", expecting, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcatenatedKey(t *testing.T) {
|
||||
@@ -641,97 +645,93 @@ func TestInfieldSplit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMandatory(t *testing.T) {
|
||||
_, err := FmtFieldWidth("", "", 0, "", "", true)
|
||||
if err == nil {
|
||||
func TestFmtFieldWidth(t *testing.T) {
|
||||
//Mandatory0
|
||||
if _, err := FmtFieldWidth("", "", 0, "", "", true); err == nil {
|
||||
t.Errorf("Failed to detect mandatory value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxLen(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 4, "", "", false)
|
||||
expected := "test"
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"test\" was \"%s\"", result)
|
||||
//width = 0
|
||||
if result, err := FmtFieldWidth("", "test", 0, "", "", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != "test" {
|
||||
t.Errorf("Expecting 'test', received %+q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRPadding(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 8, "", "right", false)
|
||||
expected := "test "
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
//MaxLen
|
||||
if result, err := FmtFieldWidth("", "test", 4, "", "", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != "test" {
|
||||
t.Errorf("Expected \"test\" received: \"%s\"", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaddingFiller(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "", 8, "", "right", false)
|
||||
//RPadding
|
||||
if result, err := FmtFieldWidth("", "test", 8, "", "right", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != "test " {
|
||||
t.Errorf("Expected <\"test \"> \" received: \"%s\"", result)
|
||||
}
|
||||
//PaddingFiller
|
||||
expected := " "
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
if result, err := FmtFieldWidth("", "", 8, "", "right", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLPadding(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 8, "", "left", false)
|
||||
expected := " test"
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
//LPadding
|
||||
expected = " test"
|
||||
if result, err := FmtFieldWidth("", "test", 8, "", "left", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestZeroLPadding(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 8, "", "zeroleft", false)
|
||||
expected := "0000test"
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
//ZeroLPadding
|
||||
expected = "0000test"
|
||||
if result, err := FmtFieldWidth("", "test", 8, "", "zeroleft", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRStrip(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 2, "right", "", false)
|
||||
expected := "te"
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
//RStrip
|
||||
expected = "te"
|
||||
if result, err := FmtFieldWidth("", "test", 2, "right", "", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestXRStrip(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 3, "xright", "", false)
|
||||
expected := "tex"
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
//XRStrip
|
||||
expected = "tex"
|
||||
if result, err := FmtFieldWidth("", "test", 3, "xright", "", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLStrip(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 2, "left", "", false)
|
||||
expected := "st"
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
//LStrip
|
||||
expected = "st"
|
||||
if result, err := FmtFieldWidth("", "test", 2, "left", "", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestXLStrip(t *testing.T) {
|
||||
result, err := FmtFieldWidth("", "test", 3, "xleft", "", false)
|
||||
expected := "xst"
|
||||
if err != nil || result != expected {
|
||||
t.Errorf("Expected \"%s \" was \"%s\"", expected, result)
|
||||
//XLStrip
|
||||
expected = "xst"
|
||||
if result, err := FmtFieldWidth("", "test", 3, "xleft", "", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripNotAllowed(t *testing.T) {
|
||||
_, err := FmtFieldWidth("", "test", 3, "", "", false)
|
||||
if err == nil {
|
||||
//StripNotAllowed
|
||||
if _, err := FmtFieldWidth("", "test", 3, "", "", false); err == nil {
|
||||
t.Error("Expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaddingNotAllowed(t *testing.T) {
|
||||
_, err := FmtFieldWidth("", "test", 5, "", "", false)
|
||||
if err == nil {
|
||||
//PaddingNotAllowed
|
||||
if _, err := FmtFieldWidth("", "test", 5, "", "", false); err == nil {
|
||||
t.Error("Expected error")
|
||||
}
|
||||
//
|
||||
expected = "test"
|
||||
if result, err := FmtFieldWidth("", "test", 3, "wrong", "", false); err != nil {
|
||||
t.Error(err)
|
||||
} else if result != expected {
|
||||
t.Errorf("Expected \"%s \" received: \"%s\"", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCastIfToString(t *testing.T) {
|
||||
@@ -747,12 +747,36 @@ func TestCastIfToString(t *testing.T) {
|
||||
} else if sOut != "1" {
|
||||
t.Errorf("Received: %+v", sOut)
|
||||
}
|
||||
v = interface{}((int64)(1))
|
||||
if sOut, casts := CastIfToString(v); !casts {
|
||||
t.Error("Does not cast")
|
||||
} else if sOut != "1" {
|
||||
t.Errorf("Received: %+v", sOut)
|
||||
}
|
||||
v = interface{}(true)
|
||||
if sOut, casts := CastIfToString(v); !casts {
|
||||
t.Error("Does not cast")
|
||||
} else if sOut != "true" {
|
||||
t.Errorf("Received: %+v", sOut)
|
||||
}
|
||||
v = interface{}([]byte("test"))
|
||||
if sOut, casts := CastIfToString(v); !casts {
|
||||
t.Error("Does not cast")
|
||||
} else if sOut != "test" {
|
||||
t.Errorf("Received: %+v", sOut)
|
||||
}
|
||||
v = interface{}(1.2)
|
||||
if sOut, casts := CastIfToString(v); !casts {
|
||||
t.Error("Does not cast")
|
||||
} else if sOut != "1.2" {
|
||||
t.Errorf("Received: %+v", sOut)
|
||||
}
|
||||
//default
|
||||
v = interface{}([]string{"test"})
|
||||
if _, casts := CastIfToString(v); casts {
|
||||
t.Error("Does cast")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestEndOfMonth(t *testing.T) {
|
||||
@@ -781,6 +805,10 @@ func TestEndOfMonth(t *testing.T) {
|
||||
if !eom.Equal(expected) {
|
||||
t.Errorf("Expected %v was %v", expected, eom)
|
||||
}
|
||||
eom = GetEndOfMonth(time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC))
|
||||
if time.Now().Add(-1).Before(eom) {
|
||||
t.Errorf("Expected %v was %v", expected, eom)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseHierarchyPath(t *testing.T) {
|
||||
@@ -855,12 +883,43 @@ func TestClone(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFib(t *testing.T) {
|
||||
fib := Fib()
|
||||
if tmp := fib(); tmp != 1 {
|
||||
t.Error("Expecting: 1, received ", tmp)
|
||||
}
|
||||
if tmp := fib(); tmp != 1 {
|
||||
t.Error("Expecting: 1, received ", tmp)
|
||||
}
|
||||
if tmp := fib(); tmp != 2 {
|
||||
t.Error("Expecting: 2, received ", tmp)
|
||||
}
|
||||
if tmp := fib(); tmp != 3 {
|
||||
t.Error("Expecting: 3, received ", tmp)
|
||||
}
|
||||
if tmp := fib(); tmp != 5 {
|
||||
t.Error("Expecting: 5, received ", tmp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringPointer(t *testing.T) {
|
||||
result := StringPointer("*zero")
|
||||
if *result != EmptyString {
|
||||
t.Errorf("Expecting: `%+q`, received `%+q`", EmptyString, *result)
|
||||
}
|
||||
str := "test_string"
|
||||
result = StringPointer(str)
|
||||
expected := &str
|
||||
if *result != *expected {
|
||||
t.Errorf("Expecting: %+v, received: %+v", &str, result)
|
||||
}
|
||||
}
|
||||
func TestIntPointer(t *testing.T) {
|
||||
t1 := 14
|
||||
result := IntPointer(t1)
|
||||
expected := &t1
|
||||
if *expected != *result {
|
||||
t.Error("Expected:", expected, ", received:", result)
|
||||
t.Errorf("Expecting: %+v, received: %+v", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -900,6 +959,15 @@ func TestStringMapPointer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapStringStringPointer(t *testing.T) {
|
||||
mp := map[string]string{"string1": "string2"}
|
||||
result := MapStringStringPointer(mp)
|
||||
expected := &mp
|
||||
if *result == nil {
|
||||
t.Errorf("Expected: %+q, received: nil", expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimePointer(t *testing.T) {
|
||||
t1, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
|
||||
if err != nil {
|
||||
@@ -912,6 +980,24 @@ func TestTimePointer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDurationPointer(t *testing.T) {
|
||||
duration := time.Duration(10)
|
||||
result := DurationPointer(duration)
|
||||
expected := &duration
|
||||
if *expected != *result {
|
||||
t.Errorf("Expected: %+q, received: %+q", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToIJSON(t *testing.T) {
|
||||
str := "string"
|
||||
received := ToIJSON(str)
|
||||
expected := "\"string\""
|
||||
if !reflect.DeepEqual(received, expected) {
|
||||
t.Errorf("Expected: %+q, received: %+q", expected, received)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLen(t *testing.T) {
|
||||
t1 := Int64Slice{2112443, 414241412, 41231241}
|
||||
result := t1.Len()
|
||||
@@ -998,19 +1084,6 @@ func TestCounterConcurrent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGZIPGUnZIP(t *testing.T) {
|
||||
src := []byte("CGRateS.org")
|
||||
gzipped, err := GZIPContent(src)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dst, err := GUnZIPContent(gzipped); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(src, dst) {
|
||||
t.Error("not matching initial source")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFFNNewFallbackFileNameFronString(t *testing.T) {
|
||||
fileName := "cdr|*http_json_cdr|http%3A%2F%2F127.0.0.1%3A12080%2Finvalid_json|1acce2c9-3f2d-4774-8662-c28872dad515.json"
|
||||
eFFN := &FallbackFileName{Module: "cdr",
|
||||
|
||||
Reference in New Issue
Block a user