56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package eventbus
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
|
)
|
|
|
|
func TestNATSSinkRoutesRawAndUnifiedSubjects(t *testing.T) {
|
|
publisher := &recordingNATSPublisher{}
|
|
sink := newNATSSinkWithPublisher(publisher, NATSConfig{
|
|
RawSubjects: map[envelope.Protocol]string{
|
|
envelope.ProtocolJT808: "vehicle.raw.jt808.v1",
|
|
},
|
|
UnifiedSubject: "vehicle.event.unified.v1",
|
|
})
|
|
env := envelope.FrameEnvelope{Protocol: envelope.ProtocolJT808, Phone: "13307795425", MessageID: "0x0200"}
|
|
|
|
if err := sink.PublishRaw(context.Background(), env); err != nil {
|
|
t.Fatalf("PublishRaw() error = %v", err)
|
|
}
|
|
if err := sink.PublishUnified(context.Background(), env); err != nil {
|
|
t.Fatalf("PublishUnified() error = %v", err)
|
|
}
|
|
|
|
if got, want := publisher.messages[0].subject, "vehicle.raw.jt808.v1"; got != want {
|
|
t.Fatalf("raw subject = %q, want %q", got, want)
|
|
}
|
|
if got, want := publisher.messages[1].subject, "vehicle.event.unified.v1"; got != want {
|
|
t.Fatalf("unified subject = %q, want %q", got, want)
|
|
}
|
|
var decoded envelope.FrameEnvelope
|
|
if err := json.Unmarshal(publisher.messages[0].data, &decoded); err != nil {
|
|
t.Fatalf("raw payload is not envelope json: %v", err)
|
|
}
|
|
if decoded.Phone != "13307795425" {
|
|
t.Fatalf("decoded phone = %q", decoded.Phone)
|
|
}
|
|
}
|
|
|
|
type recordingNATSPublisher struct {
|
|
messages []recordedNATSMessage
|
|
}
|
|
|
|
type recordedNATSMessage struct {
|
|
subject string
|
|
data []byte
|
|
}
|
|
|
|
func (p *recordingNATSPublisher) Publish(_ context.Context, subject string, data []byte, _ ...NATSPublishOption) error {
|
|
p.messages = append(p.messages, recordedNATSMessage{subject: subject, data: append([]byte(nil), data...)})
|
|
return nil
|
|
}
|