Пример #1
0
func TestIntellectualObjectSave(t *testing.T) {
	testServer := httptest.NewServer(http.HandlerFunc(intellectualObjectSaveHandler))
	defer testServer.Close()

	client, err := network.NewPharosClient(testServer.URL, "v2", "user", "key")
	if err != nil {
		t.Error(err)
		return
	}

	// ---------------------------------------------
	// First, test create...
	// ---------------------------------------------
	obj := testutil.MakeIntellectualObject(0, 0, 0, 0)
	obj.Id = 0
	response := client.IntellectualObjectSave(obj)

	// Check the request URL and method
	assert.Equal(t, "POST", response.Request.Method)
	expectedUrl := fmt.Sprintf("/api/v2/objects/%s", obj.Institution)
	assert.Equal(t, expectedUrl, 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)

	// 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.MakeIntellectualObject(0, 0, 0, 0)
	origModTime := obj.UpdatedAt
	response = client.IntellectualObjectSave(obj)

	// Check the request URL and method
	expectedUrl = fmt.Sprintf("/api/v2/objects/%s", strings.Replace(obj.Identifier, "/", "%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.IntellectualObject()
	assert.EqualValues(t, "IntellectualObject", response.ObjectType())
	if obj == nil {
		t.Errorf("IntellectualObject should not be nil")
	}
	assert.NotEqual(t, "", obj.Identifier)
	assert.Equal(t, 1000, obj.Id)
	assert.NotEqual(t, origModTime, obj.UpdatedAt)
}
Пример #2
0
func TestObjPropagateIdsToChildren(t *testing.T) {
	// Make intel obj with 5 files, 5 events, 2 checksums, 0 tags
	// Obj will have 5 events, as will all GenericFiles
	obj := testutil.MakeIntellectualObject(5, 5, 2, 0)
	assert.Equal(t, 5, len(obj.GenericFiles))
	assert.Equal(t, 5, len(obj.PremisEvents))

	obj.PropagateIdsToChildren()

	for _, event := range obj.PremisEvents {
		assert.Equal(t, obj.Id, event.IntellectualObjectId)
		assert.Equal(t, obj.Identifier, event.IntellectualObjectIdentifier)
	}

	for _, gf := range obj.GenericFiles {
		assert.Equal(t, obj.Id, gf.IntellectualObjectId)
		assert.Equal(t, obj.Identifier, gf.IntellectualObjectIdentifier)
		for _, event := range gf.PremisEvents {
			assert.Equal(t, gf.Id, event.GenericFileId)
			assert.Equal(t, gf.Identifier, event.GenericFileIdentifier)
			assert.Equal(t, gf.IntellectualObjectId, event.IntellectualObjectId)
			assert.Equal(t, gf.IntellectualObjectIdentifier, event.IntellectualObjectIdentifier)
		}
		for _, checksum := range gf.Checksums {
			assert.Equal(t, gf.Id, checksum.GenericFileId)
		}
	}
}
Пример #3
0
func TestObjBuildIngestChecksums(t *testing.T) {
	// Make intel obj with 5 files, no events, checksums or tags
	obj := testutil.MakeIntellectualObject(5, 0, 0, 0)
	assert.Equal(t, 5, len(obj.GenericFiles))
	assert.Equal(t, 0, len(obj.PremisEvents))

	err := obj.BuildIngestChecksums()
	assert.Nil(t, err)

	for _, gf := range obj.GenericFiles {
		assert.Equal(t, 2, len(gf.Checksums))
		md5 := gf.GetChecksumByAlgorithm(constants.AlgMd5)
		sha256 := gf.GetChecksumByAlgorithm(constants.AlgSha256)
		require.NotNil(t, md5)
		require.NotNil(t, sha256)

		assert.Equal(t, md5.GenericFileId, gf.Id)
		assert.Equal(t, constants.AlgMd5, md5.Algorithm)
		assert.False(t, md5.DateTime.IsZero())
		assert.Equal(t, 32, len(md5.Digest))

		assert.Equal(t, sha256.GenericFileId, gf.Id)
		assert.Equal(t, constants.AlgSha256, sha256.Algorithm)
		assert.False(t, sha256.DateTime.IsZero())
		assert.Equal(t, 64, len(sha256.Digest))
	}

	// Calling this function again should not generate new checksums
	// when all the checksums are already present.
	err = obj.BuildIngestChecksums()
	assert.Nil(t, err)
	for _, gf := range obj.GenericFiles {
		assert.Equal(t, 2, len(gf.Checksums))
	}
}
Пример #4
0
func TestMakeIntellectualObject(t *testing.T) {
	obj := testutil.MakeIntellectualObject(2, 4, 6, 8)
	if obj == nil {
		t.Errorf("MakeIntellectualObject() returned nil")
		return
	}
	assert.NotEqual(t, 0, obj.Id)
	assert.NotEqual(t, "", obj.Identifier)
	assert.NotEqual(t, "", obj.BagName)
	assert.NotEqual(t, "", obj.Institution)
	assert.NotEqual(t, 0, obj.InstitutionId)
	assert.NotEqual(t, "", obj.Title)
	assert.NotEqual(t, "", obj.Description)
	assert.NotEqual(t, "", obj.Access)
	assert.NotEqual(t, "", obj.AltIdentifier)
	assert.False(t, obj.CreatedAt.IsZero())
	assert.False(t, obj.UpdatedAt.IsZero())

	assert.Equal(t, 2, len(obj.GenericFiles))
	for _, gf := range obj.GenericFiles {
		if gf == nil {
			t.Errorf("GenericFile should not be nil")
		} else {
			assert.Equal(t, obj.Identifier, gf.IntellectualObjectIdentifier)
			assert.Equal(t, 4, len(gf.PremisEvents))
			assert.Equal(t, 6, len(gf.Checksums))
		}
	}

	assert.Equal(t, 4, len(obj.PremisEvents))
	for _, event := range obj.PremisEvents {
		assert.NotNil(t, event)
	}

	assert.Equal(t, 8, len(obj.IngestTags))
	for _, tag := range obj.IngestTags {
		assert.NotNil(t, tag)
		if tag != nil {
			assert.NotEqual(t, "", tag.Label)
			assert.NotEqual(t, "", tag.Value)
		}
	}

	assert.NotEqual(t, "", obj.Institution)
	assert.NotEqual(t, "", obj.IngestS3Bucket)
	assert.NotEqual(t, "", obj.IngestS3Key)
	assert.NotEqual(t, "", obj.IngestTarFilePath)
	assert.NotEqual(t, "", obj.IngestUntarredPath)
	assert.NotEqual(t, "", obj.IngestRemoteMd5)
	assert.NotEqual(t, "", obj.IngestLocalMd5)
	assert.False(t, obj.IngestMd5Verified)
	assert.False(t, obj.IngestMd5Verifiable)
	assert.Equal(t, "", obj.IngestErrorMessage)
}
Пример #5
0
func intellectualObjectListHandler(w http.ResponseWriter, r *http.Request) {
	list := make([]*models.IntellectualObject, 4)
	for i := 0; i < 4; i++ {
		list[i] = testutil.MakeIntellectualObject(2, 3, 4, 5)
	}
	data := listResponseData()
	data["results"] = list
	listJson, _ := json.Marshal(data)
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintln(w, string(listJson))
}
Пример #6
0
func TestObjBuildIngestEvents(t *testing.T) {
	// Make intel obj with 5 files, no events, checksums or tags
	obj := testutil.MakeIntellectualObject(5, 0, 0, 0)
	assert.Equal(t, 5, len(obj.GenericFiles))
	assert.Equal(t, 0, len(obj.PremisEvents))

	err := obj.BuildIngestEvents()
	assert.Nil(t, err)

	// Expecting four PREMIS events total for IntelObj, each with
	// correct IntelObj.Id and IntelObj.Identifier.
	assert.Equal(t, 4, len(obj.PremisEvents))
	assert.Equal(t, 1, len(obj.FindEventsByType(constants.EventCreation)))
	assert.Equal(t, 1, len(obj.FindEventsByType(constants.EventIdentifierAssignment)))
	assert.Equal(t, 1, len(obj.FindEventsByType(constants.EventAccessAssignment)))
	assert.Equal(t, 1, len(obj.FindEventsByType(constants.EventIngestion)))

	for _, event := range obj.PremisEvents {
		assert.Equal(t, obj.Id, event.IntellectualObjectId)
		assert.Equal(t, obj.Identifier, event.IntellectualObjectIdentifier)
	}

	// PREMIS events should be set correctly for all of this
	// object's GenericFiles
	for _, gf := range obj.GenericFiles {
		assert.Equal(t, 6, len(gf.PremisEvents))
		assert.Equal(t, 1, len(gf.FindEventsByType(constants.EventFixityCheck)))
		assert.Equal(t, 1, len(gf.FindEventsByType(constants.EventDigestCalculation)))
		assert.Equal(t, 2, len(gf.FindEventsByType(constants.EventIdentifierAssignment)))
		assert.Equal(t, 1, len(gf.FindEventsByType(constants.EventReplication)))
		assert.Equal(t, 1, len(gf.FindEventsByType(constants.EventIngestion)))

		for _, event := range gf.PremisEvents {
			assert.Equal(t, gf.IntellectualObjectId, event.IntellectualObjectId)
			assert.Equal(t, gf.IntellectualObjectIdentifier, event.IntellectualObjectIdentifier)
			assert.Equal(t, gf.Id, event.GenericFileId)
			assert.Equal(t, gf.Identifier, event.GenericFileIdentifier)
		}
	}

	// Calling this function again should not generate new events
	// if all the events are there.
	err = obj.BuildIngestEvents()
	assert.Nil(t, err)
	assert.Equal(t, 4, len(obj.PremisEvents))
	for _, gf := range obj.GenericFiles {
		assert.Equal(t, 6, len(gf.PremisEvents))
	}
}
Пример #7
0
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))
	}
}
Пример #8
0
func intellectualObjectGetHandler(w http.ResponseWriter, r *http.Request) {
	obj := testutil.MakeIntellectualObject(2, 3, 4, 5)
	objJson, _ := json.Marshal(obj)
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintln(w, string(objJson))
}