blob: e351392ba1cf8d7cdbe4650b522a13741ce9261d (
plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Rpm::RepositoryMetadata::UpdateXmlService do
describe '#execute' do
subject { described_class.new(filename: filename, xml: xml, data: data).execute }
let(:xml) { nil }
let(:data) { nil }
shared_examples 'handling not implemented xml filename' do
let(:filename) { :not_implemented_yet }
let(:empty_xml) { '' }
it 'raise error' do
expect { subject }.to raise_error(ArgumentError)
end
end
shared_context 'with primary xml file data' do
let(:filename) { :primary }
let(:empty_xml) do
<<~XML
<?xml version="1.0" encoding="UTF-8"?>
<metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"/>
XML
end
end
shared_context 'with other xml file data' do
let(:filename) { :other }
let(:empty_xml) do
<<~XML
<?xml version="1.0" encoding="UTF-8"?>
<otherdata xmlns="http://linux.duke.edu/metadata/other" packages="0"/>
XML
end
end
shared_context 'with filelist xml file data' do
let(:filename) { :filelist }
let(:empty_xml) do
<<~XML
<?xml version="1.0" encoding="UTF-8"?>
<filelists xmlns="http://linux.duke.edu/metadata/filelists" packages="0"/>
XML
end
end
context 'when building empty xml' do
shared_examples 'generating empty xml' do
it 'generate expected xml' do
expect(subject).to eq(empty_xml)
end
end
it_behaves_like 'handling not implemented xml filename'
context "for 'primary' xml file" do
include_context 'with primary xml file data'
it_behaves_like 'generating empty xml'
end
context "for 'other' xml file" do
include_context 'with other xml file data'
it_behaves_like 'generating empty xml'
end
context "for 'filelist' xml file" do
include_context 'with filelist xml file data'
it_behaves_like 'generating empty xml'
end
end
context 'when updating xml file' do
include_context 'with rpm package data'
let(:xml) { empty_xml }
let(:data) { xml_update_params }
let(:builder_class) { described_class::BUILDERS[filename] }
shared_examples 'updating rpm xml file' do
context 'when updating existing xml' do
shared_examples 'changing root tag attribute' do
it "increment previous 'packages' value by 1" do
previous_value = Nokogiri::XML(xml).at(builder_class::ROOT_TAG).attributes["packages"].value.to_i
new_value = Nokogiri::XML(subject).at(builder_class::ROOT_TAG).attributes["packages"].value.to_i
expect(previous_value + 1).to eq(new_value)
end
end
it 'generate valid xml add expected xml node to existing xml' do
# Have one root attribute
result = Nokogiri::XML::Document.parse(subject).remove_namespaces!
expect(result.children.count).to eq(1)
# Root node has 1 child with generated node
expect(result.xpath("//#{builder_class::ROOT_TAG}/package").count).to eq(1)
end
context 'when empty xml' do
it_behaves_like 'changing root tag attribute'
end
context 'when xml has children' do
context "when node with given 'pkgid' does not exist yet" do
let(:uniq_node_data) do
xml_update_params.tap do |data|
data[:pkgid] = SecureRandom.uuid
end
end
let(:xml) { build_xml_from(uniq_node_data) }
it 'has children nodes' do
existing_xml = Nokogiri::XML::Document.parse(xml).remove_namespaces!
expect(existing_xml.xpath('//package').count).to eq(1)
end
it_behaves_like 'changing root tag attribute'
end
context "when node with given 'pkgid' already exist" do
let(:existing_node_data) do
existing_data = data.dup
existing_data[:name] = FFaker::Lorem.word
existing_data
end
let(:xml) { build_xml_from(existing_node_data) }
it 'has children nodes' do
existing_xml = Nokogiri::XML::Document.parse(xml).remove_namespaces!
expect(existing_xml.xpath('//package').count).to eq(1)
end
it 'replace existing node with new data' do
existing_xml = Nokogiri::XML::Document.parse(xml).remove_namespaces!
result = Nokogiri::XML::Document.parse(subject).remove_namespaces!
expect(result.xpath('//package').count).to eq(1)
expect(result.xpath('//package').first.to_xml).not_to eq(existing_xml.xpath('//package').first.to_xml)
end
end
def build_xml_from(data)
described_class.new(filename: filename, xml: empty_xml, data: data).execute
end
end
end
end
it_behaves_like 'handling not implemented xml filename'
context "for 'primary' xml file" do
include_context 'with primary xml file data'
it_behaves_like 'updating rpm xml file'
end
context "for 'other' xml file" do
include_context 'with other xml file data'
it_behaves_like 'updating rpm xml file'
end
context "for 'filelist' xml file" do
include_context 'with filelist xml file data'
it_behaves_like 'updating rpm xml file'
end
end
end
end
|