converter_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2022-2023 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 TestEmbedType(t *testing.T) {
  62. c, err := NewConverter("../../schema/test/test3.proto", "", "DrivingData")
  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. "drvg_mod": int64(1),
  74. "brk_pedal_sts": map[string]interface{}{
  75. "valid": int64(0),
  76. },
  77. "average_speed": 90.56,
  78. },
  79. r: []byte{0x08, 0x01, 0x11, 0xa4, 0x70, 0x3d, 0x0a, 0xd7, 0xa3, 0x56, 0x40, 0x1a, 0x02, 0x08, 0x00},
  80. },
  81. }
  82. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  83. for i, tt := range tests {
  84. a, err := c.Encode(tt.m)
  85. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  86. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  87. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  88. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  89. }
  90. m, err := c.Decode(a)
  91. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  92. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  93. } else if tt.e == "" && !reflect.DeepEqual(tt.m, m) {
  94. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, m)
  95. }
  96. }
  97. }
  98. func TestDecode(t *testing.T) {
  99. c, err := NewConverter("../../schema/test/test1.proto", "", "Person")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. tests := []struct {
  104. m map[string]interface{}
  105. r []byte
  106. e string
  107. }{
  108. {
  109. m: map[string]interface{}{
  110. "name": "test",
  111. "id": int64(1),
  112. "email": "Dddd",
  113. "code": []interface{}{},
  114. },
  115. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
  116. },
  117. }
  118. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  119. for i, tt := range tests {
  120. a, err := c.Decode(tt.r)
  121. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  122. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  123. } else if tt.e == "" && !reflect.DeepEqual(tt.m, a) {
  124. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, a)
  125. }
  126. }
  127. }
  128. func TestStatic(t *testing.T) {
  129. dataDir, err := conf.GetDataLoc()
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. etcDir := filepath.Join(dataDir, "schemas", "custom")
  134. err = os.MkdirAll(etcDir, os.ModePerm)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. defer func() {
  139. err = os.RemoveAll(etcDir)
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. }()
  144. // build the so file into data/test prior to running the test
  145. // Copy the helloworld.so
  146. bytesRead, err := os.ReadFile(filepath.Join(dataDir, "helloworld.so"))
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. err = os.WriteFile(filepath.Join(etcDir, "helloworld.so"), bytesRead, 0o755)
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. schema.InitRegistry()
  155. c, err := NewConverter("../../schema/test/test1.proto", "../../../data/test/schemas/custom/helloworld.so", "HelloReply")
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. tests := []struct {
  160. m map[string]interface{}
  161. r []byte
  162. e string
  163. }{
  164. {
  165. m: map[string]interface{}{
  166. "message": "test",
  167. },
  168. r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74},
  169. }, {
  170. m: map[string]interface{}{
  171. "message": "another test 2",
  172. },
  173. r: []byte{0x0a, 0x0e, 0x61, 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x32},
  174. },
  175. }
  176. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  177. for i, tt := range tests {
  178. a, err := c.Encode(tt.m)
  179. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  180. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  181. } else if tt.e == "" && !reflect.DeepEqual(tt.r, a) {
  182. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%x\n\ngot=%x\n\n", i, tt.r, a)
  183. }
  184. m, err := c.Decode(tt.r)
  185. if !reflect.DeepEqual(tt.e, testx.Errstring(err)) {
  186. t.Errorf("%d.error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  187. } else if tt.e == "" && !reflect.DeepEqual(tt.m, m) {
  188. t.Errorf("%d. \n\nresult mismatch:\n\nexp=%v\n\ngot=%v\n\n", i, tt.m, m)
  189. }
  190. }
  191. }