diff options
Diffstat (limited to 'src/orchestrator/pkg')
-rw-r--r-- | src/orchestrator/pkg/module/project.go | 6 | ||||
-rw-r--r-- | src/orchestrator/pkg/module/project_test.go | 107 |
2 files changed, 109 insertions, 4 deletions
diff --git a/src/orchestrator/pkg/module/project.go b/src/orchestrator/pkg/module/project.go index 02f6d827..e86266b9 100644 --- a/src/orchestrator/pkg/module/project.go +++ b/src/orchestrator/pkg/module/project.go @@ -55,7 +55,7 @@ func (pk ProjectKey) String() string { // ProjectManager is an interface exposes the Project functionality type ProjectManager interface { - CreateProject(pr Project) (Project, error) + CreateProject(pr Project, exists bool) (Project, error) GetProject(name string) (Project, error) DeleteProject(name string) error GetAllProjects() ([]Project, error) @@ -78,7 +78,7 @@ func NewProjectClient() *ProjectClient { } // CreateProject a new collection based on the project -func (v *ProjectClient) CreateProject(p Project) (Project, error) { +func (v *ProjectClient) CreateProject(p Project, exists bool) (Project, error) { //Construct the composite key to select the entry key := ProjectKey{ @@ -87,7 +87,7 @@ func (v *ProjectClient) CreateProject(p Project) (Project, error) { //Check if this Project already exists _, err := v.GetProject(p.MetaData.Name) - if err == nil { + if err == nil && !exists { return Project{}, pkgerrors.New("Project already exists") } diff --git a/src/orchestrator/pkg/module/project_test.go b/src/orchestrator/pkg/module/project_test.go index f6856f86..947478b4 100644 --- a/src/orchestrator/pkg/module/project_test.go +++ b/src/orchestrator/pkg/module/project_test.go @@ -62,13 +62,39 @@ func TestCreateProject(t *testing.T) { Err: pkgerrors.New("Error Creating Project"), }, }, + { + label: "Create Existing Project", + inp: Project{ + MetaData: ProjectMetaData{ + Name: "testProject", + Description: "A sample Project used for unit testing", + UserData1: "data1", + UserData2: "data2", + }, + }, + expectedError: "Project already exists", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProjectKey{ProjectName: "testProject"}.String(): { + "projectmetadata": []byte( + "{" + + "\"metadata\" : {" + + "\"Name\":\"testProject\"," + + "\"Description\":\"Test project for unit testing\"," + + "\"UserData1\":\"userData1\"," + + "\"UserData2\":\"userData2\"}" + + "}"), + }, + }, + }, + }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { db.DBconn = testCase.mockdb impl := NewProjectClient() - got, err := impl.CreateProject(testCase.inp) + got, err := impl.CreateProject(testCase.inp, false) if err != nil { if testCase.expectedError == "" { t.Fatalf("Create returned an unexpected error %s", err) @@ -86,6 +112,85 @@ func TestCreateProject(t *testing.T) { } } +func TestUpdateProject(t *testing.T) { + testCases := []struct { + label string + inp Project + expectedError string + mockdb *db.MockDB + expected Project + }{ + { + label: "Update Project", + inp: Project{ + MetaData: ProjectMetaData{ + Name: "testProject", + Description: "Test project for unit testing", + UserData1: "update userData1", + UserData2: "update userData2", + }, + }, + expected: Project{ + MetaData: ProjectMetaData{ + Name: "testProject", + Description: "Test project for unit testing", + UserData1: "update userData1", + UserData2: "update userData2", + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProjectKey{ProjectName: "testProject"}.String(): { + "projectmetadata": []byte( + "{" + + "\"metadata\" : {" + + "\"Name\":\"testProject\"," + + "\"Description\":\"Test project for unit testing\"," + + "\"UserData1\":\"userData1\"," + + "\"UserData2\":\"userData2\"}" + + "}"), + }, + }, + }, + }, + { + label: "Failed Update Project", + inp: Project{ + MetaData: ProjectMetaData{ + Name: "unknownProject", + Description: "Unknown project for unit testing", + }, + }, + expectedError: "Creating DB Entry", + mockdb: &db.MockDB{ + Err: pkgerrors.New("Error Updating Project"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewProjectClient() + got, err := impl.CreateProject(testCase.inp, true) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Update returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Update returned an unexpected error %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("Update returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestGetProject(t *testing.T) { testCases := []struct { |