|
21 | 21 | import com.fasterxml.jackson.databind.JsonNode; |
22 | 22 |
|
23 | 23 | import java.nio.charset.StandardCharsets; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
24 | 26 | import org.apache.commons.io.IOUtils; |
25 | 27 | import org.cyclonedx.generators.BomGeneratorFactory; |
26 | 28 | import org.cyclonedx.generators.json.BomJsonGenerator; |
27 | 29 | import org.cyclonedx.generators.xml.BomXmlGenerator; |
28 | | -import org.cyclonedx.model.Bom; |
29 | | -import org.cyclonedx.model.Component; |
| 30 | +import org.cyclonedx.model.*; |
30 | 31 | import org.cyclonedx.model.Component.Type; |
31 | | -import org.cyclonedx.model.License; |
32 | | -import org.cyclonedx.model.LicenseChoice; |
33 | | -import org.cyclonedx.model.Metadata; |
34 | | -import org.cyclonedx.model.Service; |
35 | 32 | import org.cyclonedx.model.license.Expression; |
36 | 33 | import org.cyclonedx.parsers.JsonParser; |
37 | 34 | import org.cyclonedx.parsers.XmlParser; |
|
48 | 45 | import java.nio.file.Files; |
49 | 46 | import java.nio.file.Path; |
50 | 47 | import java.util.ArrayList; |
| 48 | +import java.util.Iterator; |
| 49 | +import java.util.List; |
51 | 50 | import java.util.stream.Stream; |
52 | 51 | import java.util.Objects; |
53 | 52 |
|
@@ -603,15 +602,64 @@ public void testIssue492() throws Exception { |
603 | 602 | } |
604 | 603 |
|
605 | 604 | @Test |
606 | | - public void testComponentAuthorsDeserializationJsonObject16() throws Exception { |
607 | | - Bom bom = createCommonJsonBom("/1.6/valid-component-authors-json-object-1.6.json"); |
608 | | - Component component = bom.getComponents().get(0); |
609 | | - assertNotNull(component.getAuthors()); |
610 | | - assertEquals(2, component.getAuthors().size()); |
611 | | - assertEquals("Test Author 1", component.getAuthors().get(0).getName()); |
612 | | - assertEquals("author1@example.com", component.getAuthors().get(0).getEmail()); |
613 | | - assertEquals("Test Author 2", component.getAuthors().get(1).getName()); |
614 | | - assertNull(component.getAuthors().get(1).getEmail()); |
| 605 | + public void testComponentAuthorsSerializationAndDeserialization() throws Exception { |
| 606 | + Version version = Version.VERSION_16; |
| 607 | + Bom bom = createCommonJsonBom("/1.6/valid-component-authors-1.6.json"); |
| 608 | + |
| 609 | + assertNotNull(bom.getComponents()); |
| 610 | + assertEquals(1, bom.getComponents().size()); |
| 611 | + |
| 612 | + Component bomComponent = bom.getComponents().get(0); |
| 613 | + assertEquals("Outer Author with String value", bomComponent.getAuthor()); |
| 614 | + |
| 615 | + List<OrganizationalContact> bomAuthors = bomComponent.getAuthors(); |
| 616 | + assertNotNull(bomAuthors); |
| 617 | + assertEquals(2, bomAuthors.size()); |
| 618 | + |
| 619 | + OrganizationalContact bomAuthor1 = bomAuthors.get(0); |
| 620 | + OrganizationalContact bomAuthor2 = bomAuthors.get(1); |
| 621 | + |
| 622 | + assertNotNull(bomAuthor1); |
| 623 | + assertEquals("Test Author 1", bomAuthor1.getName()); |
| 624 | + assertEquals("author1@example.com", bomAuthor1.getEmail()); |
| 625 | + assertEquals("123", bomAuthor1.getPhone()); |
| 626 | + |
| 627 | + assertNotNull(bomAuthor2); |
| 628 | + assertEquals("Test Author 2", bomAuthor2.getName()); |
| 629 | + assertEquals("author2@example.com", bomAuthor2.getEmail()); |
| 630 | + assertEquals("456", bomAuthor2.getPhone()); |
| 631 | + |
| 632 | + BomJsonGenerator generator = BomGeneratorFactory.createJson(version, bom); |
| 633 | + String jsonString = generator.toJsonString(); |
| 634 | + |
| 635 | + File loadedFile = writeToFile(jsonString); |
| 636 | + JsonParser parser = new JsonParser(); |
| 637 | + assertTrue(parser.isValid(loadedFile, version)); |
| 638 | + |
| 639 | + // Verify the json content |
| 640 | + ObjectMapper mapper = new ObjectMapper(); |
| 641 | + JsonNode rootNode = mapper.readTree(jsonString); |
| 642 | + |
| 643 | + JsonNode component = rootNode.path("components").get(0); |
| 644 | + assertNotNull(component); |
| 645 | + |
| 646 | + String outerAuthor = component.path("author").asText(); |
| 647 | + assertEquals("Outer Author with String value", outerAuthor, "Outer author value mismatch"); |
| 648 | + |
| 649 | + JsonNode authorsNode = component.path("authors"); |
| 650 | + assertTrue(authorsNode.isArray()); |
| 651 | + assertEquals(2, authorsNode.size(), "Authors list size mismatch"); |
| 652 | + |
| 653 | + Iterator<JsonNode> elements = authorsNode.elements(); |
| 654 | + JsonNode author1 = elements.next(); |
| 655 | + assertEquals("Test Author 1", author1.path("name").asText()); |
| 656 | + assertEquals("author1@example.com", author1.path("email").asText()); |
| 657 | + assertEquals("123", author1.path("phone").asText()); |
| 658 | + |
| 659 | + JsonNode author2 = elements.next(); |
| 660 | + assertEquals("Test Author 2", author2.path("name").asText()); |
| 661 | + assertEquals("author2@example.com", author2.path("email").asText()); |
| 662 | + assertEquals("456", author2.path("phone").asText()); |
615 | 663 | } |
616 | 664 |
|
617 | 665 | private void assertExternalReferenceInfo(Bom bom) { |
|
0 commit comments