github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/test/test.go (about) 1 package test 2 3 import ( 4 "bytes" 5 "encoding/xml" 6 "fmt" 7 "net/http" 8 "testing" 9 10 "github.com/gin-gonic/gin" 11 "github.com/mundipagg/boleto-api/models" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 //Parameter Parâmetro de teste com input generico 16 type Parameter struct { 17 Line int 18 Input interface{} 19 Expected interface{} 20 Length int 21 } 22 23 type XmlNode struct { 24 XMLName xml.Name 25 Content []byte `xml:",innerxml"` 26 Nodes []XmlNode `xml:",any"` 27 } 28 29 //UInt64TestParameter Parâmetro de teste com input do tipo uint64 30 type UInt64TestParameter struct { 31 Input uint64 32 Expected string 33 } 34 35 // ExpectNoError falha o teste se e != nil 36 func ExpectNoError(e error, t *testing.T) { 37 if e != nil { 38 t.Fail() 39 } 40 } 41 42 // ExpectError falha o teste se e == nil 43 func ExpectError(e error, t *testing.T) { 44 if e == nil { 45 t.Fail() 46 } 47 } 48 49 // ExpectTrue falha o teste caso a condição não seja verdadeira 50 func ExpectTrue(condition bool, t *testing.T) { 51 if !condition { 52 t.Fail() 53 } 54 } 55 56 // ExpectFalse falha o teste caso a condição não seja falsa 57 func ExpectFalse(condition bool, t *testing.T) { 58 if condition { 59 t.Fail() 60 } 61 } 62 63 // ExpectNil falha o teste caso obj seja diferente de nil 64 func ExpectNil(obj interface{}, t *testing.T) { 65 if obj != nil { 66 t.Fail() 67 } 68 } 69 70 //AssertProcessBoletoWithSuccess Valida se o boleto foi gerado com sucesso 71 func AssertProcessBoletoWithSuccess(t *testing.T, response models.BoletoResponse) { 72 assert.Empty(t, response.Errors, "Não deve ocorrer erros") 73 assert.NotEmpty(t, response.BarCodeNumber, "Deve haver um Barcode") 74 assert.NotEmpty(t, response.DigitableLine, "Deve haver uma linha digitável") 75 } 76 77 //AssertProcessBoletoFailed Valida se o houve um erro no processamento do boleto 78 func AssertProcessBoletoFailed(t *testing.T, response models.BoletoResponse) { 79 assert.Greater(t, len(response.Errors), 0, "Devem ocorrer erros ") 80 assert.Empty(t, response.BarCodeNumber, "Não deve haver um Barcode") 81 assert.Empty(t, response.DigitableLine, "Não deve haver uma linha digitável") 82 } 83 84 //AssertError Valida a existência de erros internos 85 func AssertError(t *testing.T, err error, errType interface{}) { 86 assert.NotNil(t, err, "Deve haver um erro") 87 assert.NotEmpty(t, err.Error(), "Deve haver uma mensagem de erro") 88 assert.IsType(t, errType, err, "Deve ser um erro do tipo "+fmt.Sprintf("%T", err)) 89 } 90 91 //CreateClientIP cria IP no contexto 92 func CreateClientIP(c *gin.Context) { 93 c.Request = new(http.Request) 94 c.Request.Header = make(map[string][]string) 95 c.Request.Header.Add("X-Forwarded-For", "0.0.0.0") 96 } 97 98 func WalkThroughXml(nodes []XmlNode, f func(XmlNode) bool) { 99 for _, n := range nodes { 100 if f(n) { 101 WalkThroughXml(n.Nodes, f) 102 } 103 } 104 } 105 106 func GetNode(bodyXml string, tagName string) string { 107 nodeContent := "" 108 109 buf := bytes.NewBuffer([]byte(bodyXml)) 110 dec := xml.NewDecoder(buf) 111 112 var n XmlNode 113 err := dec.Decode(&n) 114 if err != nil { 115 panic(err) 116 } 117 118 WalkThroughXml([]XmlNode{n}, func(n XmlNode) bool { 119 if n.XMLName.Local == tagName { 120 nodeContent += string(n.Content) 121 } 122 return true 123 }) 124 return nodeContent 125 }