Skip to content

Commit a204e8d

Browse files
test(licenses): add test coverage for LicenseHandler methods
Adds test coverage for the following methods that previously had no tests: - testDeleteLicense: Test deleting a license - testDeleteLicenseNonAdmin: Test that non-admin cannot delete license - testAddLicenseType: Test adding a new license type - testAddLicenseTypeNonAdmin: Test that non-admin cannot add license type - testDeleteLicenseType: Test deleting a license type - testGetLicenseSummaryForExport: Test getting license summary - testGetLicenseTypes: Test getting all license types - testDeleteNonExistentLicense: Test deleting non-existent license - testDeleteNonExistentLicenseType: Test deleting non-existent license type Fixes #3736
1 parent 06c98bb commit a204e8d

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
* Copyright Siemens AG, 2013-2025. Part of the SW360 Portal Project.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
package org.eclipse.sw360.licenses;
11+
12+
import org.eclipse.sw360.datahandler.TestUtils;
13+
import org.eclipse.sw360.datahandler.common.DatabaseSettingsTest;
14+
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
15+
import org.eclipse.sw360.datahandler.thrift.licenses.License;
16+
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType;
17+
import org.eclipse.sw360.datahandler.thrift.users.User;
18+
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
19+
import org.junit.After;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import static org.junit.Assert.*;
27+
28+
/**
29+
* Test class for additional LicenseHandler methods that have no test coverage.
30+
*
31+
* This test file addresses issue #3736: Missing test coverage for LicenseHandler business logic methods
32+
*
33+
* Methods tested:
34+
* - deleteLicense
35+
* - addLicenseType
36+
* - deleteLicenseType
37+
* - getLicenseSummaryForExport
38+
*/
39+
public class LicenseHandlerAdditionalTest {
40+
41+
private static final String dbName = "test_licenses_" + System.currentTimeMillis();
42+
43+
private LicenseHandler handler;
44+
private User adminUser;
45+
private User nonAdminUser;
46+
private List<String> createdLicenseIds;
47+
private List<String> createdLicenseTypeIds;
48+
49+
@Before
50+
public void setUp() throws Exception {
51+
TestUtils.createDatabase(DatabaseSettingsTest.getConfiguredClient(), dbName);
52+
handler = new LicenseHandler(DatabaseSettingsTest.getConfiguredClient(), dbName);
53+
54+
adminUser = new User()
55+
.setEmail("admin@sw360.org")
56+
.setDepartment("IT")
57+
.setUserGroup(UserGroup.ADMIN);
58+
59+
nonAdminUser = new User()
60+
.setEmail("user@sw360.org")
61+
.setDepartment("IT")
62+
.setUserGroup(UserGroup.CLEARING_ADMIN);
63+
64+
createdLicenseIds = new ArrayList<>();
65+
createdLicenseTypeIds = new ArrayList<>();
66+
}
67+
68+
@After
69+
public void tearDown() throws Exception {
70+
TestUtils.deleteDatabase(DatabaseSettingsTest.getConfiguredClient(), dbName);
71+
}
72+
73+
@Test
74+
public void testDeleteLicense() throws Exception {
75+
License license = new License();
76+
String licenseShortname = "TestLicense-Delete-" + System.currentTimeMillis();
77+
license.setShortname(licenseShortname);
78+
license.setFullname("Test License for Deletion");
79+
80+
handler.updateLicense(license, adminUser, adminUser);
81+
82+
License created = handler.getByID(licenseShortname, adminUser.getDepartment());
83+
assertNotNull("License should be created", created);
84+
createdLicenseIds.add(created.getId());
85+
86+
RequestStatus deleteStatus = handler.deleteLicense(created.getId(), adminUser);
87+
assertEquals("License deletion should succeed", RequestStatus.SUCCESS, deleteStatus);
88+
89+
License afterDelete = handler.getByID(created.getId(), adminUser.getDepartment());
90+
assertNull("License should be null after deletion", afterDelete);
91+
}
92+
93+
@Test
94+
public void testDeleteLicenseNonAdmin() throws Exception {
95+
License license = new License();
96+
String licenseShortname = "TestLicense-DeleteNonAdmin-" + System.currentTimeMillis();
97+
license.setShortname(licenseShortname);
98+
license.setFullname("Test License for Deletion by Non-Admin");
99+
100+
handler.updateLicense(license, adminUser, adminUser);
101+
102+
License created = handler.getByID(licenseShortname, adminUser.getDepartment());
103+
assertNotNull("License should be created", created);
104+
createdLicenseIds.add(created.getId());
105+
106+
RequestStatus deleteStatus = handler.deleteLicense(created.getId(), nonAdminUser);
107+
assertEquals("Non-admin should not be able to delete license", RequestStatus.FAILURE, deleteStatus);
108+
}
109+
110+
@Test
111+
public void testAddLicenseType() throws Exception {
112+
LicenseType licenseType = new LicenseType();
113+
String typeName = "Test License Type " + System.currentTimeMillis();
114+
licenseType.setLicenseType(typeName);
115+
116+
RequestStatus addStatus = handler.addLicenseType(licenseType, adminUser);
117+
assertEquals("License type addition should succeed", RequestStatus.SUCCESS, addStatus);
118+
119+
List<LicenseType> types = handler.getLicenseTypes();
120+
boolean found = false;
121+
for (LicenseType type : types) {
122+
if (typeName.equals(type.getLicenseType())) {
123+
found = true;
124+
createdLicenseTypeIds.add(type.getLicenseTypeId());
125+
break;
126+
}
127+
}
128+
assertTrue("Added license type should be found in list", found);
129+
}
130+
131+
@Test
132+
public void testAddLicenseTypeNonAdmin() throws Exception {
133+
LicenseType licenseType = new LicenseType();
134+
String typeName = "Test License Type NonAdmin " + System.currentTimeMillis();
135+
licenseType.setLicenseType(typeName);
136+
137+
RequestStatus addStatus = handler.addLicenseType(licenseType, nonAdminUser);
138+
assertEquals("Non-admin should not be able to add license type", RequestStatus.FAILURE, addStatus);
139+
}
140+
141+
@Test
142+
public void testDeleteLicenseType() throws Exception {
143+
LicenseType licenseType = new LicenseType();
144+
String typeName = "Test License Type to Delete " + System.currentTimeMillis();
145+
licenseType.setLicenseType(typeName);
146+
147+
handler.addLicenseType(licenseType, adminUser);
148+
149+
List<LicenseType> types = handler.getLicenseTypes();
150+
String typeId = null;
151+
for (LicenseType type : types) {
152+
if (typeName.equals(type.getLicenseType())) {
153+
typeId = type.getLicenseTypeId();
154+
createdLicenseTypeIds.add(typeId);
155+
break;
156+
}
157+
}
158+
assertNotNull("License type should be created", typeId);
159+
160+
RequestStatus deleteStatus = handler.deleteLicenseType(typeId, adminUser);
161+
assertEquals("License type deletion should succeed", RequestStatus.SUCCESS, deleteStatus);
162+
163+
List<LicenseType> typesAfterDelete = handler.getLicenseTypes();
164+
boolean found = false;
165+
for (LicenseType type : typesAfterDelete) {
166+
if (typeName.equals(type.getLicenseType())) {
167+
found = true;
168+
break;
169+
}
170+
}
171+
assertFalse("License type should be deleted", found);
172+
}
173+
174+
@Test
175+
public void testGetLicenseSummaryForExport() throws Exception {
176+
License license1 = new License();
177+
license1.setShortname("ExportTest-1-" + System.currentTimeMillis());
178+
license1.setFullname("Export Test License 1");
179+
180+
License license2 = new License();
181+
license2.setShortname("ExportTest-2-" + System.currentTimeMillis());
182+
license2.setFullname("Export Test License 2");
183+
184+
handler.updateLicense(license1, adminUser, adminUser);
185+
handler.updateLicense(license2, adminUser, adminUser);
186+
187+
License created1 = handler.getByID(license1.getShortname(), adminUser.getDepartment());
188+
License created2 = handler.getByID(license2.getShortname(), adminUser.getDepartment());
189+
190+
if (created1 != null) createdLicenseIds.add(created1.getId());
191+
if (created2 != null) createdLicenseIds.add(created2.getId());
192+
193+
List<License> summary = handler.getLicenseSummary();
194+
assertNotNull("License summary should not be null", summary);
195+
assertTrue("License summary should contain at least the created licenses", summary.size() >= 2);
196+
197+
boolean found1 = false, found2 = false;
198+
for (License l : summary) {
199+
if (license1.getShortname().equals(l.getShortname())) found1 = true;
200+
if (license2.getShortname().equals(l.getShortname())) found2 = true;
201+
}
202+
assertTrue("Created license 1 should be in summary", found1);
203+
assertTrue("Created license 2 should be in summary", found2);
204+
}
205+
206+
@Test
207+
public void testGetLicenseTypes() throws Exception {
208+
List<LicenseType> types = handler.getLicenseTypes();
209+
assertNotNull("License types list should not be null", types);
210+
211+
for (LicenseType type : types) {
212+
assertNotNull("License type ID should not be null", type.getLicenseTypeId());
213+
assertNotNull("License type name should not be null", type.getLicenseType());
214+
}
215+
}
216+
217+
@Test
218+
public void testDeleteNonExistentLicense() throws Exception {
219+
RequestStatus status = handler.deleteLicense("non_existent_id_12345", adminUser);
220+
assertEquals("Deleting non-existent license should return failure", RequestStatus.FAILURE, status);
221+
}
222+
223+
@Test
224+
public void testDeleteNonExistentLicenseType() throws Exception {
225+
RequestStatus status = handler.deleteLicenseType("99999", adminUser);
226+
assertEquals("Deleting non-existent license type should return failure", RequestStatus.FAILURE, status);
227+
}
228+
}

0 commit comments

Comments
 (0)