edgex_source_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // +build edgex
  2. package extensions
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "github.com/edgexfoundry/go-mod-core-contracts/models"
  7. "github.com/edgexfoundry/go-mod-messaging/pkg/types"
  8. "github.com/emqx/kuiper/common"
  9. "reflect"
  10. "testing"
  11. )
  12. var es = EdgexSource{valueDescs: map[string]string{
  13. "b1": "bool",
  14. "i1": "int8",
  15. "i2": "INT16",
  16. "i3": "INT32",
  17. "i4": "INT64",
  18. "i5": "UINT8",
  19. "i6": "UINT16",
  20. "i7": "UINT32",
  21. "s1": "String",
  22. "f1": "Float32", //FLOAT32 will be handled by special case
  23. "f2": "Float64", //FLOAT64 will be handled by special case
  24. "i8": "UINT64", //UINT64 will be handled by special case
  25. "ba": "BOOLARRAY",
  26. "ia1": "INT8ARRAY",
  27. "ia2": "INT16ARRAY",
  28. "ia3": "INT32ARRAY",
  29. "ia4": "INT64ARRAY",
  30. "ia5": "UINT8ARRAY",
  31. "ia6": "UINT16ARRAY",
  32. "ia7": "UINT32ARRAY",
  33. "ia8": "UINT64ARRAY",
  34. "fa1": "FLOAT32ARRAY",
  35. "fa2": "FLOAT64ARRAY",
  36. },
  37. }
  38. func TestGetValue_IntFloat(t *testing.T) {
  39. var testEvent = models.Event{Device: "test"}
  40. for i := 1; i < 8; i++ {
  41. r1 := models.Reading{Name: fmt.Sprintf("i%d", i), Value: "1"}
  42. testEvent.Readings = append(testEvent.Readings, r1)
  43. }
  44. for _, r := range testEvent.Readings {
  45. if v, e := es.getValue(r, common.Log); e != nil {
  46. t.Errorf("%s", e)
  47. } else {
  48. expectOne(t, v)
  49. }
  50. }
  51. rf_01 := models.Reading{Name: "f1", Value: "fwtOaw=="}
  52. if v, e := es.getValue(rf_01, common.Log); e != nil {
  53. t.Errorf("%s", e)
  54. } else {
  55. if v1, ok := v.(float64); ok {
  56. if v1 != 185169860786896613617389922448534667264.000000 {
  57. t.Errorf("expected 185169860786896613617389922448534667264.000000, but it's %f.", v1)
  58. }
  59. } else {
  60. t.Errorf("expected float32 type, but it's %T.", v)
  61. }
  62. }
  63. rf_02 := models.Reading{Name: "f2", Value: "QAkeuFHrhR8="}
  64. if v, e := es.getValue(rf_02, common.Log); e != nil {
  65. t.Errorf("%s", e)
  66. } else {
  67. if v1, ok := v.(float64); ok {
  68. if v1 != 3.14 {
  69. t.Errorf("expected 3.14, but it's %f.", v1)
  70. }
  71. } else {
  72. t.Errorf("expected float64 type, but it's %T.", v)
  73. }
  74. }
  75. r1 := models.Reading{Name: "i8", Value: "10796529505058023104"}
  76. if v, e := es.getValue(r1, common.Log); e != nil {
  77. t.Errorf("%s", e)
  78. } else {
  79. if v1, ok := v.(uint64); ok {
  80. if v1 != 10796529505058023104 {
  81. t.Errorf("expected 10796529505058023104, but it's %d.", v1)
  82. }
  83. }
  84. }
  85. r2 := models.Reading{Name: "f1", Value: "3.14"}
  86. if v, e := es.getValue(r2, common.Log); e != nil {
  87. t.Errorf("%s", e)
  88. } else {
  89. if v1, ok := v.(float64); ok {
  90. if v1 != 3.14 {
  91. t.Errorf("expected 3.14, but it's %f.", v1)
  92. }
  93. }
  94. }
  95. }
  96. func TestGetValue_IntFloatArr(t *testing.T) {
  97. var testEvent = models.Event{Device: "test"}
  98. for i := 1; i < 8; i++ {
  99. ia := []int{i, i * 2}
  100. jsonValue, _ := json.Marshal(ia)
  101. r1 := models.Reading{Name: fmt.Sprintf("ia%d", i), Value: string(jsonValue)}
  102. testEvent.Readings = append(testEvent.Readings, r1)
  103. }
  104. for i, r := range testEvent.Readings {
  105. if v, e := es.getValue(r, common.Log); e != nil {
  106. t.Errorf("%s", e)
  107. } else {
  108. checkArray(t, i, v)
  109. }
  110. }
  111. r1 := models.Reading{Name: "ia8", Value: string(`[10796529505058023104, 10796529505058023105]`)}
  112. testEvent.Readings = append(testEvent.Readings, r1)
  113. if v, e := es.getValue(r1, common.Log); e != nil {
  114. t.Errorf("%s", e)
  115. } else {
  116. if v1, ok := v.([]uint64); ok {
  117. if v1[0] != 10796529505058023104 || v1[1] != 10796529505058023105 {
  118. t.Errorf("Failed, the array value is not correct %v.", v1)
  119. }
  120. } else {
  121. t.Errorf("expected uint64 array type, but it's %T.", v1)
  122. }
  123. }
  124. rf_00 := models.Reading{Name: "fa1", Value: `[3.14, 2.71828]`}
  125. if v, e := es.getValue(rf_00, common.Log); e != nil {
  126. t.Errorf("%s", e)
  127. } else {
  128. if v1, ok := v.([]float64); ok {
  129. if v1[0] != 3.14 || v1[1] != 2.71828 {
  130. t.Errorf("expected 3.14 & 2.71828, but it's %v.", v1)
  131. }
  132. } else {
  133. t.Errorf("expected float32 array type, but it's %T.", v)
  134. }
  135. }
  136. rf_01 := models.Reading{Name: "fa1", Value: `["fwtOaw==","fwtOaw=="]`}
  137. if v, e := es.getValue(rf_01, common.Log); e != nil {
  138. t.Errorf("%s", e)
  139. } else {
  140. if v1, ok := v.([]float64); ok {
  141. if v1[0] != 185169860786896613617389922448534667264.000000 || v1[1] != 185169860786896613617389922448534667264.000000 {
  142. t.Errorf("expected 185169860786896613617389922448534667264.000000, but it's %v.", v1)
  143. }
  144. } else {
  145. t.Errorf("expected float64 array type, but it's %T.", v)
  146. }
  147. }
  148. rf_02 := models.Reading{Name: "fa2", Value: `["QAkeuFHrhR8=","QAW/CZWq95A="]`}
  149. if v, e := es.getValue(rf_02, common.Log); e != nil {
  150. t.Errorf("%s", e)
  151. } else {
  152. if v1, ok := v.([]float64); ok {
  153. if v1[0] != 3.14 || v1[1] != 2.71828 {
  154. t.Errorf("expected 3.14 and 2.71828, but it's %v.", v1)
  155. }
  156. } else {
  157. t.Errorf("expected float64 array type, but it's %T.", v)
  158. }
  159. }
  160. }
  161. func checkArray(t *testing.T, index int, val interface{}) {
  162. if v1, ok := val.([]int); ok {
  163. newIdx := index + 1
  164. if v1[0] != newIdx || v1[1] != newIdx*2 {
  165. t.Errorf("Failed, the array value is not correct %v.", v1)
  166. }
  167. } else {
  168. t.Errorf("expected int array type, but it's %T.", val)
  169. }
  170. }
  171. func expectOne(t *testing.T, expected interface{}) {
  172. if v1, ok := expected.(int); ok {
  173. if v1 != 1 {
  174. t.Errorf("expected 1, but it's %d.", v1)
  175. }
  176. } else {
  177. t.Errorf("expected int type, but it's %T.", expected)
  178. }
  179. }
  180. func TestGetValue_Float(t *testing.T) {
  181. var testEvent = models.Event{Device: "test"}
  182. for i := 1; i < 3; i++ {
  183. r1 := models.Reading{Name: fmt.Sprintf("f%d", i), Value: "3.14"}
  184. testEvent.Readings = append(testEvent.Readings, r1)
  185. }
  186. for _, r := range testEvent.Readings {
  187. if v, e := es.getValue(r, common.Log); e != nil {
  188. t.Errorf("%s", e)
  189. } else {
  190. expectPi(t, v)
  191. }
  192. }
  193. }
  194. func expectPi(t *testing.T, expected interface{}) {
  195. if v1, ok := expected.(float64); ok {
  196. if v1 != 3.14 {
  197. t.Errorf("expected 3.14, but it's %f.", v1)
  198. }
  199. } else {
  200. t.Errorf("expected float type, but it's %T.", expected)
  201. }
  202. }
  203. func TestGetValue_Bool(t *testing.T) {
  204. ///////////True
  205. trues := []string{"1", "t", "T", "true", "TRUE", "True"}
  206. for _, v := range trues {
  207. r1 := models.Reading{Name: "b1", Value: v}
  208. if v, e := es.getValue(r1, common.Log); e != nil {
  209. t.Errorf("%s", e)
  210. } else {
  211. expectTrue(t, v)
  212. }
  213. }
  214. r1 := models.Reading{Name: "b1", Value: "TRue"}
  215. if _, e := es.getValue(r1, common.Log); e == nil {
  216. t.Errorf("%s", e)
  217. }
  218. ///////////False
  219. falses := []string{"0", "f", "F", "false", "FALSE", "False"}
  220. for _, v := range falses {
  221. r1 := models.Reading{Name: "b1", Value: v}
  222. if v, e := es.getValue(r1, common.Log); e != nil {
  223. t.Errorf("%s", e)
  224. } else {
  225. expectFalse(t, v)
  226. }
  227. }
  228. r1 = models.Reading{Name: "b1", Value: "FAlse"}
  229. if _, e := es.getValue(r1, common.Log); e == nil {
  230. t.Errorf("%s", e)
  231. }
  232. }
  233. func expectTrue(t *testing.T, expected interface{}) {
  234. if v1, ok := expected.(bool); ok {
  235. if !v1 {
  236. t.Errorf("expected true, but it's false.")
  237. }
  238. } else {
  239. t.Errorf("expected boolean type, but it's %t.", expected)
  240. }
  241. }
  242. func expectFalse(t *testing.T, expected interface{}) {
  243. if v1, ok := expected.(bool); ok {
  244. if v1 {
  245. t.Errorf("expected false, but it's true.")
  246. }
  247. } else {
  248. t.Errorf("expected boolean type, but it's %t.", expected)
  249. }
  250. }
  251. func TestWrongType(t *testing.T) {
  252. es1 := EdgexSource{valueDescs: map[string]string{
  253. "f": "FLOAT", //A not exsited type
  254. },
  255. }
  256. r1 := models.Reading{Name: "f", Value: "100"}
  257. if v, _ := es1.getValue(r1, common.Log); v != "100" {
  258. t.Errorf("Expected 100, but it's %s!", v)
  259. }
  260. }
  261. func TestWrongValue(t *testing.T) {
  262. var testEvent = models.Event{Device: "test"}
  263. r1 := models.Reading{Name: "b1", Value: "100"} //100 cannot be converted to a boolean value
  264. r2 := models.Reading{Name: "i1", Value: "int"} //'int' string cannot be converted to int value
  265. r3 := models.Reading{Name: "f1", Value: "float"} //'float' string cannot be converted to int value
  266. testEvent.Readings = append(testEvent.Readings, r1, r2, r3)
  267. for _, v := range testEvent.Readings {
  268. if _, e := es.getValue(v, common.Log); e == nil {
  269. t.Errorf("Expected an error!")
  270. }
  271. }
  272. }
  273. func TestCastToString(t *testing.T) {
  274. if v, ok := CastToString(12); v != "12" || !ok {
  275. t.Errorf("Failed to cast int.")
  276. }
  277. if v, ok := CastToString(true); v != "true" || !ok {
  278. t.Errorf("Failed to cast bool.")
  279. }
  280. if v, ok := CastToString("hello"); v != "hello" || !ok {
  281. t.Errorf("Failed to cast string.")
  282. }
  283. if v, ok := CastToString(12.3); v != "12.30" || !ok {
  284. t.Errorf("Failed to cast float.")
  285. }
  286. }
  287. func TestPrintConf(t *testing.T) {
  288. expMbconf := types.MessageBusConfig{SubscribeHost: types.HostInfo{Protocol: "tcp", Host: "127.0.0.1", Port: 6625}, Type: "mbus", Optional: map[string]string{
  289. "proa": "proa",
  290. "Password": "fafsadfsadf=",
  291. "Prob": "Prob",
  292. }}
  293. mbconf := types.MessageBusConfig{SubscribeHost: types.HostInfo{Protocol: "tcp", Host: "127.0.0.1", Port: 6625}, Type: "mbus", Optional: map[string]string{
  294. "proa": "proa",
  295. "Password": "fafsadfsadf=",
  296. "Prob": "Prob",
  297. }}
  298. printConf(mbconf)
  299. if !reflect.DeepEqual(expMbconf, mbconf) {
  300. t.Errorf("conf changed after printing")
  301. }
  302. }