Skip to content

Commit

Permalink
Merge pull request #19 from RockefellerArchiveCenter/fornax_post
Browse files Browse the repository at this point in the history
Fornax post
  • Loading branch information
helrond authored Oct 30, 2018
2 parents e54bdbf + b5d28b4 commit 2732ff4
Show file tree
Hide file tree
Showing 4 changed files with 1,464 additions and 11 deletions.
17 changes: 17 additions & 0 deletions bagdiscovery/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from structlog import wrap_logger
from uuid import uuid4
import tarfile

import requests
from django.core.serializers.json import DjangoJSONEncoder

from .models import Bag
from ursa_major import settings

Expand Down Expand Up @@ -55,6 +59,12 @@ def run(self):
except Exception as e:
self.log.error("Error moving bag: {}".format(e))
raise BagDiscoveryException("Error moving bag: {}".format(e))

try:
self.post_to_fornax(bag)
except Exception as e:
raise BagDiscoveryException("Error sending POST of metadata to Fornax: {}".format(e))

else:
continue
return True
Expand All @@ -79,6 +89,13 @@ def move_bag(self, bag):
bag.bag_path = new_path
bag.save()

def post_to_fornax(self, bag):
# Have to change this from a hardcoded endpoint. Will update as the Gateway is developed
r = requests.post("http://fornax-web:8003/sips/", data=json.dumps(bag.data), headers={"Content-Type": "application/json"})
if r.status_code != 200:
raise BagDiscoveryException(r.status_code, r.reason)
return True


def isdatavalid(data):
requiredKeys = ("extent_files", "url", "acquisition_type", "use_restrictions",
Expand Down
31 changes: 21 additions & 10 deletions bagdiscovery/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from os.path import isdir, join
from os import makedirs, listdir, remove
import shutil
import vcr

from django.test import TestCase
from django.urls import reverse
Expand All @@ -16,6 +17,15 @@
data_fixture_dir = join(settings.BASE_DIR, 'fixtures', 'json')
bag_fixture_dir = join(settings.BASE_DIR, 'fixtures', 'bags')

process_vcr = vcr.VCR(
serializer='json',
cassette_library_dir='fixtures/cassettes',
record_mode='once',
match_on=['path', 'method', 'query'],
filter_query_parameters=['username', 'password'],
filter_headers=['Authorization'],
)


class BagTestCase(TestCase):

Expand All @@ -41,18 +51,19 @@ def createobjects(self):
self.assertEqual(len(Bag.objects.all()), transfer_count, "Wrong number of transfers created")

def processbags(self):
print('*** Test BagDiscovery routine ***')
shutil.copytree(bag_fixture_dir, settings.TEST_LANDING_DIR)
processor = BagDiscovery(dirs={"landing": settings.TEST_LANDING_DIR, "storage": settings.TEST_STORAGE_DIR}).run()
self.assertTrue(processor)
for bag in Bag.objects.all():
self.assertTrue(bag.data)
with process_vcr.use_cassette('process_bags.json'):
shutil.copytree(bag_fixture_dir, settings.TEST_LANDING_DIR)
processor = BagDiscovery(dirs={"landing": settings.TEST_LANDING_DIR, "storage": settings.TEST_STORAGE_DIR}).run()
self.assertTrue(processor)
for bag in Bag.objects.all():
self.assertTrue(bag.data)

def run_view(self):
print('*** Test run view ***')
request = self.factory.post(reverse('bagdiscovery'), {"test": True})
response = BagDiscoveryView.as_view()(request)
self.assertEqual(response.status_code, 200, "Wrong HTTP code")
with process_vcr.use_cassette('process_bags.json'):
print('*** Test run view ***')
request = self.factory.post(reverse('bagdiscovery'), {"test": True})
response = BagDiscoveryView.as_view()(request)
self.assertEqual(response.status_code, 200, "Wrong HTTP code")

def schema(self):
print('*** Getting schema view ***')
Expand Down
Loading

0 comments on commit 2732ff4

Please sign in to comment.