Skip to content
Snippets Groups Projects
service_test.go 9.82 KiB
Newer Older
  • Learn to ignore specific revisions
  • package tasklist_test
    
    import (
    	"context"
    	"testing"
    
    	"github.com/stretchr/testify/assert"
    	"go.uber.org/zap"
    
    	"code.vereign.com/gaiax/tsa/golib/errors"
    	goatasklist "code.vereign.com/gaiax/tsa/task/gen/task_list"
    	"code.vereign.com/gaiax/tsa/task/internal/service/task"
    	"code.vereign.com/gaiax/tsa/task/internal/service/tasklist"
    	"code.vereign.com/gaiax/tsa/task/internal/service/tasklist/tasklistfakes"
    )
    
    func TestNew(t *testing.T) {
    
    	svc := tasklist.New(nil, nil, nil, zap.NewNop())
    
    	assert.Implements(t, (*goatasklist.Service)(nil), svc)
    }
    
    func Test_Create(t *testing.T) {
    	tests := []struct {
    		name    string
    		req     *goatasklist.CreateTaskListRequest
    		storage *tasklistfakes.FakeStorage
    		queue   *tasklistfakes.FakeQueue
    
    		errkind errors.Kind
    		errtext string
    	}{
    		{
    			name:    "empty taskList name",
    			req:     &goatasklist.CreateTaskListRequest{},
    			errkind: errors.BadRequest,
    			errtext: "missing taskListName",
    		},
    		{
    			name: "taskList template not found",
    			req:  &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
    					return nil, errors.New(errors.NotFound)
    				},
    			},
    			errkind: errors.NotFound,
    			errtext: "not found",
    		},
    		{
    			name: "error getting task templates form storage",
    			req:  &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
    					return &tasklist.Template{}, nil
    				},
    				TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
    					return nil, errors.New(errors.Internal, "internal error")
    				},
    			},
    			errkind: errors.Internal,
    			errtext: "internal error",
    		},
    		{
    			name: "error creating tasks for a taskList, task template not found",
    			req:  &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
    					return &tasklist.Template{
    						Groups: []tasklist.GroupTemplate{
    							{
    								Tasks: []string{"non-existent task template"},
    							},
    						},
    					}, nil
    				},
    				TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
    					return map[string]*task.Task{"template": &task.Task{}}, nil
    				},
    			},
    			errkind: errors.NotFound,
    			errtext: "failed to find task template",
    		},
    		{
    			name: "failed to add taskList and tasks to queue",
    			req:  &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
    					return &tasklist.Template{}, nil
    				},
    				TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
    					return map[string]*task.Task{"template": &task.Task{}}, nil
    				},
    			},
    			queue: &tasklistfakes.FakeQueue{
    				AddTaskListStub: func(ctx context.Context, list *tasklist.TaskList, tasks []*task.Task) error {
    					return errors.New("storage error")
    				},
    			},
    			errkind: errors.Unknown,
    			errtext: "storage error",
    		},
    		{
    			name: "successfully add taskList and tasks to queue",
    			req:  &goatasklist.CreateTaskListRequest{TaskListName: "taskList name"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListTemplateStub: func(ctx context.Context, s string) (*tasklist.Template, error) {
    					return &tasklist.Template{}, nil
    				},
    				TaskTemplatesStub: func(ctx context.Context, strings []string) (map[string]*task.Task, error) {
    					return map[string]*task.Task{"template": &task.Task{}}, nil
    				},
    			},
    			queue: &tasklistfakes.FakeQueue{
    				AddTaskListStub: func(ctx context.Context, list *tasklist.TaskList, tasks []*task.Task) error {
    					return nil
    				},
    			},
    		},
    	}
    
    	for _, test := range tests {
    		t.Run(test.name, func(t *testing.T) {
    
    			svc := tasklist.New(test.storage, test.queue, nil, zap.NewNop())
    
    			res, err := svc.Create(context.Background(), test.req)
    			if err != nil {
    				assert.NotEmpty(t, test.errtext)
    				e, ok := err.(*errors.Error)
    				assert.True(t, ok)
    				assert.Equal(t, test.errkind, e.Kind)
    				assert.Contains(t, e.Error(), test.errtext)
    				assert.Nil(t, res)
    			} else {
    				assert.Empty(t, test.errtext)
    				assert.NotNil(t, res)
    				assert.NotEmpty(t, res.TaskListID)
    			}
    
    		})
    	}
    }
    
    
    func Test_TaskListResult(t *testing.T) {
    	tests := []struct {
    		name    string
    		req     *goatasklist.TaskListResultRequest
    		storage *tasklistfakes.FakeStorage
    		queue   *tasklistfakes.FakeQueue
    		cache   *tasklistfakes.FakeCache
    
    		errkind errors.Kind
    		errtext string
    	}{
    		{
    			name:    "missing taskList ID",
    			req:     &goatasklist.TaskListResultRequest{},
    			errkind: errors.BadRequest,
    			errtext: "missing taskListID",
    		},
    		{
    			name: "error getting taskList form history collection",
    			req:  &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return nil, errors.New("some error")
    				},
    			},
    			errkind: errors.Unknown,
    			errtext: "some error",
    		},
    		{
    			name: "taskList not found",
    			req:  &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return nil, errors.New(errors.NotFound)
    				},
    				TaskListStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return nil, errors.New(errors.NotFound)
    				},
    			},
    			errkind: errors.NotFound,
    			errtext: "taskList is not found",
    		},
    		{
    			name: "error getting taskList from taskLists collection",
    			req:  &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return nil, errors.New(errors.NotFound)
    				},
    				TaskListStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return nil, errors.New("some error")
    				},
    			},
    			errkind: errors.Unknown,
    			errtext: "some error",
    		},
    		{
    			name: "error calculating taskList state",
    			req:  &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return pendingTaskList, nil
    				},
    				GetGroupTasksStub: func(ctx context.Context, group *tasklist.Group) ([]*task.Task, error) {
    					return nil, errors.New("some error")
    				},
    			},
    			errkind: errors.Unknown,
    			errtext: "some error",
    		},
    		{
    			name: "error getting taskList from cache",
    			req:  &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return doneTaskList, nil
    				},
    			},
    			cache: &tasklistfakes.FakeCache{
    				GetStub: func(ctx context.Context, key, namespace, scope string) ([]byte, error) {
    					return nil, errors.New("some cache error")
    				},
    			},
    			errkind: errors.Unknown,
    			errtext: "some cache error",
    		},
    		{
    			name: "successfully get taskList state on pending task",
    			req:  &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return pendingTaskList, nil
    				},
    				GetGroupTasksStub: func(ctx context.Context, group *tasklist.Group) ([]*task.Task, error) {
    					return []*task.Task{}, nil
    				},
    			},
    		},
    		{
    			name: "successfully get taskList state on executed task",
    			req:  &goatasklist.TaskListResultRequest{TaskListID: "d16996cd-1977-42a9-90b2-b4548a35c1b4"},
    			storage: &tasklistfakes.FakeStorage{
    				TaskListHistoryStub: func(ctx context.Context, taskListID string) (*tasklist.TaskList, error) {
    					return doneTaskList, nil
    				},
    			},
    			cache: &tasklistfakes.FakeCache{
    				GetStub: func(ctx context.Context, key, namespace, scope string) ([]byte, error) {
    					return doneTaskState, nil
    				},
    			},
    		},
    	}
    
    	for _, test := range tests {
    		t.Run(test.name, func(t *testing.T) {
    			svc := tasklist.New(test.storage, test.queue, test.cache, zap.NewNop())
    			res, err := svc.TaskListResult(context.Background(), test.req)
    			if err != nil {
    				assert.NotEmpty(t, test.errtext)
    				e, ok := err.(*errors.Error)
    				assert.True(t, ok)
    				assert.Equal(t, test.errkind, e.Kind)
    				assert.Contains(t, e.Error(), test.errtext)
    				assert.Nil(t, res)
    			} else {
    				assert.Empty(t, test.errtext)
    				assert.NotNil(t, res)
    				assert.NotEmpty(t, res.ID)
    				assert.NotEmpty(t, res.Status)
    				assert.NotEmpty(t, res.Groups)
    			}
    		})
    	}
    }
    
    //nolint:gosec
    var pendingTaskList = &tasklist.TaskList{
    	ID:    "16996cd-1977-42a9-90b2-b4548a35c1b4",
    	State: "pending",
    	Groups: []tasklist.Group{
    		{
    			ID:    "074076d5-c995-4d2d-8d38-da57360453d4",
    			Tasks: []string{"createdTask", "createdTask2"},
    			State: "created",
    		},
    	},
    }
    
    //nolint:gosec
    var doneTaskList = &tasklist.TaskList{
    	ID:    "16996cd-1977-42a9-90b2-b4548a35c1b4",
    	State: "done",
    }
    
    //nolint:gosec
    var doneTaskState = []byte(`{
      "id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
      "status": "done",
      "groups": [
    	{
    	  "id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
    	  "type": "sequential",
    	  "status": "done",
    	  "tasks": [
    		{
    		  "id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
    		  "status": "done"
    		},
    		{
    		  "id": "ad641603-1ca0-4342-ad73-d70a6b1ec502",
    		  "status": "done"
    		}
    	  ]
    	}
      ]
    }`)