Esempio n. 1
0
// GenericFileSaveBatch saves a batch of Generic File records to Pharos.
// This performs a POST to create a new records, so all of the GenericFiles
// passed in param objList should have Ids of zero. Each record
// must also have an IntellectualObject ID. The response object will
// be a list containing a new copy of each GenericFile that was saved.
// The new copies have correct ids and timestamps. On the Pharos end,
// the batch insert is run as a transaction, so either all inserts
// succeed, or the whole transaction is rolled back and no inserts
// occur.
func (client *PharosClient) GenericFileSaveBatch(objList []*models.GenericFile) *PharosResponse {
	// Set up the response object
	resp := NewPharosResponse(PharosGenericFile)
	resp.files = make([]*models.GenericFile, len(objList))

	if len(objList) == 0 {
		resp.Error = fmt.Errorf("GenericFileSaveBatch was asked to save an empty list.")
		return resp
	}
	for _, gf := range objList {
		if gf.Id != 0 {
			resp.Error = fmt.Errorf("One or more GenericFiles in the list " +
				"passed to GenericFileSaveBatch has a non-zero id. This call " +
				"is for creating new GenericFiles only.")
			return resp
		}
	}

	// URL and method
	relativeUrl := fmt.Sprintf("/api/%s/files/%d/create_batch",
		client.apiVersion, objList[0].IntellectualObjectId)
	httpMethod := "POST"
	absoluteUrl := client.BuildUrl(relativeUrl)

	// Prepare the JSON data
	// TODO: Define a PharosGenericFile object, and serialize a list of those.
	batch := make([]*models.GenericFileForPharos, len(objList))
	for i, gf := range objList {
		batch[i] = models.NewGenericFileForPharos(gf)
	}

	// Prepare the JSON data
	postData, err := json.Marshal(batch)
	if err != nil {
		resp.Error = fmt.Errorf("Error marshalling GenericFile batch to JSON: %v", err)
		return resp
	}

	// Run the request
	client.DoRequest(resp, httpMethod, absoluteUrl, bytes.NewBuffer(postData))
	if resp.Error != nil {
		return resp
	}

	resp.UnmarshalJsonList()
	return resp
}
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)
	}
}