From 94fc3f0b8f3c93d7209a24bc91736149d43fde87 Mon Sep 17 00:00:00 2001 From: Brandon Forster Date: Mon, 27 Jan 2020 10:52:46 -0600 Subject: [PATCH] #196: Interval and Interval Action now have 100% coverage. Signed-off-by: Brandon Forster --- clients/scheduler/interval_action_test.go | 159 +++++++++++++--- clients/scheduler/interval_test.go | 217 +++++++++++++++++----- 2 files changed, 301 insertions(+), 75 deletions(-) diff --git a/clients/scheduler/interval_action_test.go b/clients/scheduler/interval_action_test.go index b3adf576..6ee2dd3e 100644 --- a/clients/scheduler/interval_action_test.go +++ b/clients/scheduler/interval_action_test.go @@ -19,6 +19,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "reflect" "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" @@ -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") + } + }) } } @@ -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") + } + }) } } diff --git a/clients/scheduler/interval_test.go b/clients/scheduler/interval_test.go index c82c9e92..6bd3738a 100644 --- a/clients/scheduler/interval_test.go +++ b/clients/scheduler/interval_test.go @@ -19,6 +19,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "reflect" "testing" "github.com/edgexfoundry/go-mod-core-contracts/clients" @@ -64,26 +65,46 @@ func TestIntervalRestClient_Add(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, + var tests = []struct { + name string + interval models.Interval + ic IntervalClient + expectedError bool + }{ + {"happy path", + testInterval1, + NewIntervalClient(types.EndpointParams{ + ServiceKey: clients.SupportSchedulerServiceKey, + Path: clients.ApiIntervalRoute, + UseRegistry: false, + Url: ts.URL + clients.ApiIntervalRoute, + Interval: clients.ClientMonitorDefault, + }, MockEndpoint{}), + false, + }, + { + "nil client", + testInterval1, + NewIntervalClient(types.EndpointParams{}, nil), + true, + }, } - - ic := NewIntervalClient(params, MockEndpoint{}) - - res, err := ic.Add(&testInterval1, context.Background()) - - if res == "" { - t.Fatal("unexpected empty string response") - } - - if err != nil { - t.Fatalf("unexpected error %s", err.Error()) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + res, err := tt.ic.Add(&tt.interval, context.Background()) + + if !tt.expectedError && res == "" { + t.Error("unexpected empty string response") + } else if tt.expectedError && res != "" { + t.Errorf("expected empty string 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") + } + }) } } @@ -155,24 +176,77 @@ func TestIntervalRestClient_Interval(t *testing.T) { w.Write(data) })) - defer ts.Close() - - url := ts.URL + clients.ApiIntervalRoute + badJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - 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 := NewIntervalClient(params, MockEndpoint{}) + expectedURL := clients.ApiIntervalRoute + "/" + testID1 + if r.URL.EscapedPath() != expectedURL { + t.Fatalf("expected uri path is %s, actual uri path is %s", expectedURL, r.URL.EscapedPath()) + } - _, err := ic.Interval(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 + intervalID string + ic IntervalClient + expectedError bool + }{ + {"happy path", + testInterval1.ID, + NewIntervalClient(types.EndpointParams{ + ServiceKey: clients.SupportSchedulerServiceKey, + Path: clients.ApiIntervalRoute, + UseRegistry: false, + Url: ts.URL + clients.ApiIntervalRoute, + Interval: clients.ClientMonitorDefault, + }, MockEndpoint{}), + false, + }, + { + "nil client", + testInterval1.ID, + NewIntervalClient(types.EndpointParams{}, nil), + 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{}), + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + res, err := tt.ic.Interval(tt.intervalID, context.Background()) + + emptyInterval := models.Interval{} + + if !tt.expectedError && res == emptyInterval { + t.Error("unexpected empty response") + } else if tt.expectedError && res != emptyInterval { + 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") + } + }) } } @@ -239,24 +313,73 @@ func TestIntervalRestClient_Intervals(t *testing.T) { w.Write(data) })) - defer ts.Close() - - url := ts.URL + clients.ApiIntervalRoute + badJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) - params := types.EndpointParams{ - ServiceKey: clients.SupportSchedulerServiceKey, - Path: clients.ApiIntervalRoute, - 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 := NewIntervalClient(params, MockEndpoint{}) + expectedURL := clients.ApiIntervalRoute + if r.URL.EscapedPath() != expectedURL { + t.Fatalf("expected uri path is %s, actual uri path is %s", expectedURL, r.URL.EscapedPath()) + } - _, err := ic.Intervals(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 IntervalClient + expectedError bool + }{ + {"happy path", + NewIntervalClient(types.EndpointParams{ + ServiceKey: clients.SupportSchedulerServiceKey, + Path: clients.ApiIntervalRoute, + UseRegistry: false, + Url: ts.URL + clients.ApiIntervalRoute, + Interval: clients.ClientMonitorDefault, + }, MockEndpoint{}), + false, + }, + { + "nil client", + NewIntervalClient(types.EndpointParams{}, nil), + true, + }, + {"bad JSON marshal", + NewIntervalClient(types.EndpointParams{ + ServiceKey: clients.SupportSchedulerServiceKey, + Path: clients.ApiIntervalRoute, + UseRegistry: false, + Url: badJSONServer.URL + clients.ApiIntervalRoute, + Interval: clients.ClientMonitorDefault, + }, MockEndpoint{}), + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + res, err := tt.ic.Intervals(context.Background()) + + emptyIntervalSlice := []models.Interval{} + + if !tt.expectedError && reflect.DeepEqual(res, emptyIntervalSlice) { + t.Error("unexpected empty response") + } else if tt.expectedError && !reflect.DeepEqual(res, emptyIntervalSlice) { + 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") + } + }) } }