Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

276 rindas
6.1 KiB

  1. package decoder
  2. import (
  3. "testing"
  4. "github.com/AFASystems/presence/internal/pkg/model"
  5. )
  6. func TestParserRegistry_AddParser(t *testing.T) {
  7. // Setup
  8. registry := &model.ParserRegistry{}
  9. // Add a parser
  10. config := model.Config{
  11. Name: "test-parser",
  12. Prefix: "02",
  13. Length: 2,
  14. }
  15. registry.Register("test-parser", config)
  16. // Verify parser was added
  17. if len(registry.ParserList) != 1 {
  18. t.Errorf("Expected 1 parser in registry, got %d", len(registry.ParserList))
  19. }
  20. if _, exists := registry.ParserList["test-parser"]; !exists {
  21. t.Error("Parser 'test-parser' should exist in registry")
  22. }
  23. }
  24. func TestParserRegistry_RemoveParser(t *testing.T) {
  25. // Setup
  26. registry := &model.ParserRegistry{}
  27. config := model.Config{
  28. Name: "test-parser",
  29. Prefix: "02",
  30. Length: 2,
  31. }
  32. registry.Register("test-parser", config)
  33. // Remove parser
  34. registry.Unregister("test-parser")
  35. // Verify parser was removed
  36. if len(registry.ParserList) != 0 {
  37. t.Errorf("Expected 0 parsers in registry, got %d", len(registry.ParserList))
  38. }
  39. if _, exists := registry.ParserList["test-parser"]; exists {
  40. t.Error("Parser 'test-parser' should not exist in registry")
  41. }
  42. }
  43. func TestParserRegistry_UpdateParser(t *testing.T) {
  44. // Setup
  45. registry := &model.ParserRegistry{}
  46. // Add initial parser
  47. config1 := model.Config{
  48. Name: "test-parser",
  49. Prefix: "02",
  50. Length: 2,
  51. }
  52. registry.Register("test-parser", config1)
  53. // Update parser
  54. config2 := model.Config{
  55. Name: "test-parser",
  56. Prefix: "03",
  57. Length: 3,
  58. }
  59. registry.Register("test-parser", config2)
  60. // Verify only one parser exists
  61. if len(registry.ParserList) != 1 {
  62. t.Errorf("Expected 1 parser in registry, got %d", len(registry.ParserList))
  63. }
  64. // Verify it was updated (the new config should be used)
  65. if _, exists := registry.ParserList["test-parser"]; !exists {
  66. t.Error("Parser 'test-parser' should exist in registry")
  67. }
  68. }
  69. func TestParserRegistry_MultipleParsers(t *testing.T) {
  70. // Setup
  71. registry := &model.ParserRegistry{}
  72. // Add multiple parsers
  73. parsers := []model.Config{
  74. {Name: "parser-1", Prefix: "02", Length: 2},
  75. {Name: "parser-2", Prefix: "03", Length: 3},
  76. {Name: "parser-3", Prefix: "04", Length: 4},
  77. }
  78. for _, p := range parsers {
  79. registry.Register(p.Name, p)
  80. }
  81. // Verify all parsers were added
  82. if len(registry.ParserList) != 3 {
  83. t.Errorf("Expected 3 parsers in registry, got %d", len(registry.ParserList))
  84. }
  85. for _, p := range parsers {
  86. if _, exists := registry.ParserList[p.Name]; !exists {
  87. t.Errorf("Parser '%s' should exist in registry", p.Name)
  88. }
  89. }
  90. }
  91. func TestParserRegistry_RemoveNonExistent(t *testing.T) {
  92. // Setup
  93. registry := &model.ParserRegistry{}
  94. // Try to remove non-existent parser - should not panic
  95. registry.Unregister("non-existent")
  96. // Verify registry is still empty
  97. if len(registry.ParserList) != 0 {
  98. t.Errorf("Expected 0 parsers, got %d", len(registry.ParserList))
  99. }
  100. }
  101. func TestParserRegistry_ConcurrentAccess(t *testing.T) {
  102. // Setup
  103. registry := &model.ParserRegistry{}
  104. done := make(chan bool)
  105. // Concurrent additions
  106. for i := 0; i < 10; i++ {
  107. go func(index int) {
  108. config := model.Config{
  109. Name: "parser-" + string(rune('A'+index)),
  110. Prefix: "02",
  111. Length: 2,
  112. }
  113. registry.Register(config.Name, config)
  114. done <- true
  115. }(i)
  116. }
  117. // Wait for all goroutines
  118. for i := 0; i < 10; i++ {
  119. <-done
  120. }
  121. // Verify all parsers were added
  122. if len(registry.ParserList) != 10 {
  123. t.Errorf("Expected 10 parsers, got %d", len(registry.ParserList))
  124. }
  125. }
  126. func TestParserConfig_Structure(t *testing.T) {
  127. config := model.Config{
  128. Name: "test-config",
  129. Prefix: "0201",
  130. MinLength: 10,
  131. MaxLength: 30,
  132. ParserType: "sensor",
  133. }
  134. if config.Name != "test-config" {
  135. t.Errorf("Expected name 'test-config', got '%s'", config.Name)
  136. }
  137. if config.Prefix != "0201" {
  138. t.Errorf("Expected prefix '0201', got '%s'", config.Prefix)
  139. }
  140. if config.MinLength != 10 {
  141. t.Errorf("Expected MinLength 10, got %d", config.MinLength)
  142. }
  143. if config.MaxLength != 30 {
  144. t.Errorf("Expected MaxLength 30, got %d", config.MaxLength)
  145. }
  146. }
  147. func TestKafkaParser_MessageTypes(t *testing.T) {
  148. testCases := []struct {
  149. name string
  150. id string
  151. config model.Config
  152. expected string
  153. }{
  154. {
  155. name: "add parser",
  156. id: "add",
  157. config: model.Config{Name: "new-parser", Prefix: "02", Length: 2},
  158. expected: "add",
  159. },
  160. {
  161. name: "delete parser",
  162. id: "delete",
  163. config: model.Config{Name: "old-parser", Prefix: "02", Length: 2},
  164. expected: "delete",
  165. },
  166. {
  167. name: "update parser",
  168. id: "update",
  169. config: model.Config{Name: "updated-parser", Prefix: "03", Length: 3},
  170. expected: "update",
  171. },
  172. }
  173. for _, tc := range testCases {
  174. t.Run(tc.name, func(t *testing.T) {
  175. msg := model.KafkaParser{
  176. ID: tc.id,
  177. Name: tc.config.Name,
  178. Config: tc.config,
  179. }
  180. if msg.ID != tc.expected {
  181. t.Errorf("Expected ID '%s', got '%s'", tc.expected, msg.ID)
  182. }
  183. if msg.Name != tc.config.Name {
  184. t.Errorf("Expected Name '%s', got '%s'", tc.config.Name, msg.Name)
  185. }
  186. })
  187. }
  188. }
  189. func TestParserRegistry_EmptyRegistry(t *testing.T) {
  190. // Setup empty registry
  191. registry := &model.ParserRegistry{}
  192. // Verify it's empty
  193. if len(registry.ParserList) != 0 {
  194. t.Errorf("Expected empty registry, got %d parsers", len(registry.ParserList))
  195. }
  196. // Should be safe to call Unregister on empty registry
  197. registry.Unregister("anything")
  198. }
  199. func TestParserRegistry_ParserReplacement(t *testing.T) {
  200. // Setup
  201. registry := &model.ParserRegistry{}
  202. // Add parser with config 1
  203. config1 := model.Config{
  204. Name: "test-parser",
  205. Prefix: "02",
  206. Length: 2,
  207. }
  208. registry.Register("test-parser", config1)
  209. // Replace with config 2 (same name)
  210. config2 := model.Config{
  211. Name: "test-parser",
  212. Prefix: "03",
  213. Length: 3,
  214. }
  215. registry.Register("test-parser", config2)
  216. // Verify only one entry exists
  217. if len(registry.ParserList) != 1 {
  218. t.Errorf("Expected 1 parser after replacement, got %d", len(registry.ParserList))
  219. }
  220. // Verify the parser still exists
  221. if _, exists := registry.ParserList["test-parser"]; !exists {
  222. t.Error("Parser 'test-parser' should still exist")
  223. }
  224. }