From b102d4ab1a47809f514213eb1f997d4f60893c9f Mon Sep 17 00:00:00 2001 From: Larry Sachs Date: Mon, 20 Jul 2020 22:46:49 -0700 Subject: Adds PUT api to v2/projects Add functionality to support the PUT api for v2/projects/{project-name} Also add unit tests for the PUT api Issue-ID: MULTICLOUD-1130 Change-Id: Ia271569c5f0dec3152952e64171fd5a182aaa333 Signed-off-by: Larry Sachs --- src/orchestrator/api/projecthandler_test.go | 95 ++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) (limited to 'src/orchestrator/api/projecthandler_test.go') diff --git a/src/orchestrator/api/projecthandler_test.go b/src/orchestrator/api/projecthandler_test.go index dae87e2b..6810099f 100644 --- a/src/orchestrator/api/projecthandler_test.go +++ b/src/orchestrator/api/projecthandler_test.go @@ -40,7 +40,7 @@ type mockProjectManager struct { Err error } -func (m *mockProjectManager) CreateProject(inp moduleLib.Project) (moduleLib.Project, error) { +func (m *mockProjectManager) CreateProject(inp moduleLib.Project, exists bool) (moduleLib.Project, error) { if m.Err != nil { return moduleLib.Project{}, m.Err } @@ -144,6 +144,99 @@ func TestProjectCreateHandler(t *testing.T) { } } +func TestProjectUpdateHandler(t *testing.T) { + testCases := []struct { + label, name string + reader io.Reader + expected moduleLib.Project + expectedCode int + projectClient *mockProjectManager + }{ + { + label: "Missing Project Name in Request Body", + name: "testProject", + reader: bytes.NewBuffer([]byte(`{ + "description":"test description" + }`)), + expectedCode: http.StatusBadRequest, + projectClient: &mockProjectManager{}, + }, + { + label: "Missing Body Failure", + name: "testProject", + expectedCode: http.StatusBadRequest, + projectClient: &mockProjectManager{}, + }, + { + label: "Mismatched Name Failure", + name: "testProject", + expectedCode: http.StatusBadRequest, + reader: bytes.NewBuffer([]byte(`{ + "metadata" : { + "name": "testProjectNameMismatch", + "description": "Test Project used for unit testing" + } + }`)), + projectClient: &mockProjectManager{}, + }, + { + label: "Update Project", + name: "testProject", + expectedCode: http.StatusOK, + reader: bytes.NewBuffer([]byte(`{ + "metadata" : { + "name": "testProject", + "description": "Test Project used for unit testing" + } + }`)), + expected: moduleLib.Project{ + MetaData: moduleLib.ProjectMetaData{ + Name: "testProject", + Description: "Test Project used for unit testing", + UserData1: "update data1", + UserData2: "update data2", + }, + }, + projectClient: &mockProjectManager{ + //Items that will be returned by the mocked Client + Items: []moduleLib.Project{ + { + MetaData: moduleLib.ProjectMetaData{ + Name: "testProject", + Description: "Test Project used for unit testing", + UserData1: "update data1", + UserData2: "update data2", + }, + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("PUT", "/v2/projects/"+testCase.name, testCase.reader) + resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil, nil, nil, nil, 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 := moduleLib.Project{} + json.NewDecoder(resp.Body).Decode(&got) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("updateHandler returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestProjectGetHandler(t *testing.T) { testCases := []struct { -- cgit 1.2.3-korg