func TestManifestBuildReplicationTransfer(t *testing.T) {
	nsqMessage := testutil.MakeNsqMessage("999")
	manifest := models.NewDPNIngestManifest(nsqMessage)
	fromNode := "aptrust"
	toNode := "chron"
	link := "[email protected]:outbound/1234567.tar"
	xfer, err := manifest.BuildReplicationTransfer(fromNode, toNode, link)
	// No DPNBag attached to manifest. Should get error.
	require.NotNil(t, err)

	manifest.DPNBag = testutil.MakeDPNBag()
	xfer, err = manifest.BuildReplicationTransfer(fromNode, toNode, link)
	require.Nil(t, err)
	require.NotNil(t, xfer)

	assert.Equal(t, fromNode, xfer.FromNode)
	assert.Equal(t, toNode, xfer.ToNode)
	assert.Equal(t, manifest.DPNBag.UUID, xfer.Bag)
	assert.True(t, util.LooksLikeUUID(xfer.ReplicationId))

	assert.Equal(t, constants.AlgSha256, xfer.FixityAlgorithm)
	assert.Nil(t, xfer.FixityNonce)
	assert.Nil(t, xfer.FixityValue)
	assert.Equal(t, "rsync", xfer.Protocol)
	assert.Equal(t, link, xfer.Link)
	assert.False(t, xfer.CreatedAt.IsZero())
	assert.False(t, xfer.UpdatedAt.IsZero())
}
func TestManifestBuildDPNIdentifierEvent(t *testing.T) {
	nsqMessage := testutil.MakeNsqMessage("999")
	manifest := models.NewDPNIngestManifest(nsqMessage)
	manifest.StorageURL = "https://example.com/my_bag.tar"

	// Should give error, since DPNBag is nil
	event, err := manifest.BuildDPNIdentifierEvent()
	require.NotNil(t, err)

	manifest.DPNBag = testutil.MakeDPNBag()
	event, err = manifest.BuildDPNIdentifierEvent()
	require.Nil(t, err)
	require.NotNil(t, event)

	assert.True(t, util.LooksLikeUUID(event.Identifier))
	assert.Equal(t, constants.EventIdentifierAssignment, event.EventType)
	assert.False(t, event.DateTime.IsZero())
	assert.Equal(t, "Item assigned DPN UUID", event.Detail)
	assert.Equal(t, constants.StatusSuccess, event.Outcome)
	assert.Equal(t, manifest.DPNBag.UUID, event.OutcomeDetail)
	assert.Equal(t, "APTrust exchange using Satori go.uuid", event.Object)
	assert.Equal(t, "https://github.com/satori/go.uuid", event.Agent)
	assert.True(t, strings.Contains(event.OutcomeInformation, manifest.DPNBag.UUID))
	assert.Equal(t, manifest.DPNBag.LocalId, event.IntellectualObjectIdentifier)
}
func TestRestoreTransferUpdate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)

	// The transfer request must refer to an actual bag,
	// so let's make a bag...
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)

	// Make sure we can create a transfer request.
	xfer := testutil.MakeRestoreRequest("chron", "aptrust", bag.UUID)
	createResp := client.RestoreTransferCreate(xfer)
	require.NotNil(t, createResp)
	require.Nil(t, createResp.Error)

	newXfer := createResp.RestoreTransfer()
	require.NotNil(t, newXfer)

	// Accept this one...
	newXfer.Accepted = true

	updateResp := client.RestoreTransferUpdate(newXfer)
	require.NotNil(t, updateResp)
	require.Nil(t, updateResp.Error)

	updatedXfer := updateResp.RestoreTransfer()
	require.NotNil(t, updatedXfer)

	assert.True(t, updatedXfer.Accepted)
}
func TestRestoreTransferCreate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)

	// The transfer request must refer to an actual bag,
	// so let's make a bag...
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)

	// Make sure we can create a transfer request.
	xfer := testutil.MakeRestoreRequest("tdr", "aptrust", bag.UUID)
	createResp := client.RestoreTransferCreate(xfer)
	require.NotNil(t, createResp)
	require.Nil(t, createResp.Error)

	createdXfer := createResp.RestoreTransfer()
	require.NotNil(t, createdXfer)
	assert.Equal(t, xfer.FromNode, createdXfer.FromNode)
	assert.Equal(t, xfer.ToNode, createdXfer.ToNode)
	assert.Equal(t, xfer.Bag, createdXfer.Bag)
	assert.NotEmpty(t, createdXfer.RestoreId)
	assert.Equal(t, xfer.Protocol, createdXfer.Protocol)
	assert.Equal(t, xfer.Link, createdXfer.Link)
	assert.NotEmpty(t, createdXfer.CreatedAt)
	assert.NotEmpty(t, createdXfer.UpdatedAt)
}
func TestDPNBagUpdate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)

	// We have to set UpdatedAt ahead, or the server won't update
	// record we're sending.
	newTimestamp := time.Now().UTC().Add(1 * time.Second).Truncate(time.Second)
	newLocalId := fmt.Sprintf("GO-TEST-BAG-%s", uuid.NewV4().String())

	dpnBag := resp.Bag()
	dpnBag.UpdatedAt = newTimestamp
	dpnBag.LocalId = newLocalId

	updateResp := client.DPNBagUpdate(dpnBag)
	require.NotNil(t, updateResp)
	require.Nil(t, updateResp.Error)
	updatedBag := updateResp.Bag()
	require.NotNil(t, updatedBag)
	assert.InDelta(t, newTimestamp.Unix(), updatedBag.UpdatedAt.Unix(), float64(2.0))
	assert.Equal(t, newLocalId, updatedBag.LocalId)
}
func TestDigestCreate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)

	// We have to make a new bag first, because
	// the existing bag already has a sha256 digest.
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)
	newBag := resp.Bag()
	require.NotNil(t, newBag)

	digest := testutil.MakeMessageDigest(newBag.UUID, "aptrust")
	resp = client.DigestCreate(digest)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)
	assert.NotNil(t, resp.Digest())
}
func TestDPNBagCreate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)
	newBag := resp.Bag()
	require.NotNil(t, newBag)
	assert.Equal(t, bag.UUID, newBag.UUID)
	assert.Equal(t, bag.LocalId, newBag.LocalId)
	assert.Equal(t, bag.Size, newBag.Size)
	assert.Equal(t, bag.FirstVersionUUID, newBag.FirstVersionUUID)
	assert.Equal(t, bag.Version, newBag.Version)
	assert.Equal(t, bag.BagType, newBag.BagType)
	assert.NotEmpty(t, newBag.IngestNode)
	assert.Equal(t, newBag.IngestNode, newBag.AdminNode)
	assert.NotEmpty(t, newBag.CreatedAt)
	assert.NotEmpty(t, newBag.UpdatedAt)
}
func TestReplicationTransferCreate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)

	// The transfer request must refer to an actual bag,
	// so let's make a bag...
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)

	// Make sure we can create a transfer request.
	xfer := testutil.MakeXferRequest("aptrust", "chron", resp.Bag().UUID)
	xferResp := client.ReplicationTransferCreate(xfer)
	require.NotNil(t, xferResp)
	require.Nil(t, xferResp.Error)

	newXfer := xferResp.ReplicationTransfer()
	require.NotNil(t, newXfer)

	assert.Equal(t, xfer.FromNode, newXfer.FromNode)
	assert.Equal(t, xfer.ToNode, newXfer.ToNode)
	assert.Equal(t, xfer.Bag, newXfer.Bag)
	assert.NotEmpty(t, newXfer.ReplicationId)
	assert.Equal(t, xfer.FixityAlgorithm, newXfer.FixityAlgorithm)
	assert.Equal(t, xfer.FixityNonce, newXfer.FixityNonce)
	assert.Equal(t, xfer.FixityValue, newXfer.FixityValue)
	assert.Equal(t, xfer.Stored, newXfer.Stored)
	assert.Equal(t, xfer.StoreRequested, newXfer.StoreRequested)
	assert.Equal(t, xfer.Cancelled, newXfer.Cancelled)
	assert.Equal(t, xfer.CancelReason, newXfer.CancelReason)
	assert.Equal(t, xfer.Protocol, newXfer.Protocol)
	assert.Equal(t, xfer.Link, newXfer.Link)
	assert.NotEmpty(t, newXfer.CreatedAt)
	assert.NotEmpty(t, newXfer.UpdatedAt)
}
func TestReplicationTransferUpdate(t *testing.T) {
	if runRestTests(t) == false {
		return
	}
	client := getClient(t)

	// The transfer request must refer to an actual bag,
	// so let's make a bag...
	bag := testutil.MakeDPNBag()
	resp := client.DPNBagCreate(bag)
	require.NotNil(t, resp)
	require.Nil(t, resp.Error)

	// Make sure we can create a transfer request.
	xfer := testutil.MakeXferRequest("chron", "aptrust", bag.UUID)

	// Null out the fixity value, because once it's set, we can't change
	// it. And below, we want to set a bad fixity value to see what happens.
	xfer.FixityValue = nil
	xferResp := client.ReplicationTransferCreate(xfer)
	require.NotNil(t, xferResp)
	require.Nil(t, xferResp.Error)

	// Mark as received, with a bad fixity.
	newXfer := xferResp.ReplicationTransfer()
	newFixityValue := "1234567890"
	newXfer.UpdatedAt = newXfer.UpdatedAt.Add(1 * time.Second)
	newXfer.FixityValue = &newFixityValue

	updateResp := client.ReplicationTransferUpdate(newXfer)
	require.NotNil(t, updateResp)
	require.Nil(t, updateResp.Error)
	updatedXfer := updateResp.ReplicationTransfer()
	require.NotNil(t, updatedXfer)
	assert.False(t, updatedXfer.StoreRequested)
}