Skip to content

Commit

Permalink
edgexfoundry#204: Unit tests pass.
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Forster <me@brandonforster.com>
  • Loading branch information
brandonforster committed Feb 1, 2020
1 parent 2e62e9e commit 5010e33
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 722 deletions.
59 changes: 6 additions & 53 deletions clients/command/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,15 @@ 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) {
ts := testHttpServer(t, http.MethodGet, clients.ApiDeviceRoute+"/device1/command/command1")

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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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)
}
Expand Down
73 changes: 6 additions & 67 deletions clients/coredata/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand All @@ -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())

Expand All @@ -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 + "\"" +
"}," +
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
75 changes: 36 additions & 39 deletions clients/coredata/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -92,63 +92,60 @@ 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) {
return clients.CountRequest(ctx, "/count", r.urlClient)
}

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(
Expand All @@ -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)
}
Loading

0 comments on commit 5010e33

Please sign in to comment.