From 412d02f7bd53a9e810be2c17d1c391c9bc6dda13 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Thu, 18 Jul 2019 14:49:25 -0700 Subject: Add list api for profiles Add a list api for profiles for a specific definition and version. GET /v1/rb/definition/name/version/profile will list all the profiles. Issue-ID: MULTICLOUD-730 Change-Id: If1b8e6910c276a0f7139ab13340721c6ec8a49e8 Signed-off-by: Kiran Kamineni --- src/k8splugin/api/profilehandler_test.go | 101 +++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'src/k8splugin/api/profilehandler_test.go') diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go index e81fb262..4dae377c 100644 --- a/src/k8splugin/api/profilehandler_test.go +++ b/src/k8splugin/api/profilehandler_test.go @@ -23,6 +23,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "sort" "testing" "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" @@ -57,6 +58,14 @@ func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error return m.Items[0], nil } +func (m *mockRBProfile) List(rbname, rbversion string) ([]rb.Profile, error) { + if m.Err != nil { + return []rb.Profile{}, m.Err + } + + return m.Items, nil +} + func (m *mockRBProfile) Delete(rbname, rbversion, prname string) error { return m.Err } @@ -210,6 +219,98 @@ func TestRBProfileGetHandler(t *testing.T) { } } +func TestRBProfileListHandler(t *testing.T) { + + testCases := []struct { + def string + version string + label string + expected []rb.Profile + expectedCode int + rbProClient *mockRBProfile + }{ + { + def: "test-rbdef", + version: "v1", + label: "List Profiles", + expectedCode: http.StatusOK, + expected: []rb.Profile{ + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + ReleaseName: "testprofilereleasename", + Namespace: "ns1", + KubernetesVersion: "1.12.3", + }, + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + ReleaseName: "testprofilereleasename", + Namespace: "ns2", + KubernetesVersion: "1.12.3", + }, + }, + rbProClient: &mockRBProfile{ + // list of Profiles that will be returned by the mockclient + Items: []rb.Profile{ + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + ReleaseName: "testprofilereleasename", + Namespace: "ns1", + KubernetesVersion: "1.12.3", + }, + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + ReleaseName: "testprofilereleasename", + Namespace: "ns2", + KubernetesVersion: "1.12.3", + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.def+"/"+testCase.version+"/profile", nil) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []rb.Profile{} + json.NewDecoder(resp.Body).Decode(&got) + + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(got, func(i, j int) bool { + return got[i].ProfileName < got[j].ProfileName + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(testCase.expected, func(i, j int) bool { + return testCase.expected[i].ProfileName < testCase.expected[j].ProfileName + }) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("listHandler returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestRBProfileDeleteHandler(t *testing.T) { testCases := []struct { -- cgit 1.2.3-korg