Skip to content

Commit 329371a

Browse files
Add collection integration for job migration
1 parent f68f20a commit 329371a

29 files changed

+920
-194
lines changed

application/application.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type JobService interface {
2323
GetJob(ctx context.Context, jobNumber int) (*domain.Job, error)
2424
ClaimJob(ctx context.Context) (*domain.Job, error)
2525
UpdateJobState(ctx context.Context, jobNumber int, newState domain.State, userID string) error
26+
UpdateJobCollectionID(ctx context.Context, jobNumber int, collectionID string) error
2627
GetJobs(ctx context.Context, field sort.SortParameterField, direction sort.SortParameterDirection, states []domain.State, limit, offset int) ([]*domain.Job, int, error)
2728
GetJobTasks(ctx context.Context, states []domain.State, jobNumber int, limit, offset int) ([]*domain.Task, int, error)
2829
CreateTask(ctx context.Context, jobNumber int, task *domain.Task) (*domain.Task, error)
@@ -112,6 +113,27 @@ func (js *jobService) GetJob(ctx context.Context, jobNumber int) (*domain.Job, e
112113
return js.store.GetJob(ctx, jobNumber)
113114
}
114115

116+
// UpdateJobCollectionID updates the collection ID of a migration job.
117+
func (js *jobService) UpdateJobCollectionID(ctx context.Context, jobNumber int, collectionID string) error {
118+
job, err := js.store.GetJob(ctx, jobNumber)
119+
if err != nil {
120+
return err
121+
}
122+
123+
job.Config.CollectionID = collectionID
124+
125+
now := time.Now().UTC()
126+
127+
job.LastUpdated = now
128+
129+
err = js.store.UpdateJob(ctx, job)
130+
if err != nil {
131+
return fmt.Errorf("failed to update job collection ID: %w", err)
132+
}
133+
134+
return nil
135+
}
136+
115137
// UpdateJobState updates the state of a migration job and logs
116138
// an event with the requesting user's ID.
117139
func (js *jobService) UpdateJobState(ctx context.Context, jobNumber int, newState domain.State, userID string) error {

application/application_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,120 @@ func TestUpdateTaskState(t *testing.T) {
12951295
})
12961296
}
12971297

1298+
func TestUpdateJobCollectionID(t *testing.T) {
1299+
Convey("Given a job service and store with an existing job", t, func() {
1300+
oldTime := time.Date(2024, time.January, 10, 12, 0, 0, 0, time.UTC)
1301+
fakeJob := &domain.Job{
1302+
ID: "test-job-id",
1303+
JobNumber: testJobNumber,
1304+
Config: &domain.JobConfig{
1305+
CollectionID: "old-collection-id",
1306+
},
1307+
LastUpdated: oldTime,
1308+
}
1309+
1310+
mockMongo := &storeMocks.MongoDBMock{
1311+
GetJobFunc: func(ctx context.Context, jobNumber int) (*domain.Job, error) {
1312+
return fakeJob, nil
1313+
},
1314+
UpdateJobFunc: func(ctx context.Context, job *domain.Job) error {
1315+
return nil
1316+
},
1317+
}
1318+
1319+
mockStore := store.Datastore{
1320+
Backend: mockMongo,
1321+
}
1322+
1323+
mockClients := clients.ClientList{}
1324+
cfg := &config.Config{EnableEventLogging: false}
1325+
jobService := Setup(&mockStore, &mockClients, cfg)
1326+
1327+
ctx := context.Background()
1328+
newCollectionID := "new-collection-id"
1329+
1330+
Convey("When UpdateJobCollectionID is called", func() {
1331+
err := jobService.UpdateJobCollectionID(ctx, fakeJob.JobNumber, newCollectionID)
1332+
1333+
Convey("Then the store should be called to update the job", func() {
1334+
So(len(mockMongo.UpdateJobCalls()), ShouldEqual, 1)
1335+
So(mockMongo.UpdateJobCalls()[0].Job.Config.CollectionID, ShouldEqual, newCollectionID)
1336+
So(mockMongo.UpdateJobCalls()[0].Job.LastUpdated.After(oldTime), ShouldBeTrue)
1337+
1338+
Convey("And no error should be returned", func() {
1339+
So(err, ShouldBeNil)
1340+
})
1341+
})
1342+
})
1343+
})
1344+
1345+
Convey("Given a job service and store that returns an error when getting a job", t, func() {
1346+
mockMongo := &storeMocks.MongoDBMock{
1347+
GetJobFunc: func(ctx context.Context, jobNumber int) (*domain.Job, error) {
1348+
return nil, appErrors.ErrJobNotFound
1349+
},
1350+
}
1351+
1352+
mockStore := store.Datastore{
1353+
Backend: mockMongo,
1354+
}
1355+
1356+
mockClients := clients.ClientList{}
1357+
cfg := &config.Config{EnableEventLogging: false}
1358+
jobService := Setup(&mockStore, &mockClients, cfg)
1359+
1360+
ctx := context.Background()
1361+
1362+
Convey("When UpdateJobCollectionID is called", func() {
1363+
err := jobService.UpdateJobCollectionID(ctx, testJobNumber, "new-collection-id")
1364+
1365+
Convey("Then the error should be returned and no update performed", func() {
1366+
So(err, ShouldEqual, appErrors.ErrJobNotFound)
1367+
So(len(mockMongo.UpdateJobCalls()), ShouldEqual, 0)
1368+
})
1369+
})
1370+
})
1371+
1372+
Convey("Given a job service and store that returns an error when updating a job", t, func() {
1373+
fakeJob := &domain.Job{
1374+
ID: "test-job-id",
1375+
JobNumber: testJobNumber,
1376+
Config: &domain.JobConfig{
1377+
CollectionID: "old-collection-id",
1378+
},
1379+
}
1380+
1381+
mockMongo := &storeMocks.MongoDBMock{
1382+
GetJobFunc: func(ctx context.Context, jobNumber int) (*domain.Job, error) {
1383+
return fakeJob, nil
1384+
},
1385+
UpdateJobFunc: func(ctx context.Context, job *domain.Job) error {
1386+
return fmt.Errorf("fake error for testing")
1387+
},
1388+
}
1389+
1390+
mockStore := store.Datastore{
1391+
Backend: mockMongo,
1392+
}
1393+
1394+
mockClients := clients.ClientList{}
1395+
cfg := &config.Config{EnableEventLogging: false}
1396+
jobService := Setup(&mockStore, &mockClients, cfg)
1397+
1398+
ctx := context.Background()
1399+
1400+
Convey("When UpdateJobCollectionID is called", func() {
1401+
err := jobService.UpdateJobCollectionID(ctx, testJobNumber, "new-collection-id")
1402+
1403+
Convey("Then an error should be returned", func() {
1404+
So(len(mockMongo.UpdateJobCalls()), ShouldEqual, 1)
1405+
So(err, ShouldNotBeNil)
1406+
So(err.Error(), ShouldContainSubstring, "failed to update job collection ID")
1407+
})
1408+
})
1409+
})
1410+
}
1411+
12981412
func TestGetJobTasks(t *testing.T) {
12991413
Convey("Given a job service and store that has stored tasks for a job", t, func() {
13001414
mockMongo := &storeMocks.MongoDBMock{

application/mock/jobservice.go

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ type RedirectAPIClient interface {
2323
// ZebedeeClient is an interface defining the methods for the Zebedee
2424
// (github.com/ONSdigital/zebedee) client.
2525
type ZebedeeClient interface {
26+
CreateCollection(ctx context.Context, userAuthToken string, collection zebedee.Collection) (zebedee.Collection, error)
2627
GetDataset(ctx context.Context, userAccessToken, collectionID, lang, path string) (d zebedee.Dataset, err error)
2728
GetDatasetLandingPage(ctx context.Context, userAccessToken, collectionID, lang, path string) (d zebedee.DatasetLandingPage, err error)
2829
GetFileSize(ctx context.Context, userAccessToken, collectionID, lang, uri string) (f zebedee.FileSize, err error)
2930
GetPageData(ctx context.Context, userAuthToken, collectionID, lang, path string) (m zebedee.PageData, err error)
3031
GetResourceStream(ctx context.Context, userAuthToken, collectionID, lang, path string) (s io.ReadCloser, err error)
32+
SaveContentToCollection(ctx context.Context, userAuthToken, collectionID, path string, content interface{}) error
3133
}

0 commit comments

Comments
 (0)