func TestCheckumSave(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(checksumSaveHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } checksum := testutil.MakeChecksum() response := client.ChecksumSave(checksum, "test.edu/obj1/file.txt") // Check the request URL and method assert.Equal(t, "POST", response.Request.Method) assert.Equal(t, "/api/v2/checksums/test.edu%2Fobj1%2Ffile.txt", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) assert.EqualValues(t, "Checksum", response.ObjectType()) obj := response.Checksum() if obj == nil { t.Errorf("Checksum should not be nil") } assert.NotEqual(t, checksum.CreatedAt, obj.CreatedAt) assert.Equal(t, checksum.Digest, obj.Digest) }
func TestPremisEventGet(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(premisEventGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.PremisEventGet("000000000000-0000-0000-0000-00000000") // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/events/000000000000-0000-0000-0000-00000000/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj := response.PremisEvent() assert.EqualValues(t, "PremisEvent", response.ObjectType()) if obj == nil { t.Errorf("PremisEvent should not be nil") } assert.Equal(t, "000000000000-0000-0000-0000-00000000", obj.Identifier) assert.NotEqual(t, "", obj.EventType) }
func TestCheckumGet(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(checksumGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.ChecksumGet(123) // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/checksums/123/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) assert.EqualValues(t, "Checksum", response.ObjectType()) obj := response.Checksum() if obj == nil { t.Errorf("Checksum should not be nil") } assert.NotEqual(t, "", obj.Digest) }
func TestCheckumList(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(checksumListHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } params := url.Values{} params.Add("generic_file_identifier", "test.edu/obj1/file.txt") response := client.ChecksumList(params) // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/checksums/?generic_file_identifier=test.edu%2Fobj1%2Ffile.txt", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) assert.EqualValues(t, "Checksum", response.ObjectType()) list := response.Checksums() if list == nil { t.Errorf("Checksums should not be nil") } assert.Equal(t, 4, len(list)) }
func TestGenericFileSave(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(genericFileSaveHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } // --------------------------------------------- // First, test create... // --------------------------------------------- obj := testutil.MakeGenericFile(0, 0, "kollege.kom/objekt/file.xml") obj.Id = 0 response := client.GenericFileSave(obj) // Check the request URL and method assert.Equal(t, "POST", response.Request.Method) assert.Equal(t, "/api/v2/files/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj = response.GenericFile() assert.EqualValues(t, "GenericFile", response.ObjectType()) if obj == nil { t.Errorf("GenericFile should not be nil") } assert.NotEqual(t, "", obj.Identifier) // Make sure the client returns the SAVED object, // not the unsaved one we sent. assert.NotEqual(t, 0, obj.Id) // --------------------------------------------- // Now test with an update... // --------------------------------------------- obj = testutil.MakeGenericFile(0, 0, "kollege.kom/objekt/file.xml") origModTime := obj.UpdatedAt response = client.GenericFileSave(obj) // Check the request URL and method objIdEncoded := strings.Replace(obj.Identifier, " ", "%20", -1) expectedUrl := fmt.Sprintf("/api/v2/files/%s", strings.Replace(objIdEncoded, "/", "%2F", -1)) assert.Equal(t, "PUT", response.Request.Method) assert.Equal(t, expectedUrl, response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj = response.GenericFile() assert.EqualValues(t, "GenericFile", response.ObjectType()) if obj == nil { t.Errorf("GenericFile should not be nil") } assert.NotEqual(t, "", obj.Identifier) assert.Equal(t, 1000, obj.Id) assert.NotEqual(t, origModTime, obj.UpdatedAt) }
func TestDPNWorkItemGet(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(dpnWorkItemGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.DPNWorkItemGet(999) // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/dpn_item/999/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj := response.DPNWorkItem() assert.EqualValues(t, "DPNWorkItem", response.ObjectType()) if obj == nil { t.Errorf("DPNWorkItem should not be nil") } assert.NotEqual(t, "", obj.Task) assert.NotEqual(t, "", obj.Identifier) }
func TestGenericFileGet(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(genericFileGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.GenericFileGet("college.edu/object/file.xml") // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/files/college.edu%2Fobject%2Ffile.xml", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj := response.GenericFile() assert.EqualValues(t, "GenericFile", response.ObjectType()) if obj == nil { t.Errorf("GenericFile should not be nil") } assert.True(t, strings.HasPrefix(obj.Identifier, "kollege.kom/objekt")) // Check that child objects were parsed correctly assert.Equal(t, 2, len(obj.PremisEvents)) assert.Equal(t, 3, len(obj.Checksums)) }
func TestIntellectualObjectPushToDPN(t *testing.T) { // Note the handler. That's not an error, because this call returns // a WorkItem object. testServer := httptest.NewServer(http.HandlerFunc(workItemGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } // This method is used only in integration tests. response := client.IntellectualObjectPushToDPN("college.edu/object") // Check the request URL and method assert.Equal(t, "PUT", response.Request.Method) assert.Equal(t, "/api/v2/objects/college.edu%2Fobject/dpn", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) assert.EqualValues(t, "WorkItem", response.ObjectType()) item := response.WorkItem() require.NotNil(t, item) assert.NotEqual(t, 0, item.Id) assert.NotEqual(t, "", item.ObjectIdentifier) }
func TestPremisEventSave(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(premisEventSaveHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } // --------------------------------------------- // Test create only. PremisEvents cannot be updaed // --------------------------------------------- obj := testutil.MakePremisEvent() obj.Id = 0 response := client.PremisEventSave(obj) // Check the request URL and method assert.Equal(t, "POST", response.Request.Method) assert.Equal(t, "/api/v2/events/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj = response.PremisEvent() assert.EqualValues(t, "PremisEvent", response.ObjectType()) if obj == nil { t.Errorf("PremisEvent should not be nil") } assert.NotEqual(t, "", obj.Identifier) // Make sure the client returns the SAVED object, // not the unsaved one we sent. assert.NotEqual(t, 0, obj.Id) }
func TestWorkStateItemGet(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(workItemStateGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.WorkItemStateGet(999) // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/item_state/999/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj := response.WorkItemState() assert.EqualValues(t, "WorkItemState", response.ObjectType()) if obj == nil { t.Errorf("WorkItemState should not be nil") } assert.NotEqual(t, "", obj.Action) assert.NotEqual(t, "", obj.State) assert.False(t, obj.CreatedAt.IsZero()) assert.False(t, obj.UpdatedAt.IsZero()) }
func TestDPNWorkItemSave(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(dpnWorkItemSaveHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } // --------------------------------------------- // First, test create... // --------------------------------------------- obj := testutil.MakeDPNWorkItem() obj.Id = 0 response := client.DPNWorkItemSave(obj) // Check the request URL and method assert.Equal(t, "POST", response.Request.Method) assert.Equal(t, "/api/v2/dpn_item/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj = response.DPNWorkItem() assert.EqualValues(t, "DPNWorkItem", response.ObjectType()) if obj == nil { t.Errorf("DPNWorkItem should not be nil") } assert.NotEqual(t, "", obj.Task) assert.NotEqual(t, "", obj.Identifier) // Make sure the client returns the SAVED object, // not the unsaved one we sent. assert.NotEqual(t, 0, obj.Id) // --------------------------------------------- // Now test with an update... // --------------------------------------------- obj = testutil.MakeDPNWorkItem() origModTime := obj.UpdatedAt response = client.DPNWorkItemSave(obj) // Check the request URL and method expectedUrl := fmt.Sprintf("/api/v2/dpn_item/%d/", obj.Id) assert.Equal(t, "PUT", response.Request.Method) assert.Equal(t, expectedUrl, response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj = response.DPNWorkItem() assert.EqualValues(t, "DPNWorkItem", response.ObjectType()) if obj == nil { t.Errorf("DPNWorkItem should not be nil") } assert.Equal(t, 1000, obj.Id) assert.NotEqual(t, origModTime, obj.UpdatedAt) }
func TestInstitution(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(institutionGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v1", "user", "key") if err != nil { t.Error(err) return } resp := client.InstitutionGet("college.edu") assert.NotNil(t, resp.Institution()) }
func TestPremisEvents(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(premisEventListHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v1", "user", "key") if err != nil { t.Error(err) return } resp := client.PremisEventList(nil) assert.NotEmpty(t, resp.PremisEvents()) }
func TestWorkItemState(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(workItemStateGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v1", "user", "key") if err != nil { t.Error(err) return } resp := client.WorkItemStateGet(1000) assert.NotNil(t, resp.WorkItemState()) }
func TestGenericFile(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(genericFileGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v1", "user", "key") if err != nil { t.Error(err) return } resp := client.GenericFileGet("college.edu/object/file.xml") assert.NotNil(t, resp.GenericFile()) }
func TestIntellectualObject(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(intellectualObjectGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v1", "user", "key") if err != nil { t.Error(err) return } resp := client.IntellectualObjectGet("college.edu/object", true, false) assert.NotNil(t, resp.IntellectualObject()) }
func TestDPNWorkItem(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(dpnWorkItemGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v1", "user", "key") if err != nil { t.Error(err) return } resp := client.DPNWorkItemGet(999) assert.Nil(t, resp.Error) assert.NotNil(t, resp.DPNWorkItem()) }
// Initializes a reusable Pharos client. func (context *Context) initPharosClient() { pharosClient, err := network.NewPharosClient( context.Config.PharosURL, context.Config.PharosAPIVersion, os.Getenv("PHAROS_API_USER"), os.Getenv("PHAROS_API_KEY")) if err != nil { message := fmt.Sprintf("Exiting. Cannot initialize Pharos Client: %v", err) fmt.Fprintln(os.Stderr, message) context.MessageLog.Fatal(message) } context.PharosClient = pharosClient }
func TestIntellectualObjectGet(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(intellectualObjectGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.IntellectualObjectGet("college.edu/object", false, false) // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/objects/college.edu%2Fobject", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj := response.IntellectualObject() assert.EqualValues(t, "IntellectualObject", response.ObjectType()) if obj == nil { t.Errorf("IntellectualObject should not be nil") } assert.NotEqual(t, "", obj.Identifier) // Check that child objects were parsed correctly assert.Equal(t, 2, len(obj.GenericFiles)) assert.Equal(t, 3, len(obj.PremisEvents)) assert.Equal(t, 4, len(obj.GenericFiles[0].Checksums)) assert.Equal(t, 5, len(obj.IngestTags)) // Check with includeFiles option response = client.IntellectualObjectGet("college.edu/object", true, false) assert.Equal(t, "/api/v2/objects/college.edu%2Fobject?include_files=true", response.Request.URL.Opaque) // Check with includeEvents option response = client.IntellectualObjectGet("college.edu/object", false, true) assert.Equal(t, "/api/v2/objects/college.edu%2Fobject?include_events=true", response.Request.URL.Opaque) // Check with includeFiles and includeEvents options response = client.IntellectualObjectGet("college.edu/object", true, true) assert.Equal(t, "/api/v2/objects/college.edu%2Fobject?include_all_relations=true", response.Request.URL.Opaque) }
func TestRawResponseData(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(institutionGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v1", "user", "key") if err != nil { t.Error(err) return } resp := client.InstitutionGet("college.edu") // Should be able to call repeatedly without error. // Incorrect implementation would try to read from // closed network socket. for i := 0; i < 3; i++ { bytes, err := resp.RawResponseData() assert.NotNil(t, bytes) assert.NotEmpty(t, bytes) assert.Nil(t, err) } }
func TestDPNWorkItemList(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(dpnWorkItemListHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.DPNWorkItemList(nil) // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/dpn_item/?", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) assert.EqualValues(t, "DPNWorkItem", response.ObjectType()) list := response.DPNWorkItems() if list == nil { t.Errorf("DPNWorkItem list should not be nil") return } if len(list) != 4 { t.Errorf("DPNWorkItems list should have four items. Found %d.", len(list)) return } for _, obj := range list { assert.NotEqual(t, "", obj.Task) assert.NotEqual(t, "", obj.Identifier) } // Make sure params are added to URL params := sampleParams() response = client.DPNWorkItemList(params) expectedUrl := fmt.Sprintf("/api/v2/dpn_item/?%s", params.Encode()) assert.Equal(t, expectedUrl, response.Request.URL.Opaque) }
func TestGenericFileSaveBatch(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(genericFileSaveBatchHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } // --------------------------------------------- // Make an IntellectualObject with 5 GenericFiles, // each with 2 checksums and 2 events. // --------------------------------------------- obj := testutil.MakeIntellectualObject(5, 2, 2, 0) for _, gf := range obj.GenericFiles { gf.Id = 0 } response := client.GenericFileSaveBatch(obj.GenericFiles) // Check the request URL and method expectedUrl := fmt.Sprintf("/api/v2/files/%d/create_batch", obj.GenericFiles[0].IntellectualObjectId) assert.Equal(t, "POST", response.Request.Method) assert.Equal(t, expectedUrl, response.Request.URL.Opaque) // Basic sanity check on response values require.Nil(t, response.Error) savedFiles := response.GenericFiles() assert.EqualValues(t, "GenericFile", response.ObjectType()) assert.Equal(t, 5, len(savedFiles)) // Make sure the client returns the SAVED object, // not the unsaved one we sent. for _, gf := range savedFiles { assert.NotEqual(t, 0, gf.Id) assert.Equal(t, 2, len(gf.Checksums)) assert.Equal(t, 2, len(gf.PremisEvents)) } }
func TestInstitutionGet(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(institutionGetHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } response := client.InstitutionGet("college.edu") // Check the request URL and method assert.Equal(t, "GET", response.Request.Method) assert.Equal(t, "/api/v2/institutions/college.edu/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) assert.EqualValues(t, "Institution", response.ObjectType()) if response.Institution() == nil { t.Errorf("Institution should not be nil") } assert.NotEqual(t, "", response.Institution().Identifier) }
func TestWorkItemStateSave(t *testing.T) { testServer := httptest.NewServer(http.HandlerFunc(workItemStateSaveHandler)) defer testServer.Close() client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key") if err != nil { t.Error(err) return } // --------------------------------------------- // First, test create... // --------------------------------------------- obj := testutil.MakeWorkItemState() obj.Id = 0 response := client.WorkItemStateSave(obj) // Check the request URL and method assert.Equal(t, "POST", response.Request.Method) assert.Equal(t, "/api/v2/item_state/", response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj = response.WorkItemState() assert.EqualValues(t, "WorkItemState", response.ObjectType()) if obj == nil { t.Errorf("WorkItemState should not be nil") } assert.NotEqual(t, 0, obj.Id) assert.NotEqual(t, 0, obj.WorkItemId) assert.Equal(t, "Ingest", obj.Action) assert.Equal(t, `{"key1":"value1","key2":"value2"}`, obj.State) assert.False(t, obj.CreatedAt.IsZero()) assert.False(t, obj.UpdatedAt.IsZero()) // Make sure the client returns the SAVED object, // not the unsaved one we sent. assert.NotEqual(t, 0, obj.Id) // --------------------------------------------- // Now test with an update... // --------------------------------------------- obj = testutil.MakeWorkItemState() origModTime := obj.UpdatedAt origId := obj.Id origWorkItemId := obj.WorkItemId // Change this obj.State = `{"key3":"value3"}` response = client.WorkItemStateSave(obj) // Check the request URL and method expectedUrl := fmt.Sprintf("/api/v2/item_state/%d/", obj.Id) assert.Equal(t, "PUT", response.Request.Method) assert.Equal(t, expectedUrl, response.Request.URL.Opaque) // Basic sanity check on response values assert.Nil(t, response.Error) obj = response.WorkItemState() assert.EqualValues(t, "WorkItemState", response.ObjectType()) if obj == nil { t.Errorf("WorkItemState should not be nil") } assert.Equal(t, origId, obj.Id) assert.Equal(t, origWorkItemId, obj.WorkItemId) assert.Equal(t, `{"key3":"value3"}`, obj.State) assert.NotEqual(t, origModTime, obj.UpdatedAt) }
func TestNewPharosClient(t *testing.T) { _, err := network.NewPharosClient("http://example.com", "v2", "user", "key") if err != nil { t.Error(err) } }