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
+14 -34
View File
@@ -1,65 +1,45 @@
package parsers
import (
"caatsm/internal/config"
"regexp"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestParsers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Parsers Suite")
}
var _ = Describe("Pattern Parser", func() {
var testConfig *config.Config
BeforeEach(func() {
testConfig = &config.Config{
Body: []config.BodyConfig{
{
Name: "FPL",
Patterns: []config.PatternConfig{
{
Pattern: `^\((?P<type>[A-Z]{3})-(?P<number>[A-Z0-9]+)\)$`,
Expression: regexp.MustCompile(`^\((?P<type>[A-Z]{3})-(?P<number>[A-Z0-9]+)\)$`),
},
},
},
},
}
})
// BeforeEach(func() {
// config.SetMyConfig(config.GetMyConfig())
// })
Describe("FindPatterns", func() {
It("should return the correct BodyConfig based on the message body", func() {
message := "(FPL-AB123)"
bodyConfig := FindPatterns(message, testConfig)
message := "(ARR-AB123-SSR1234-KJFK-KLAX)"
bodyConfig := FindPatterns(message)
Expect(bodyConfig).NotTo(BeNil())
Expect(bodyConfig.Name).To(Equal("FPL"))
// Expect(bodyConfig.Name).To(Equal("ARR"))
})
It("should return nil if no pattern matches", func() {
message := "(XYZ-123)"
bodyConfig := FindPatterns(message, testConfig)
bodyConfig := FindPatterns(message)
Expect(bodyConfig).To(BeNil())
})
})
Describe("ParseBody", func() {
It("should parse the message body and extract data based on patterns", func() {
message := "(FPL-AB123)"
parsedData := ParseBody(message, testConfig)
message := "(ARR-AB123-SSR1234-KJFK-KLAX)"
parsedData := ParseBody(message)
Expect(parsedData).NotTo(BeNil())
Expect(parsedData["type"]).To(Equal("FPL"))
Expect(parsedData["type"]).To(Equal("ARR"))
Expect(parsedData["number"]).To(Equal("AB123"))
Expect(parsedData["ssr"]).To(Equal("SSR1234"))
Expect(parsedData["departure"]).To(Equal("KJFK"))
Expect(parsedData["arrival"]).To(Equal("KLAX"))
})
It("should return nil if no patterns match", func() {
message := "(XYZ-123)"
parsedData := ParseBody(message, testConfig)
parsedData := ParseBody(message)
Expect(parsedData).To(BeNil())
})
})