feat: Add AFTN message parsing functionality

This commit adds functionality to parse AFTN messages in the `aftn_parser.go` file. The `Parse` method now correctly parses the message based on its type and extracts the relevant data. Additionally, the `FindPatterns` and `ParseBody` functions have been updated to remove the dependency on the `config` package and instead use the `config.GetBodyPatterns` function to retrieve the body patterns. This change improves the modularity and maintainability of the code.

Note: This commit message assumes that the `config` package has been refactored to use the `GetBodyPatterns` function.
This commit is contained in:
windyboy
2024-07-20 00:01:04 +08:00
parent dbbf6133be
commit 969bb57902
7 changed files with 150 additions and 227 deletions
+56 -79
View File
@@ -2,11 +2,11 @@ package config
import (
"os"
"path/filepath"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/viper"
)
func TestConfig(t *testing.T) {
@@ -20,32 +20,10 @@ var _ = Describe("Config", func() {
BeforeEach(func() {
// Save the original GO_ENV value
originalEnv = os.Getenv("GO_ENV")
})
AfterEach(func() {
// Restore the original GO_ENV value
os.Setenv("GO_ENV", originalEnv)
})
// Helper function to create temporary TOML files in the "configs" directory
createTempConfigFile := func(env, content string) string {
dir := "configs"
err := os.MkdirAll(dir, 0755)
Expect(err).NotTo(HaveOccurred(), "failed to create config directory")
filename := filepath.Join(dir, "config."+env+".toml")
tmpfile, err := os.Create(filename)
Expect(err).NotTo(HaveOccurred(), "failed to create temp config file")
_, err = tmpfile.Write([]byte(content))
Expect(err).NotTo(HaveOccurred(), "failed to write to temp config file")
err = tmpfile.Close()
Expect(err).NotTo(HaveOccurred(), "failed to close temp config file")
return filename
}
Context("Loading and validating a valid TOML file", func() {
It("should load and validate the configuration correctly", func() {
content := `
// Set up a temporary configuration file for testing
viper.Reset()
viper.SetConfigType("toml")
configContent := `
[nats]
client = "test-client"
url = "nats://localhost:4222"
@@ -60,69 +38,68 @@ server_timeout = "30s"
reconnect_wait = "10s"
close_timeout = "10s"
ack_wait_timeout = "5s"
[[body]]
name = "FPL"
[[body.patterns]]
pattern = "^\\((?P<type>[A-Z]{3})\\-(?P<number>[A-Z]+\\d+)\\-(?P<indicator>[A-Z]{2})(?:.*\\s*)?\\-(?P<aircraft>[A-Z]+\\d+/?[A-Z]?)\\s*\\-(?P<surve>.*)\\s*\\-(?P<departure>[A-Z]{4})(?P<departure_time>\\d{4})\\s*\\-(?P<speed>[A-Z]+\\d+)(?P<level>[A-Z0-9]+)\\s(?P<route>.*)\\s*\\-(?P<destination>[A-Z]{4})(?P<estt>\\d{4})\\s(?P<alter>[A-Z]{4})\\s*\\-(?P<pbn>PBN\\/[A-Z0-9]+)\\s(?P<nav>NAV\\/\\w+)\\sREG\\/(?P<reg>[A-Z0-9]+)\\sEET\\/(?P<eet>\\w{4}\\d{4})\\sSEL\\/(?P<sel>\\w+)\\sPER\\/(?P<performance>\\w)\\sRIF\\/(?P<rif>\\w+\\s[A-Z0-9]+\\s[A-Z]+)\\s*RMK\\/(?P<remark>.*)\\)$"
comments = "FPL multi line expression"
`
os.Setenv("GO_ENV", "test")
createTempConfigFile("test", content)
defer os.RemoveAll("configs")
tmpFile, err := os.CreateTemp("", "config.*.toml")
Expect(err).NotTo(HaveOccurred())
_, err = tmpFile.Write([]byte(configContent))
Expect(err).NotTo(HaveOccurred())
err = tmpFile.Close()
Expect(err).NotTo(HaveOccurred())
config, err := LoadConfig()
Expect(err).NotTo(HaveOccurred(), "failed to load valid config")
Expect(config).NotTo(BeNil(), "config should not be nil")
Expect(config.Nats.Client).To(Equal("test-client"), "nats.client should be 'test-client'")
Expect(config.Subscription.Topic).To(Equal("example-topic"), "subscription.topic should be 'example-topic'")
viper.SetConfigFile(tmpFile.Name())
err = viper.ReadInConfig()
Expect(err).NotTo(HaveOccurred())
// Validate the config
err = ValidateConfig(config)
Expect(err).NotTo(HaveOccurred(), "validation should pass for valid config")
// Load the configuration
MyConfig = &Config{}
err = viper.Unmarshal(MyConfig)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
// Restore the original GO_ENV value
os.Setenv("GO_ENV", originalEnv)
})
Context("Loading configuration", func() {
It("should load the configuration correctly", func() {
cfg := GetMyConfig()
Expect(cfg).NotTo(BeNil())
Expect(cfg.Nats.Client).To(Equal("test-client"))
Expect(cfg.Nats.URL).To(Equal("nats://localhost:4222"))
Expect(cfg.Subscription.Topic).To(Equal("example-topic"))
})
})
Context("Loading and validating a non-existent file", func() {
It("should return an error", func() {
os.Setenv("GO_ENV", "nonexistent")
defer os.RemoveAll("configs")
_, err := LoadConfig()
Expect(err).To(HaveOccurred(), "expected error for non-existent config file")
Context("Validating configuration", func() {
It("should validate a valid configuration", func() {
cfg := GetMyConfig()
err := ValidateConfig(cfg)
Expect(err).NotTo(HaveOccurred())
})
})
Context("Loading and validating a file with invalid TOML format", func() {
It("should return an error", func() {
content := `
invalid TOML content
`
os.Setenv("GO_ENV", "invalid")
createTempConfigFile("invalid", content)
defer os.RemoveAll("configs")
_, err := LoadConfig()
Expect(err).To(HaveOccurred(), "expected error for invalid TOML format")
It("should return an error for missing NATS client", func() {
cfg := GetMyConfig()
cfg.Nats.Client = ""
err := ValidateConfig(cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("nats client is required"))
})
})
Context("Validating an invalid config structure", func() {
It("should return an error for missing required fields", func() {
invalidConfig := &Config{
Nats: NatsConfig{
Client: "",
URL: "",
},
Subscription: SubscriptionConfig{
Topic: "",
},
Body: []BodyConfig{},
}
It("should return an error for missing NATS URL", func() {
cfg := GetMyConfig()
cfg.Nats.URL = ""
err := ValidateConfig(cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("nats URL is required"))
})
err := ValidateConfig(invalidConfig)
Expect(err).To(HaveOccurred(), "expected validation error for invalid config")
Expect(err.Error()).To(ContainSubstring("nats client is required"), "expected error for missing nats client")
It("should return an error for missing subscription topic", func() {
cfg := GetMyConfig()
cfg.Subscription.Topic = ""
err := ValidateConfig(cfg)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("subscription topic is required"))
})
})
})