edgex_source_test.go 8.1 KB

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