converter_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package protobuf
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/schema"
  23. "github.com/lf-edge/ekuiper/internal/testx"
  24. )
  25. func TestEncode(t *testing.T) {
  26. c, err := NewConverter("../../schema/test/test1.proto", "", "Person")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. tests := []struct {
  31. m map[string]interface{}
  32. r []byte
  33. e string
  34. }{
  35. {
  36. m: map[string]interface{}{
  37. "name": "test",
  38. "id": 1,
  39. "age": 1,
  40. },
  41. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x00},
  42. }, {
  43. m: map[string]interface{}{
  44. "name": "test",
  45. "id": 1,
  46. "email": "Dddd",
  47. },
  48. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
  49. },
  50. }
  51. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  52. for i, tt := range tests {
  53. a, err := c.Encode(tt.m)
  54. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  55. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  56. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  57. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  58. }
  59. }
  60. }
  61. func TestDecode(t *testing.T) {
  62. c, err := NewConverter("../../schema/test/test1.proto", "", "Person")
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. tests := []struct {
  67. m map[string]interface{}
  68. r []byte
  69. e string
  70. }{
  71. {
  72. m: map[string]interface{}{
  73. "name": "test",
  74. "id": int64(1),
  75. "email": "Dddd",
  76. "code": []interface{}{},
  77. },
  78. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
  79. },
  80. }
  81. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  82. for i, tt := range tests {
  83. a, err := c.Decode(tt.r)
  84. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  85. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  86. } else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
  87. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, a)
  88. }
  89. }
  90. }
  91. func TestStatic(t *testing.T) {
  92. dataDir, err := conf.GetDataLoc()
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. etcDir := filepath.Join(dataDir, "schemas", "custom")
  97. err = os.MkdirAll(etcDir, os.ModePerm)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. defer func() {
  102. err = os.RemoveAll(etcDir)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. }()
  107. // build the so file into data/test prior to running the test
  108. // Copy the helloworld.so
  109. bytesRead, err := os.ReadFile(filepath.Join(dataDir, "helloworld.so"))
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. err = os.WriteFile(filepath.Join(etcDir, "helloworld.so"), bytesRead, 0o755)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. schema.InitRegistry()
  118. c, err := NewConverter("../../schema/test/test1.proto", "../../../data/test/schemas/custom/helloworld.so", "HelloReply")
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. tests := []struct {
  123. m map[string]interface{}
  124. r []byte
  125. e string
  126. }{
  127. {
  128. m: map[string]interface{}{
  129. "message": "test",
  130. },
  131. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74},
  132. }, {
  133. m: map[string]interface{}{
  134. "message": "another test 2",
  135. },
  136. r: []byte{0x0a, 0x0e, 0x61, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x32},
  137. },
  138. }
  139. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  140. for i, tt := range tests {
  141. a, err := c.Encode(tt.m)
  142. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  143. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  144. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  145. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  146. }
  147. m, err := c.Decode(tt.r)
  148. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  149. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  150. } else if tt.e == "" && !reflect.DeepEqual(tt.m, m) {
  151. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, m)
  152. }
  153. }
  154. }