Skip to content

Commit

Permalink
edgexfoundry#196: Interval and Interval Action now have 100% coverage.
Browse files Browse the repository at this point in the history
Signed-off-by: Brandon Forster <me@brandonforster.com>
  • Loading branch information
brandonforster committed Jan 27, 2020
1 parent 25d993d commit 94fc3f0
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 75 deletions.
159 changes: 131 additions & 28 deletions clients/scheduler/interval_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/clients"
Expand Down Expand Up @@ -162,24 +163,77 @@ func TestIntervalActionRestClient_IntervalAction(t *testing.T) {
w.Write(data)
}))

defer ts.Close()

url := ts.URL + clients.ApiIntervalActionRoute
badJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)

params := types.EndpointParams{
ServiceKey: clients.SupportSchedulerServiceKey,
Path: clients.ApiIntervalActionRoute,
UseRegistry: false,
Url: url,
Interval: clients.ClientMonitorDefault,
}
if r.Method != http.MethodGet {
t.Fatalf("expected http method is GET, active http method is : %s", r.Method)
}

ic := NewIntervalActionClient(params, MockEndpoint{})
expectedURL := clients.ApiIntervalActionRoute + "/" + testID1
if r.URL.EscapedPath() != expectedURL {
t.Fatalf("expected uri path is %s, actual uri path is %s", expectedURL, r.URL.EscapedPath())
}

_, err := ic.IntervalAction(testID1, context.Background())
w.Write([]byte{1, 2, 3, 4})
}))

if err != nil {
t.Fatalf("unexpected error %s", err.Error())
defer ts.Close()
defer badJSONServer.Close()

var tests = []struct {
name string
IntervalActionID string
ic IntervalActionClient
expectedError bool
}{
{"happy path",
testIntervalAction1.ID,
NewIntervalActionClient(types.EndpointParams{
ServiceKey: clients.SupportSchedulerServiceKey,
Path: clients.ApiIntervalActionRoute,
UseRegistry: false,
Url: ts.URL + clients.ApiIntervalActionRoute,
Interval: clients.ClientMonitorDefault,
}, MockEndpoint{}),
false,
},
{
"nil client",
testIntervalAction1.ID,
NewIntervalActionClient(types.EndpointParams{}, nil),
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{}),
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := tt.ic.IntervalAction(tt.IntervalActionID, context.Background())

emptyIntervalAction := models.IntervalAction{}

if !tt.expectedError && res == emptyIntervalAction {
t.Error("unexpected empty response")
} else if tt.expectedError && res != emptyIntervalAction {
t.Errorf("expected empty response, was %s", res)
}

if !tt.expectedError && err != nil {
t.Errorf("unexpected error %s", err.Error())
} else if tt.expectedError && err == nil {
t.Error("expected error")
}
})
}
}

Expand Down Expand Up @@ -246,24 +300,73 @@ func TestIntervalActionRestClient_IntervalActions(t *testing.T) {
w.Write(data)
}))

defer ts.Close()

url := ts.URL + clients.ApiIntervalActionRoute
badJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)

params := types.EndpointParams{
ServiceKey: clients.SupportSchedulerServiceKey,
Path: clients.ApiIntervalActionRoute,
UseRegistry: false,
Url: url,
Interval: clients.ClientMonitorDefault,
}
if r.Method != http.MethodGet {
t.Fatalf("expected http method is GET, active http method is : %s", r.Method)
}

ic := NewIntervalActionClient(params, MockEndpoint{})
expectedURL := clients.ApiIntervalActionRoute
if r.URL.EscapedPath() != expectedURL {
t.Fatalf("expected uri path is %s, actual uri path is %s", expectedURL, r.URL.EscapedPath())
}

_, err := ic.IntervalActions(context.Background())
w.Write([]byte{1, 2, 3, 4})
}))

if err != nil {
t.Fatalf("unexpected error %s", err.Error())
defer ts.Close()
defer badJSONServer.Close()

var tests = []struct {
name string
ic IntervalActionClient
expectedError bool
}{
{"happy path",
NewIntervalActionClient(types.EndpointParams{
ServiceKey: clients.SupportSchedulerServiceKey,
Path: clients.ApiIntervalActionRoute,
UseRegistry: false,
Url: ts.URL + clients.ApiIntervalActionRoute,
Interval: clients.ClientMonitorDefault,
}, MockEndpoint{}),
false,
},
{
"nil client",
NewIntervalActionClient(types.EndpointParams{}, nil),
true,
},
{"bad JSON marshal",
NewIntervalActionClient(types.EndpointParams{
ServiceKey: clients.SupportSchedulerServiceKey,
Path: clients.ApiIntervalActionRoute,
UseRegistry: false,
Url: badJSONServer.URL + clients.ApiIntervalActionRoute,
Interval: clients.ClientMonitorDefault,
}, MockEndpoint{}),
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := tt.ic.IntervalActions(context.Background())

emptyIntervalActionSlice := []models.IntervalAction{}

if !tt.expectedError && reflect.DeepEqual(res, emptyIntervalActionSlice) {
t.Error("unexpected empty response")
} else if tt.expectedError && !reflect.DeepEqual(res, emptyIntervalActionSlice) {
t.Errorf("expected empty response, was %s", res)
}

if !tt.expectedError && err != nil {
t.Errorf("unexpected error %s", err.Error())
} else if tt.expectedError && err == nil {
t.Error("expected error")
}
})
}
}

Expand Down
Loading

0 comments on commit 94fc3f0

Please sign in to comment.