dspace_harvest_test.rb
5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require File.dirname(__FILE__) + '/../test_helper.rb'
class DspaceHarvestTest < ActiveSupport::TestCase
def setup
@environment = Environment.default
end
attr_reader :environment
def mock_settings
{ :virtuoso_uri=>"http://virtuoso.noosfero.com",
:virtuoso_username=>"username",
:virtuoso_password=>"password",
:virtuoso_readonly_username=>"readonly_username",
:virtuoso_readonly_password=>"readonly_password",
:dspace_servers=>[
{"dspace_uri"=>"http://dspace1.noosfero.com","last_harvest" => Time.now.utc },
{"dspace_uri"=>"http://dspace2.noosfero.com"},
{"dspace_uri"=>"http://dspace3.noosfero.com", "last_harvest" => Time.now.utc},
{"dspace_uri"=>"http://dspace4.noosfero.com", "last_harvest" => nil},
{"dspace_uri"=>"http://dspace5.noosfero.com", "last_harvest" => Time.now.utc},
]
}
end
should 'initialize with dspace_uri' do
harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace1.noosfero.com"})
assert harvest.dspace_uri, "http://dspace1.noosfero.com"
end
should 'initialize with dspace_uri and last_harvest' do
harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace9.noosfero.com", "last_harvest" => 5})
assert harvest.dspace_uri, "http://dspace9.noosfero.com"
assert harvest.last_harvest, 5
end
should 'save_harvest_time_settings' do
Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace5.noosfero.com", "last_harvest" => 9})
assert harvest.last_harvest, 9
harvest.save_harvest_time_settings(10)
assert harvest.last_harvest, 10
settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin)
dspace_server = settings.dspace_servers[4]
settings = Noosfero::Plugin::Settings.new(environment.reload, VirtuosoPlugin)
dspace_server = settings.dspace_servers[4]
assert_equal 10, dspace_server["last_harvest"]
end
should 'create delayed job when start' do
settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
settings.save!
harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace1.noosfero.com", "last_harvest" => 5})
assert !harvest.find_job.present?
harvest.start
assert harvest.find_job.present?
end
should 'not duplicate harvest job' do
settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
settings.save!
harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace1.noosfero.com", "last_harvest" => 5})
assert_difference "harvest.find_job.count", 1 do
5.times { harvest.start }
end
end
should 'try to harvest all dspaces from start without any setting' do
VirtuosoPlugin::DspaceHarvest.harvest_all(environment, true)
end
should 'try to harvest all dspaces from start with mock configuration' do
settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
settings.save!
VirtuosoPlugin::DspaceHarvest.harvest_all(environment, true)
end
should 'try to harvest all dspaces without any setting' do
VirtuosoPlugin::DspaceHarvest.harvest_all(environment, false)
end
should 'try to harvest all dspaces with mock configuration' do
settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, mock_settings)
settings.save!
VirtuosoPlugin::DspaceHarvest.harvest_all(environment, false)
end
should 'update last_harvest after harvert' do
time = Time.now.utc
settings =
{ :virtuoso_uri=>"http://virtuoso",
:virtuoso_username=>"username",
:virtuoso_password=>"password",
:virtuoso_readonly_username=>"username",
:virtuoso_readonly_password=>"password",
:dspace_servers=>[
{"dspace_uri"=>"http://dspace","last_harvest" => time }
]
}
@settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, settings)
harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://dspace" })
dspace_client = mock
harvest.expects(:dspace_client).returns(dspace_client)
dspace_client.expects(:list_records).returns([])
harvest.run
assert_not_equal harvest.last_harvest, nil
Noosfero::Plugin::Settings.new(environment.reload, VirtuosoPlugin)
assert_not_equal harvest.last_harvest, nil
assert_not_equal harvest.last_harvest, Time.now.utc
end
should 'list records' do
settings =
{ :virtuoso_uri=>"http://hom.virtuoso.participa.br",
:virtuoso_username=>"dba",
:virtuoso_password=>"dba",
:virtuoso_readonly_username=>"dba",
:virtuoso_readonly_password=>"dba",
:dspace_servers=>[
{"dspace_uri"=>"http://hom.dspace.participa.br"}
]
}
settings = Noosfero::Plugin::Settings.new(environment, VirtuosoPlugin, settings)
harvest = VirtuosoPlugin::DspaceHarvest.new(environment, {"dspace_uri"=>"http://hom.dspace.participa.br"})
params = harvest.last_harvest ? {:from => harvest.last_harvest.utc} : {}
records = harvest.dspace_client.list_records(params)
assert_not_equal records.count, 0
end
end