From 5010e33b24fe9de6cf1d5b7bcf9ce8cc545c7351 Mon Sep 17 00:00:00 2001 From: Brandon Forster Date: Sat, 1 Feb 2020 11:55:46 -0600 Subject: [PATCH] #204: Unit tests pass. Signed-off-by: Brandon Forster --- clients/command/client_test.go | 59 +--------- clients/coredata/event_test.go | 73 +------------ clients/coredata/reading.go | 75 ++++++------- clients/coredata/reading_test.go | 23 +--- clients/coredata/value_descriptor_test.go | 58 ++-------- clients/general/client_test.go | 39 +------ clients/metadata/addressable_test.go | 70 ++---------- clients/metadata/command_test.go | 12 +- clients/metadata/device.go | 102 ++++++++--------- clients/metadata/device_profile_test.go | 22 +--- clients/metadata/device_service_test.go | 10 +- clients/metadata/device_test.go | 35 +----- clients/metadata/mocks/DeviceClient.go | 38 +++---- clients/metadata/provision_watcher_test.go | 24 +--- clients/notifications/client_test.go | 28 +---- clients/scheduler/interval_action_test.go | 121 ++++----------------- clients/scheduler/interval_test.go | 117 ++++---------------- clients/urlclient/local.go | 4 +- clients/urlclient/local_test.go | 6 +- clients/urlclient/registry_test.go | 4 +- dummy.go | 28 ----- 21 files changed, 226 insertions(+), 722 deletions(-) delete mode 100644 dummy.go diff --git a/clients/command/client_test.go b/clients/command/client_test.go index fff26c82..a316bb70 100644 --- a/clients/command/client_test.go +++ b/clients/command/client_test.go @@ -21,7 +21,7 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" ) func TestGetDeviceCommandById(t *testing.T) { @@ -29,17 +29,7 @@ func TestGetDeviceCommandById(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiDeviceRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreCommandServiceKey, - Path: clients.ApiDeviceRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - cc := NewCommandClient(params, MockEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + cc := NewCommandClient(urlclient.NewLocalClient(ts.URL + clients.ApiDeviceRoute)) res, _ := cc.Get(context.Background(), "device1", "command1") @@ -53,17 +43,7 @@ func TestPutDeviceCommandById(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiDeviceRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreCommandServiceKey, - Path: clients.ApiDeviceRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - cc := NewCommandClient(params, MockEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + cc := NewCommandClient(urlclient.NewLocalClient(ts.URL + clients.ApiDeviceRoute)) res, _ := cc.Put(context.Background(), "device1", "command1", "body") @@ -77,17 +57,7 @@ func TestGetDeviceByName(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiDeviceRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreCommandServiceKey, - Path: clients.ApiDeviceRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - cc := NewCommandClient(params, MockEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + cc := NewCommandClient(urlclient.NewLocalClient(ts.URL + clients.ApiDeviceRoute)) res, _ := cc.GetDeviceCommandByNames(context.Background(), "device1", "command1") @@ -101,17 +71,7 @@ func TestPutDeviceCommandByNames(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiDeviceRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreCommandServiceKey, - Path: clients.ApiDeviceRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - cc := NewCommandClient(params, MockEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + cc := NewCommandClient(urlclient.NewLocalClient(ts.URL + clients.ApiDeviceRoute)) res, _ := cc.PutDeviceCommandByNames(context.Background(), "device1", "command1", "body") @@ -120,20 +80,13 @@ func TestPutDeviceCommandByNames(t *testing.T) { } } -type MockEndpoint struct { -} - -func (e MockEndpoint) Monitor(params types.EndpointParams) chan string { - return make(chan string, 1) -} - // testHttpServer instantiates a test HTTP Server to be used for conveniently verifying a client's invocation func testHttpServer(t *testing.T, matchingRequestMethod string, matchingRequestUri string) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) if r.Method == matchingRequestMethod && r.RequestURI == matchingRequestUri { - w.Write([]byte("Ok")) + _, _ = w.Write([]byte("Ok")) } else { t.Errorf("expected endpoint %s to be invoked by client, %s invoked", matchingRequestUri, r.RequestURI) } diff --git a/clients/coredata/event_test.go b/clients/coredata/event_test.go index 64ae4e1c..b76c1409 100644 --- a/clients/coredata/event_test.go +++ b/clients/coredata/event_test.go @@ -19,13 +19,12 @@ package coredata import ( "context" "encoding/json" - "fmt" "net/http" "net/http/httptest" "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" "github.com/ugorji/go/codec" @@ -57,16 +56,7 @@ func TestMarkPushed(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiEventRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiEventRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - - ec := NewEventClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ec := NewEventClient(urlclient.NewLocalClient(ts.URL + clients.ApiEventRoute)) err := ec.MarkPushed(TestId, context.Background()) @@ -92,16 +82,7 @@ func TestMarkPushedByChecksum(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiEventRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiEventRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - - ec := NewEventClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ec := NewEventClient(urlclient.NewLocalClient(ts.URL + clients.ApiEventRoute)) err := ec.MarkPushedByChecksum(TestChecksum, context.Background()) @@ -122,7 +103,7 @@ func TestGetEvents(t *testing.T) { t.Errorf("expected uri path is %s, actual uri path is %s", clients.ApiEventRoute, r.URL.EscapedPath()) } - w.Write([]byte("[" + + _, _ = w.Write([]byte("[" + "{" + "\"Device\" : \"" + TestEventDevice1 + "\"" + "}," + @@ -135,16 +116,7 @@ func TestGetEvents(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiEventRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiEventRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - - ec := NewEventClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ec := NewEventClient(urlclient.NewLocalClient(ts.URL + clients.ApiEventRoute)) eArr, err := ec.Events(context.Background()) if err != nil { @@ -166,31 +138,6 @@ func TestGetEvents(t *testing.T) { } } -func TestNewEventClientWithConsul(t *testing.T) { - deviceUrl := "http://localhost:48080" + clients.ApiEventRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiEventRoute, - UseRegistry: true, - Url: deviceUrl, - Interval: clients.ClientMonitorDefault} - - ec := NewEventClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) - - r, ok := ec.(*eventRestClient) - if !ok { - t.Error("ec is not of expected type") - } - - url, err := r.urlClient.Prefix() - - if err != nil { - t.Error("url was not initialized") - } else if url != deviceUrl { - t.Errorf("unexpected url value %s", url) - } -} - func TestMarshalEvent(t *testing.T) { var eventResult models.Event binaryEvent := testEvent @@ -199,7 +146,7 @@ func TestMarshalEvent(t *testing.T) { regularEvent := testEvent regularEvent.Readings = append(regularEvent.Readings, testReading) - client := NewEventClient(types.EndpointParams{Url: "test"}, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + client := NewEventClient(urlclient.NewLocalClient("test")) tests := []struct { name string @@ -236,11 +183,3 @@ func TestMarshalEvent(t *testing.T) { }) } } - -type mockCoreDataEndpoint struct{} - -func (e mockCoreDataEndpoint) Monitor(params types.EndpointParams) chan string { - ch := make(chan string, 1) - ch <- fmt.Sprintf("http://%s:%v%s", "localhost", 48080, params.Path) - return ch -} diff --git a/clients/coredata/reading.go b/clients/coredata/reading.go index 9dcd81fa..082e9599 100644 --- a/clients/coredata/reading.go +++ b/clients/coredata/reading.go @@ -35,25 +35,25 @@ type ReadingClient interface { // ReadingCount returns a count of the total readings ReadingCount(ctx context.Context) (int, error) // Reading returns a reading by its id - Reading(id string, ctx context.Context) (models.Reading, error) + Reading(ctx context.Context, id string) (models.Reading, error) // ReadingsForDevice returns readings up to a specified limit for a given device - ReadingsForDevice(deviceId string, limit int, ctx context.Context) ([]models.Reading, error) + ReadingsForDevice(ctx context.Context, deviceId string, limit int) ([]models.Reading, error) // ReadingsForNameAndDevice returns readings up to a specified limit for a given device and value descriptor name - ReadingsForNameAndDevice(name string, deviceId string, limit int, ctx context.Context) ([]models.Reading, error) + ReadingsForNameAndDevice(ctx context.Context, name string, deviceId string, limit int) ([]models.Reading, error) // ReadingsForName returns readings up to a specified limit for a given value descriptor name - ReadingsForName(name string, limit int, ctx context.Context) ([]models.Reading, error) + ReadingsForName(ctx context.Context, name string, limit int) ([]models.Reading, error) // ReadingsForUOMLabel returns readings up to a specified limit for a given UOM label - ReadingsForUOMLabel(uomLabel string, limit int, ctx context.Context) ([]models.Reading, error) + ReadingsForUOMLabel(ctx context.Context, uomLabel string, limit int) ([]models.Reading, error) // ReadingsForLabel returns readings up to a specified limit for a given label - ReadingsForLabel(label string, limit int, ctx context.Context) ([]models.Reading, error) + ReadingsForLabel(ctx context.Context, label string, limit int) ([]models.Reading, error) // ReadingsForType returns readings up to a specified limit of a given type - ReadingsForType(readingType string, limit int, ctx context.Context) ([]models.Reading, error) + ReadingsForType(ctx context.Context, readingType string, limit int) ([]models.Reading, error) // ReadingsForInterval returns readings up to a specified limit generated within a specific time period ReadingsForInterval(start int, end int, limit int, ctx context.Context) ([]models.Reading, error) // Add a new reading Add(readiing *models.Reading, ctx context.Context) (string, error) // Delete eliminates a reading by its id - Delete(id string, ctx context.Context) error + Delete(ctx context.Context, id string) error } type readingRestClient struct { @@ -68,7 +68,7 @@ func NewReadingClient(urlClient interfaces.URLClient) ReadingClient { } // Helper method to request and decode a reading slice -func (r *readingRestClient) requestReadingSlice(urlSuffix string, ctx context.Context) ([]models.Reading, error) { +func (r *readingRestClient) requestReadingSlice(ctx context.Context, urlSuffix string) ([]models.Reading, error) { data, err := clients.GetRequest(ctx, urlSuffix, r.urlClient) if err != nil { return []models.Reading{}, err @@ -80,7 +80,7 @@ func (r *readingRestClient) requestReadingSlice(urlSuffix string, ctx context.Co } // Helper method to request and decode a reading -func (r *readingRestClient) requestReading(urlSuffix string, ctx context.Context) (models.Reading, error) { +func (r *readingRestClient) requestReading(ctx context.Context, urlSuffix string) (models.Reading, error) { data, err := clients.GetRequest(ctx, urlSuffix, r.urlClient) if err != nil { return models.Reading{}, err @@ -92,11 +92,11 @@ func (r *readingRestClient) requestReading(urlSuffix string, ctx context.Context } func (r *readingRestClient) Readings(ctx context.Context) ([]models.Reading, error) { - return r.requestReadingSlice("", ctx) + return r.requestReadingSlice(ctx, "") } -func (r *readingRestClient) Reading(id string, ctx context.Context) (models.Reading, error) { - return r.requestReading("/"+id, ctx) +func (r *readingRestClient) Reading(ctx context.Context, id string) (models.Reading, error) { + return r.requestReading(ctx, "/"+id) } func (r *readingRestClient) ReadingCount(ctx context.Context) (int, error) { @@ -104,51 +104,48 @@ func (r *readingRestClient) ReadingCount(ctx context.Context) (int, error) { } func (r *readingRestClient) ReadingsForDevice( + ctx context.Context, deviceId string, - limit int, - ctx context.Context) ([]models.Reading, error) { + limit int) ([]models.Reading, error) { - return r.requestReadingSlice("/device/"+url.QueryEscape(deviceId)+"/"+strconv.Itoa(limit), ctx) + return r.requestReadingSlice(ctx, "/device/"+url.QueryEscape(deviceId)+"/"+strconv.Itoa(limit)) } func (r *readingRestClient) ReadingsForNameAndDevice( + ctx context.Context, name string, deviceId string, - limit int, - ctx context.Context) ([]models.Reading, error) { + limit int) ([]models.Reading, error) { - return r.requestReadingSlice( - "/name/"+ - url.QueryEscape(name)+ - "/device/"+ - url.QueryEscape(deviceId)+ - "/"+strconv.Itoa(limit), - ctx, - ) + return r.requestReadingSlice(ctx, "/name/"+ + url.QueryEscape(name)+ + "/device/"+ + url.QueryEscape(deviceId)+ + "/"+strconv.Itoa(limit), ) } -func (r *readingRestClient) ReadingsForName(name string, limit int, ctx context.Context) ([]models.Reading, error) { - return r.requestReadingSlice("/name/"+url.QueryEscape(name)+"/"+strconv.Itoa(limit), ctx) +func (r *readingRestClient) ReadingsForName(ctx context.Context, name string, limit int) ([]models.Reading, error) { + return r.requestReadingSlice(ctx, "/name/"+url.QueryEscape(name)+"/"+strconv.Itoa(limit)) } func (r *readingRestClient) ReadingsForUOMLabel( + ctx context.Context, uomLabel string, - limit int, - ctx context.Context) ([]models.Reading, error) { + limit int) ([]models.Reading, error) { - return r.requestReadingSlice("/uomlabel/"+url.QueryEscape(uomLabel)+"/"+strconv.Itoa(limit), ctx) + return r.requestReadingSlice(ctx, "/uomlabel/"+url.QueryEscape(uomLabel)+"/"+strconv.Itoa(limit)) } -func (r *readingRestClient) ReadingsForLabel(label string, limit int, ctx context.Context) ([]models.Reading, error) { - return r.requestReadingSlice("/label/"+url.QueryEscape(label)+"/"+strconv.Itoa(limit), ctx) +func (r *readingRestClient) ReadingsForLabel(ctx context.Context, label string, limit int) ([]models.Reading, error) { + return r.requestReadingSlice(ctx, "/label/"+url.QueryEscape(label)+"/"+strconv.Itoa(limit)) } func (r *readingRestClient) ReadingsForType( + ctx context.Context, readingType string, - limit int, - ctx context.Context) ([]models.Reading, error) { + limit int) ([]models.Reading, error) { - return r.requestReadingSlice("/type/"+url.QueryEscape(readingType)+"/"+strconv.Itoa(limit), ctx) + return r.requestReadingSlice(ctx, "/type/"+url.QueryEscape(readingType)+"/"+strconv.Itoa(limit)) } func (r *readingRestClient) ReadingsForInterval( @@ -157,13 +154,13 @@ func (r *readingRestClient) ReadingsForInterval( limit int, ctx context.Context) ([]models.Reading, error) { - return r.requestReadingSlice("/"+strconv.Itoa(start)+"/"+strconv.Itoa(end)+"/"+strconv.Itoa(limit), ctx) + return r.requestReadingSlice(ctx, "/"+strconv.Itoa(start)+"/"+strconv.Itoa(end)+"/"+strconv.Itoa(limit)) } func (r *readingRestClient) Add(reading *models.Reading, ctx context.Context) (string, error) { return clients.PostJsonRequest("", reading, ctx, r.urlClient) } -func (r *readingRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest("/id/"+id, ctx, r.urlClient) +func (r *readingRestClient) Delete(ctx context.Context, id string) error { + return clients.DeleteRequest(ctx, "/id/"+id, r.urlClient) } diff --git a/clients/coredata/reading_test.go b/clients/coredata/reading_test.go index 57e6d116..1c340c4f 100644 --- a/clients/coredata/reading_test.go +++ b/clients/coredata/reading_test.go @@ -24,7 +24,7 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) @@ -67,16 +67,7 @@ func TestGetReadings(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiReadingRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiReadingRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - - rc := NewReadingClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + rc := NewReadingClient(urlclient.NewLocalClient(ts.URL + clients.ApiReadingRoute)) rArr, err := rc.Readings(context.Background()) if err != nil { @@ -101,14 +92,8 @@ func TestGetReadings(t *testing.T) { func TestNewReadingClientWithConsul(t *testing.T) { deviceUrl := "http://localhost:48080" + clients.ApiReadingRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiReadingRoute, - UseRegistry: true, - Url: deviceUrl, - Interval: clients.ClientMonitorDefault} - - rc := NewReadingClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + + rc := NewReadingClient(urlclient.NewLocalClient(deviceUrl)) r, ok := rc.(*readingRestClient) if !ok { diff --git a/clients/coredata/value_descriptor_test.go b/clients/coredata/value_descriptor_test.go index e04094e5..70888cfa 100644 --- a/clients/coredata/value_descriptor_test.go +++ b/clients/coredata/value_descriptor_test.go @@ -23,7 +23,7 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) @@ -65,22 +65,13 @@ func TestGetvaluedescriptors(t *testing.T) { if err != nil { t.Errorf("marshaling error: %s", err.Error()) } - w.Write(data) + _, _ = w.Write(data) })) defer ts.Close() - url := ts.URL + clients.ApiValueDescriptorRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiValueDescriptorRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - - vdc := NewValueDescriptorClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + vdc := NewValueDescriptorClient(urlclient.NewLocalClient(ts.URL + clients.ApiValueDescriptorRoute)) vdArr, err := vdc.ValueDescriptors(context.Background()) if err != nil { @@ -119,21 +110,12 @@ func TestValueDescriptorUsage(t *testing.T) { if err != nil { t.Errorf("marshaling error: %s", err.Error()) } - w.Write(data) + _, _ = w.Write(data) })) defer ts.Close() - url := ts.URL + clients.ApiValueDescriptorRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiValueDescriptorRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - - vdc := NewValueDescriptorClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + vdc := NewValueDescriptorClient(urlclient.NewLocalClient(ts.URL + clients.ApiValueDescriptorRoute)) usage, err := vdc.ValueDescriptorsUsage([]string{testValueDesciptorDescription1, testValueDesciptorDescription2}, context.Background()) if err != nil { t.Errorf(err.Error()) @@ -151,16 +133,7 @@ func TestValueDescriptorUsageSerializationError(t *testing.T) { })) defer ts.Close() - url := ts.URL + clients.ApiValueDescriptorRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiValueDescriptorRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - - vdc := NewValueDescriptorClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + vdc := NewValueDescriptorClient(urlclient.NewLocalClient(ts.URL + clients.ApiValueDescriptorRoute)) _, err := vdc.ValueDescriptorsUsage([]string{testValueDesciptorDescription1, testValueDesciptorDescription2}, context.Background()) if err == nil { t.Error("Expected an error") @@ -169,14 +142,7 @@ func TestValueDescriptorUsageSerializationError(t *testing.T) { } func TestValueDescriptorUsageGetRequestError(t *testing.T) { - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiValueDescriptorRoute, - UseRegistry: false, - Url: "!@#", - Interval: clients.ClientMonitorDefault} - - vdc := NewValueDescriptorClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + vdc := NewValueDescriptorClient(urlclient.NewLocalClient("!%&")) _, err := vdc.ValueDescriptorsUsage([]string{testValueDesciptorDescription1, testValueDesciptorDescription2}, context.Background()) if err == nil { t.Error("Expected an error") @@ -186,14 +152,8 @@ func TestValueDescriptorUsageGetRequestError(t *testing.T) { func TestNewValueDescriptorClientWithConsul(t *testing.T) { deviceUrl := "http://localhost:48080" + clients.ApiValueDescriptorRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreDataServiceKey, - Path: clients.ApiValueDescriptorRoute, - UseRegistry: true, - Url: deviceUrl, - Interval: clients.ClientMonitorDefault} - - vdc := NewValueDescriptorClient(params, mockCoreDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + + vdc := NewValueDescriptorClient(urlclient.NewLocalClient(deviceUrl)) r, ok := vdc.(*valueDescriptorRestClient) if !ok { diff --git a/clients/general/client_test.go b/clients/general/client_test.go index 7a2990ff..d44d0b23 100644 --- a/clients/general/client_test.go +++ b/clients/general/client_test.go @@ -22,25 +22,17 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" ) const ( TestUnexpectedMsgFormatStr = "unexpected result, active: '%s' but expected: '%s'" ) -type mockGeneralEndpoint struct { -} - -func (e mockGeneralEndpoint) Monitor(params types.EndpointParams) chan string { - return make(chan string, 1) -} - func TestGetConfig(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("{ 'status' : 'OK' }")) + _, _ = w.Write([]byte("{ 'status' : 'OK' }")) if r.Method != http.MethodGet { t.Errorf(TestUnexpectedMsgFormatStr, r.Method, http.MethodGet) } @@ -51,17 +43,7 @@ func TestGetConfig(t *testing.T) { defer ts.Close() - url := ts.URL - - params := types.EndpointParams{ - ServiceKey: clients.SystemManagementAgentServiceKey, - Path: "/", - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - mc := NewGeneralClient(params, mockGeneralEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + mc := NewGeneralClient(urlclient.NewLocalClient(ts.URL)) responseJSON, err := mc.FetchConfiguration(context.Background()) if err != nil { @@ -70,10 +52,9 @@ func TestGetConfig(t *testing.T) { } func TestGetMetrics(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("{ 'status' : 'OK' }")) + _, _ = w.Write([]byte("{ 'status' : 'OK' }")) if r.Method != http.MethodGet { t.Errorf(TestUnexpectedMsgFormatStr, r.Method, http.MethodGet) } @@ -84,17 +65,7 @@ func TestGetMetrics(t *testing.T) { defer ts.Close() - url := ts.URL - - params := types.EndpointParams{ - ServiceKey: clients.SystemManagementAgentServiceKey, - Path: "/", - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - mc := NewGeneralClient(params, mockGeneralEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + mc := NewGeneralClient(urlclient.NewLocalClient(ts.URL)) responseJSON, err := mc.FetchMetrics(context.Background()) if err != nil { diff --git a/clients/metadata/addressable_test.go b/clients/metadata/addressable_test.go index 3b77b552..94110a55 100644 --- a/clients/metadata/addressable_test.go +++ b/clients/metadata/addressable_test.go @@ -24,20 +24,14 @@ import ( "github.com/google/uuid" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) func TestNewAddressableClientWithConsul(t *testing.T) { addressableURL := "http://localhost:48081" + clients.ApiAddressableRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiAddressableRoute, - UseRegistry: true, - Url: addressableURL, - Interval: clients.ClientMonitorDefault} - ac := NewAddressableClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ac := NewAddressableClient(urlclient.NewLocalClient(addressableURL)) r, ok := ac.(*addressableRestClient) if !ok { @@ -73,21 +67,13 @@ func TestAddAddressable(t *testing.T) { t.Errorf("expected uri path is %s, actual uri path is %s", clients.ApiAddressableRoute, r.URL.EscapedPath()) } - w.Write([]byte(addingAddressableID)) + _, _ = w.Write([]byte(addingAddressableID)) })) defer ts.Close() - url := ts.URL + clients.ApiAddressableRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiAddressableRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - ac := NewAddressableClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) receivedAddressableID, err := ac.Add(&addressable, context.Background()) if err != nil { @@ -119,24 +105,16 @@ func TestGetAddressable(t *testing.T) { } w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(addressable) + _ = json.NewEncoder(w).Encode(addressable) })) defer ts.Close() - url := ts.URL + clients.ApiAddressableRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiAddressableRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - ac := NewAddressableClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) receivedAddressable, err := ac.Addressable(addressable.Id, context.Background()) if err != nil { - t.Error(err.Error()) + t.Fatal(err.Error()) } if receivedAddressable.String() != addressable.String() { @@ -164,24 +142,16 @@ func TestGetAddressableForName(t *testing.T) { } w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(addressable) + _ = json.NewEncoder(w).Encode(addressable) })) defer ts.Close() - url := ts.URL + clients.ApiAddressableRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiAddressableRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - ac := NewAddressableClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) receivedAddressable, err := ac.AddressableForName(addressable.Name, context.Background()) if err != nil { - t.Error(err.Error()) + t.Fatal(err.Error()) } if receivedAddressable.String() != addressable.String() { @@ -211,15 +181,7 @@ func TestUpdateAddressable(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiAddressableRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiAddressableRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - ac := NewAddressableClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) err := ac.Update(addressable, context.Background()) if err != nil { @@ -250,15 +212,7 @@ func TestDeleteAddressable(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiAddressableRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiAddressableRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - ac := NewAddressableClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + ac := NewAddressableClient(urlclient.NewLocalClient(ts.URL + clients.ApiAddressableRoute)) err := ac.Delete(addressable.Id, context.Background()) if err != nil { diff --git a/clients/metadata/command_test.go b/clients/metadata/command_test.go index 18c0b8b8..6da2bf3c 100644 --- a/clients/metadata/command_test.go +++ b/clients/metadata/command_test.go @@ -18,19 +18,13 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" ) func TestNewCommandClientWithConsul(t *testing.T) { deviceUrl := "http://localhost:48081" + clients.ApiCommandRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiCommandRoute, - UseRegistry: true, - Url: deviceUrl, - Interval: clients.ClientMonitorDefault} - - cc := NewCommandClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + + cc := NewCommandClient(urlclient.NewLocalClient(deviceUrl)) r, ok := cc.(*commandRestClient) if !ok { diff --git a/clients/metadata/device.go b/clients/metadata/device.go index 3c7d14b9..4bb5e8cf 100644 --- a/clients/metadata/device.go +++ b/clients/metadata/device.go @@ -28,47 +28,47 @@ import ( // DeviceClient defines the interface for interactions with the Device endpoint on core-metadata. type DeviceClient interface { // Add creates a new device - Add(dev *models.Device, ctx context.Context) (string, error) + Add(ctx context.Context, dev *models.Device) (string, error) // Delete eliminates a device for the specified ID - Delete(id string, ctx context.Context) error + Delete(ctx context.Context, id string) error // DeleteByName eliminates a device for the specified name DeleteByName(name string, ctx context.Context) error // CheckForDevice will return a Device if one already exists for the specified device name - CheckForDevice(token string, ctx context.Context) (models.Device, error) + CheckForDevice(ctx context.Context, token string) (models.Device, error) // Device loads the device for the specified ID - Device(id string, ctx context.Context) (models.Device, error) + Device(ctx context.Context, id string) (models.Device, error) // DeviceForName loads the device for the specified name - DeviceForName(name string, ctx context.Context) (models.Device, error) + DeviceForName(ctx context.Context, name string) (models.Device, error) // Devices lists all devices Devices(ctx context.Context) ([]models.Device, error) // DevicesByLabel lists all devices for the specified label - DevicesByLabel(label string, ctx context.Context) ([]models.Device, error) + DevicesByLabel(ctx context.Context, label string) ([]models.Device, error) // DevicesForProfile lists all devices for the specified profile ID - DevicesForProfile(profileid string, ctx context.Context) ([]models.Device, error) + DevicesForProfile(ctx context.Context, profileid string) ([]models.Device, error) // DevicesForProfileByName lists all devices for the specified profile name - DevicesForProfileByName(profileName string, ctx context.Context) ([]models.Device, error) + DevicesForProfileByName(ctx context.Context, profileName string) ([]models.Device, error) // DevicesForService lists all devices for the specified device service ID - DevicesForService(serviceid string, ctx context.Context) ([]models.Device, error) + DevicesForService(ctx context.Context, serviceid string) ([]models.Device, error) // DevicesForServiceByName lists all devices for the specified device service name - DevicesForServiceByName(serviceName string, ctx context.Context) ([]models.Device, error) + DevicesForServiceByName(ctx context.Context, serviceName string) ([]models.Device, error) // Update the specified device - Update(dev models.Device, ctx context.Context) error + Update(ctx context.Context, dev models.Device) error // UpdateAdminState modifies a device's AdminState for the specified device ID - UpdateAdminState(id string, adminState string, ctx context.Context) error + UpdateAdminState(ctx context.Context, id string, adminState string) error // UpdateAdminStateByName modifies a device's AdminState according to the specified device name - UpdateAdminStateByName(name string, adminState string, ctx context.Context) error + UpdateAdminStateByName(ctx context.Context, name string, adminState string) error // UpdateLastConnected updates a device's last connected timestamp according to the specified device ID - UpdateLastConnected(id string, time int64, ctx context.Context) error + UpdateLastConnected(ctx context.Context, id string, time int64) error // UpdateLastConnectedByName updates a device's last connected timestamp according to the specified device name - UpdateLastConnectedByName(name string, time int64, ctx context.Context) error + UpdateLastConnectedByName(ctx context.Context, name string, time int64) error // UpdateLastReported updates a device's last reported timestamp according to the specified device ID - UpdateLastReported(id string, time int64, ctx context.Context) error + UpdateLastReported(ctx context.Context, id string, time int64) error // UpdateLastReportedByName updates a device's last reported timestamp according to the specified device name - UpdateLastReportedByName(name string, time int64, ctx context.Context) error + UpdateLastReportedByName(ctx context.Context, name string, time int64) error // UpdateOpState updates a device's last OperatingState according to the specified device ID - UpdateOpState(id string, opState string, ctx context.Context) error + UpdateOpState(ctx context.Context, id string, opState string) error // UpdateOpStateByName updates a device's last OperatingState according to the specified device name - UpdateOpStateByName(name string, opState string, ctx context.Context) error + UpdateOpStateByName(ctx context.Context, name string, opState string) error } type deviceRestClient struct { @@ -83,8 +83,8 @@ func NewDeviceClient(urlClient interfaces.URLClient) DeviceClient { } // Helper method to request and decode a device -func (d *deviceRestClient) requestDevice(urlSuffix string, ctx context.Context) (models.Device, error) { - data, err := clients.GetRequest(urlSuffix, ctx, d.urlClient) +func (d *deviceRestClient) requestDevice(ctx context.Context, urlSuffix string) (models.Device, error) { + data, err := clients.GetRequest(ctx, urlSuffix, d.urlClient) if err != nil { return models.Device{}, err } @@ -95,7 +95,7 @@ func (d *deviceRestClient) requestDevice(urlSuffix string, ctx context.Context) } // Helper method to request and decode a device slice -func (d *deviceRestClient) requestDeviceSlice(urlSuffix string, ctx context.Context) ([]models.Device, error) { +func (d *deviceRestClient) requestDeviceSlice(ctx context.Context, urlSuffix string) ([]models.Device, error) { data, err := clients.GetRequest(ctx, urlSuffix, d.urlClient) if err != nil { return []models.Device{}, err @@ -106,92 +106,92 @@ func (d *deviceRestClient) requestDeviceSlice(urlSuffix string, ctx context.Cont return dSlice, err } -func (d *deviceRestClient) CheckForDevice(token string, ctx context.Context) (models.Device, error) { - return d.requestDevice("/check/"+token, ctx) +func (d *deviceRestClient) CheckForDevice(ctx context.Context, token string) (models.Device, error) { + return d.requestDevice(ctx, "/check/"+token) } -func (d *deviceRestClient) Device(id string, ctx context.Context) (models.Device, error) { - return d.requestDevice("/"+id, ctx) +func (d *deviceRestClient) Device(ctx context.Context, id string) (models.Device, error) { + return d.requestDevice(ctx, "/"+id) } func (d *deviceRestClient) Devices(ctx context.Context) ([]models.Device, error) { - return d.requestDeviceSlice("", ctx) + return d.requestDeviceSlice(ctx, "") } -func (d *deviceRestClient) DeviceForName(name string, ctx context.Context) (models.Device, error) { - return d.requestDevice("/name/"+url.QueryEscape(name), ctx) +func (d *deviceRestClient) DeviceForName(ctx context.Context, name string) (models.Device, error) { + return d.requestDevice(ctx, "/name/"+url.QueryEscape(name)) } -func (d *deviceRestClient) DevicesByLabel(label string, ctx context.Context) ([]models.Device, error) { - return d.requestDeviceSlice("/label/"+url.QueryEscape(label), ctx) +func (d *deviceRestClient) DevicesByLabel(ctx context.Context, label string) ([]models.Device, error) { + return d.requestDeviceSlice(ctx, "/label/"+url.QueryEscape(label)) } -func (d *deviceRestClient) DevicesForService(serviceId string, ctx context.Context) ([]models.Device, error) { - return d.requestDeviceSlice("/service/"+serviceId, ctx) +func (d *deviceRestClient) DevicesForService(ctx context.Context, serviceId string) ([]models.Device, error) { + return d.requestDeviceSlice(ctx, "/service/"+ serviceId) } -func (d *deviceRestClient) DevicesForServiceByName(serviceName string, ctx context.Context) ([]models.Device, error) { - return d.requestDeviceSlice("/servicename/"+url.QueryEscape(serviceName), ctx) +func (d *deviceRestClient) DevicesForServiceByName(ctx context.Context, serviceName string) ([]models.Device, error) { + return d.requestDeviceSlice(ctx, "/servicename/"+url.QueryEscape(serviceName)) } -func (d *deviceRestClient) DevicesForProfile(profileId string, ctx context.Context) ([]models.Device, error) { - return d.requestDeviceSlice("/profile/"+profileId, ctx) +func (d *deviceRestClient) DevicesForProfile(ctx context.Context, profileId string) ([]models.Device, error) { + return d.requestDeviceSlice(ctx, "/profile/"+profileId) } -func (d *deviceRestClient) DevicesForProfileByName(profileName string, ctx context.Context) ([]models.Device, error) { - return d.requestDeviceSlice("/profilename/"+url.QueryEscape(profileName), ctx) +func (d *deviceRestClient) DevicesForProfileByName(ctx context.Context, profileName string) ([]models.Device, error) { + return d.requestDeviceSlice(ctx, "/profilename/"+url.QueryEscape(profileName)) } -func (d *deviceRestClient) Add(dev *models.Device, ctx context.Context) (string, error) { +func (d *deviceRestClient) Add(ctx context.Context, dev *models.Device) (string, error) { return clients.PostJsonRequest("", dev, ctx, d.urlClient) } -func (d *deviceRestClient) Update(dev models.Device, ctx context.Context) error { +func (d *deviceRestClient) Update(ctx context.Context, dev models.Device) error { return clients.UpdateRequest(ctx, "", dev, d.urlClient) } -func (d *deviceRestClient) UpdateLastConnected(id string, time int64, ctx context.Context) error { +func (d *deviceRestClient) UpdateLastConnected(ctx context.Context, id string, time int64) error { _, err := clients.PutRequest(ctx, "/"+id+"/lastconnected/"+strconv.FormatInt(time, 10), nil, d.urlClient) return err } -func (d *deviceRestClient) UpdateLastConnectedByName(name string, time int64, ctx context.Context) error { +func (d *deviceRestClient) UpdateLastConnectedByName(ctx context.Context, name string, time int64) error { _, err := clients.PutRequest(ctx, "/name/"+url.QueryEscape(name)+"/lastconnected/"+strconv.FormatInt(time, 10), nil, d.urlClient, ) return err } -func (d *deviceRestClient) UpdateLastReported(id string, time int64, ctx context.Context) error { +func (d *deviceRestClient) UpdateLastReported(ctx context.Context, id string, time int64) error { _, err := clients.PutRequest(ctx, "/"+id+"/lastreported/"+strconv.FormatInt(time, 10), nil, d.urlClient) return err } -func (d *deviceRestClient) UpdateLastReportedByName(name string, time int64, ctx context.Context) error { +func (d *deviceRestClient) UpdateLastReportedByName(ctx context.Context, name string, time int64) error { _, err := clients.PutRequest(ctx, "/name/"+url.QueryEscape(name)+"/lastreported/"+strconv.FormatInt(time, 10), nil, d.urlClient, ) return err } -func (d *deviceRestClient) UpdateOpState(id string, opState string, ctx context.Context) error { +func (d *deviceRestClient) UpdateOpState(ctx context.Context, id string, opState string) error { _, err := clients.PutRequest(ctx, "/"+id+"/opstate/"+opState, nil, d.urlClient) return err } -func (d *deviceRestClient) UpdateOpStateByName(name string, opState string, ctx context.Context) error { +func (d *deviceRestClient) UpdateOpStateByName(ctx context.Context, name string, opState string) error { _, err := clients.PutRequest(ctx, "/name/"+url.QueryEscape(name)+"/opstate/"+opState, nil, d.urlClient) return err } -func (d *deviceRestClient) UpdateAdminState(id string, adminState string, ctx context.Context) error { +func (d *deviceRestClient) UpdateAdminState(ctx context.Context, id string, adminState string) error { _, err := clients.PutRequest(ctx, "/"+id+"/adminstate/"+adminState, nil, d.urlClient) return err } -func (d *deviceRestClient) UpdateAdminStateByName(name string, adminState string, ctx context.Context) error { +func (d *deviceRestClient) UpdateAdminStateByName(ctx context.Context, name string, adminState string) error { _, err := clients.PutRequest(ctx, "/name/"+url.QueryEscape(name)+"/adminstate/"+adminState, nil, d.urlClient) return err } -func (d *deviceRestClient) Delete(id string, ctx context.Context) error { - return clients.DeleteRequest("/id/"+id, ctx, d.urlClient) +func (d *deviceRestClient) Delete(ctx context.Context, id string) error { + return clients.DeleteRequest(ctx, "/id/"+id, d.urlClient) } func (d *deviceRestClient) DeleteByName(name string, ctx context.Context) error { diff --git a/clients/metadata/device_profile_test.go b/clients/metadata/device_profile_test.go index e68be767..af7b1082 100644 --- a/clients/metadata/device_profile_test.go +++ b/clients/metadata/device_profile_test.go @@ -21,20 +21,14 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) func TestNewDeviceProfileClientWithConsul(t *testing.T) { deviceUrl := "http://localhost:48081" + clients.ApiCommandRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiCommandRoute, - UseRegistry: true, - Url: deviceUrl, - Interval: clients.ClientMonitorDefault} - dpc := NewDeviceProfileClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + dpc := NewDeviceProfileClient(urlclient.NewLocalClient(deviceUrl)) r, ok := dpc.(*deviceProfileRestClient) if !ok { @@ -68,21 +62,13 @@ func TestUpdateDeviceProfile(t *testing.T) { t.Errorf("expected uri path is %s, actual uri path is %s", clients.ApiDeviceProfileRoute, r.URL.EscapedPath()) } - w.Write([]byte("true")) + _, _ = w.Write([]byte("true")) })) defer ts.Close() - url := ts.URL + clients.ApiDeviceProfileRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiDeviceProfileRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - dpc := NewDeviceProfileClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + dpc := NewDeviceProfileClient(urlclient.NewLocalClient(ts.URL + clients.ApiDeviceProfileRoute)) err := dpc.Update(p, context.Background()) if err != nil { diff --git a/clients/metadata/device_service_test.go b/clients/metadata/device_service_test.go index c5a11b36..39de8d99 100644 --- a/clients/metadata/device_service_test.go +++ b/clients/metadata/device_service_test.go @@ -18,19 +18,13 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" ) func TestNewDeviceServiceClientWithConsul(t *testing.T) { deviceServiceUrl := "http://localhost:48081" + clients.ApiDeviceServiceRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiDeviceServiceRoute, - UseRegistry: true, - Url: deviceServiceUrl, - Interval: clients.ClientMonitorDefault} - dsc := NewDeviceServiceClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + dsc := NewDeviceServiceClient(urlclient.NewLocalClient(deviceServiceUrl)) r, ok := dsc.(*deviceServiceRestClient) if !ok { t.Error("dsc is not of expected type") diff --git a/clients/metadata/device_test.go b/clients/metadata/device_test.go index 638eb13d..6bbfb486 100644 --- a/clients/metadata/device_test.go +++ b/clients/metadata/device_test.go @@ -16,18 +16,15 @@ package metadata import ( "context" - "fmt" "net/http" "net/http/httptest" "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) -// Test adding a device using the device urlClient - // Test adding a device using the device urlClient func TestAddDevice(t *testing.T) { @@ -53,23 +50,15 @@ func TestAddDevice(t *testing.T) { t.Errorf("expected uri path is %s, actual uri path is %s", clients.ApiDeviceRoute, r.URL.EscapedPath()) } - w.Write([]byte(addingDeviceId)) + _, _ = w.Write([]byte(addingDeviceId)) })) defer ts.Close() - url := ts.URL + clients.ApiDeviceRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiDeviceRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - dc := NewDeviceClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + dc := NewDeviceClient(urlclient.NewLocalClient(ts.URL + clients.ApiDeviceRoute)) - receivedDeviceId, err := dc.Add(&d, context.Background()) + receivedDeviceId, err := dc.Add(context.Background(), &d) if err != nil { t.Error(err.Error()) } @@ -81,14 +70,8 @@ func TestAddDevice(t *testing.T) { func TestNewDeviceClientWithConsul(t *testing.T) { deviceUrl := "http://localhost:48081" + clients.ApiDeviceRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiDeviceRoute, - UseRegistry: true, - Url: deviceUrl, - Interval: clients.ClientMonitorDefault} - dc := NewDeviceClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + dc := NewDeviceClient(urlclient.NewLocalClient(deviceUrl)) r, ok := dc.(*deviceRestClient) if !ok { @@ -103,11 +86,3 @@ func TestNewDeviceClientWithConsul(t *testing.T) { t.Errorf("unexpected url value %s", url) } } - -type mockCoreMetaDataEndpoint struct{} - -func (e mockCoreMetaDataEndpoint) Monitor(params types.EndpointParams) chan string { - ch := make(chan string, 1) - ch <- fmt.Sprintf("http://%s:%v%s", "localhost", 48081, params.Path) - return ch -} diff --git a/clients/metadata/mocks/DeviceClient.go b/clients/metadata/mocks/DeviceClient.go index 478e09f3..d54a9c99 100644 --- a/clients/metadata/mocks/DeviceClient.go +++ b/clients/metadata/mocks/DeviceClient.go @@ -13,7 +13,7 @@ type DeviceClient struct { } // Add provides a mock function with given fields: dev, ctx -func (_m *DeviceClient) Add(dev *models.Device, ctx context.Context) (string, error) { +func (_m *DeviceClient) Add(ctx context.Context, dev *models.Device) (string, error) { ret := _m.Called(dev, ctx) var r0 string @@ -34,7 +34,7 @@ func (_m *DeviceClient) Add(dev *models.Device, ctx context.Context) (string, er } // CheckForDevice provides a mock function with given fields: token, ctx -func (_m *DeviceClient) CheckForDevice(token string, ctx context.Context) (models.Device, error) { +func (_m *DeviceClient) CheckForDevice(ctx context.Context, token string) (models.Device, error) { ret := _m.Called(token, ctx) var r0 models.Device @@ -55,7 +55,7 @@ func (_m *DeviceClient) CheckForDevice(token string, ctx context.Context) (model } // Delete provides a mock function with given fields: id, ctx -func (_m *DeviceClient) Delete(id string, ctx context.Context) error { +func (_m *DeviceClient) Delete(ctx context.Context, id string) error { ret := _m.Called(id, ctx) var r0 error @@ -83,7 +83,7 @@ func (_m *DeviceClient) DeleteByName(name string, ctx context.Context) error { } // Device provides a mock function with given fields: id, ctx -func (_m *DeviceClient) Device(id string, ctx context.Context) (models.Device, error) { +func (_m *DeviceClient) Device(ctx context.Context, id string) (models.Device, error) { ret := _m.Called(id, ctx) var r0 models.Device @@ -104,7 +104,7 @@ func (_m *DeviceClient) Device(id string, ctx context.Context) (models.Device, e } // DeviceForName provides a mock function with given fields: name, ctx -func (_m *DeviceClient) DeviceForName(name string, ctx context.Context) (models.Device, error) { +func (_m *DeviceClient) DeviceForName(ctx context.Context, name string) (models.Device, error) { ret := _m.Called(name, ctx) var r0 models.Device @@ -148,7 +148,7 @@ func (_m *DeviceClient) Devices(ctx context.Context) ([]models.Device, error) { } // DevicesByLabel provides a mock function with given fields: label, ctx -func (_m *DeviceClient) DevicesByLabel(label string, ctx context.Context) ([]models.Device, error) { +func (_m *DeviceClient) DevicesByLabel(ctx context.Context, label string) ([]models.Device, error) { ret := _m.Called(label, ctx) var r0 []models.Device @@ -217,7 +217,7 @@ func (_m *DeviceClient) DevicesForAddressableByName(addressableName string, ctx } // DevicesForProfile provides a mock function with given fields: profileid, ctx -func (_m *DeviceClient) DevicesForProfile(profileid string, ctx context.Context) ([]models.Device, error) { +func (_m *DeviceClient) DevicesForProfile(ctx context.Context, profileid string) ([]models.Device, error) { ret := _m.Called(profileid, ctx) var r0 []models.Device @@ -240,7 +240,7 @@ func (_m *DeviceClient) DevicesForProfile(profileid string, ctx context.Context) } // DevicesForProfileByName provides a mock function with given fields: profileName, ctx -func (_m *DeviceClient) DevicesForProfileByName(profileName string, ctx context.Context) ([]models.Device, error) { +func (_m *DeviceClient) DevicesForProfileByName(ctx context.Context, profileName string) ([]models.Device, error) { ret := _m.Called(profileName, ctx) var r0 []models.Device @@ -263,7 +263,7 @@ func (_m *DeviceClient) DevicesForProfileByName(profileName string, ctx context. } // DevicesForService provides a mock function with given fields: serviceid, ctx -func (_m *DeviceClient) DevicesForService(serviceid string, ctx context.Context) ([]models.Device, error) { +func (_m *DeviceClient) DevicesForService(ctx context.Context, serviceid string) ([]models.Device, error) { ret := _m.Called(serviceid, ctx) var r0 []models.Device @@ -286,7 +286,7 @@ func (_m *DeviceClient) DevicesForService(serviceid string, ctx context.Context) } // DevicesForServiceByName provides a mock function with given fields: serviceName, ctx -func (_m *DeviceClient) DevicesForServiceByName(serviceName string, ctx context.Context) ([]models.Device, error) { +func (_m *DeviceClient) DevicesForServiceByName(ctx context.Context, serviceName string) ([]models.Device, error) { ret := _m.Called(serviceName, ctx) var r0 []models.Device @@ -309,7 +309,7 @@ func (_m *DeviceClient) DevicesForServiceByName(serviceName string, ctx context. } // Update provides a mock function with given fields: dev, ctx -func (_m *DeviceClient) Update(dev models.Device, ctx context.Context) error { +func (_m *DeviceClient) Update(ctx context.Context, dev models.Device) error { ret := _m.Called(dev, ctx) var r0 error @@ -323,7 +323,7 @@ func (_m *DeviceClient) Update(dev models.Device, ctx context.Context) error { } // UpdateAdminState provides a mock function with given fields: id, adminState, ctx -func (_m *DeviceClient) UpdateAdminState(id string, adminState string, ctx context.Context) error { +func (_m *DeviceClient) UpdateAdminState(ctx context.Context, id string, adminState string) error { ret := _m.Called(id, adminState, ctx) var r0 error @@ -337,7 +337,7 @@ func (_m *DeviceClient) UpdateAdminState(id string, adminState string, ctx conte } // UpdateAdminStateByName provides a mock function with given fields: name, adminState, ctx -func (_m *DeviceClient) UpdateAdminStateByName(name string, adminState string, ctx context.Context) error { +func (_m *DeviceClient) UpdateAdminStateByName(ctx context.Context, name string, adminState string) error { ret := _m.Called(name, adminState, ctx) var r0 error @@ -351,7 +351,7 @@ func (_m *DeviceClient) UpdateAdminStateByName(name string, adminState string, c } // UpdateLastConnected provides a mock function with given fields: id, time, ctx -func (_m *DeviceClient) UpdateLastConnected(id string, time int64, ctx context.Context) error { +func (_m *DeviceClient) UpdateLastConnected(ctx context.Context, id string, time int64) error { ret := _m.Called(id, time, ctx) var r0 error @@ -365,7 +365,7 @@ func (_m *DeviceClient) UpdateLastConnected(id string, time int64, ctx context.C } // UpdateLastConnectedByName provides a mock function with given fields: name, time, ctx -func (_m *DeviceClient) UpdateLastConnectedByName(name string, time int64, ctx context.Context) error { +func (_m *DeviceClient) UpdateLastConnectedByName(ctx context.Context, name string, time int64) error { ret := _m.Called(name, time, ctx) var r0 error @@ -379,7 +379,7 @@ func (_m *DeviceClient) UpdateLastConnectedByName(name string, time int64, ctx c } // UpdateLastReported provides a mock function with given fields: id, time, ctx -func (_m *DeviceClient) UpdateLastReported(id string, time int64, ctx context.Context) error { +func (_m *DeviceClient) UpdateLastReported(ctx context.Context, id string, time int64) error { ret := _m.Called(id, time, ctx) var r0 error @@ -393,7 +393,7 @@ func (_m *DeviceClient) UpdateLastReported(id string, time int64, ctx context.Co } // UpdateLastReportedByName provides a mock function with given fields: name, time, ctx -func (_m *DeviceClient) UpdateLastReportedByName(name string, time int64, ctx context.Context) error { +func (_m *DeviceClient) UpdateLastReportedByName(ctx context.Context, name string, time int64) error { ret := _m.Called(name, time, ctx) var r0 error @@ -407,7 +407,7 @@ func (_m *DeviceClient) UpdateLastReportedByName(name string, time int64, ctx co } // UpdateOpState provides a mock function with given fields: id, opState, ctx -func (_m *DeviceClient) UpdateOpState(id string, opState string, ctx context.Context) error { +func (_m *DeviceClient) UpdateOpState(ctx context.Context, id string, opState string) error { ret := _m.Called(id, opState, ctx) var r0 error @@ -421,7 +421,7 @@ func (_m *DeviceClient) UpdateOpState(id string, opState string, ctx context.Con } // UpdateOpStateByName provides a mock function with given fields: name, opState, ctx -func (_m *DeviceClient) UpdateOpStateByName(name string, opState string, ctx context.Context) error { +func (_m *DeviceClient) UpdateOpStateByName(ctx context.Context, name string, opState string) error { ret := _m.Called(name, opState, ctx) var r0 error diff --git a/clients/metadata/provision_watcher_test.go b/clients/metadata/provision_watcher_test.go index 83ca8ee5..c185ebb7 100644 --- a/clients/metadata/provision_watcher_test.go +++ b/clients/metadata/provision_watcher_test.go @@ -22,7 +22,7 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" "github.com/edgexfoundry/go-mod-core-contracts/models" ) @@ -49,21 +49,13 @@ func TestAddProvisionWatcher(t *testing.T) { t.Errorf("expected uri path is %s, actual uri path is %s", clients.ApiProvisionWatcherRoute, r.URL.EscapedPath()) } - w.Write([]byte(addingProvisionWatcherID)) + _, _ = w.Write([]byte(addingProvisionWatcherID)) })) defer ts.Close() - url := ts.URL + clients.ApiProvisionWatcherRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiProvisionWatcherRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault} - sc := NewProvisionWatcherClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + sc := NewProvisionWatcherClient(urlclient.NewLocalClient(ts.URL + clients.ApiProvisionWatcherRoute)) receivedProvisionWatcherID, err := sc.Add(&se, context.Background()) if err != nil { @@ -77,14 +69,8 @@ func TestAddProvisionWatcher(t *testing.T) { func TestNewProvisionWatcherClientWithConsul(t *testing.T) { provisionWatcherURL := "http://localhost:48081" + clients.ApiProvisionWatcherRoute - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiProvisionWatcherRoute, - UseRegistry: true, - Url: provisionWatcherURL, - Interval: clients.ClientMonitorDefault} - - sc := NewProvisionWatcherClient(params, mockCoreMetaDataEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + + sc := NewProvisionWatcherClient(urlclient.NewLocalClient(provisionWatcherURL)) r, ok := sc.(*provisionWatcherRestClient) if !ok { diff --git a/clients/notifications/client_test.go b/clients/notifications/client_test.go index e1207e3e..b9848cf1 100644 --- a/clients/notifications/client_test.go +++ b/clients/notifications/client_test.go @@ -15,7 +15,7 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" ) // Test common const @@ -39,7 +39,7 @@ const ( func TestReceiveNotification(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("{ 'status' : 'OK' }")) + _, _ = w.Write([]byte("{ 'status' : 'OK' }")) if r.Method != http.MethodPost { t.Errorf(TestUnexpectedMsgFormatStr, r.Method, http.MethodPost) } @@ -48,10 +48,10 @@ func TestReceiveNotification(t *testing.T) { } result, _ := ioutil.ReadAll(r.Body) - r.Body.Close() + _ = r.Body.Close() var receivedNotification Notification - json.Unmarshal([]byte(result), &receivedNotification) + _ = json.Unmarshal([]byte(result), &receivedNotification) if receivedNotification.Sender != TestNotificationSender { t.Errorf(TestUnexpectedMsgFormatStr, receivedNotification.Sender, TestNotificationSender) @@ -93,17 +93,7 @@ func TestReceiveNotification(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiNotificationRoute - - params := types.EndpointParams{ - ServiceKey: clients.CoreMetaDataServiceKey, - Path: clients.ApiNotificationRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - nc := NewNotificationsClient(params, mockNotificationEndpoint{}, types.URLClientParams{Interval: 500, Timeout: 10}) + nc := NewNotificationsClient(urlclient.NewLocalClient(ts.URL + clients.ApiNotificationRoute)) notification := Notification{ Sender: TestNotificationSender, @@ -115,11 +105,5 @@ func TestReceiveNotification(t *testing.T) { Labels: []string{TestNotificationLabel1, TestNotificationLabel2}, } - nc.SendNotification(notification, context.Background()) -} - -type mockNotificationEndpoint struct{} - -func (e mockNotificationEndpoint) Monitor(params types.EndpointParams) chan string { - return make(chan string, 1) + _ = nc.SendNotification(notification, context.Background()) } diff --git a/clients/scheduler/interval_action_test.go b/clients/scheduler/interval_action_test.go index 626d3b6f..bf6bbd33 100644 --- a/clients/scheduler/interval_action_test.go +++ b/clients/scheduler/interval_action_test.go @@ -23,7 +23,9 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/retry" "github.com/edgexfoundry/go-mod-core-contracts/models" ) @@ -72,17 +74,7 @@ func TestIntervalActionRestClient_Add(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalActionRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - iac := NewIntervalActionClient(params, MockEndpoint{}) + iac := NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)) res, err := iac.Add(&testIntervalAction1, context.Background()) @@ -100,17 +92,7 @@ func TestIntervalActionRestClient_Delete(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalActionRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalActionClient(params, MockEndpoint{}) + ic := NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)) err := ic.Delete(testID1, context.Background()) @@ -124,17 +106,7 @@ func TestIntervalActionRestClient_DeleteByName(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalActionRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalActionClient(params, MockEndpoint{}) + ic := NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)) err := ic.DeleteByName(testIntervalAction1.Name, context.Background()) @@ -189,30 +161,21 @@ func TestIntervalActionRestClient_IntervalAction(t *testing.T) { }{ {"happy path", testIntervalAction1.ID, - NewIntervalActionClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: ts.URL + clients.ApiIntervalActionRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)), false, }, { - "nil client", + "client error", testIntervalAction1.ID, - NewIntervalActionClient(types.EndpointParams{}, nil), + NewIntervalActionClient( + urlclient.NewRegistryClient(make(chan interfaces.URLStream), + retry.NewPeriodicRetry(1, 0)), + ), true, }, {"bad JSON marshal", testIntervalAction1.ID, - NewIntervalActionClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: badJSONServer.URL + clients.ApiIntervalActionRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalActionClient(urlclient.NewLocalClient(badJSONServer.URL + clients.ApiIntervalActionRoute)), true, }, } @@ -259,17 +222,7 @@ func TestIntervalActionRestClient_IntervalActionForName(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalActionRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalActionClient(params, MockEndpoint{}) + ic := NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)) _, err := ic.IntervalActionForName(testIntervalAction1.Name, context.Background()) @@ -324,28 +277,18 @@ func TestIntervalActionRestClient_IntervalActions(t *testing.T) { expectedError bool }{ {"happy path", - NewIntervalActionClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: ts.URL + clients.ApiIntervalActionRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)), false, }, { - "nil client", - NewIntervalActionClient(types.EndpointParams{}, nil), + "client error", + NewIntervalActionClient(urlclient.NewRegistryClient(make(chan interfaces.URLStream), + retry.NewPeriodicRetry(1, 0)), + ), true, }, {"bad JSON marshal", - NewIntervalActionClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: badJSONServer.URL + clients.ApiIntervalActionRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalActionClient(urlclient.NewLocalClient(badJSONServer.URL + clients.ApiIntervalActionRoute)), true, }, } @@ -394,17 +337,7 @@ func TestIntervalActionRestClient_IntervalActionsForTargetByName(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalActionRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalActionClient(params, MockEndpoint{}) + ic := NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)) _, err := ic.IntervalActionsForTargetByName(testIntervalAction1.Target, context.Background()) @@ -418,17 +351,7 @@ func TestIntervalActionRestClient_Update(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalActionRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalActionRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalActionClient(params, MockEndpoint{}) + ic := NewIntervalActionClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalActionRoute)) err := ic.Update(testIntervalAction1, context.Background()) diff --git a/clients/scheduler/interval_test.go b/clients/scheduler/interval_test.go index 4fa20682..b9fc25bd 100644 --- a/clients/scheduler/interval_test.go +++ b/clients/scheduler/interval_test.go @@ -23,7 +23,9 @@ import ( "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" - "github.com/edgexfoundry/go-mod-core-contracts/clients/types" + "github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" + "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/retry" "github.com/edgexfoundry/go-mod-core-contracts/models" ) @@ -73,19 +75,15 @@ func TestIntervalRestClient_Add(t *testing.T) { }{ {"happy path", testInterval1, - NewIntervalClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: ts.URL + clients.ApiIntervalRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalRoute)), false, }, { - "nil client", + "client error", testInterval1, - NewIntervalClient(types.EndpointParams{}, nil), + NewIntervalClient(urlclient.NewRegistryClient(make(chan interfaces.URLStream), + retry.NewPeriodicRetry(1, 0)), + ), true, }, } @@ -113,17 +111,7 @@ func TestIntervalRestClient_Delete(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalClient(params, MockEndpoint{}) + ic := NewIntervalClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalRoute)) err := ic.Delete(testID1, context.Background()) @@ -137,17 +125,7 @@ func TestIntervalRestClient_DeleteByName(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalClient(params, MockEndpoint{}) + ic := NewIntervalClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalRoute)) err := ic.DeleteByName(testInterval1.Name, context.Background()) @@ -202,30 +180,20 @@ func TestIntervalRestClient_Interval(t *testing.T) { }{ {"happy path", testInterval1.ID, - NewIntervalClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: ts.URL + clients.ApiIntervalRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalRoute)), false, }, { - "nil client", + "client error", testInterval1.ID, - NewIntervalClient(types.EndpointParams{}, nil), + NewIntervalClient(urlclient.NewRegistryClient(make(chan interfaces.URLStream), + retry.NewPeriodicRetry(1, 0)), + ), true, }, {"bad JSON marshal", testInterval1.ID, - NewIntervalClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: badJSONServer.URL + clients.ApiIntervalRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalClient(urlclient.NewLocalClient(badJSONServer.URL + clients.ApiIntervalRoute)), true, }, } @@ -272,17 +240,7 @@ func TestIntervalRestClient_IntervalForName(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalClient(params, MockEndpoint{}) + ic := NewIntervalClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalRoute)) _, err := ic.IntervalForName(testInterval1.Name, context.Background()) @@ -337,28 +295,18 @@ func TestIntervalRestClient_Intervals(t *testing.T) { expectedError bool }{ {"happy path", - NewIntervalClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: ts.URL + clients.ApiIntervalRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalRoute)), false, }, { - "nil client", - NewIntervalClient(types.EndpointParams{}, nil), + "client error", + NewIntervalClient(urlclient.NewRegistryClient(make(chan interfaces.URLStream), + retry.NewPeriodicRetry(1, 0)), + ), true, }, {"bad JSON marshal", - NewIntervalClient(types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: badJSONServer.URL + clients.ApiIntervalRoute, - Interval: clients.ClientMonitorDefault, - }, MockEndpoint{}), + NewIntervalClient(urlclient.NewLocalClient(badJSONServer.URL + clients.ApiIntervalRoute)), true, }, } @@ -388,17 +336,7 @@ func TestIntervalRestClient_Update(t *testing.T) { defer ts.Close() - url := ts.URL + clients.ApiIntervalRoute - - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - UseRegistry: false, - Url: url, - Interval: clients.ClientMonitorDefault, - } - - ic := NewIntervalClient(params, MockEndpoint{}) + ic := NewIntervalClient(urlclient.NewLocalClient(ts.URL + clients.ApiIntervalRoute)) err := ic.Update(testInterval1, context.Background()) @@ -407,13 +345,6 @@ func TestIntervalRestClient_Update(t *testing.T) { } } -type MockEndpoint struct { -} - -func (e MockEndpoint) Monitor(_ types.EndpointParams) chan string { - return make(chan string, 1) -} - // testHttpServer instantiates a test HTTP Server to be used for conveniently verifying a client's invocation func testHttpServer(t *testing.T, matchingRequestMethod string, matchingRequestUri string) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/clients/urlclient/local.go b/clients/urlclient/local.go index 308d9554..1da13794 100644 --- a/clients/urlclient/local.go +++ b/clients/urlclient/local.go @@ -19,8 +19,8 @@ type localClient struct { url string } -// newLocalClient returns a pointer to a localClient. -func newLocalClient(url string) *localClient { +// NewLocalClient returns a pointer to a localClient. +func NewLocalClient(url string) *localClient { return &localClient{ url: url, } diff --git a/clients/urlclient/local_test.go b/clients/urlclient/local_test.go index ae9ebad3..63aa2154 100644 --- a/clients/urlclient/local_test.go +++ b/clients/urlclient/local_test.go @@ -19,15 +19,15 @@ import ( ) func TestNewLocalClient(t *testing.T) { - actualClient := newLocalClient(expectedURL) + actualClient := NewLocalClient(expectedURL) if actualClient == nil { - t.Fatal("nil returned from newLocalClient") + t.Fatal("nil returned from NewLocalClient") } } func TestLocalClient_URLPrefix(t *testing.T) { - urlClient := newLocalClient(expectedURL) + urlClient := NewLocalClient(expectedURL) actualURL, err := urlClient.Prefix() diff --git a/clients/urlclient/registry_test.go b/clients/urlclient/registry_test.go index 09f02ed0..77b1e6b6 100644 --- a/clients/urlclient/registry_test.go +++ b/clients/urlclient/registry_test.go @@ -63,7 +63,7 @@ func TestRegistryClient_Prefix_Periodic(t *testing.T) { func TestRegistryClient_Prefix_Periodic_Initialized(t *testing.T) { // use impossible timing to ensure that if hit, the retry logic will error out - strategy := retry.NewPeriodicRetry(100000000, 10) + strategy := retry.NewPeriodicRetry(1, 0) testStream := makeTestStream() urlClient := NewRegistryClient( @@ -90,7 +90,7 @@ func TestRegistryClient_Prefix_Periodic_Initialized(t *testing.T) { func TestRegistryClient_Prefix_Periodic_TimedOut(t *testing.T) { urlClient := NewRegistryClient( makeTestStream(), - retry.NewPeriodicRetry(100000000,1), + retry.NewPeriodicRetry(1,0), ) actualURL, err := urlClient.Prefix() diff --git a/dummy.go b/dummy.go deleted file mode 100644 index cdb58f7e..00000000 --- a/dummy.go +++ /dev/null @@ -1,28 +0,0 @@ -/******************************************************************************* - * Copyright 2020 Dell Inc. - * - * 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 go_mod_core_contracts - -import ( - "github.com/edgexfoundry/go-mod-core-contracts/clients/command" - "github.com/edgexfoundry/go-mod-core-contracts/clients/interfaces" - "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient" - "github.com/edgexfoundry/go-mod-core-contracts/clients/urlclient/retry" -) - -func foo(urlStream interfaces.URLStream) { - commandClient := command.NewCommandClient( - urlclient.NewRegistryClient(urlStream, retry.NewPeriodicRetry(1, 10)), - ) -}