FOSSology  4.7.1
Open Source License Compliance by Open Source Software
test_worker.cc
Go to the documentation of this file.
1 /*
2  SPDX-License-Identifier: GPL-2.0-only
3  Author: Dietmar Helmut Leher <helmut.leher.ext@vaillant-group.com>
4  SPDX-FileCopyrightText: © 2026 Vaillant GmbH
5 */
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 
18 #include "ReuserState.hpp"
19 #include "ReuserTypes.hpp"
20 
28 static bool runProcessUpload(ReuserDatabaseHandler& db, int uploadId,
29  int groupId, int userId)
30 {
31  if (!db.processBulkReuser(uploadId, groupId, userId))
32  {
33  return false;
34  }
35 
36  auto reusedUploads = db.getReusedUploads(uploadId, groupId);
37  for (const auto& triple : reusedUploads)
38  {
39  ItemTreeBounds bounds;
40  if (!db.getParentItemBounds(triple.reusedUploadId, bounds))
41  continue;
42 
43  if (triple.reuseMode & REUSE_ENHANCED)
44  {
45  if (!db.processEnhancedUploadReuse(uploadId, triple.reusedUploadId,
46  groupId, triple.reusedGroupId, userId))
47  return false;
48  }
49  else
50  {
51  if (!db.processUploadReuse(uploadId, triple.reusedUploadId,
52  groupId, triple.reusedGroupId, userId))
53  return false;
54  }
55 
56  if (triple.reuseMode & REUSE_MAIN)
57  db.reuseMainLicense(uploadId, groupId, triple.reusedUploadId,
58  triple.reusedGroupId);
59 
60  if (triple.reuseMode & REUSE_CONF)
61  db.reuseConfSettings(uploadId, triple.reusedUploadId);
62 
63  if (triple.reuseMode & REUSE_COPYRIGHT)
64  db.reuseCopyrights(uploadId, triple.reusedUploadId, userId);
65  }
66  return true;
67 }
68 
69 class ReuserWorkerTest : public CPPUNIT_NS::TestFixture
70 {
71  CPPUNIT_TEST_SUITE(ReuserWorkerTest);
72  CPPUNIT_TEST(testStandardReuseDispatch);
73  CPPUNIT_TEST(testEnhancedReuseDispatch);
74  CPPUNIT_TEST(testAllFlagsDispatch);
75  CPPUNIT_TEST(testNoReusedUploadsSucceeds);
78  CPPUNIT_TEST_SUITE_END();
79 
80 protected:
88  {
90 
91  bool standardCalled = false;
92  bool enhancedCalled = false;
93 
94  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
95  { return {{10, 1, 0 /* no flags -> standard */}}; };
96 
97  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
98  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
99 
100  handler.onProcessUploadReuse =
101  [&](int, int, int, int, int) -> bool
102  { standardCalled = true; return true; };
103 
104  handler.onProcessEnhancedUploadReuse =
105  [&](int, int, int, int, int) -> bool
106  { enhancedCalled = true; return true; };
107 
108  bool ok = runProcessUpload(handler, 1, 1, 1);
109 
110  CPPUNIT_ASSERT(ok);
111  CPPUNIT_ASSERT(standardCalled);
112  CPPUNIT_ASSERT(!enhancedCalled);
113  }
114 
122  {
124 
125  bool standardCalled = false;
126  bool enhancedCalled = false;
127 
128  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
129  { return {{10, 1, REUSE_ENHANCED}}; };
130 
131  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
132  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
133 
134  handler.onProcessUploadReuse =
135  [&](int, int, int, int, int) -> bool
136  { standardCalled = true; return true; };
137 
138  handler.onProcessEnhancedUploadReuse =
139  [&](int, int, int, int, int) -> bool
140  { enhancedCalled = true; return true; };
141 
142  bool ok = runProcessUpload(handler, 1, 1, 1);
143 
144  CPPUNIT_ASSERT(ok);
145  CPPUNIT_ASSERT(!standardCalled);
146  CPPUNIT_ASSERT(enhancedCalled);
147  }
148 
157  {
159 
160  bool mainCalled = false;
161  bool confCalled = false;
162  bool copyrightCalled = false;
163 
164  int allFlags = REUSE_ENHANCED | REUSE_MAIN | REUSE_CONF | REUSE_COPYRIGHT;
165  handler.onGetReusedUploads = [allFlags](int, int) -> std::vector<ReuseTriple>
166  { return {{10, 1, allFlags}}; };
167 
168  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
169  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
170 
171  handler.onReuseMainLicense =
172  [&](int, int, int, int) -> bool
173  { mainCalled = true; return true; };
174 
175  handler.onReuseConfSettings =
176  [&](int, int) -> bool
177  { confCalled = true; return true; };
178 
179  handler.onReuseCopyrights =
180  [&](int, int, int) -> bool
181  { copyrightCalled = true; return true; };
182 
183  bool ok = runProcessUpload(handler, 1, 1, 1);
184 
185  CPPUNIT_ASSERT(ok);
186  CPPUNIT_ASSERT(mainCalled);
187  CPPUNIT_ASSERT(confCalled);
188  CPPUNIT_ASSERT(copyrightCalled);
189  }
190 
198  {
200  // Default: onGetReusedUploads not set -> returns empty vector.
201  bool ok = runProcessUpload(handler, 1, 1, 1);
202  CPPUNIT_ASSERT(ok);
203  }
204 
213  {
215 
216  int processCalledCount = 0;
217 
218  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
219  { return {{10, 1, 0}, {20, 1, 0}}; };
220 
221  // First upload: bounds not found -> skip.
222  // Second upload: bounds found -> process.
223  handler.onGetParentItemBounds =
224  [](int uploadId, ItemTreeBounds& out) -> bool
225  {
226  if (uploadId == 10) return false;
227  out = {1, "uploadtree", uploadId, 1, 10};
228  return true;
229  };
230 
231  handler.onProcessUploadReuse =
232  [&](int, int, int, int, int) -> bool
233  { ++processCalledCount; return true; };
234 
235  bool ok = runProcessUpload(handler, 1, 1, 1);
236 
237  CPPUNIT_ASSERT(ok);
238  CPPUNIT_ASSERT_EQUAL(1, processCalledCount); // only second upload processed
239  }
240 
248  {
250 
251  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
252  { return {{10, 1, 0}}; };
253 
254  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
255  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
256 
257  handler.onProcessUploadReuse =
258  [](int, int, int, int, int) -> bool
259  { return false; }; // simulate DB error
260 
261  bool ok = runProcessUpload(handler, 1, 1, 1);
262 
263  CPPUNIT_ASSERT(!ok);
264  }
265 };
266 
267 CPPUNIT_TEST_SUITE_REGISTRATION(ReuserWorkerTest);
Mock ReuserDatabaseHandler for unit tests.
Test double for ReuserDatabaseHandler.
Database handler for the reuser agent.
void testProcessUploadReuseFailurePropagates()
A false return from processUploadReuse propagates to the caller.
Definition: test_worker.cc:247
void testStandardReuseDispatch()
reuseMode == 0 routes to processUploadReuse (standard path).
Definition: test_worker.cc:87
void testMissingParentBoundsSkipsUpload()
A reuse link whose parent bounds cannot be determined is skipped.
Definition: test_worker.cc:212
void testEnhancedReuseDispatch()
REUSE_ENHANCED bit routes to processEnhancedUploadReuse.
Definition: test_worker.cc:121
void testAllFlagsDispatch()
All optional flag bits together cause all optional methods to be called.
Definition: test_worker.cc:156
void testNoReusedUploadsSucceeds()
An empty reuse list results in immediate success without any processing.
Definition: test_worker.cc:197
Definition: db.php:28
Bounds of an item within an uploadtree table.
Definition: ReuserTypes.hpp:14
static bool runProcessUpload(ReuserDatabaseHandler &db, int uploadId, int groupId, int userId)
Minimal replica of processUploadId for unit-testing.
Definition: test_worker.cc:28