Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide integration tests for the key functionalities - digital twin command response #31

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 54 additions & 14 deletions integration/ldt_desired_properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package integration
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strings"
"reflect"
"testing"

"github.com/eclipse/ditto-clients-golang/model"
"github.com/eclipse/ditto-clients-golang/protocol/things"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand All @@ -50,7 +50,6 @@ func TestDesiredPropertiesSuite(t *testing.T) {
}

func (suite *ldtDesiredPropertiesSuite) TestEventModifyOrCreateDesiredProperties() {
properties := map[string]interface{}{desiredProperty: value}
tests := map[string]ldtTestCaseData{
"test_create_desired_properties": {
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().FeatureDesiredProperties(featureID).Modify(properties),
Expand All @@ -59,8 +58,7 @@ func (suite *ldtDesiredPropertiesSuite) TestEventModifyOrCreateDesiredProperties
},

"test_modify_desired_properties": {
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().
FeatureDesiredProperties(featureID).Modify(properties),
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().FeatureDesiredProperties(featureID).Modify(properties),
expectedTopic: suite.twinEventTopicModified,
feature: featureWithDesiredProperties,
},
Expand All @@ -69,24 +67,66 @@ func (suite *ldtDesiredPropertiesSuite) TestEventModifyOrCreateDesiredProperties
for testName, testCase := range tests {
suite.Run(testName, func() {
suite.createTestFeature(testCase.feature, featureID)
suite.executeCommand("e", suite.messagesFilter, properties, testCase.command, suite.expectedPath, testCase.expectedTopic)
b, _ := json.Marshal(properties)
body, err := suite.getAllDesiredPropertiesOfFeature(featureID)
suite.executeCommandEvent("e", suite.messagesFilter, properties, testCase.command, suite.expectedPath, testCase.expectedTopic)
expectedBody, _ := json.Marshal(properties)

actualBody, err := suite.getAllDesiredPropertiesOfFeature(featureID)
require.NoError(suite.T(), err, "unable to get desired properties")
assert.Equal(suite.T(), string(b), strings.TrimSpace(string(body)), "desired properties don't match")

assert.True(suite.T(), reflect.DeepEqual(suite.convertToMap(expectedBody), suite.convertToMap(actualBody)))
suite.removeTestFeatures()
})
}
}

func (suite *ldtDesiredPropertiesSuite) TestEventDeleteDesiredProperties() {
command := things.NewCommand(suite.namespacedID).FeatureDesiredProperties(featureID).Delete()
expectedTopic := suite.twinEventTopicDeleted

suite.createTestFeature(featureWithDesiredProperties, featureID)
suite.executeCommand("e", suite.messagesFilter, nil, command, suite.expectedPath, expectedTopic)
suite.executeCommandEvent("e", suite.messagesFilter, nil, things.NewCommand(suite.namespacedID).FeatureDesiredProperties(featureID).Delete(), suite.expectedPath, suite.twinEventTopicDeleted)

body, err := suite.getAllDesiredPropertiesOfFeature(featureID)
require.Error(suite.T(), err, "desired properties of feature should have been deleted")
assert.Nil(suite.T(), body, "body should be nil")
}

func (suite *ldtDesiredPropertiesSuite) TestCommandResponseModifyOrCreateDesiredProperties() {
tests := map[string]ldtTestCaseData{
"test_create_desired_properties": {
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().FeatureDesiredProperties(featureID).Modify(properties),
expectedStatusCode: 201,
feature: emptyFeature,
},

"test_modify_desired_properties": {
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().FeatureDesiredProperties(featureID).Modify(properties),
expectedStatusCode: 204,
feature: featureWithDesiredProperties,
},
}
for testName, testCase := range tests {
suite.Run(testName, func() {
suite.createTestFeature(testCase.feature, featureID)
response, err := suite.executeCommandResponse(testCase.command)
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), testCase.expectedStatusCode, response.Status, "unexpected status code")
suite.removeTestFeatures()
})
}
}

func (suite *ldtDesiredPropertiesSuite) TestCommandResponseDeleteDesiredProperties() {
suite.createTestFeature(featureWithDesiredProperties, featureID)
response, err := suite.executeCommandResponse(things.NewCommand(suite.namespacedID).FeatureDesiredProperties(featureID).Delete())
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), 204, response.Status, "unexpected status code")
}

func (suite *ldtDesiredPropertiesSuite) TestCommandResponseRetrieveDesiredProperties() {
suite.createTestFeature(featureWithDesiredProperties, featureID)
response, err := suite.executeCommandResponse(things.NewCommand(suite.namespacedID).FeatureDesiredProperties(featureID).Retrieve())
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), 200, response.Status, "unexpected status code")

actualBody, err := suite.getAllDesiredPropertiesOfFeature(featureID)
require.NoError(suite.T(), err, "unable to get desired properties")
assert.True(suite.T(), reflect.DeepEqual(response.Value, suite.convertToMap(actualBody)))
}
60 changes: 51 additions & 9 deletions integration/ldt_desired_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package integration
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"strings"
"testing"

"github.com/eclipse/ditto-clients-golang/model"
"github.com/eclipse/ditto-clients-golang/protocol/things"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -58,16 +58,15 @@ func (suite *ldtDesiredPropertySuite) TestEventModifyOrCreateProperty() {
},

"test_modify_desired_property": {
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().
FeatureDesiredProperty(featureID, desiredProperty).Modify(value),
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().FeatureDesiredProperty(featureID, desiredProperty).Modify(value),
expectedTopic: suite.twinEventTopicModified,
feature: featureWithDesiredProperties,
},
}
for testName, testCase := range tests {
suite.Run(testName, func() {
suite.createTestFeature(testCase.feature, featureID)
suite.executeCommand("e", suite.messagesFilter, value, testCase.command, suite.expectedPath, testCase.expectedTopic)
suite.executeCommandEvent("e", suite.messagesFilter, value, testCase.command, suite.expectedPath, testCase.expectedTopic)
b, _ := json.Marshal(value)
body, err := suite.getDesiredPropertyOfFeature(featureID, desiredProperty)
require.NoError(suite.T(), err, "unable to get property")
Expand All @@ -78,14 +77,57 @@ func (suite *ldtDesiredPropertySuite) TestEventModifyOrCreateProperty() {
}

func (suite *ldtDesiredPropertySuite) TestEventDeleteDesiredProperty() {
command := things.NewCommand(suite.namespacedID).FeatureDesiredProperty(featureID, desiredProperty).Delete()
expectedTopic := suite.twinEventTopicDeleted

suite.createTestFeature(featureWithDesiredProperties, featureID)
suite.executeCommand("e", suite.messagesFilter, nil, command, suite.expectedPath, expectedTopic)
suite.executeCommandEvent("e", suite.messagesFilter, nil, things.NewCommand(suite.namespacedID).FeatureDesiredProperty(featureID, desiredProperty).Delete(), suite.expectedPath, suite.twinEventTopicDeleted)

body, err := suite.getDesiredPropertyOfFeature(featureID, desiredProperty)
require.Error(suite.T(), err, fmt.Sprintf("Desired property with key: '%s' should have been deleted", desiredProperty))
assert.Nil(suite.T(), body, "body should be nil")
}

func (suite *ldtDesiredPropertySuite) TestCommandResponseModifyOrCreateDesiredProperty() {
tests := map[string]ldtTestCaseData{
"test_create_desired_property": {
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().FeatureDesiredProperty(featureID, desiredProperty).Modify(value),
expectedStatusCode: 201,
feature: emptyFeature,
},

"test_modify_desired_property": {
command: things.NewCommand(model.NewNamespacedIDFrom(suite.ThingCfg.DeviceID)).Twin().FeatureDesiredProperty(featureID, desiredProperty).Modify(value),
expectedStatusCode: 204,
feature: featureWithDesiredProperties,
},
}
for testName, testCase := range tests {
suite.Run(testName, func() {
suite.createTestFeature(testCase.feature, featureID)
response, err := suite.executeCommandResponse(testCase.command)
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), testCase.expectedStatusCode, response.Status, "unexpected status code")
suite.removeTestFeatures()
})
}
}

func (suite *ldtDesiredPropertySuite) TestCommandResponseDeleteDesiredProperty() {
suite.createTestFeature(featureWithDesiredProperties, featureID)
response, err := suite.executeCommandResponse(things.NewCommand(suite.namespacedID).FeatureDesiredProperty(featureID, desiredProperty).Delete())
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), 204, response.Status, "unexpected status code")

}

func (suite *ldtDesiredPropertySuite) TestCommandResponseRetrieveDesiredProperty() {
suite.createTestFeature(featureWithDesiredProperties, featureID)
response, err := suite.executeCommandResponse(things.NewCommand(suite.namespacedID).FeatureDesiredProperty(featureID, desiredProperty).Retrieve())
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), 200, response.Status, "unexpected status code")

desiredProperties, err := suite.getDesiredPropertyOfFeature(featureID, desiredProperty)
require.NoError(suite.T(), err, "unable to get desired properties")

respVal, err := json.Marshal(response.Value)
require.NoError(suite.T(), err, "unable to marshal the response value")
assert.Equal(suite.T(), string(respVal), strings.TrimSpace(string(desiredProperties)))
}
67 changes: 53 additions & 14 deletions integration/ldt_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ package integration
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"reflect"
"testing"

"github.com/eclipse/ditto-clients-golang/protocol/things"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -52,14 +52,12 @@ func TestFeatureSuite(t *testing.T) {
func (suite *ldtFeatureSuite) TestEventModifyOrCreateFeature() {
tests := map[string]ldtTestCaseData{
"test_create_feature": {
command: things.NewCommand(suite.namespacedID).Twin().
Feature(featureID).Modify(emptyFeature),
command: things.NewCommand(suite.namespacedID).Twin().Feature(featureID).Modify(emptyFeature),
expectedTopic: suite.twinEventTopicCreated,
},

"test_modify_feature": {
command: things.NewCommand(suite.namespacedID).Twin().
Feature(featureID).Modify(emptyFeature),
command: things.NewCommand(suite.namespacedID).Twin().Feature(featureID).Modify(emptyFeature),
expectedTopic: suite.twinEventTopicModified,
feature: emptyFeature,
},
Expand All @@ -69,24 +67,65 @@ func (suite *ldtFeatureSuite) TestEventModifyOrCreateFeature() {
if testCase.feature != nil {
suite.createTestFeature(testCase.feature, featureID)
}
suite.executeCommand("e", suite.messagesFilter, emptyFeature, testCase.command, suite.expectedPath, testCase.expectedTopic)
b, _ := json.Marshal(emptyFeature)
body, err := suite.getFeature(featureID)
suite.executeCommandEvent("e", suite.messagesFilter, emptyFeature, testCase.command, suite.expectedPath, testCase.expectedTopic)
expectedBody, _ := json.Marshal(emptyFeature)
actualBody, err := suite.getFeature(featureID)
require.NoError(suite.T(), err, "unable to get feature")
assert.Equal(suite.T(), string(b), strings.TrimSpace(string(body)), "feature doesn't match")

assert.True(suite.T(), reflect.DeepEqual(suite.convertToMap(expectedBody), suite.convertToMap(actualBody)))
suite.removeTestFeatures()
})
}
}

func (suite *ldtFeatureSuite) TestEventDeleteFeature() {
command := things.NewCommand(suite.namespacedID).Twin().Feature(featureID).Delete()
expectedTopic := suite.twinEventTopicDeleted

suite.createTestFeature(emptyFeature, featureID)
suite.executeCommand("e", suite.messagesFilter, nil, command, suite.expectedPath, expectedTopic)
suite.executeCommandEvent("e", suite.messagesFilter, nil, things.NewCommand(suite.namespacedID).Twin().Feature(featureID).Delete(), suite.expectedPath, suite.twinEventTopicDeleted)

body, err := suite.getFeature(featureID)
require.Error(suite.T(), err, "feature should have been deleted")
assert.Nil(suite.T(), body, "body should be nil")
}

func (suite *ldtFeatureSuite) TestCommandResponseModifyOrCreateFeature() {
tests := map[string]ldtTestCaseData{
"test_create_feature": {
command: things.NewCommand(suite.namespacedID).Twin().Feature(featureID).Modify(emptyFeature),
expectedStatusCode: 201,
},

"test_modify_feature": {
command: things.NewCommand(suite.namespacedID).Twin().Feature(featureID).Modify(emptyFeature),
expectedStatusCode: 204,
feature: emptyFeature,
},
}
for testName, testCase := range tests {
suite.Run(testName, func() {
if testCase.feature != nil {
suite.createTestFeature(testCase.feature, featureID)
}
response, err := suite.executeCommandResponse(testCase.command)
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), testCase.expectedStatusCode, response.Status, "unexpected status code")
suite.removeTestFeatures()
})
}
}

func (suite *ldtFeatureSuite) TestCommandResponseDeleteFeature() {
suite.createTestFeature(emptyFeature, featureID)
response, err := suite.executeCommandResponse(things.NewCommand(suite.namespacedID).Feature(featureID).Delete())
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), 204, response.Status, "unexpected status code")
}

func (suite *ldtFeatureSuite) TestCommandResponseRetrieveFeature() {
suite.createTestFeature(emptyFeature, featureID)
response, err := suite.executeCommandResponse(things.NewCommand(suite.namespacedID).Feature(featureID).Retrieve())
require.NoError(suite.T(), err, "could not get response")
assert.Equal(suite.T(), 200, response.Status, "unexpected status code")
actualBody, err := suite.getFeature(featureID)
require.NoError(suite.T(), err, "unable to get feature")
assert.True(suite.T(), reflect.DeepEqual(response.Value, suite.convertToMap(actualBody)))
}
Loading