diff --git a/Readme.md b/Readme.md index 456400d..f756836 100644 --- a/Readme.md +++ b/Readme.md @@ -15,7 +15,7 @@ it, you get the same bits back. ## TODO Alpha -- [ ] Test domain +- [x] Test domain - [ ] Test integration - [ ] Test application - [ ] Concurrent access safety diff --git a/domain/services.go b/domain/services.go index 9fa3222..5e016e7 100644 --- a/domain/services.go +++ b/domain/services.go @@ -10,34 +10,49 @@ import ( type BogFileService struct { user_agent_repo UserAgentRepository file_data_repo FileDataRepository + default_allowance_bytes int64 + default_allowance_duration time.Duration } -func NewBogFileService(user_agent_repo UserAgentRepository, file_data_repo FileDataRepository) BogFileService { - return BogFileService {user_agent_repo, file_data_repo} +func NewBogFileService( + user_agent_repo UserAgentRepository, + file_data_repo FileDataRepository, + da_bytes int64, + da_duration time.Duration, +) BogFileService { + return BogFileService {user_agent_repo, file_data_repo, da_bytes, da_duration} } -func (b BogFileService) SaveFile(ref FileReference, src io.Reader, size int64) error { - user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent) - - if !user_agent.FileQuota.Allows(size) { - return ErrExceedQuota - } +func (b BogFileService) getOrCreateUA(useragent_in string) *UserAgent{ + ua, err := b.user_agent_repo.GetByName(useragent_in) if err == ErrNotExists { - // TODO make this into a factory method? new_ua := UserAgent { - 0, ref.UserAgent, time.Now(), time.Duration(time.Second * 400), FileSizeQuota {10, 10 }, - } - user_agent, err = b.user_agent_repo.Create(new_ua) + 0, useragent_in, time.Now(), b.default_allowance_duration, FileSizeQuota { b.default_allowance_bytes, 0 }, + } + created_ua, err := b.user_agent_repo.Create(new_ua) + if err != nil { panic(err) } + + return created_ua } if err != nil { panic(err) } + return ua +} + +func (b BogFileService) SaveFile(ref FileReference, src io.Reader, size int64) error { + user_agent := b.getOrCreateUA(ref.UserAgent) + + if !user_agent.FileQuota.Allows(size) { + return ErrExceedQuota + } + f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10)) if err != nil { diff --git a/test/domain/services_test.go b/test/domain/services_test.go index 5394f98..e6ada0d 100644 --- a/test/domain/services_test.go +++ b/test/domain/services_test.go @@ -1,30 +1,73 @@ package test import ( + "time" + "bytes" "testing" "caj-larsson/bog/domain" "caj-larsson/bog/test/mock" - ) var file_ref1 = domain.FileReference { "path1", "ua1" } //var file_ref2 = domain.FileReference { "path1", "ua2" } -//var file_ref3 = domain.FileReference { "path2", "ua1" } +var file_ref3 = domain.FileReference { "path2", "ua1" } -func TestFileService(t *testing.T) { +func NewTestBogFileService() domain.BogFileService { file_repo := mock.NewMockFileRepository() ua_repo := mock.NewMockUserAgentRepository() - service := domain.NewBogFileService(ua_repo, file_repo) + return domain.NewBogFileService(ua_repo, file_repo, 1024, time.Hour) +} - outfile, err := service.OpenOutFile(file_ref1) +func TestFileDontExist(t *testing.T) { + s := NewTestBogFileService() + outfile, err := s.OpenOutFile(file_ref1) if outfile != nil && err != domain.ErrNotExists { t.Errorf("File shall not exist by default") } +} + +func TestFileIsStored(t *testing.T) { + s := NewTestBogFileService() + + fakefile := bytes.NewBufferString("My bog data") + + err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len())) + + if err != nil { + t.Errorf("A small file should be writable %s", err) + } + + largefakefile := bytes.NewBufferString("") + + for largefakefile.Len() < 64000 { + _, err = largefakefile.WriteString("A very repetitive file") + } + + err = s.SaveFile(file_ref3, largefakefile, int64(largefakefile.Len())) + + if err != domain.ErrExceedQuota { + t.Errorf("too large files should not be excepted") + } +} +func TestFileIsReadBack(t *testing.T) { + s := NewTestBogFileService() + infile := bytes.NewBufferString("My bog data") + _ = s.SaveFile(file_ref1, infile, int64(infile.Len())) + + + outbogfile, _ := s.OpenOutFile(file_ref1) + + outfile := bytes.NewBufferString("") + _, _ = outfile.ReadFrom(outbogfile) + + if outfile.String() != "My bog data" { + t.Errorf("file corrupted") + } }