forked from edgexfoundry/app-functions-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement transform to add Tags to Event
closes #455 & #369 Signed-off-by: lenny <[email protected]>
- Loading branch information
lenny
committed
Sep 4, 2020
1 parent
337bfa7
commit acb6ca3
Showing
9 changed files
with
285 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// Copyright (c) 2020 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package transforms | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/models" | ||
|
||
"github.com/edgexfoundry/app-functions-sdk-go/appcontext" | ||
) | ||
|
||
// Tags contains the list of Tag key/values | ||
type Tags struct { | ||
tags map[string]string | ||
} | ||
|
||
// NewTags creates, initializes and returns a new instance of Tags | ||
func NewTags(tags map[string]string) Tags { | ||
return Tags{ | ||
tags: tags, | ||
} | ||
} | ||
|
||
// AddTags adds the pre-configured list of tags to the Event's tags collection. | ||
func (t *Tags) AddTags(edgexcontext *appcontext.Context, params ...interface{}) (bool, interface{}) { | ||
edgexcontext.LoggingClient.Debug("Adding tags to Event") | ||
|
||
if len(params) < 1 { | ||
return false, errors.New("no Event Received") | ||
} | ||
|
||
event, ok := params[0].(models.Event) | ||
if !ok { | ||
return false, errors.New("type received is not an Event") | ||
} | ||
|
||
if len(t.tags) > 0 { | ||
if event.Tags == nil { | ||
event.Tags = make(map[string]string) | ||
} | ||
|
||
for tag, value := range t.tags { | ||
event.Tags[tag] = value | ||
} | ||
edgexcontext.LoggingClient.Debug(fmt.Sprintf("Tags added to Event. Event tags=%v", event.Tags)) | ||
} else { | ||
edgexcontext.LoggingClient.Debug("No tags added to Event. Add tags list is empty.") | ||
} | ||
|
||
return true, event | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// Copyright (c) 2020 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package transforms | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger" | ||
"github.com/edgexfoundry/go-mod-core-contracts/models" | ||
|
||
"github.com/edgexfoundry/app-functions-sdk-go/appcontext" | ||
) | ||
|
||
var tagsToAdd = map[string]string{ | ||
"GatewayId": "HoustonStore000123", | ||
"Latitude": "29.630771", | ||
"Longitude": "-95.377603", | ||
} | ||
|
||
var eventWithExistingTags = models.Event{ | ||
Tags: map[string]string{ | ||
"Tag1": "Value1", | ||
"Tag2": "Value2", | ||
}, | ||
} | ||
|
||
var allTagsAdded = map[string]string{ | ||
"Tag1": "Value1", | ||
"Tag2": "Value2", | ||
"GatewayId": "HoustonStore000123", | ||
"Latitude": "29.630771", | ||
"Longitude": "-95.377603", | ||
} | ||
|
||
func TestTags_AddTags(t *testing.T) { | ||
appContext := appcontext.Context{ | ||
LoggingClient: logger.NewClientStdOut("Unit Test", false, "DEBUG"), | ||
} | ||
|
||
tests := []struct { | ||
Name string | ||
FunctionInput interface{} | ||
TagsToAdd map[string]string | ||
Expected map[string]string | ||
ErrorExpected bool | ||
ErrorContains string | ||
}{ | ||
{"Happy path - no existing Event tags", models.Event{}, tagsToAdd, tagsToAdd, false, ""}, | ||
{"Happy path - Event has existing tags", eventWithExistingTags, tagsToAdd, allTagsAdded, false, ""}, | ||
{"Happy path - No tags added", eventWithExistingTags, map[string]string{}, eventWithExistingTags.Tags, false, ""}, | ||
{"Error - No data", nil, nil, nil, true, "no Event Received"}, | ||
{"Error - Input not event", "Not an Event", nil, nil, true, "not an Event"}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
var continuePipeline bool | ||
var result interface{} | ||
|
||
target := NewTags(testCase.TagsToAdd) | ||
|
||
if testCase.FunctionInput != nil { | ||
continuePipeline, result = target.AddTags(&appContext, testCase.FunctionInput) | ||
} else { | ||
continuePipeline, result = target.AddTags(&appContext) | ||
} | ||
|
||
if testCase.ErrorExpected { | ||
err := result.(error) | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), testCase.ErrorContains) | ||
require.False(t, continuePipeline) | ||
return // Test completed | ||
} | ||
|
||
assert.True(t, continuePipeline) | ||
actual, ok := result.(models.Event) | ||
require.True(t, ok, "Result not an Event") | ||
assert.Equal(t, testCase.Expected, actual.Tags) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters