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 &Endpoint{
encoded string Name: "endpoint1",
oldEncoded string Request: &Value{
}{ Name: "request",
{ Type: "request",
&Endpoint{ },
Name: "Endpoint", Response: &Value{
Request: &Value{ Name: "response",
Name: "request", Type: "response",
Type: "Request", },
}, Metadata: map[string]string{
Response: &Value{ "foo1": "bar1",
Name: "response", },
Type: "Response", },
}, &Endpoint{
Metadata: map[string]string{ Name: "endpoint2",
"foo": "bar", 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",
}, },
"e-789caa56ca4bcc4d55b25272cd4b29c8cfcc2b51d2512a4a2d2c4d2d2e51b2824bc24474944a2a0b4002417081b2c41c204bc92aaf3427a716a4b7b8203faf38154533540849375c044d7b6e6a49624a624922487b5a7e3e506d526291526d2d200000ffffb9fb3937",
`e={"name":"Endpoint","request":{"name":"request","type":"Request","values":null},"response":{"name":"response","type":"Response","values":null},"metadata":{"foo":"bar"}}`,
}, },
} }
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}) for _, ep := range eps {
if len(d) == 0 { // JSON encoded
t.Fatalf("Expected %v got %v", data.decoded, d) jencoded, err := json.Marshal(ep)
if err != nil {
t.Fatal(err)
} }
if d[0].Name != data.decoded.Name { // HEX encoded
t.Fatalf("Expected ep %s got %s", data.decoded.Name, d[0].Name) hencoded := encode(jencoded)
} // endpoint tag
jepTag := "e=" + string(jencoded)
hepTag := "e-" + hencoded
for k, v := range data.decoded.Metadata { // test old
if gv := d[0].Metadata[k]; gv != v { testEp(ep, jepTag)
t.Fatalf("Expected key %s val %s got val %s", k, v, gv) // test new
} testEp(ep, hepTag)
}
} }
} }