Skip to content

Commit

Permalink
refactor: Update code for latest core contracts V1 removal (#892)
Browse files Browse the repository at this point in the history
* refactor: Update code for latest core contracts V1 removal

Updated to all latest go-mods
Refactored code for new import paths and removed v1 code
Refactored Version check to use new V2 Common Client.

closes #885

Signed-off-by: lenny <[email protected]>
  • Loading branch information
Lenny Goodell authored Jun 10, 2021
1 parent 4c3c66c commit 5506df4
Show file tree
Hide file tree
Showing 48 changed files with 401 additions and 414 deletions.
10 changes: 4 additions & 6 deletions app-service-template/functions/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import (
"errors"
"strings"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"

"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
)

// TODO: Create your custom type and function(s) and remove these samples
Expand Down Expand Up @@ -61,7 +59,7 @@ func (s *Sample) LogEventDetails(ctx interfaces.AppFunctionContext, data interfa
len(event.Readings))
for index, reading := range event.Readings {
switch strings.ToLower(reading.ValueType) {
case strings.ToLower(v2.ValueTypeBinary):
case strings.ToLower(common.ValueTypeBinary):
lc.Infof(
"Reading #%d received with ID=%s, Resource=%s, ValueType=%s, MediaType=%s and BinaryValue of size=`%d`",
index+1,
Expand Down Expand Up @@ -134,7 +132,7 @@ func (s *Sample) OutputXML(ctx interfaces.AppFunctionContext, data interface{})
// HTTP response to for the HTTP Trigger
// For more details on the SetResponseData() function go here: https://docs.edgexfoundry.org/1.3/microservices/application/ContextAPI/#complete
ctx.SetResponseData([]byte(xml))
ctx.SetResponseContentType(clients.ContentTypeXML)
ctx.SetResponseContentType(common.ContentTypeXML)

// Returning false terminates the pipeline execution, so this should be last function specified in the pipeline,
// which is typical in conjunction with usage of .SetResponseData() function.
Expand Down
15 changes: 7 additions & 8 deletions app-service-template/functions/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ package functions
import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"
"github.com/stretchr/testify/require"

"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// This file contains example of how to unit test pipeline functions
Expand Down Expand Up @@ -79,7 +78,7 @@ func TestSample_OutputXML(t *testing.T) {
testEvent := createTestEvent(t)
xml, _ := testEvent.ToXML()
expectedContinuePipeline := false
expectedContentType := clients.ContentTypeXML
expectedContentType := common.ContentTypeXML

target := NewSample()
actualContinuePipeline, result := target.OutputXML(appContext, xml)
Expand All @@ -97,7 +96,7 @@ func createTestEvent(t *testing.T) dtos.Event {
resourceName := "MyResource"

event := dtos.NewEvent(profileName, deviceName, sourceName)
err := event.AddSimpleReading(resourceName, v2.ValueTypeInt32, int32(1234))
err := event.AddSimpleReading(resourceName, common.ValueTypeInt32, int32(1234))
require.NoError(t, err)

event.Tags = map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion app-service-template/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ go 1.15

require (
github.com/edgexfoundry/app-functions-sdk-go/v2 v2.0.0-dev.52
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.98
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.100
github.com/google/uuid v1.2.0
github.com/stretchr/testify v1.7.0
)
Expand Down
6 changes: 3 additions & 3 deletions app-service-template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"os"
"reflect"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"new-app-service/config"
"new-app-service/functions"

"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/transforms"
"new-app-service/config"

"new-app-service/functions"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
)

const (
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ require (
bitbucket.org/bertimus9/systemstat v0.0.0-20180207000608-0eeff89b0690
github.com/diegoholiveira/jsonlogic v1.0.1-0.20200220175622-ab7989be08b9
github.com/eclipse/paho.mqtt.golang v1.3.5
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.0.0-dev.61
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.98
github.com/edgexfoundry/go-mod-messaging/v2 v2.0.0-dev.13
github.com/edgexfoundry/go-mod-bootstrap/v2 v2.0.0-dev.63
github.com/edgexfoundry/go-mod-core-contracts/v2 v2.0.0-dev.100
github.com/edgexfoundry/go-mod-messaging/v2 v2.0.0-dev.16
github.com/edgexfoundry/go-mod-registry/v2 v2.0.0-dev.7
github.com/fxamacker/cbor/v2 v2.2.0
github.com/gomodule/redigo v2.0.0+incompatible
Expand Down
6 changes: 3 additions & 3 deletions internal/app/configurable.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/transforms"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/util"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
)

const (
Expand Down Expand Up @@ -222,13 +222,13 @@ func (app *Configurable) PushToCore(parameters map[string]string) interfaces.App
var transform *transforms.CoreData

// Converts to upper case and validates it is a validates ValueType
valueType, err := v2.NormalizeValueType(valueType)
valueType, err := common.NormalizeValueType(valueType)
if err != nil {
app.lc.Error(err.Error())
return nil
}

if valueType == v2.ValueTypeBinary {
if valueType == common.ValueTypeBinary {
mediaType, ok := parameters[MediaType]
if !ok {
app.lc.Error("Could not find " + MediaType)
Expand Down
5 changes: 3 additions & 2 deletions internal/app/configurable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
"net/http"
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -154,7 +155,7 @@ func TestHTTPExport(t *testing.T) {
configurable := Configurable{lc: lc}

testUrl := "http://url"
testMimeType := clients.ContentTypeJSON
testMimeType := common.ContentTypeJSON
testPersistOnError := "false"
testBadPersistOnError := "bogus"
testContinueOnSendError := "true"
Expand Down
21 changes: 10 additions & 11 deletions internal/app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ import (
"sync"
"syscall"

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/models"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
clientInterfaces "github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/interfaces"
"github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
"github.com/edgexfoundry/go-mod-registry/v2/registry"

"github.com/edgexfoundry/app-functions-sdk-go/v2/internal"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/container"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/handlers"
Expand All @@ -51,6 +44,12 @@ import (
bootstrapInterfaces "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
clientInterfaces "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
commonConstants "github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/models"
"github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
"github.com/edgexfoundry/go-mod-registry/v2/registry"

"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -108,10 +107,10 @@ type contextGroup struct {

// AddRoute allows you to leverage the existing webserver to add routes.
func (svc *Service) AddRoute(route string, handler func(nethttp.ResponseWriter, *nethttp.Request), methods ...string) error {
if route == v2.ApiPingRoute ||
route == v2.ApiConfigRoute ||
route == v2.ApiMetricsRoute ||
route == v2.ApiVersionRoute ||
if route == commonConstants.ApiPingRoute ||
route == commonConstants.ApiConfigRoute ||
route == commonConstants.ApiMetricsRoute ||
route == commonConstants.ApiVersionRoute ||
route == internal.ApiTriggerRoute {
return errors.New("route is reserved")
}
Expand Down
16 changes: 8 additions & 8 deletions internal/app/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/trigger/messagebus"
"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/webserver"
"github.com/edgexfoundry/app-functions-sdk-go/v2/pkg/interfaces"
v2clients "github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http"

bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
clients "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/http"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -566,7 +566,7 @@ func TestService_EventClient(t *testing.T) {

dic.Update(di.ServiceConstructorMap{
container.EventClientName: func(get di.Get) interface{} {
return v2clients.NewEventClient(baseUrl + "59880")
return clients.NewEventClient(baseUrl + "59880")
},
})

Expand All @@ -580,7 +580,7 @@ func TestService_CommandClient(t *testing.T) {

dic.Update(di.ServiceConstructorMap{
container.CommandClientName: func(get di.Get) interface{} {
return v2clients.NewCommandClient(baseUrl + "59882")
return clients.NewCommandClient(baseUrl + "59882")
},
})

Expand All @@ -594,7 +594,7 @@ func TestService_DeviceServiceClient(t *testing.T) {

dic.Update(di.ServiceConstructorMap{
container.DeviceServiceClientName: func(get di.Get) interface{} {
return v2clients.NewDeviceServiceClient(baseUrl + "59881")
return clients.NewDeviceServiceClient(baseUrl + "59881")
},
})

Expand All @@ -609,7 +609,7 @@ func TestService_DeviceProfileClient(t *testing.T) {

dic.Update(di.ServiceConstructorMap{
container.DeviceProfileClientName: func(get di.Get) interface{} {
return v2clients.NewDeviceProfileClient(baseUrl + "59881")
return clients.NewDeviceProfileClient(baseUrl + "59881")
},
})

Expand All @@ -623,7 +623,7 @@ func TestService_DeviceClient(t *testing.T) {

dic.Update(di.ServiceConstructorMap{
container.DeviceClientName: func(get di.Get) interface{} {
return v2clients.NewDeviceClient(baseUrl + "59881")
return clients.NewDeviceClient(baseUrl + "59881")
},
})

Expand All @@ -638,7 +638,7 @@ func TestService_NotificationClient(t *testing.T) {

dic.Update(di.ServiceConstructorMap{
container.NotificationClientName: func(get di.Get) interface{} {
return v2clients.NewNotificationClient(baseUrl + "59860")
return clients.NewNotificationClient(baseUrl + "59860")
},
})

Expand All @@ -653,7 +653,7 @@ func TestService_SubscriptionClient(t *testing.T) {

dic.Update(di.ServiceConstructorMap{
container.SubscriptionClientName: func(get di.Get) interface{} {
return v2clients.NewSubscriptionClient(baseUrl + "59860")
return clients.NewSubscriptionClient(baseUrl + "59860")
},
})

Expand Down
12 changes: 6 additions & 6 deletions internal/appfunction/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
"strings"
"time"

"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/container"
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"

"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/requests"

"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/container"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/requests"
)

// NewContext creates, initializes and return a new Context with implements the interfaces.AppFunctionContext interface
Expand Down
27 changes: 13 additions & 14 deletions internal/appfunction/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ import (
"time"

"github.com/edgexfoundry/app-functions-sdk-go/v2/internal/bootstrap/container"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/responses"

bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"

"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces/mocks"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
v2clients "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/http"
clientMocks "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/interfaces/mocks"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
v2clients "github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/http"
clientMocks "github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/interfaces/mocks"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
commonDtos "github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/responses"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -183,7 +182,7 @@ func TestContext_SetCorrelationID(t *testing.T) {
}

func TestContext_InputContentType(t *testing.T) {
expected := clients.ContentTypeXML
expected := common.ContentTypeXML
target.inputContentType = expected

actual := target.InputContentType()
Expand All @@ -192,7 +191,7 @@ func TestContext_InputContentType(t *testing.T) {
}

func TestContext_SetInputContentType(t *testing.T) {
expected := clients.ContentTypeCBOR
expected := common.ContentTypeCBOR

target.SetInputContentType(expected)
actual := target.inputContentType
Expand All @@ -201,7 +200,7 @@ func TestContext_SetInputContentType(t *testing.T) {
}

func TestContext_ResponseContentType(t *testing.T) {
expected := clients.ContentTypeJSON
expected := common.ContentTypeJSON
target.responseContentType = expected

actual := target.ResponseContentType()
Expand All @@ -210,7 +209,7 @@ func TestContext_ResponseContentType(t *testing.T) {
}

func TestContext_SetResponseContentType(t *testing.T) {
expected := clients.ContentTypeText
expected := common.ContentTypeText

target.SetResponseContentType(expected)
actual := target.responseContentType
Expand Down Expand Up @@ -415,15 +414,15 @@ func TestContext_ApplyValues_MissingPlaceholder(t *testing.T) {

func TestContext_PushToCore(t *testing.T) {
mockClient := clientMocks.EventClient{}
mockClient.On("Add", mock.Anything, mock.Anything).Return(common.BaseWithIdResponse{}, nil)
mockClient.On("Add", mock.Anything, mock.Anything).Return(commonDtos.BaseWithIdResponse{}, nil)
dic.Update(di.ServiceConstructorMap{
container.EventClientName: func(get di.Get) interface{} {
return &mockClient
},
})

event := dtos.NewEvent("MyProfile", "MyDevice", "MyResource")
err := event.AddSimpleReading("MyResource", v2.ValueTypeInt32, int32(1234))
err := event.AddSimpleReading("MyResource", common.ValueTypeInt32, int32(1234))
require.NoError(t, err)

_, err = target.PushToCore(event)
Expand Down
2 changes: 1 addition & 1 deletion internal/bootstrap/container/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package container

import (
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/clients/interfaces"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/interfaces"
)

// EventClientName contains the name of the EventClient's implementation in the DIC.
Expand Down
Loading

0 comments on commit 5506df4

Please sign in to comment.