Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PUT metadata endpoint #51

Merged
merged 10 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions dataset/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ type DatasetClient interface {
GetEdition(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID, edition string) (m datasetclient.Edition, err error)
GetEditions(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID string) (m []datasetclient.Edition, err error)
GetVersion(ctx context.Context, userAuthToken, serviceAuthToken, downloadServiceAuthToken, collectionID, datasetID, edition, version string) (m datasetclient.Version, err error)
GetVersionWithHeaders(ctx context.Context, userAuthToken, serviceAuthToken, downloadServiceAuthToken, collectionID, datasetID, edition, version string) (datasetclient.Version, datasetclient.ResponseHeaders, error)
GetInstance(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, instanceID, ifMatch string) (i datasetclient.Instance, eTag string, err error)
PutDataset(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID string, d datasetclient.DatasetDetails) error
PutVersion(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID, edition, version string, v datasetclient.Version) error
PutInstance(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, instanceID string, i datasetclient.UpdateInstance, ifMatch string) (eTag string, err error)
PutMetadata(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID, edition, version string, metadata datasetclient.EditableMetadata, versionEtag string) error
}

type ZebedeeClient interface {
Expand Down
7 changes: 4 additions & 3 deletions dataset/get-metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func getEditMetadataHandler(w http.ResponseWriter, req *http.Request, dc Dataset
"version": version,
}

v, err := dc.GetVersion(ctx, userAccessToken, "", "", collectionID, datasetID, edition, version)
v, headers, err := dc.GetVersionWithHeaders(ctx, userAccessToken, "", "", collectionID, datasetID, edition, version)
if err != nil {
log.Error(ctx, "failed Get version details", err, log.Data(logInfo))
setErrorStatusCode(req, w, err, datasetID)
Expand Down Expand Up @@ -85,9 +85,10 @@ func getEditMetadataHandler(w http.ResponseWriter, req *http.Request, dc Dataset
return
}

p := mapper.EditMetadata(d.Next, v, dims, c)
editMetadata := mapper.EditMetadata(d.Next, v, dims, c)
editMetadata.VersionEtag = headers.ETag

b, err := json.Marshal(p)
b, err := json.Marshal(editMetadata)
if err != nil {
log.Error(ctx, "failed marshalling page into bytes", err)
setErrorStatusCode(req, w, err, datasetID)
Expand Down
116 changes: 66 additions & 50 deletions dataset/get-metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -37,13 +36,11 @@ func doTestRequest(target string, req *http.Request, handlerFunc http.HandlerFun

func TestUnitHandlers(t *testing.T) {
t.Parallel()
const mockUserAuthToken = ""
const mockServiceAuthToken = ""
const mockDownloadToken = ""
const mockCollectionID = ""
const mockUserAuthToken = "testuser"
const mockDatasetID = "bar"
const mockEdition = "baz"
const mockVersionNum = "1"
const mockCollectionId = "test-collection"
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

Expand All @@ -61,8 +58,9 @@ func TestUnitHandlers(t *testing.T) {
Convey("test getEditMetadataHandler", t, func() {

mockDatasetDetails := dataset.DatasetDetails{
ID: "test-dataset",
Links: dataset.Links{LatestVersion: dataset.Link{URL: "/v1/datasets/test/editions/test/version/1"}},
ID: "test-dataset",
CollectionID: mockCollectionId,
Links: dataset.Links{LatestVersion: dataset.Link{URL: "/v1/datasets/test/editions/test/version/1"}},
}

mockDataset := dataset.Dataset{
Expand All @@ -75,78 +73,96 @@ func TestUnitHandlers(t *testing.T) {
Version: 1,
}

datasetCollectionItem := zebedee.CollectionItem{
ID: mockDatasetDetails.ID,
State: "inProgress",
LastEditedBy: "an-user",
}

mockCollection := zebedee.Collection{
ID: "test-collection",
ID: mockCollectionId,
Datasets: []zebedee.CollectionItem{

{
ID: "foo",
State: "inProgress",
ID: "foo",
State: "reviewed",
LastEditedBy: "other-user",
},
datasetCollectionItem,
},
}

responseHeaders := dataset.ResponseHeaders{ETag: "version-etag"}

mockZebedeeClient := &ZebedeeClientMock{
GetCollectionFunc: func(ctx context.Context, userAccessToken, collectionID string) (c zebedeeclient.Collection, err error) {
return mockCollection, nil
if collectionID == mockCollectionId {
return mockCollection, nil
} else {
return c, errors.New("collection not found")
}
},
}

Convey("when Version.State is NOT edition-confirmed returns correctly with empty dimensions struct", func() {
mockDatasetClient := &DatasetClientMock{
GetDatasetCurrentAndNextFunc: func(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID string) (m datasetclient.Dataset, err error) {
return mockDataset, nil
},
GetVersionFunc: func(ctx context.Context, userAuthToken, serviceAuthToken, downloadServiceAuthToken, collectionID, datasetID, edition, version string) (datasetclient.Version, error) {
return mockVersionDetails, nil
},
GetVersionWithHeadersFunc: func(ctx context.Context, userAuthToken, serviceAuthToken, downloadServiceAuthToken, collectionID, datasetID, edition, version string) (datasetclient.Version, datasetclient.ResponseHeaders, error) {
return mockVersionDetails, responseHeaders, nil
},
}

mockDatasetClient := &DatasetClientMock{
GetDatasetCurrentAndNextFunc: func(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID string) (m datasetclient.Dataset, err error) {
return mockDataset, nil
},
GetVersionFunc: func(ctx context.Context, userAuthToken, serviceAuthToken, downloadServiceAuthToken, collectionID, datasetID, edition, version string) (m datasetclient.Version, err error) {
return mockVersionDetails, nil
},
}
Convey("when Version.State is NOT edition-confirmed returns correctly with empty dimensions struct", func() {
mockVersionDetails.State = "associated"

req := httptest.NewRequest("GET", "/datasets/bar/editions/baz/versions/1", nil)
req.Header.Set("Collection-Id", "testcollection")
req.Header.Set("X-Florence-Token", "testuser")
req.Header.Set("Collection-Id", mockCollectionId)
req.Header.Set("X-Florence-Token", mockUserAuthToken)
w := doTestRequest("/datasets/{datasetID}/editions/{editionID}/versions/{versionID}", req, GetMetadataHandler(mockDatasetClient, mockZebedeeClient), nil)

So(w.Code, ShouldEqual, http.StatusOK)
So(w.Body.String(), ShouldNotBeNil)

var body model.EditMetadata
err := json.Unmarshal(w.Body.Bytes(), &body)
So(err, ShouldBeNil)
So(body.Version, ShouldResemble, mockVersionDetails)
So(body.Dataset, ShouldResemble, *mockDataset.Next)
So(body.Dimensions, ShouldBeEmpty)
So(body.VersionEtag, ShouldEqual, responseHeaders.ETag)
So(body.CollectionID, ShouldEqual, mockCollectionId)
So(body.CollectionState, ShouldEqual, datasetCollectionItem.State)
So(body.CollectionLastEditedBy, ShouldEqual, datasetCollectionItem.LastEditedBy)
})

Convey("when Version.State is edition-confirmed returns correctly with populated dimensions struct", func() {
mockVersionDetails.State = "edition-confirmed"
mockVersionDetails.Version = 2

mockVersion := mockVersionDetails
mockVersion.State = "edition-confirmed"
mockVersion.Version = 2

var count int
mockDatasetClient := &DatasetClientMock{
GetDatasetCurrentAndNextFunc: func(ctx context.Context, userAuthToken, serviceAuthToken, collectionID, datasetID string) (m datasetclient.Dataset, err error) {
return mockDataset, nil
},
GetVersionFunc: func(ctx context.Context, userAuthToken, serviceAuthToken, downloadServiceAuthToken, collectionID, datasetID, edition, version string) (m datasetclient.Version, err error) {
var data datasetclient.Version
if count == 0 {
data = mockVersion
}
if count == 1 {
data = datasetclient.Version{Dimensions: []datasetclient.VersionDimension{{ID: "dim001", Label: "Test dimension"}}}
}
count++
return data, nil
},
}
mockVersionDetails.Dimensions = []datasetclient.VersionDimension{{ID: "dim001", Label: "Test dimension"}}

req := httptest.NewRequest("GET", "/datasets/bar/editions/baz/versions/1", nil)
req.Header.Set("Collection-Id", "testcollection")
req.Header.Set("X-Florence-Token", "testuser")
req.Header.Set("Collection-Id", mockCollectionId)
req.Header.Set("X-Florence-Token", mockUserAuthToken)
w := doTestRequest("/datasets/{datasetID}/editions/{editionID}/versions/{versionID}", req, GetMetadataHandler(mockDatasetClient, mockZebedeeClient), nil)

So(w.Code, ShouldEqual, http.StatusOK)
So(w.Body.String(), ShouldNotBeNil)

var body model.EditMetadata
b, _ := ioutil.ReadAll(w.Body)
_ = json.Unmarshal(b, &body)
So(body.Dataset.ID, ShouldEqual, "test-dataset")
So(body.Version.ID, ShouldEqual, "test-version")
So(len(body.Dimensions), ShouldBeGreaterThan, 0)
err := json.Unmarshal(w.Body.Bytes(), &body)
So(err, ShouldBeNil)
So(body.Version, ShouldResemble, mockVersionDetails)
So(body.Dataset, ShouldResemble, *mockDataset.Next)
So(body.Dimensions, ShouldResemble, mockVersionDetails.Dimensions)
So(body.VersionEtag, ShouldEqual, responseHeaders.ETag)
So(body.CollectionID, ShouldEqual, mockCollectionId)
So(body.CollectionState, ShouldEqual, datasetCollectionItem.State)
So(body.CollectionLastEditedBy, ShouldEqual, datasetCollectionItem.LastEditedBy)
})
})

Expand Down
Loading