update encoding test

This commit is contained in:
Asim 2016-03-29 18:16:55 +01:00
parent 61178d1b45
commit cb89b2ebca

View File

@ -1,71 +1,116 @@
package registry package registry
import ( import (
"encoding/json"
"testing" "testing"
) )
func TestEncodingEndpoints(t *testing.T) { func TestEncodingEndpoints(t *testing.T) {
testData := []struct { eps := []*Endpoint{
decoded *Endpoint
encoded string
oldEncoded string
}{
{
&Endpoint{ &Endpoint{
Name: "Endpoint", Name: "endpoint1",
Request: &Value{ Request: &Value{
Name: "request", Name: "request",
Type: "Request", Type: "request",
}, },
Response: &Value{ Response: &Value{
Name: "response", Name: "response",
Type: "Response", Type: "response",
}, },
Metadata: map[string]string{ Metadata: map[string]string{
"foo": "bar", "foo1": "bar1",
}, },
}, },
"e-789caa56ca4bcc4d55b25272cd4b29c8cfcc2b51d2512a4a2d2c4d2d2e51b2824bc24474944a2a0b4002417081b2c41c204bc92aaf3427a716a4b7b8203faf38154533540849375c044d7b6e6a49624a624922487b5a7e3e506d526291526d2d200000ffffb9fb3937", &Endpoint{
`e={"name":"Endpoint","request":{"name":"request","type":"Request","values":null},"response":{"name":"response","type":"Response","values":null},"metadata":{"foo":"bar"}}`, Name: "endpoint2",
Request: &Value{
Name: "request",
Type: "request",
},
Response: &Value{
Name: "response",
Type: "response",
},
Metadata: map[string]string{
"foo2": "bar2",
},
},
&Endpoint{
Name: "endpoint3",
Request: &Value{
Name: "request",
Type: "request",
},
Response: &Value{
Name: "response",
Type: "response",
},
Metadata: map[string]string{
"foo3": "bar3",
},
}, },
} }
for _, data := range testData { testEp := func(ep *Endpoint, enc string) {
e := encodeEndpoints([]*Endpoint{data.decoded}) // encode endpoint
e := encodeEndpoints([]*Endpoint{ep})
if len(e) != 2 || e[1] != data.encoded { // check there are two tags; old and new
t.Fatalf("Expected %s got %s", data.encoded, e) if len(e) != 2 {
t.Fatal("Expected 2 encoded tags, got %v", e)
} }
d := decodeEndpoints(e) // check old encoding
var seen bool
for _, en := range e {
if en == enc {
seen = true
break
}
}
if !seen {
t.Fatal("Expected %s but not found", enc)
}
// decode
d := decodeEndpoints([]string{enc})
if len(d) == 0 { if len(d) == 0 {
t.Fatalf("Expected %v got %v", data.decoded, d) t.Fatalf("Expected %v got %v", ep, d)
} }
if d[0].Name != data.decoded.Name { // check name
t.Fatalf("Expected ep %s got %s", data.decoded.Name, d[0].Name) if d[0].Name != ep.Name {
t.Fatalf("Expected ep %s got %s", ep.Name, d[0].Name)
} }
for k, v := range data.decoded.Metadata { // check all the metadata exists
for k, v := range ep.Metadata {
if gv := d[0].Metadata[k]; gv != v { if gv := d[0].Metadata[k]; gv != v {
t.Fatalf("Expected key %s val %s got val %s", k, v, gv) t.Fatalf("Expected key %s val %s got val %s", k, v, gv)
} }
} }
d = decodeEndpoints([]string{data.oldEncoded})
if len(d) == 0 {
t.Fatalf("Expected %v got %v", data.decoded, d)
} }
if d[0].Name != data.decoded.Name { for _, ep := range eps {
t.Fatalf("Expected ep %s got %s", data.decoded.Name, d[0].Name) // JSON encoded
jencoded, err := json.Marshal(ep)
if err != nil {
t.Fatal(err)
} }
for k, v := range data.decoded.Metadata { // HEX encoded
if gv := d[0].Metadata[k]; gv != v { hencoded := encode(jencoded)
t.Fatalf("Expected key %s val %s got val %s", k, v, gv) // endpoint tag
} jepTag := "e=" + string(jencoded)
} hepTag := "e-" + hencoded
// test old
testEp(ep, jepTag)
// test new
testEp(ep, hepTag)
} }
} }