|
- package decoder
-
- import (
- "testing"
-
- "github.com/AFASystems/presence/internal/pkg/model"
- )
-
- func TestParserRegistry_AddParser(t *testing.T) {
- // Setup
- registry := &model.ParserRegistry{}
-
- // Add a parser
- config := model.Config{
- Name: "test-parser",
- Prefix: "02",
- Length: 2,
- }
-
- registry.Register("test-parser", config)
-
- // Verify parser was added
- if len(registry.ParserList) != 1 {
- t.Errorf("Expected 1 parser in registry, got %d", len(registry.ParserList))
- }
-
- if _, exists := registry.ParserList["test-parser"]; !exists {
- t.Error("Parser 'test-parser' should exist in registry")
- }
- }
-
- func TestParserRegistry_RemoveParser(t *testing.T) {
- // Setup
- registry := &model.ParserRegistry{}
-
- config := model.Config{
- Name: "test-parser",
- Prefix: "02",
- Length: 2,
- }
-
- registry.Register("test-parser", config)
-
- // Remove parser
- registry.Unregister("test-parser")
-
- // Verify parser was removed
- if len(registry.ParserList) != 0 {
- t.Errorf("Expected 0 parsers in registry, got %d", len(registry.ParserList))
- }
-
- if _, exists := registry.ParserList["test-parser"]; exists {
- t.Error("Parser 'test-parser' should not exist in registry")
- }
- }
-
- func TestParserRegistry_UpdateParser(t *testing.T) {
- // Setup
- registry := &model.ParserRegistry{}
-
- // Add initial parser
- config1 := model.Config{
- Name: "test-parser",
- Prefix: "02",
- Length: 2,
- }
-
- registry.Register("test-parser", config1)
-
- // Update parser
- config2 := model.Config{
- Name: "test-parser",
- Prefix: "03",
- Length: 3,
- }
-
- registry.Register("test-parser", config2)
-
- // Verify only one parser exists
- if len(registry.ParserList) != 1 {
- t.Errorf("Expected 1 parser in registry, got %d", len(registry.ParserList))
- }
-
- // Verify it was updated (the new config should be used)
- if _, exists := registry.ParserList["test-parser"]; !exists {
- t.Error("Parser 'test-parser' should exist in registry")
- }
- }
-
- func TestParserRegistry_MultipleParsers(t *testing.T) {
- // Setup
- registry := &model.ParserRegistry{}
-
- // Add multiple parsers
- parsers := []model.Config{
- {Name: "parser-1", Prefix: "02", Length: 2},
- {Name: "parser-2", Prefix: "03", Length: 3},
- {Name: "parser-3", Prefix: "04", Length: 4},
- }
-
- for _, p := range parsers {
- registry.Register(p.Name, p)
- }
-
- // Verify all parsers were added
- if len(registry.ParserList) != 3 {
- t.Errorf("Expected 3 parsers in registry, got %d", len(registry.ParserList))
- }
-
- for _, p := range parsers {
- if _, exists := registry.ParserList[p.Name]; !exists {
- t.Errorf("Parser '%s' should exist in registry", p.Name)
- }
- }
- }
-
- func TestParserRegistry_RemoveNonExistent(t *testing.T) {
- // Setup
- registry := &model.ParserRegistry{}
-
- // Try to remove non-existent parser - should not panic
- registry.Unregister("non-existent")
-
- // Verify registry is still empty
- if len(registry.ParserList) != 0 {
- t.Errorf("Expected 0 parsers, got %d", len(registry.ParserList))
- }
- }
-
- func TestParserRegistry_ConcurrentAccess(t *testing.T) {
- // Setup
- registry := &model.ParserRegistry{}
- done := make(chan bool)
-
- // Concurrent additions
- for i := 0; i < 10; i++ {
- go func(index int) {
- config := model.Config{
- Name: "parser-" + string(rune('A'+index)),
- Prefix: "02",
- Length: 2,
- }
- registry.Register(config.Name, config)
- done <- true
- }(i)
- }
-
- // Wait for all goroutines
- for i := 0; i < 10; i++ {
- <-done
- }
-
- // Verify all parsers were added
- if len(registry.ParserList) != 10 {
- t.Errorf("Expected 10 parsers, got %d", len(registry.ParserList))
- }
- }
-
- func TestParserConfig_Structure(t *testing.T) {
- config := model.Config{
- Name: "test-config",
- Prefix: "0201",
- MinLength: 10,
- MaxLength: 30,
- ParserType: "sensor",
- }
-
- if config.Name != "test-config" {
- t.Errorf("Expected name 'test-config', got '%s'", config.Name)
- }
-
- if config.Prefix != "0201" {
- t.Errorf("Expected prefix '0201', got '%s'", config.Prefix)
- }
-
- if config.MinLength != 10 {
- t.Errorf("Expected MinLength 10, got %d", config.MinLength)
- }
-
- if config.MaxLength != 30 {
- t.Errorf("Expected MaxLength 30, got %d", config.MaxLength)
- }
- }
-
- func TestKafkaParser_MessageTypes(t *testing.T) {
- testCases := []struct {
- name string
- id string
- config model.Config
- expected string
- }{
- {
- name: "add parser",
- id: "add",
- config: model.Config{Name: "new-parser", Prefix: "02", Length: 2},
- expected: "add",
- },
- {
- name: "delete parser",
- id: "delete",
- config: model.Config{Name: "old-parser", Prefix: "02", Length: 2},
- expected: "delete",
- },
- {
- name: "update parser",
- id: "update",
- config: model.Config{Name: "updated-parser", Prefix: "03", Length: 3},
- expected: "update",
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- msg := model.KafkaParser{
- ID: tc.id,
- Name: tc.config.Name,
- Config: tc.config,
- }
-
- if msg.ID != tc.expected {
- t.Errorf("Expected ID '%s', got '%s'", tc.expected, msg.ID)
- }
-
- if msg.Name != tc.config.Name {
- t.Errorf("Expected Name '%s', got '%s'", tc.config.Name, msg.Name)
- }
- })
- }
- }
-
- func TestParserRegistry_EmptyRegistry(t *testing.T) {
- // Setup empty registry
- registry := &model.ParserRegistry{}
-
- // Verify it's empty
- if len(registry.ParserList) != 0 {
- t.Errorf("Expected empty registry, got %d parsers", len(registry.ParserList))
- }
-
- // Should be safe to call Unregister on empty registry
- registry.Unregister("anything")
- }
-
- func TestParserRegistry_ParserReplacement(t *testing.T) {
- // Setup
- registry := &model.ParserRegistry{}
-
- // Add parser with config 1
- config1 := model.Config{
- Name: "test-parser",
- Prefix: "02",
- Length: 2,
- }
-
- registry.Register("test-parser", config1)
-
- // Replace with config 2 (same name)
- config2 := model.Config{
- Name: "test-parser",
- Prefix: "03",
- Length: 3,
- }
-
- registry.Register("test-parser", config2)
-
- // Verify only one entry exists
- if len(registry.ParserList) != 1 {
- t.Errorf("Expected 1 parser after replacement, got %d", len(registry.ParserList))
- }
-
- // Verify the parser still exists
- if _, exists := registry.ParserList["test-parser"]; !exists {
- t.Error("Parser 'test-parser' should still exist")
- }
- }
|