|  | 
|  | 1 | +// | 
|  | 2 | +// Copyright (c) 2020 Intel Corporation | 
|  | 3 | +// | 
|  | 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +// you may not use this file except in compliance with the License. | 
|  | 6 | +// You may obtain a copy of the License at | 
|  | 7 | +// | 
|  | 8 | +//      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +// | 
|  | 10 | +// Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +// distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +// See the License for the specific language governing permissions and | 
|  | 14 | +// limitations under the License. | 
|  | 15 | +// | 
|  | 16 | + | 
|  | 17 | +package transforms | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"testing" | 
|  | 21 | + | 
|  | 22 | +	"github.com/stretchr/testify/assert" | 
|  | 23 | +	"github.com/stretchr/testify/require" | 
|  | 24 | + | 
|  | 25 | +	"github.com/edgexfoundry/go-mod-core-contracts/clients/logger" | 
|  | 26 | +	"github.com/edgexfoundry/go-mod-core-contracts/models" | 
|  | 27 | + | 
|  | 28 | +	"github.com/edgexfoundry/app-functions-sdk-go/appcontext" | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | +var tagsToAdd = map[string]string{ | 
|  | 32 | +	"GatewayId": "HoustonStore000123", | 
|  | 33 | +	"Latitude":  "29.630771", | 
|  | 34 | +	"Longitude": "-95.377603", | 
|  | 35 | +} | 
|  | 36 | + | 
|  | 37 | +var eventWithExistingTags = models.Event{ | 
|  | 38 | +	Tags: map[string]string{ | 
|  | 39 | +		"Tag1": "Value1", | 
|  | 40 | +		"Tag2": "Value2", | 
|  | 41 | +	}, | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +var allTagsAdded = map[string]string{ | 
|  | 45 | +	"Tag1":      "Value1", | 
|  | 46 | +	"Tag2":      "Value2", | 
|  | 47 | +	"GatewayId": "HoustonStore000123", | 
|  | 48 | +	"Latitude":  "29.630771", | 
|  | 49 | +	"Longitude": "-95.377603", | 
|  | 50 | +} | 
|  | 51 | + | 
|  | 52 | +func TestTags_AddTags(t *testing.T) { | 
|  | 53 | +	appContext := appcontext.Context{ | 
|  | 54 | +		LoggingClient: logger.NewClientStdOut("Unit Test", false, "DEBUG"), | 
|  | 55 | +	} | 
|  | 56 | + | 
|  | 57 | +	tests := []struct { | 
|  | 58 | +		Name          string | 
|  | 59 | +		FunctionInput interface{} | 
|  | 60 | +		TagsToAdd     map[string]string | 
|  | 61 | +		Expected      map[string]string | 
|  | 62 | +		ErrorExpected bool | 
|  | 63 | +		ErrorContains string | 
|  | 64 | +	}{ | 
|  | 65 | +		{"Happy path - no existing Event tags", models.Event{}, tagsToAdd, tagsToAdd, false, ""}, | 
|  | 66 | +		{"Happy path - Event has existing tags", eventWithExistingTags, tagsToAdd, allTagsAdded, false, ""}, | 
|  | 67 | +		{"Happy path - No tags added", eventWithExistingTags, map[string]string{}, eventWithExistingTags.Tags, false, ""}, | 
|  | 68 | +		{"Error - No data", nil, nil, nil, true, "no Event Received"}, | 
|  | 69 | +		{"Error - Input not event", "Not an Event", nil, nil, true, "not an Event"}, | 
|  | 70 | +	} | 
|  | 71 | + | 
|  | 72 | +	for _, testCase := range tests { | 
|  | 73 | +		t.Run(testCase.Name, func(t *testing.T) { | 
|  | 74 | +			var continuePipeline bool | 
|  | 75 | +			var result interface{} | 
|  | 76 | + | 
|  | 77 | +			target := NewTags(testCase.TagsToAdd) | 
|  | 78 | + | 
|  | 79 | +			if testCase.FunctionInput != nil { | 
|  | 80 | +				continuePipeline, result = target.AddTags(&appContext, testCase.FunctionInput) | 
|  | 81 | +			} else { | 
|  | 82 | +				continuePipeline, result = target.AddTags(&appContext) | 
|  | 83 | +			} | 
|  | 84 | + | 
|  | 85 | +			if testCase.ErrorExpected { | 
|  | 86 | +				err := result.(error) | 
|  | 87 | +				require.Error(t, err) | 
|  | 88 | +				assert.Contains(t, err.Error(), testCase.ErrorContains) | 
|  | 89 | +				require.False(t, continuePipeline) | 
|  | 90 | +				return // Test completed | 
|  | 91 | +			} | 
|  | 92 | + | 
|  | 93 | +			assert.True(t, continuePipeline) | 
|  | 94 | +			actual, ok := result.(models.Event) | 
|  | 95 | +			require.True(t, ok, "Result not an Event") | 
|  | 96 | +			assert.Equal(t, testCase.Expected, actual.Tags) | 
|  | 97 | +		}) | 
|  | 98 | +	} | 
|  | 99 | +} | 
0 commit comments