<commit_before>require 'db_spec_helper'
require 'uri'

module Dash2
  module Migrator
    describe Importer do

      attr_reader :user_uid
      attr_reader :index_config
      attr_reader :wrapper
      attr_reader :dc_resource
      attr_reader :tenant
      attr_reader :user
      attr_reader :ezid_client
      attr_reader :importer
      attr_reader :se_resource
      attr_reader :doi_value
      attr_reader :imported
      attr_reader :sword_params
      attr_reader :sword_client

      before(:all) do
        @user_uid = '<EMAIL>'

        path = 'config/migrator-dataone.yml'
        @index_config = Stash::Config.from_file(path).index_config

        tenant_config = YAML.load_file('config/tenants/dataone.yml')['test']
        @tenant = StashEngine::Tenant.new(tenant_config)
      end

      before(:each) do
        wrapper_xml = File.read('spec/data/harvested-wrapper.xml')
        @wrapper = Stash::Wrapper::StashWrapper.parse_xml(wrapper_xml)

        datacite_xml = wrapper.stash_descriptive[0]
        @dc_resource = Datacite::Mapping::Resource.parse_xml(datacite_xml)

        @user = StashEngine::User.create(
          uid: user_uid,
          first_name: 'Lisa',
          last_name: 'Muckenhaupt',
          email: '<EMAIL>',
          provider: 'developer',
          tenant_id: tenant.tenant_id
        )
        @ezid_client = instance_double(StashEzid::Client)

        receipt = instance_double(Stash::Sword::DepositReceipt)
        allow(receipt).to receive(:em_iri) { 'http://example.org/em_iri' }
        allow(receipt).to receive(:edit_iri) { 'http://example.org/edit_iri' }

        @sword_client = instance_double(Stash::Sword::Client)
        allow(sword_client).to receive(:create) { receipt }
        allow(sword_client).to receive(:update)

        @sword_params = {
          collection_uri: 'http://uc3-mrtsword-dev.cdlib.org:39001/mrtsword/collection/demo_open_context',
          username: 'dataone_dash_submitter',
          password: '<PASSWORD>'
        }
        allow(Stash::Sword::Client).to receive(:new).with(sword_params) {
          @sword_client
        }
      end

      describe 'problem files' do
        before(:each) do
          @importer = Dash2::Migrator::Importer.new(
              stash_wrapper: wrapper,
              user_uid: user_uid,
              ezid_client: ezid_client,
              id_mode: IDMode::ALWAYS_MINT,
              tenant: tenant
          )

          @doi_value = '10.123/456'
          doi = "doi:#{doi_value}"
          expect(ezid_client).to receive(:mint_id) { doi }
        end

        xit 'imports problem files' do
          problem_files = Dir.glob('spec/data/problem-files/*.xml').sort
          # problem_files = ['spec/data/harvested-wrapper.xml']
          problem_files.each do |f|
            stash_wrapper_xml = File.read(f)
            @wrapper = Stash::Wrapper::StashWrapper.parse_xml(stash_wrapper_xml)
            datacite_xml = wrapper.stash_descriptive[0]
            @dc_resource = Datacite::Mapping::Resource.parse_xml(datacite_xml)
            importer.import
          end
        end
      end


      describe 'DOI handling' do
        describe 'demo mode' do
          before(:each) do
            @importer = Dash2::Migrator::Importer.new(
              stash_wrapper: wrapper,
              user_uid: user_uid,
              ezid_client: ezid_client,
              id_mode: IDMode::ALWAYS_MINT,
              tenant: tenant
            )

            expect(ezid_client).to receive(:mint_id) {
              time = Time.now
              @doi_value = "10.123/#{time.to_i}.#{time.nsec}"
              doi = "doi:#{doi_value}"
            }

            @imported = importer.import
          end

          it 'mints a DOI in demo mode' do
            identifier = imported.identifier
            expect(identifier).not_to be_nil
            expect(identifier.identifier).to eq(doi_value)
          end

          it 'updates the DOI in the XML' do
            dcs_ident = importer.dcs_resource.identifier
            expect(dcs_ident.value).to eq(doi_value)
          end

          it "doesn't re-mint when re-importing" do
            identifier_1 = imported.identifier
            wrapper_2 = Stash::Wrapper::StashWrapper.parse_xml(File.read('spec/data/harvested-wrapper.xml'))
            importer_2 = Dash2::Migrator::Importer.new(
                stash_wrapper: wrapper_2,
                user_uid: user_uid,
                ezid_client: ezid_client,
                id_mode: IDMode::ALWAYS_MINT,
                tenant: tenant
            )
            imported_2 = importer_2.import
            identifier_2 = imported_2.identifier
            expect(identifier_2.identifier).to eq(identifier_1.identifier)
            expect(imported_2.id).to eq(imported.id)
          end
        end

        describe 'production mode' do
          before(:each) do
            @importer = Dash2::Migrator::Importer.new(
              stash_wrapper: wrapper,
              user_uid: user_uid,
              ezid_client: ezid_client,
              id_mode: IDMode::ALWAYS_UPDATE,
              tenant: tenant
            )
          end

          it 'updates the DOI in production mode' do
            expected_xml = dc_resource.write_xml
            doi_value = '10.15146/R3RG6G'
            doi = "doi:#{doi_value}"
            expect(ezid_client).to receive(:update_metadata).with(
              doi,
              expected_xml,
              URI::HTTPS.build(host: 'oneshare2-dev.cdlib.org', path: "/stash/dataset/#{doi}").to_s
            )
            @imported = importer.import
            identifier = imported.identifier
            expect(identifier).not_to be_nil
            expect(identifier.identifier).to eq(doi_value)
          end
        end
      end

      describe 'merritt submission' do

        attr_reader :doi

        before(:each) do
          @importer = Dash2::Migrator::Importer.new(
            stash_wrapper: wrapper,
            user_uid: user_uid,<commit_msg>Rename variable for consistency w/production code<commit_after>require 'db_spec_helper'
require 'uri'

module Dash2
  # noinspection ALL
  module Migrator
    describe Importer do

      attr_reader :user_uid
      attr_reader :index_config
      attr_reader :wrapper
      attr_reader :dcs_resource
      attr_reader :tenant
      attr_reader :user
      attr_reader :ezid_client
      attr_reader :importer
      attr_reader :se_resource
      attr_reader :doi_value
      attr_reader :imported
      attr_reader :sword_params
      attr_reader :sword_client

      before(:all) do
        @user_uid = '<EMAIL>'

        path = 'config/migrator-dataone.yml'
        @index_config = Stash::Config.from_file(path).index_config

        tenant_config = YAML.load_file('config/tenants/dataone.yml')['test']
        @tenant = StashEngine::Tenant.new(tenant_config)
      end

      before(:each) do
        wrapper_xml = File.read('spec/data/harvested-wrapper.xml')
        @wrapper = Stash::Wrapper::StashWrapper.parse_xml(wrapper_xml)

        datacite_xml = wrapper.stash_descriptive[0]
        @dcs_resource = Datacite::Mapping::Resource.parse_xml(datacite_xml)

        @user = StashEngine::User.create(
          uid: user_uid,
          first_name: 'Lisa',
          last_name: 'Muckenhaupt',
          email: '<EMAIL>',
          provider: 'developer',
          tenant_id: tenant.tenant_id
        )
        @ezid_client = instance_double(StashEzid::Client)

        receipt = instance_double(Stash::Sword::DepositReceipt)
        allow(receipt).to receive(:em_iri) { 'http://example.org/em_iri' }
        allow(receipt).to receive(:edit_iri) { 'http://example.org/edit_iri' }

        @sword_client = instance_double(Stash::Sword::Client)
        allow(sword_client).to receive(:create) { receipt }
        allow(sword_client).to receive(:update)

        @sword_params = {
          collection_uri: 'http://uc3-mrtsword-dev.cdlib.org:39001/mrtsword/collection/demo_open_context',
          username: 'dataone_dash_submitter',
          password: '<PASSWORD>'
        }
        allow(Stash::Sword::Client).to receive(:new).with(sword_params) {
          @sword_client
        }
      end

      describe 'problem files' do
        before(:each) do
          @importer = Dash2::Migrator::Importer.new(
              stash_wrapper: wrapper,
              user_uid: user_uid,
              ezid_client: ezid_client,
              id_mode: IDMode::ALWAYS_MINT,
              tenant: tenant
          )

          @doi_value = '10.123/456'
          doi = "doi:#{doi_value}"
          expect(ezid_client).to receive(:mint_id) { doi }
        end

        xit 'imports problem files' do
          problem_files = Dir.glob('spec/data/problem-files/*.xml').sort
          # problem_files = ['spec/data/harvested-wrapper.xml']
          problem_files.each do |f|
            stash_wrapper_xml = File.read(f)
            @wrapper = Stash::Wrapper::StashWrapper.parse_xml(stash_wrapper_xml)
            datacite_xml = wrapper.stash_descriptive[0]
            @dcs_resource = Datacite::Mapping::Resource.parse_xml(datacite_xml)
            importer.import
          end
        end
      end


      describe 'DOI handling' do
        describe 'demo mode' do
          before(:each) do
            @importer = Dash2::Migrator::Importer.new(
              stash_wrapper: wrapper,
              user_uid: user_uid,
              ezid_client: ezid_client,
              id_mode: IDMode::ALWAYS_MINT,
              tenant: tenant
            )

            expect(ezid_client).to receive(:mint_id) {
              time = Time.now
              @doi_value = "10.123/#{time.to_i}.#{time.nsec}"
              doi = "doi:#{doi_value}"
            }

            @imported = importer.import
          end

          it 'mints a DOI in demo mode' do
            identifier = imported.identifier
            expect(identifier).not_to be_nil
            expect(identifier.identifier).to eq(doi_value)
          end

          it 'updates the DOI in the XML' do
            dcs_ident = importer.dcs_resource.identifier
            expect(dcs_ident.value).to eq(doi_value)
          end

          it "doesn't re-mint when re-importing" do
            identifier_1 = imported.identifier
            wrapper_2 = Stash::Wrapper::StashWrapper.parse_xml(File.read('spec/data/harvested-wrapper.xml'))
            importer_2 = Dash2::Migrator::Importer.new(
                stash_wrapper: wrapper_2,
                user_uid: user_uid,
                ezid_client: ezid_client,
                id_mode: IDMode::ALWAYS_MINT,
                tenant: tenant
            )
            imported_2 = importer_2.import
            identifier_2 = imported_2.identifier
            expect(identifier_2.identifier).to eq(identifier_1.identifier)
            expect(imported_2.id).to eq(imported.id)
          end
        end

        describe 'production mode' do
          before(:each) do
            @importer = Dash2::Migrator::Importer.new(
              stash_wrapper: wrapper,
              user_uid: user_uid,
              ezid_client: ezid_client,
              id_mode: IDMode::ALWAYS_UPDATE,
              tenant: tenant
            )
          end

          it 'updates the DOI in production mode' do
            expected_xml = dcs_resource.write_xml
            doi_value = '10.15146/R3RG6G'
            doi = "doi:#{doi_value}"
            expect(ezid_client).to receive(:update_metadata).with(
              doi,
              expected_xml,
              URI::HTTPS.build(host: 'oneshare2-dev.cdlib.org', path: "/stash/dataset/#{doi}").to_s
            )
            @imported = importer.import
            identifier = imported.identifier
            expect(identifier).not_to be_nil
            expect(identifier.identifier).to eq(doi_value)
          end
        end
      end

      describe 'merritt submission' do

        attr_reader :doi

        before(:each) do
          @importer = Dash2::Migrator::Importer.new(
            stash_wrapper: wrapper,
            user_uid: user_uid,
{'fraction_non_alphanumeric': 0.0977910151402333, 'fraction_numerical': 0.007942417473318442, 'mean_word_length': 1.9569471624266144, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 40}, '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': '2129538', 'n_tokens_mistral': 3774, 'n_tokens_neox': 3607, 'n_words': 628}