<commit_before>package core_test

import (
	"github.com/APTrust/bagit/core"
	// "github.com/APTrust/bagit/util/fileutil"
	"github.com/APTrust/bagit/util/testutil"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"io/ioutil"
	"os"
	// "path/filepath"
	"strings"
	"testing"
)

var APTrustDefaultTags = map[string]string{
	"BagIt-Version":               "0.97",
	"Tag-File-Character-Encoding": "UTF-8",
	"Source-Organization":         "APTrust",
	"Title":                       "Test Object",
	"Access":                      "Institution",
}

var DPNDefaultTags = map[string]string{
	"BagIt-Version":               "0.97",
	"Tag-File-Character-Encoding": "UTF-8",
	"Contact-Name":                "<NAME>",
	"Contact-Phone":               "555-555-1212",
	"Bag-Size":                    "411",
	"Bag-Group-Identifier":        "None",
	"Source-Organization":         "APTrust",
	"Organization-Address":        "160 McCormick Rd, Charlottesville, VA 22904",
	"Contact-Email":               "<EMAIL>",
	"Bagging-Date":                "2017-07-26",
	"Bag-Count":                   "1",
	"Ingest-Node-Name":            "aptrust",
	"Ingest-Node-Contact-Name":    "<NAME>",
	"Ingest-Node-Contact-Email":   "<EMAIL>",
	"First-Version-Object-ID":     "00af15fd-1046-4811-8cb5-878ec66cf0da",
	"Interpretive-Object-ID":      "83bbc27a-86ef-4d1d-be09-4d78cf9e7df3",
	"Rights-Object-ID":            "3559d615-6df9-4f30-a2b0-511568359787",
	"DPN-Object-ID":               "00af15fd-1046-4811-8cb5-878ec66cf0da",
	"Local-ID":                    "Homer's Beer Can Collection",
	"Ingest-Node-Address":         "160 McCormick Rd, Charlottesville, VA 22904",
	"Version-Number":              "1",
	"Bag-Type":                    "data",
}

func getBaggerPreReqs(t *testing.T) (tempDir string, aptrustProfile *core.BagItProfile) {
	tempDir, err := ioutil.TempDir("", "bagger_test")
	require.Nil(t, err)

	profilePath, err := testutil.GetPathToTestProfile("aptrust_bagit_profile_2.0.json")
	require.Nil(t, err)
	aptrustProfile, err = core.LoadBagItProfile(profilePath)
	require.Nil(t, err)

	return tempDir, aptrustProfile
}

func TestNewBagger(t *testing.T) {
	tempDir, aptrustProfile := getBaggerPreReqs(t)
	defer os.RemoveAll(tempDir)

	bagger, err := core.NewBagger("", aptrustProfile)
	require.NotNil(t, err)
	assert.Equal(t, "Param bagPath cannot be empty", err.Error())

	bagger, err = core.NewBagger(tempDir, nil)
	require.NotNil(t, err)
	assert.Equal(t, "Param profile cannot be nil", err.Error())

	bagger, err = core.NewBagger(tempDir, aptrustProfile)
	require.Nil(t, err)
	require.NotNil(t, bagger)
	require.NotNil(t, bagger.Bag())
	require.NotNil(t, bagger.Profile())
}

func TestHasRequiredTags(t *testing.T) {
	tempDir, aptrustProfile := getBaggerPreReqs(t)
	defer os.RemoveAll(tempDir)

	// Make sure we flag missing required APTrust tags
	bagger, err := core.NewBagger(tempDir, aptrustProfile)
	require.Nil(t, err)
	require.NotNil(t, bagger)

	assert.False(t, bagger.WriteBag(true, true))
	errors := bagger.Errors()
	require.NotEmpty(t, errors)
	require.Equal(t, len(APTrustDefaultTags), len(errors))

	for tagName, _ := range APTrustDefaultTags {
		foundError := false
		for _, err := range errors {
			if strings.Contains(err, tagName) {
				foundError = true
			}
		}
		assert.True(t, foundError, "Bagger did not flag missing tag %s", tagName)
	}

	// Make sure bagger flags missing required DPN tags
	profilePath, err := testutil.GetPathToTestProfile("dpn_bagit_profile.json")
	require.Nil(t, err)
	dpnProfile, err := core.LoadBagItProfile(profilePath)
	require.Nil(t, err)

	bagger, err = core.NewBagger(tempDir, dpnProfile)
	require.Nil(t, err)
	require.NotNil(t, bagger)

	assert.False(t, bagger.WriteBag(true, true))
	errors = bagger.Errors()
	require.NotEmpty(t, errors)
	require.Equal(t, len(DPNDefaultTags), len(errors))

	for tagName, _ := range DPNDefaultTags {
		foundError := false
		for _, err := range errors {
			if strings.Contains(err, tagName) {
				foundError = true
			}
		}
		assert.True(t, foundError, "Bagger did not flag missing tag %s", tagName)
	}
}

// func TestBuildBag(t *testing.T) {
// 	tempFile, payloadDir, aptrustProfile := getBaggerPreReqs(t)
// 	dir := filepath.Dir(tempFile.Name())
// 	tempFile.Close()
// 	os.Remove(filepath.Dir(tempFile.Name()))

// 	bagger := core.NewBagger(dir, payloadDir, aptrustProfile, APTrustDefaultTags, true)
// 	require.NotNil(t, bagger)

// 	assert.True(t, bagger.BuildBag())
// 	require.Empty(t, bagger.Errors())<commit_msg>Restructured default tags in test<commit_after>package core_test

import (
	"github.com/APTrust/bagit/core"
	// "github.com/APTrust/bagit/util/fileutil"
	"github.com/APTrust/bagit/util/testutil"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"io/ioutil"
	"os"
	// "path/filepath"
	"strings"
	"testing"
)

var APTrustDefaultTags = map[string][]core.KeyValuePair{
	"bagit.txt": []core.KeyValuePair{
		core.NewKeyValuePair("BagIt-Version", "0.97"),
		core.NewKeyValuePair("Tag-File-Character-Encoding", "UTF-8"),
	},
	"aptrust-info.txt": []core.KeyValuePair{
		core.NewKeyValuePair("Source-Organization", "APTrust"),
		core.NewKeyValuePair("Title", "Test Object"),
		core.NewKeyValuePair("Access", "Institution"),
	},
}

var DPNDefaultTags = map[string][]core.KeyValuePair{
	"bagit.txt": []core.KeyValuePair{
		core.NewKeyValuePair("BagIt-Version", "0.97"),
		core.NewKeyValuePair("Tag-File-Character-Encoding", "UTF-8"),
	},
	"dpn-tags/dpn-info.txt": []core.KeyValuePair{
		core.NewKeyValuePair("Contact-Name", "<NAME>"),
		core.NewKeyValuePair("Contact-Phone", "555-555-1212"),
		core.NewKeyValuePair("Bag-Size", "411"),
		core.NewKeyValuePair("Bag-Group-Identifier", "None"),
		core.NewKeyValuePair("Source-Organization", "APTrust"),
		core.NewKeyValuePair("Organization-Address", "160 McCormick Rd, Charlottesville, VA 22904"),
		core.NewKeyValuePair("Contact-Email", "<EMAIL>"),
		core.NewKeyValuePair("Bagging-Date", "2017-07-26"),
		core.NewKeyValuePair("Bag-Count", "1"),
		core.NewKeyValuePair("Ingest-Node-Name", "aptrust"),
		core.NewKeyValuePair("Ingest-Node-Contact-Name", "<NAME>"),
		core.NewKeyValuePair("Ingest-Node-Contact-Email", "<EMAIL>"),
		core.NewKeyValuePair("First-Version-Object-ID", "00af15fd-1046-4811-8cb5-878ec66cf0da"),
		core.NewKeyValuePair("Interpretive-Object-ID", "83bbc27a-86ef-4d1d-be09-4d78cf9e7df3"),
		core.NewKeyValuePair("Rights-Object-ID", "3559d615-6df9-4f30-a2b0-511568359787"),
		core.NewKeyValuePair("DPN-Object-ID", "00af15fd-1046-4811-8cb5-878ec66cf0da"),
		core.NewKeyValuePair("Local-ID", "Homer's Beer Can Collection"),
		core.NewKeyValuePair("Ingest-Node-Address", "160 McCormick Rd, Charlottesville, VA 22904"),
		core.NewKeyValuePair("Version-Number", "1"),
		core.NewKeyValuePair("Bag-Type", "data"),
	},
}

func getBaggerPreReqs(t *testing.T) (tempDir string, aptrustProfile *core.BagItProfile) {
	tempDir, err := ioutil.TempDir("", "bagger_test")
	require.Nil(t, err)

	profilePath, err := testutil.GetPathToTestProfile("aptrust_bagit_profile_2.0.json")
	require.Nil(t, err)
	aptrustProfile, err = core.LoadBagItProfile(profilePath)
	require.Nil(t, err)

	return tempDir, aptrustProfile
}

func countItems(items map[string][]core.KeyValuePair) int {
	count := 0
	for _, list := range items {
		count += len(list)
	}
	return count
}

func TestNewBagger(t *testing.T) {
	tempDir, aptrustProfile := getBaggerPreReqs(t)
	defer os.RemoveAll(tempDir)

	bagger, err := core.NewBagger("", aptrustProfile)
	require.NotNil(t, err)
	assert.Equal(t, "Param bagPath cannot be empty", err.Error())

	bagger, err = core.NewBagger(tempDir, nil)
	require.NotNil(t, err)
	assert.Equal(t, "Param profile cannot be nil", err.Error())

	bagger, err = core.NewBagger(tempDir, aptrustProfile)
	require.Nil(t, err)
	require.NotNil(t, bagger)
	require.NotNil(t, bagger.Bag())
	require.NotNil(t, bagger.Profile())
}

func TestHasRequiredTags(t *testing.T) {
	tempDir, aptrustProfile := getBaggerPreReqs(t)
	defer os.RemoveAll(tempDir)

	// Make sure we flag missing required APTrust tags
	bagger, err := core.NewBagger(tempDir, aptrustProfile)
	require.Nil(t, err)
	require.NotNil(t, bagger)

	assert.False(t, bagger.WriteBag(true, true))
	errors := bagger.Errors()
	require.NotEmpty(t, errors)
	require.Equal(t, countItems(APTrustDefaultTags), len(errors))

	for tagName, _ := range APTrustDefaultTags {
		foundError := false
		for _, err := range errors {
			if strings.Contains(err, tagName) {
				foundError = true
			}
		}
		assert.True(t, foundError, "Bagger did not flag missing tag %s", tagName)
	}

	// Make sure bagger flags missing required DPN tags
	profilePath, err := testutil.GetPathToTestProfile("dpn_bagit_profile.json")
	require.Nil(t, err)
	dpnProfile, err := core.LoadBagItProfile(profilePath)
	require.Nil(t, err)

	bagger, err = core.NewBagger(tempDir, dpnProfile)
	require.Nil(t, err)
	require.NotNil(t, bagger)

	assert.False(t, bagger.WriteBag(true, true))
	errors = bagger.Errors()
	require.NotEmpty(t, errors)
	require.Equal(t, countItems(DPNDefaultTags), len(errors))

	for tagName, _ := range DPNDefaultTags {
		foundError := false
		for _, err := range errors {
			if strings.Contains(err, tagName) {
				foundError = true
			}
		}
		assert.True(t, foundError, "Bagger did not flag missing tag %s", tagName)
	}
}

// func TestBuildBag(t *testing.T) {
// 	tempFile, payloadDir, aptrustProfile := getBaggerPreReqs(t)
// 	dir := filepath.Dir(tempFile.Name())
// 	tempFile.Close()
// 	os.Remove(filepath.Dir(tempFile.Name()))

// 	bagger := core.NewBagger(dir, payloadDir, aptrustProfile, APTrustDefaultTags, true)
// 	require.NotNil(t, bagger)

// 	assert.True(t, bagger.BuildBag())
// 	require.Empty(t, bagger.Errors())
{'fraction_non_alphanumeric': 0.1576511723570547, 'fraction_numerical': 0.02704648292883587, 'mean_word_length': 4.331688596491228, 'pattern_counts': {'":': 31, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4358828', 'n_tokens_mistral': 3880, 'n_tokens_neox': 3625, 'n_words': 633}