Skip to content

Commit

Permalink
use characteristic value as default when unmarshal from ContentVariab…
Browse files Browse the repository at this point in the history
…le without sub characteristic mapping
  • Loading branch information
IngoRoessner committed Oct 27, 2023
1 parent 2e60fc7 commit adf9ecf
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/marshaller/v2/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,13 @@ func characteristicToPseudoVariable(characteristic model.Characteristic) model.C
if variableType == "" && len(subVariables) > 0 {
variableType = model.Structure
}

return model.ContentVariable{
Name: characteristic.Name,
CharacteristicId: characteristic.Id,
Type: variableType,
SubContentVariables: subVariables,
Value: characteristic.Value,
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/tests/testdata/characteristics.json
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@
"name": "b",
"rdf_type": "https://senergy.infai.org/ontology/Characteristic",
"sub_characteristics": null,
"type": "https://schema.org/Integer"
"type": "https://schema.org/Integer",
"value": 50
},
{
"display_unit": "",
Expand Down
166 changes: 166 additions & 0 deletions lib/tests/v2/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,172 @@ import (
"testing"
)

func TestUnmarshalIncompleteCharacteristic(t *testing.T) {
wg := &sync.WaitGroup{}
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apiurl := setup(ctx, wg)

functionId := "urn:infai:ses:measuring-function:bdb6a7c8-4a3d-4fe0-bab3-ce02e09b5869" //Read Color

protocol := model.Protocol{
Id: "p1",
Name: "p1",
Handler: "p1",
ProtocolSegments: []model.ProtocolSegment{
{Id: "p1.1", Name: "body"},
{Id: "p1.2", Name: "head"},
},
}
service := model.Service{
Id: "sid",
LocalId: "slid",
Name: "sname",
Interaction: model.EVENT_AND_REQUEST,
ProtocolId: "p1",
Outputs: []model.Content{
{
Id: "content",
ContentVariable: model.ContentVariable{
Id: "root",
Name: "root",
Type: model.Structure,
SubContentVariables: []model.ContentVariable{
{
Id: "color",
Name: "color",
CharacteristicId: characteristics.Hsb,
FunctionId: functionId,
AspectId: "air",
Type: model.Structure,
SubContentVariables: []model.ContentVariable{
{
Id: "hue",
Name: "hue",
Type: model.Integer,
CharacteristicId: characteristics.HsbH,
},
{
Id: "sat",
Name: "sat",
Type: model.Integer,
CharacteristicId: characteristics.HsbS,
},
},
},
{
Id: "brightness",
Name: "brightness",
Type: model.Integer,
},
},
},
Serialization: "json",
ProtocolSegmentId: "p1.1",
},
},
}

output := map[string]string{"body": `{"brightness":66,"color":{"hue": 219, "sat": 68}}`}

t.Run("run", testUnmarshal(apiurl, api.UnmarshallingV2Request{
Service: service,
Protocol: protocol,
CharacteristicId: characteristics.Rgb,
Message: output,
FunctionId: functionId,
AspectNodeId: "air",
}, map[string]interface{}{"b": 128.0, "g": 71.0, "r": 41.0}))
}

func TestUnmarshalDiscombobulatedCharacteristic(t *testing.T) {
wg := &sync.WaitGroup{}
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apiurl := setup(ctx, wg)

functionId := "urn:infai:ses:measuring-function:bdb6a7c8-4a3d-4fe0-bab3-ce02e09b5869" //Read Color

protocol := model.Protocol{
Id: "p1",
Name: "p1",
Handler: "p1",
ProtocolSegments: []model.ProtocolSegment{
{Id: "p1.1", Name: "body"},
{Id: "p1.2", Name: "head"},
},
}
service := model.Service{
Id: "sid",
LocalId: "slid",
Name: "sname",
Interaction: model.EVENT_AND_REQUEST,
ProtocolId: "p1",
Outputs: []model.Content{
{
Id: "content",
ContentVariable: model.ContentVariable{
Id: "root",
Name: "root",
Type: model.Structure,
CharacteristicId: characteristics.Hsb,
FunctionId: functionId,
AspectId: "air",
SubContentVariables: []model.ContentVariable{
{
Id: "color",
Name: "color",
Type: model.Structure,
SubContentVariables: []model.ContentVariable{
{
Id: "hue",
Name: "hue",
Type: model.Integer,
CharacteristicId: characteristics.HsbH,
},
{
Id: "sat",
Name: "sat",
Type: model.Integer,
CharacteristicId: characteristics.HsbS,
},
},
},
{
Id: "brightness",
Name: "brightness",
Type: model.Integer,
CharacteristicId: characteristics.HsbB,
},
},
},
Serialization: "json",
ProtocolSegmentId: "p1.1",
},
},
}

t.Run("run_1", testUnmarshal(apiurl, api.UnmarshallingV2Request{
Service: service,
Protocol: protocol,
CharacteristicId: characteristics.Rgb,
Message: map[string]string{"body": `{"brightness":66,"color":{"hue": 219, "sat": 68}}`},
FunctionId: functionId,
AspectNodeId: "air",
}, map[string]interface{}{"b": 168.0, "g": 94.0, "r": 54.0}))

t.Run("run_2", testUnmarshal(apiurl, api.UnmarshallingV2Request{
Service: service,
Protocol: protocol,
CharacteristicId: characteristics.Rgb,
Message: map[string]string{"body": `{"brightness":50,"color":{"hue": 219, "sat": 68}}`},
FunctionId: functionId,
AspectNodeId: "air",
}, map[string]interface{}{"b": 128.0, "g": 71.0, "r": 41.0}))
}

func TestUnmarshalPrioritySort(t *testing.T) {
wg := &sync.WaitGroup{}
defer wg.Wait()
Expand Down

0 comments on commit adf9ecf

Please sign in to comment.