Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type JobService interface {
GetJob(ctx context.Context, jobNumber int) (*domain.Job, error)
ClaimJob(ctx context.Context) (*domain.Job, error)
UpdateJobState(ctx context.Context, jobNumber int, newState domain.State, userID string) error
UpdateJobCollectionID(ctx context.Context, jobNumber int, collectionID string) error
GetJobs(ctx context.Context, field sort.SortParameterField, direction sort.SortParameterDirection, states []domain.State, limit, offset int) ([]*domain.Job, int, error)
GetJobTasks(ctx context.Context, states []domain.State, jobNumber int, limit, offset int) ([]*domain.Task, int, error)
CreateTask(ctx context.Context, jobNumber int, task *domain.Task) (*domain.Task, error)
Expand Down Expand Up @@ -112,6 +113,27 @@ func (js *jobService) GetJob(ctx context.Context, jobNumber int) (*domain.Job, e
return js.store.GetJob(ctx, jobNumber)
}

// UpdateJobCollectionID updates the collection ID of a migration job.
func (js *jobService) UpdateJobCollectionID(ctx context.Context, jobNumber int, collectionID string) error {
job, err := js.store.GetJob(ctx, jobNumber)
if err != nil {
return err
}

job.Config.CollectionID = collectionID

now := time.Now().UTC()

job.LastUpdated = now

err = js.store.UpdateJob(ctx, job)
if err != nil {
return fmt.Errorf("failed to update job collection ID: %w", err)
}

return nil
}

// UpdateJobState updates the state of a migration job and logs
// an event with the requesting user's ID.
func (js *jobService) UpdateJobState(ctx context.Context, jobNumber int, newState domain.State, userID string) error {
Expand Down
114 changes: 114 additions & 0 deletions application/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,120 @@ func TestUpdateTaskState(t *testing.T) {
})
}

func TestUpdateJobCollectionID(t *testing.T) {
Convey("Given a job service and store with an existing job", t, func() {
oldTime := time.Date(2024, time.January, 10, 12, 0, 0, 0, time.UTC)
fakeJob := &domain.Job{
ID: "test-job-id",
JobNumber: testJobNumber,
Config: &domain.JobConfig{
CollectionID: "old-collection-id",
},
LastUpdated: oldTime,
}

mockMongo := &storeMocks.MongoDBMock{
GetJobFunc: func(ctx context.Context, jobNumber int) (*domain.Job, error) {
return fakeJob, nil
},
UpdateJobFunc: func(ctx context.Context, job *domain.Job) error {
return nil
},
}

mockStore := store.Datastore{
Backend: mockMongo,
}

mockClients := clients.ClientList{}
cfg := &config.Config{EnableEventLogging: false}
jobService := Setup(&mockStore, &mockClients, cfg)

ctx := context.Background()
newCollectionID := "new-collection-id"

Convey("When UpdateJobCollectionID is called", func() {
err := jobService.UpdateJobCollectionID(ctx, fakeJob.JobNumber, newCollectionID)

Convey("Then the store should be called to update the job", func() {
So(len(mockMongo.UpdateJobCalls()), ShouldEqual, 1)
So(mockMongo.UpdateJobCalls()[0].Job.Config.CollectionID, ShouldEqual, newCollectionID)
So(mockMongo.UpdateJobCalls()[0].Job.LastUpdated.After(oldTime), ShouldBeTrue)

Convey("And no error should be returned", func() {
So(err, ShouldBeNil)
})
})
})
})

Convey("Given a job service and store that returns an error when getting a job", t, func() {
mockMongo := &storeMocks.MongoDBMock{
GetJobFunc: func(ctx context.Context, jobNumber int) (*domain.Job, error) {
return nil, appErrors.ErrJobNotFound
},
}

mockStore := store.Datastore{
Backend: mockMongo,
}

mockClients := clients.ClientList{}
cfg := &config.Config{EnableEventLogging: false}
jobService := Setup(&mockStore, &mockClients, cfg)

ctx := context.Background()

Convey("When UpdateJobCollectionID is called", func() {
err := jobService.UpdateJobCollectionID(ctx, testJobNumber, "new-collection-id")

Convey("Then the error should be returned and no update performed", func() {
So(err, ShouldEqual, appErrors.ErrJobNotFound)
So(len(mockMongo.UpdateJobCalls()), ShouldEqual, 0)
})
})
})

Convey("Given a job service and store that returns an error when updating a job", t, func() {
fakeJob := &domain.Job{
ID: "test-job-id",
JobNumber: testJobNumber,
Config: &domain.JobConfig{
CollectionID: "old-collection-id",
},
}

mockMongo := &storeMocks.MongoDBMock{
GetJobFunc: func(ctx context.Context, jobNumber int) (*domain.Job, error) {
return fakeJob, nil
},
UpdateJobFunc: func(ctx context.Context, job *domain.Job) error {
return fmt.Errorf("fake error for testing")
},
}

mockStore := store.Datastore{
Backend: mockMongo,
}

mockClients := clients.ClientList{}
cfg := &config.Config{EnableEventLogging: false}
jobService := Setup(&mockStore, &mockClients, cfg)

ctx := context.Background()

Convey("When UpdateJobCollectionID is called", func() {
err := jobService.UpdateJobCollectionID(ctx, testJobNumber, "new-collection-id")

Convey("Then an error should be returned", func() {
So(len(mockMongo.UpdateJobCalls()), ShouldEqual, 1)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "failed to update job collection ID")
})
})
})
}

func TestGetJobTasks(t *testing.T) {
Convey("Given a job service and store that has stored tasks for a job", t, func() {
mockMongo := &storeMocks.MongoDBMock{
Expand Down
56 changes: 56 additions & 0 deletions application/mock/jobservice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions clients/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ type RedirectAPIClient interface {
// ZebedeeClient is an interface defining the methods for the Zebedee
// (github.com/ONSdigital/zebedee) client.
type ZebedeeClient interface {
CreateCollection(ctx context.Context, userAuthToken string, collection zebedee.Collection) (zebedee.Collection, error)
GetDataset(ctx context.Context, userAccessToken, collectionID, lang, path string) (d zebedee.Dataset, err error)
GetDatasetLandingPage(ctx context.Context, userAccessToken, collectionID, lang, path string) (d zebedee.DatasetLandingPage, err error)
GetFileSize(ctx context.Context, userAccessToken, collectionID, lang, uri string) (f zebedee.FileSize, err error)
GetPageData(ctx context.Context, userAuthToken, collectionID, lang, path string) (m zebedee.PageData, err error)
GetResourceStream(ctx context.Context, userAuthToken, collectionID, lang, path string) (s io.ReadCloser, err error)
SaveContentToCollection(ctx context.Context, userAuthToken, collectionID, path string, content interface{}) error
}
Loading