diff --git a/go.sum b/go.sum index b391c31..dcb2eb5 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,7 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= diff --git a/mqttdevice/mqttdevice.go b/mqttdevice/mqttdevice.go index d20f7de..1fc1d1f 100644 --- a/mqttdevice/mqttdevice.go +++ b/mqttdevice/mqttdevice.go @@ -96,8 +96,6 @@ func NewMqttValue(v interface{}) MqttValue { return MqttValue(fmt.Sprintf("%0.2f", val)) case int, int8, int16, int32, int64: return MqttValue(fmt.Sprintf("%d", val)) - case mode.DriveMode: - return MqttValue(mode.ToString(val)) case bool: if val { return []byte("ON") @@ -130,6 +128,13 @@ func (m *MqttValue) Float64Value() (float64, error) { func (m *MqttValue) StringValue() (string, error) { return string(*m), nil } +func (m *MqttValue) DriveModeValue() (mode.DriveMode, error) { + val, err := m.IntValue() + if err != nil { + return mode.DriveModeInvalid, err + } + return mode.DriveMode(val), nil +} func (m *MqttValue) ByteSliceValue() ([]byte, error) { return *m, nil } diff --git a/mqttdevice/mqttdevice_test.go b/mqttdevice/mqttdevice_test.go index ae1569b..a6a359e 100644 --- a/mqttdevice/mqttdevice_test.go +++ b/mqttdevice/mqttdevice_test.go @@ -1,14 +1,15 @@ package mqttdevice import ( - "github.com/cyrilix/robocar-base/testtools" + "github.com/cyrilix/robocar-base/mode" + "github.com/cyrilix/robocar-base/testtools/docker" mqtt "github.com/eclipse/paho.mqtt.golang" "testing" ) func TestIntegration(t *testing.T) { - ctx, mqttC, mqttUri := testtools.MqttContainer(t) + ctx, mqttC, mqttUri := docker.MqttContainer(t) defer mqttC.Terminate(ctx) t.Run("ConnectAndClose", func(t *testing.T) { @@ -187,3 +188,23 @@ func TestMqttValue_StringValue(t *testing.T) { } } } + +func TestMqttValue_DriveModeValue(t *testing.T) { + cases := []struct { + value MqttValue + expected mode.DriveMode + }{ + {NewMqttValue(mode.DriveModeUser), mode.DriveModeUser}, + {NewMqttValue(mode.DriveModePilot), mode.DriveModePilot}, + {NewMqttValue(mode.DriveModeInvalid), mode.DriveModeInvalid}, + } + for _, c := range cases { + val, err := c.value.DriveModeValue() + if err != nil { + t.Errorf("unexpected conversion error: %v", err) + } + if c.expected != val { + t.Errorf("MqttValue.DriveMode(): %v, wants %v", val, c.expected) + } + } +} diff --git a/testtools/docker/docker.go b/testtools/docker/docker.go new file mode 100644 index 0000000..296291e --- /dev/null +++ b/testtools/docker/docker.go @@ -0,0 +1,37 @@ +package docker + +import ( + "context" + "fmt" + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/wait" + "testing" +) + +func MqttContainer(t *testing.T) (context.Context, testcontainers.Container, string) { +ctx := context.Background() +req := testcontainers.ContainerRequest{ +Image: "eclipse-mosquitto", +ExposedPorts: []string{"1883/tcp"}, +WaitingFor: wait.ForLog("listen socket on port 1883."), +} +mqttC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ +ContainerRequest: req, +Started: true, +}) +if err != nil { +t.Error(err) +} + +ip, err := mqttC.Host(ctx) +if err != nil { +t.Error(err) +} +port, err := mqttC.MappedPort(ctx, "1883/tcp") +if err != nil { +t.Error(err) +} + +mqttUri := fmt.Sprintf("tcp://%s:%d", ip, port.Int()) +return ctx, mqttC, mqttUri +} diff --git a/testtools/testtools.go b/testtools/testtools.go index 0b88bb5..7444595 100644 --- a/testtools/testtools.go +++ b/testtools/testtools.go @@ -1,43 +1,11 @@ package testtools import ( - "context" - "fmt" "github.com/cyrilix/robocar-base/mqttdevice" mqtt "github.com/eclipse/paho.mqtt.golang" - "github.com/testcontainers/testcontainers-go" - "github.com/testcontainers/testcontainers-go/wait" "sync" - "testing" ) -func MqttContainer(t *testing.T) (context.Context, testcontainers.Container, string) { - ctx := context.Background() - req := testcontainers.ContainerRequest{ - Image: "eclipse-mosquitto", - ExposedPorts: []string{"1883/tcp"}, - WaitingFor: wait.ForLog("listen socket on port 1883."), - } - mqttC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - t.Error(err) - } - - ip, err := mqttC.Host(ctx) - if err != nil { - t.Error(err) - } - port, err := mqttC.MappedPort(ctx, "1883/tcp") - if err != nil { - t.Error(err) - } - - mqttUri := fmt.Sprintf("tcp://%s:%d", ip, port.Int()) - return ctx, mqttC, mqttUri -} func NewFakePublisher() *FakePublisher { return &FakePublisher{msg: make(map[string]mqttdevice.MqttValue)}