This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref #99
- Loading branch information
Showing
11 changed files
with
207 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,55 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-openapi/runtime/middleware" | ||
"github.com/google/uuid" | ||
"github.com/sul-dlss-labs/taco" | ||
"github.com/sul-dlss-labs/taco/generated/models" | ||
"github.com/sul-dlss-labs/taco/generated/restapi/operations" | ||
"github.com/sul-dlss-labs/taco/persistence" | ||
) | ||
|
||
// NewDepositResource -- Accepts requests to create resource and pushes them to Kinesis. | ||
func NewDepositResource(rt *taco.Runtime) operations.DepositNewResourceHandler { | ||
return &depositResourceEntry{} | ||
return &depositResourceEntry{rt: rt} | ||
} | ||
|
||
type depositResourceEntry struct{} | ||
type depositResourceEntry struct { | ||
rt *taco.Runtime | ||
} | ||
|
||
// Handle the delete entry request | ||
func (d *depositResourceEntry) Handle(params operations.DepositNewResourceParams) middleware.Responder { | ||
// TODO: This should be a DRUID | ||
resourceID, _ := uuid.NewRandom() | ||
|
||
response := &models.DepositNewResourceOKBody{ID: resourceID.String()} | ||
resourceID := mintID() | ||
|
||
if err := d.persistResource(resourceID, params); err != nil { | ||
// TODO: handle this with an error response | ||
panic(err) | ||
} | ||
|
||
response := &models.DepositNewResourceOKBody{ID: resourceID} | ||
return operations.NewDepositNewResourceOK().WithPayload(response) | ||
} | ||
|
||
func (d *depositResourceEntry) persistResource(resourceID string, params operations.DepositNewResourceParams) error { | ||
resource := d.persistableResourceFromParams(resourceID, params) | ||
fmt.Println("Saving") | ||
return d.rt.Repository().SaveItem(resource) | ||
} | ||
|
||
func (d *depositResourceEntry) persistableResourceFromParams(resourceID string, params operations.DepositNewResourceParams) *persistence.Resource { | ||
resource := &persistence.Resource{ID: resourceID} | ||
// TODO: Expand this mapping: | ||
resource.Title = *params.Payload.Title | ||
resource.SourceID = *params.Payload.SourceID | ||
return resource | ||
} | ||
|
||
func mintID() string { | ||
// TODO: This should be a DRUID | ||
resourceID, _ := uuid.NewRandom() | ||
return resourceID.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/appleboy/gofight" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/sul-dlss-labs/taco/persistence" | ||
) | ||
|
||
func mockErrorRepo() persistence.Repository { | ||
return &fakeErroringRepository{} | ||
} | ||
|
||
type fakeErroringRepository struct{} | ||
|
||
func (f *fakeErroringRepository) GetByID(id string) (*persistence.Resource, error) { | ||
return nil, nil | ||
} | ||
|
||
func (f *fakeErroringRepository) SaveItem(resource *persistence.Resource) error { | ||
return errors.New("not found") | ||
} | ||
|
||
func TestCreateResourceHappyPath(t *testing.T) { | ||
r := gofight.New() | ||
repo := mockRepo(nil) | ||
|
||
r.POST("/v1/resource"). | ||
SetJSON(gofight.D{ | ||
"id": "oo000oo0001", | ||
"sourceId": "bib12345678", | ||
"title": "My work", | ||
}). | ||
Run(setupFakeRuntime(repo), | ||
func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { | ||
assert.Equal(t, http.StatusOK, r.Code) | ||
assert.Equal(t, 1, len(repo.(*fakeRepository).CreatedResources)) | ||
|
||
}) | ||
} | ||
|
||
func TestCreateResourceMissingSourceId(t *testing.T) { | ||
r := gofight.New() | ||
r.POST("/v1/resource"). | ||
SetJSON(gofight.D{ | ||
"id": "oo000oo0001", | ||
"title": "My work", | ||
}). | ||
Run(setupFakeRuntime(mockRepo(nil)), | ||
func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { | ||
assert.Equal(t, http.StatusUnprocessableEntity, r.Code) | ||
}) | ||
} | ||
|
||
// TODO: Handle errors | ||
// func TestCreateResourceFailure(t *testing.T) { | ||
// r := gofight.New() | ||
// r.POST("/v1/resource"). | ||
// SetJSON(gofight.D{ | ||
// "id": "oo000oo0001", | ||
// "sourceId": "bib12345678", | ||
// "title": "My work", | ||
// }). | ||
// Run(setupFakeRuntime(mockErrorRepo()), | ||
// func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { | ||
// assert.Equal(t, http.StatusUnprocessableEntity, r.Code) | ||
// }) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.