func TestNewPremisEventForPharos(t *testing.T) {
	event := testutil.MakePremisEvent()
	pharosEvent := models.NewPremisEventForPharos(event)
	assert.Equal(t, event.Id, pharosEvent.Id)
	assert.Equal(t, event.Identifier, pharosEvent.Identifier)
	assert.Equal(t, event.EventType, pharosEvent.EventType)
	assert.Equal(t, event.DateTime, pharosEvent.DateTime)
	assert.Equal(t, event.Detail, pharosEvent.Detail)
	assert.Equal(t, event.Outcome, pharosEvent.Outcome)
	assert.Equal(t, event.OutcomeDetail, pharosEvent.OutcomeDetail)
	assert.Equal(t, event.Object, pharosEvent.Object)
	assert.Equal(t, event.Agent, pharosEvent.Agent)
	assert.Equal(t, event.OutcomeInformation, pharosEvent.OutcomeInformation)
	assert.Equal(t, event.IntellectualObjectId, pharosEvent.IntellectualObjectId)
	assert.Equal(t, event.IntellectualObjectIdentifier, pharosEvent.IntellectualObjectIdentifier)
	assert.Equal(t, event.GenericFileId, pharosEvent.GenericFileId)
	assert.Equal(t, event.GenericFileIdentifier, pharosEvent.GenericFileIdentifier)
}
Exemple #2
0
// PremisEventSave saves a PREMIS event to Pharos. If the event ID is zero,
// this issues a POST request to create a new event record. If the ID is
// non-zero, this issues a PUT to update the existing event. The response
// object will have a new copy of the Premis event if the save was successful.
func (client *PharosClient) PremisEventSave(obj *models.PremisEvent) *PharosResponse {
	// Set up the response object
	resp := NewPharosResponse(PharosPremisEvent)
	resp.events = make([]*models.PremisEvent, 1)

	// URL and method
	relativeUrl := fmt.Sprintf("/api/%s/events/", client.apiVersion)
	httpMethod := "POST"
	if obj.Id > 0 {
		// PUT is not even implemented in Pharos, and never will be
		relativeUrl = fmt.Sprintf("%s/%s", relativeUrl, escapeSlashes(obj.Identifier))
		httpMethod = "PUT"
	}
	absoluteUrl := client.BuildUrl(relativeUrl)

	// Prepare the JSON data
	pharosObj := models.NewPremisEventForPharos(obj)
	postData, err := json.Marshal(pharosObj)
	if err != nil {
		resp.Error = err
	}

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

	// Parse the JSON from the response body
	event := &models.PremisEvent{}
	resp.Error = json.Unmarshal(resp.data, event)
	if resp.Error == nil {
		resp.events[0] = event
	}
	return resp
}