func TestSerializeObjectForPharos(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) if err != nil { t.Errorf("Error loading test data file '%s': %v", filename, err) } intelObj.Access = "Institution" // Make sure this is lower-cased below data, err := intelObj.SerializeForPharos() if err != nil { t.Errorf("Error serializing for Pharos: %v", err) return } hash := make(map[string]interface{}) err = json.Unmarshal(data, &hash) if err != nil { t.Errorf("Error unmarshalling data: %v", err) } objHash := hash["intellectual_object"] assert.NotNil(t, objHash) pharosObj := objHash.(map[string]interface{}) assert.Equal(t, "uc.edu/cin.675812", pharosObj["identifier"]) assert.Equal(t, "cin.675812", pharosObj["bag_name"]) assert.EqualValues(t, 12, pharosObj["institution_id"]) assert.Equal(t, "Notes from the Oesper Collections", pharosObj["title"]) assert.Equal(t, "A collection from Cincinnati", pharosObj["description"]) assert.Equal(t, "Photo Collection", pharosObj["alt_identifier"]) assert.Equal(t, "institution", pharosObj["access"]) }
func TestSerializeFileForPharos(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) genericFile := intelObj.GenericFiles[1] data, err := genericFile.SerializeForPharos() if err != nil { t.Errorf("Error serializing for Pharos: %v", err) return } topLevelHash := make(map[string]interface{}) err = json.Unmarshal(data, &topLevelHash) if err != nil { t.Errorf("Error unmarshalling data: %v", err) } require.NotNil(t, topLevelHash["generic_file"]) hash := topLevelHash["generic_file"].(map[string]interface{}) // Convert int and int64 to float64, because that's what JSON uses // TODO: Add this back when it's part of the Rails model. // assert.Equal(t, "2014-04-25T18:05:51-05:00", hash["file_modified"]) // assert.Equal(t, "2014-04-25T18:05:51-05:00", hash["file_created"]) assert.EqualValues(t, 606, hash["size"]) assert.Equal(t, "https://s3.amazonaws.com/aptrust.test.fixtures/restore_test/data/metadata.xml", hash["uri"]) assert.EqualValues(t, 741, hash["intellectual_object_id"]) assert.Equal(t, "application/xml", hash["file_format"]) assert.Equal(t, "uc.edu/cin.675812/data/metadata.xml", hash["identifier"]) // Note the Rails 4 naming convention checksums := hash["checksums_attributes"].([]interface{}) checksum0 := checksums[0].(map[string]interface{}) assert.EqualValues(t, nil, checksum0["id"]) // Don't serialize 0 ids. Pharos pukes. assert.Equal(t, "md5", checksum0["algorithm"]) assert.Equal(t, "2014-04-25T18:05:51-05:00", checksum0["datetime"]) assert.Equal(t, "c6d8080a39a0622f299750e13aa9c200", checksum0["digest"]) checksum1 := checksums[1].(map[string]interface{}) assert.EqualValues(t, nil, checksum1["id"]) // Don't serialize 0 ids. Pharos pukes. assert.Equal(t, "sha256", checksum1["algorithm"]) assert.Equal(t, "2014-08-12T20:51:20Z", checksum1["datetime"]) assert.Equal(t, "a418d61067718141d7254d7376d5499369706e3ade27cb84c4d5519f7cfed790", checksum1["digest"]) // Note the Rails 4 naming convention events := hash["premis_events_attributes"].([]interface{}) event0 := events[0].(map[string]interface{}) assert.EqualValues(t, nil, event0["id"]) // Don't serialize 0 ids. Pharos pukes. assert.EqualValues(t, 0, event0["intellectual_object_id"]) assert.Equal(t, "Success", event0["outcome"]) assert.Equal(t, "http://golang.org/pkg/crypto/md5/", event0["agent"]) assert.Equal(t, "2014-08-13T11:04:41-04:00", event0["date_time"]) assert.Equal(t, "Go crypto/md5", event0["object"]) assert.Equal(t, "Fixity matches", event0["outcome_information"]) assert.Equal(t, "md5:c6d8080a39a0622f299750e13aa9c200", event0["outcome_detail"]) assert.Equal(t, "fixity_check", event0["event_type"]) assert.Equal(t, "Fixity check against registered hash", event0["detail"]) assert.Equal(t, "uc.edu/cin.675812", event0["intellectual_object_identifier"]) }
func TestLoadIntelObjFixture(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") obj, err := testutil.LoadIntelObjFixture(filename) if err != nil { t.Error(err) } assert.NotEmpty(t, obj.Identifier) }
func intelObj(t *testing.T) *models.IntellectualObject { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") obj, err := testutil.LoadIntelObjFixture(filename) if err != nil { t.Errorf("Error loading test data file '%s': %v", filename, err) } return obj }
func TestTotalFileSize(t *testing.T) { filepath := filepath.Join("testdata", "json_objects", "intel_obj.json") obj, err := testutil.LoadIntelObjFixture(filepath) if err != nil { t.Errorf("Error loading test data file '%s': %v", filepath, err) } if obj.TotalFileSize() != 686 { t.Errorf("TotalFileSize() returned '%d', expected 686", obj.TotalFileSize()) } }
func TestChecksumSerializeForPharos(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) if err != nil { t.Errorf("Error loading test data file '%s': %v", filename, err) } checksum := intelObj.GenericFiles[0].Checksums[0] jsonData, err := checksum.SerializeForPharos() require.Nil(t, err) expected := `{"checksum":{"generic_file_id":0,"algorithm":"md5","datetime":"2014-04-25T18:05:51Z","digest":"8d7b0e3a24fc899b1d92a73537401805"}}` assert.Equal(t, expected, string(jsonData)) }
func TestNewIntellectualObjectForPharos(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) require.Nil(t, err) intelObj.Access = "INSTITUTION" // Just so we can test lowercase intelObj.DPNUUID = "a0903223-e956-40d8-a6e1-4d09edf7cea2" pharosObj := models.NewIntellectualObjectForPharos(intelObj) assert.Equal(t, intelObj.Identifier, pharosObj.Identifier) assert.Equal(t, intelObj.BagName, pharosObj.BagName) assert.Equal(t, intelObj.InstitutionId, pharosObj.InstitutionId) assert.Equal(t, intelObj.Title, pharosObj.Title) assert.Equal(t, intelObj.Description, pharosObj.Description) assert.Equal(t, intelObj.AltIdentifier, pharosObj.AltIdentifier) assert.Equal(t, strings.ToLower(intelObj.Access), pharosObj.Access) assert.Equal(t, intelObj.DPNUUID, pharosObj.DPNUUID) }
func TestAllFilesSaved(t *testing.T) { filepath := filepath.Join("testdata", "json_objects", "intel_obj.json") obj, err := testutil.LoadIntelObjFixture(filepath) if err != nil { t.Errorf("Error loading test data file '%s': %v", filepath, err) } assert.True(t, obj.AllFilesSaved()) gf := obj.FindGenericFile("data/object.properties") gf.IngestNeedsSave = true assert.False(t, obj.AllFilesSaved()) gf.IngestStorageURL = "https://example.com/primary" gf.IngestReplicationURL = "https://example.com/secondary" gf.IngestStoredAt = time.Now().UTC() gf.IngestReplicatedAt = time.Now().UTC() assert.True(t, obj.AllFilesSaved()) }
func TestFindEventByIdentifier(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) if err != nil { t.Errorf("Error loading test data file '%s': %v", filename, err) } if intelObj == nil { return } gf := intelObj.GenericFiles[0] event1 := gf.FindEventByIdentifier("6c705682-73ed-4609-6c11-30cb0cd1dcd9") event2 := gf.FindEventByIdentifier("bc78e9d9-a860-4ef1-5ae9-216151303c6a") require.NotNil(t, event1) require.NotNil(t, event2) assert.Equal(t, "identifier_assignment", event1.EventType) assert.Equal(t, "ingest", event2.EventType) }
func TestFindGenericFile(t *testing.T) { filepath := filepath.Join("testdata", "json_objects", "intel_obj.json") obj, err := testutil.LoadIntelObjFixture(filepath) if err != nil { t.Errorf("Error loading test data file '%s': %v", filepath, err) } gf1 := obj.FindGenericFile("data/object.properties") assert.NotNil(t, gf1) assert.Equal(t, "uc.edu/cin.675812/data/object.properties", gf1.Identifier) gf2 := obj.FindGenericFile("data/metadata.xml") assert.NotNil(t, gf2) assert.Equal(t, "uc.edu/cin.675812/data/metadata.xml", gf2.Identifier) // Make sure we don't get an error here assert.NotPanics(t, func() { obj.FindGenericFile("file/does/not/exist") }) gf3 := obj.FindGenericFile("file/does/not/exist") assert.Nil(t, gf3) }
func TestGetChecksumByDigest(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) if err != nil { t.Errorf("Error loading test data file '%s': %v", filename, err) } if intelObj == nil { return } genericFile := intelObj.GenericFiles[1] // MD5 md5Checksum := genericFile.GetChecksumByDigest("c6d8080a39a0622f299750e13aa9c200") assert.Equal(t, "md5", md5Checksum.Algorithm) // SHA256 sha256Checksum := genericFile.GetChecksumByDigest("a418d61067718141d7254d7376d5499369706e3ade27cb84c4d5519f7cfed790") assert.Equal(t, "sha256", sha256Checksum.Algorithm) // bogus checksum bogusChecksum := genericFile.GetChecksumByDigest("bogus") assert.Nil(t, bogusChecksum, "GetChecksum returned something it shouldn't have") }
func TestFindEventsByType(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) if err != nil { t.Errorf("Error loading test data file '%s': %v", filename, err) } if intelObj == nil { return } genericFile := intelObj.GenericFiles[1] // Typical generic file will have one ingest event, // but our fixture data shows multiple ingests. if len(genericFile.FindEventsByType("ingest")) != 2 { t.Errorf("Should have found 1 ingest event") } // Typical generic file will have two identifier assignments, // but our fixture data shows multiple ingests. if len(genericFile.FindEventsByType("identifier_assignment")) != 4 { t.Errorf("Should have found 2 identifier assignment events") } }
func TestNewGenericFileForPharos(t *testing.T) { filename := filepath.Join("testdata", "json_objects", "intel_obj.json") intelObj, err := testutil.LoadIntelObjFixture(filename) require.Nil(t, err) gf := intelObj.GenericFiles[1] pharosGf := models.NewGenericFileForPharos(gf) assert.Equal(t, gf.Identifier, pharosGf.Identifier) assert.Equal(t, gf.IntellectualObjectId, pharosGf.IntellectualObjectId) assert.Equal(t, gf.FileFormat, pharosGf.FileFormat) assert.Equal(t, gf.URI, pharosGf.URI) assert.Equal(t, gf.Size, pharosGf.Size) // TODO: Add these back when they're part of the Rails model //assert.Equal(t, gf.FileCreated, pharosGf.FileCreated) //assert.Equal(t, gf.FileModified, pharosGf.FileModified) assert.Equal(t, len(gf.Checksums), len(pharosGf.Checksums)) assert.Equal(t, len(gf.PremisEvents), len(pharosGf.PremisEvents)) for i := range gf.Checksums { assert.Equal(t, gf.Checksums[i].Digest, pharosGf.Checksums[i].Digest) } for i := range gf.PremisEvents { assert.Equal(t, gf.PremisEvents[i].EventType, pharosGf.PremisEvents[i].EventType) } }