|
50 | 50 | import org.dependencytrack.parser.spdx.json.SpdxLicenseDetailParser; |
51 | 51 | import org.dependencytrack.search.document.ComponentDocument; |
52 | 52 | import org.junit.After; |
| 53 | +import org.junit.Assert; |
53 | 54 | import org.junit.Before; |
54 | 55 | import org.junit.Test; |
55 | 56 |
|
| 57 | +import jakarta.json.Json; |
| 58 | +import jakarta.json.JsonArray; |
| 59 | +import jakarta.json.JsonObject; |
| 60 | +import jakarta.json.JsonReader; |
56 | 61 | import javax.jdo.JDOObjectNotFoundException; |
| 62 | +import java.io.StringReader; |
57 | 63 | import java.nio.charset.StandardCharsets; |
58 | 64 | import java.time.Duration; |
59 | 65 | import java.util.ArrayList; |
60 | 66 | import java.util.Arrays; |
| 67 | +import java.util.HashSet; |
61 | 68 | import java.util.List; |
62 | 69 | import java.util.Objects; |
63 | 70 | import java.util.Optional; |
@@ -1440,23 +1447,74 @@ public void informIssue3981Test() { |
1440 | 1447 | } |
1441 | 1448 |
|
1442 | 1449 | @Test |
1443 | | - public void informIssue3936Test() throws Exception{ |
| 1450 | + public void informIssue3936Test() throws Exception { |
1444 | 1451 | final Project project = qm.createProject("Acme Example", null, "1.0", null, null, null, true, false); |
1445 | 1452 | List<String> boms = new ArrayList<>(Arrays.asList("/unit/bom-issue3936-authors.json", "/unit/bom-issue3936-author.json", "/unit/bom-issue3936-both.json")); |
1446 | | - for(String bom : boms){ |
1447 | | - final var bomUploadEvent = new BomUploadEvent(qm.detach(Project.class, project.getId()), |
1448 | | - resourceToByteArray(bom)); |
1449 | | - new BomUploadProcessingTask().inform(bomUploadEvent); |
1450 | | - awaitBomProcessedNotification(bomUploadEvent); |
1451 | | - |
1452 | | - assertThat(qm.getAllComponents(project)).isNotEmpty(); |
1453 | | - Component component = qm.getAllComponents().getFirst(); |
1454 | | - assertThat(component.getAuthor()).isEqualTo("Joane Doe et al."); |
1455 | | - assertThat(component.getAuthors().get(0).getName()).isEqualTo("Joane Doe et al."); |
1456 | | - assertThat(component.getAuthors().size()).isEqualTo(1); |
| 1453 | + for (String bom : boms) { |
| 1454 | + final var bomUploadEvent = new BomUploadEvent(qm.detach(Project.class, project.getId()), |
| 1455 | + resourceToByteArray(bom)); |
| 1456 | + new BomUploadProcessingTask().inform(bomUploadEvent); |
| 1457 | + awaitBomProcessedNotification(bomUploadEvent); |
| 1458 | + |
| 1459 | + assertThat(qm.getAllComponents(project)).isNotEmpty(); |
| 1460 | + Component component = qm.getAllComponents().getFirst(); |
| 1461 | + assertThat(component.getAuthor()).isEqualTo("Joane Doe et al."); |
| 1462 | + assertThat(component.getAuthors().get(0).getName()).isEqualTo("Joane Doe et al."); |
| 1463 | + assertThat(component.getAuthors().size()).isEqualTo(1); |
1457 | 1464 | } |
1458 | 1465 | } |
1459 | 1466 |
|
| 1467 | + @Test |
| 1468 | + public void informIssue4455Test() throws Exception { |
| 1469 | + final var project = new Project(); |
| 1470 | + project.setName("acme-app"); |
| 1471 | + project.setVersion("1.2.3"); |
| 1472 | + qm.persist(project); |
| 1473 | + |
| 1474 | + var bomUploadEvent = new BomUploadEvent(qm.detach(Project.class, project.getId()), |
| 1475 | + resourceToByteArray("/unit/bom-issue4455.json")); |
| 1476 | + new BomUploadProcessingTask().inform(bomUploadEvent); |
| 1477 | + awaitBomProcessedNotification(bomUploadEvent); |
| 1478 | + |
| 1479 | + qm.getPersistenceManager().refresh(project); |
| 1480 | + assertThat(project.getDirectDependencies()).satisfies(directDependenciesJson -> { |
| 1481 | + final JsonReader jsonReader = Json.createReader( |
| 1482 | + new StringReader(directDependenciesJson)); |
| 1483 | + final JsonArray directDependenciesArray = jsonReader.readArray(); |
| 1484 | + |
| 1485 | + final var uuidsSeen = new HashSet<String>(); |
| 1486 | + for (int i = 0; i < directDependenciesArray.size(); i++) { |
| 1487 | + final JsonObject directDependencyObject = directDependenciesArray.getJsonObject(i); |
| 1488 | + final String directDependencyUuid = directDependencyObject.getString("uuid"); |
| 1489 | + if (!uuidsSeen.add(directDependencyUuid)) { |
| 1490 | + Assert.fail("Duplicate UUID %s in project's directDependencies: %s".formatted( |
| 1491 | + directDependencyUuid, directDependenciesJson)); |
| 1492 | + } |
| 1493 | + } |
| 1494 | + }); |
| 1495 | + |
| 1496 | + final List<Component> components = qm.getAllComponents(project); |
| 1497 | + assertThat(components).allSatisfy(component -> { |
| 1498 | + if (component.getDirectDependencies() == null) { |
| 1499 | + return; |
| 1500 | + } |
| 1501 | + |
| 1502 | + final JsonReader jsonReader = Json.createReader( |
| 1503 | + new StringReader(component.getDirectDependencies())); |
| 1504 | + final JsonArray directDependenciesArray = jsonReader.readArray(); |
| 1505 | + |
| 1506 | + final var uuidsSeen = new HashSet<String>(); |
| 1507 | + for (int i = 0; i < directDependenciesArray.size(); i++) { |
| 1508 | + final JsonObject directDependencyObject = directDependenciesArray.getJsonObject(i); |
| 1509 | + final String directDependencyUuid = directDependencyObject.getString("uuid"); |
| 1510 | + if (!uuidsSeen.add(directDependencyUuid)) { |
| 1511 | + Assert.fail("Duplicate UUID %s in component's directDependencies: %s".formatted( |
| 1512 | + directDependencyUuid, component.getDirectDependencies())); |
| 1513 | + } |
| 1514 | + } |
| 1515 | + }); |
| 1516 | + } |
| 1517 | + |
1460 | 1518 | private void awaitBomProcessedNotification(final BomUploadEvent bomUploadEvent) { |
1461 | 1519 | try { |
1462 | 1520 | await("BOM Processed Notification") |
|
0 commit comments