예제 #1
0
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)
}
예제 #2
0
func TestGenericFileMergeAttributes(t *testing.T) {
	gf1 := testutil.MakeGenericFile(3, 3, "test.edu/file1.txt")
	gf2 := testutil.MakeGenericFile(3, 3, "test.edu/file1.txt")

	// Match up identifiers, so copy assignment will work.
	for i, event := range gf1.PremisEvents {
		gf2.PremisEvents[i].Identifier = event.Identifier
	}
	for i, cs := range gf1.Checksums {
		gf2.Checksums[i].Algorithm = cs.Algorithm
		gf2.Checksums[i].Digest = cs.Digest
	}

	errors := gf1.MergeAttributes(gf2)
	require.Empty(t, errors)

	for i, event := range gf1.PremisEvents {
		assert.Equal(t, gf2.PremisEvents[i].Id, event.Id)
		assert.Equal(t, gf2.PremisEvents[i].CreatedAt, event.CreatedAt)
		assert.Equal(t, gf2.PremisEvents[i].UpdatedAt, event.UpdatedAt)
		// Ids and identifiers should propagate
		assert.Equal(t, gf1.Id, event.GenericFileId)
		assert.Equal(t, gf1.Identifier, event.GenericFileIdentifier)
		assert.Equal(t, gf1.IntellectualObjectId, event.IntellectualObjectId)
		assert.Equal(t, gf1.IntellectualObjectIdentifier, event.IntellectualObjectIdentifier)
	}
	for i, cs := range gf1.Checksums {
		assert.Equal(t, gf2.Checksums[i].Id, cs.Id)
		assert.Equal(t, gf2.Checksums[i].CreatedAt, cs.CreatedAt)
		assert.Equal(t, gf2.Checksums[i].UpdatedAt, cs.UpdatedAt)
		// Ids should propagate too
		assert.Equal(t, gf1.Id, cs.GenericFileId)
	}

}
예제 #3
0
func TestPropagateIdsToChildren(t *testing.T) {
	// Make a generic file with 6 events and 2 checksums
	gf := testutil.MakeGenericFile(6, 2, "test.edu/test_bag/file.txt")
	assert.Equal(t, 6, len(gf.PremisEvents))
	assert.Equal(t, 2, len(gf.Checksums))

	// Check pre-condition before actual test.
	for _, event := range gf.PremisEvents {
		assert.NotEqual(t, gf.Id, event.GenericFileId)
		assert.NotEqual(t, gf.Identifier, event.GenericFileIdentifier)
		assert.NotEqual(t, gf.IntellectualObjectId, event.IntellectualObjectId)
		assert.NotEqual(t, gf.IntellectualObjectIdentifier, event.IntellectualObjectIdentifier)
	}
	for _, checksum := range gf.Checksums {
		assert.NotEqual(t, gf.Id, checksum.GenericFileId)
	}

	gf.PropagateIdsToChildren()
	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)
	}
}
예제 #4
0
func TestBuildIngestChecksums(t *testing.T) {
	gf := testutil.MakeGenericFile(0, 0, "test.edu/test_bag/file.txt")
	assert.Equal(t, 0, len(gf.Checksums))
	err := gf.BuildIngestChecksums()
	assert.Nil(t, err)
	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 = gf.BuildIngestChecksums()
	assert.Nil(t, err)
	assert.Equal(t, 2, len(gf.Checksums))
}
예제 #5
0
func TestBuildIngestEvents(t *testing.T) {
	gf := testutil.MakeGenericFile(0, 0, "test.edu/test_bag/file.txt")
	assert.Equal(t, 0, len(gf.PremisEvents))
	err := gf.BuildIngestEvents()
	assert.Nil(t, err)
	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 = gf.BuildIngestEvents()
	assert.Nil(t, err)
	assert.Equal(t, 6, len(gf.PremisEvents))
}
예제 #6
0
func genericFileListHandler(w http.ResponseWriter, r *http.Request) {
	list := make([]*models.GenericFile, 4)
	for i := 0; i < 4; i++ {
		list[i] = testutil.MakeGenericFile(2, 3, "kollege.kom/objekt")
	}
	data := listResponseData()
	data["results"] = list
	listJson, _ := json.Marshal(data)
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintln(w, string(listJson))
}
예제 #7
0
func TestMakeGenericFile(t *testing.T) {
	objIdentifier := "virginia.edu/some_object"
	gf := testutil.MakeGenericFile(2, 3, objIdentifier)
	if gf == nil {
		t.Errorf("MakeGenericFile() returned nil")
		return
	}
	assert.NotEqual(t, 0, gf.Id)
	assert.True(t, strings.HasPrefix(gf.Identifier, objIdentifier))
	assert.NotEqual(t, 0, gf.IntellectualObjectId)
	assert.Equal(t, objIdentifier, gf.IntellectualObjectIdentifier)
	assert.NotEqual(t, "", gf.FileFormat)
	assert.True(t, strings.HasPrefix(gf.URI, constants.S3UriPrefix))
	assert.NotEqual(t, 0, gf.Size)
	assert.False(t, gf.FileCreated.IsZero())
	assert.False(t, gf.FileModified.IsZero())
	assert.False(t, gf.CreatedAt.IsZero())
	assert.False(t, gf.UpdatedAt.IsZero())
	assert.Equal(t, 3, len(gf.Checksums))
	for _, cs := range gf.Checksums {
		assert.NotNil(t, cs)
	}
	assert.Equal(t, 2, len(gf.PremisEvents))
	for _, event := range gf.PremisEvents {
		assert.NotNil(t, event)
	}
	assert.NotEqual(t, "", gf.IngestLocalPath)
	assert.NotEqual(t, "", gf.IngestMd5)
	assert.False(t, gf.IngestMd5VerifiedAt.IsZero())
	assert.NotEqual(t, "", gf.IngestSha256)
	assert.False(t, gf.IngestSha256GeneratedAt.IsZero())
	assert.NotEqual(t, "", gf.IngestUUID)
	assert.False(t, gf.IngestUUIDGeneratedAt.IsZero())
	assert.NotEqual(t, "", gf.IngestStorageURL)
	assert.False(t, gf.IngestStoredAt.IsZero())
	assert.False(t, gf.IngestPreviousVersionExists)
	assert.True(t, gf.IngestNeedsSave)
	assert.Equal(t, "", gf.IngestErrorMessage)
}
예제 #8
0
func genericFileGetHandler(w http.ResponseWriter, r *http.Request) {
	obj := testutil.MakeGenericFile(2, 3, "kollege.kom/objekt")
	objJson, _ := json.Marshal(obj)
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintln(w, string(objJson))
}