From 888e86818c1c71b77baf19f69e717e9e11386458 Mon Sep 17 00:00:00 2001 From: Alex Alzate Date: Thu, 29 Feb 2024 13:31:27 -0500 Subject: [PATCH 1/2] Polish Code and reduce duplicate code Signed-off-by: Alex Alzate --- .../cyclonedx/model/ExtensibleElement.java | 1 - .../AbstractDataTypeDeserializer.java | 65 +++++++++++++++++++ .../deserializer/InputTypeDeserializer.java | 33 ++-------- .../deserializer/OutputTypeDeserializer.java | 38 +++-------- .../AbstractDataTypeSerializer.java | 38 +++++++++++ .../util/serializer/InputTypeSerializer.java | 41 ++++-------- .../util/serializer/OutputTypeSerializer.java | 45 ++++--------- .../org/cyclonedx/BomJsonGeneratorTest.java | 10 ++- .../org/cyclonedx/BomParserFactoryTest.java | 7 +- .../org/cyclonedx/BomXmlGeneratorTest.java | 13 ++-- .../org/cyclonedx/parse/BaseParseTest.java | 5 +- .../org/cyclonedx/parse/JsonParseTest.java | 4 +- .../org/cyclonedx/parse/XmlParseTest.java | 4 +- .../schema/JsonSchemaVerificationTest.java | 3 +- .../schema/XmlSchemaVerificationTest.java | 3 +- .../java/org/cyclonedx/util/BomUtilsTest.java | 3 +- 16 files changed, 175 insertions(+), 138 deletions(-) create mode 100644 src/main/java/org/cyclonedx/util/deserializer/AbstractDataTypeDeserializer.java create mode 100644 src/main/java/org/cyclonedx/util/serializer/AbstractDataTypeSerializer.java diff --git a/src/main/java/org/cyclonedx/model/ExtensibleElement.java b/src/main/java/org/cyclonedx/model/ExtensibleElement.java index 002032068..9d3517703 100644 --- a/src/main/java/org/cyclonedx/model/ExtensibleElement.java +++ b/src/main/java/org/cyclonedx/model/ExtensibleElement.java @@ -29,7 +29,6 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; -import org.apache.commons.lang3.StringUtils; import org.cyclonedx.util.serializer.ExtensibleTypesSerializer; import org.cyclonedx.util.deserializer.ExtensionDeserializer; diff --git a/src/main/java/org/cyclonedx/util/deserializer/AbstractDataTypeDeserializer.java b/src/main/java/org/cyclonedx/util/deserializer/AbstractDataTypeDeserializer.java new file mode 100644 index 000000000..4ab86c16b --- /dev/null +++ b/src/main/java/org/cyclonedx/util/deserializer/AbstractDataTypeDeserializer.java @@ -0,0 +1,65 @@ +/* + * This file is part of CycloneDX Core (Java). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright (c) OWASP Foundation. All Rights Reserved. + */ +package org.cyclonedx.util.deserializer; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import org.cyclonedx.model.formulation.common.AbstractType; +import org.cyclonedx.model.formulation.common.EnvVariableChoice; +import org.cyclonedx.model.formulation.common.ResourceReferenceChoice; + +public abstract class AbstractDataTypeDeserializer + extends JsonDeserializer { + + protected final ObjectMapper objectMapper = new ObjectMapper(); + + protected void setEnvironmentVars(final JsonNode node, AbstractType data) throws JsonProcessingException { + JsonNode nodes = node.get("environmentVars"); + List environmentVars = new ArrayList<>(); + + ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes)); + + for (JsonNode envVarNode : environmentVarsNode) { + EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class); + environmentVars.add(envVar); + } + data.setEnvironmentVars(environmentVars); + } + + protected void setReference(JsonNode node, String fieldName, AbstractType type) + throws JsonProcessingException + { + if (node.has(fieldName)) { + JsonNode fieldNode = node.get(fieldName); + ResourceReferenceChoice reference = objectMapper.treeToValue(fieldNode, ResourceReferenceChoice.class); + + if ("source".equals(fieldName)) { + type.setSource(reference); + } else if ("target".equals(fieldName)) { + type.setTarget(reference); + } + } + } +} diff --git a/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java b/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java index 13f98108f..06b721336 100644 --- a/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java +++ b/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java @@ -19,25 +19,19 @@ package org.cyclonedx.util.deserializer; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import org.cyclonedx.model.AttachmentText; import org.cyclonedx.model.Property; -import org.cyclonedx.model.formulation.common.EnvVariableChoice; import org.cyclonedx.model.formulation.common.InputType; import org.cyclonedx.model.formulation.common.InputType.Parameter; import org.cyclonedx.model.formulation.common.ResourceReferenceChoice; -public class InputTypeDeserializer extends JsonDeserializer { - private final ObjectMapper objectMapper = new ObjectMapper(); +public class InputTypeDeserializer extends AbstractDataTypeDeserializer { private final EnvVariableChoiceDeserializer envVariableDeserializer = new EnvVariableChoiceDeserializer(); @@ -48,17 +42,8 @@ public InputType deserialize(JsonParser jsonParser, DeserializationContext deser JsonNode node = jsonParser.getCodec().readTree(jsonParser); InputType inputType = new InputType(); - if(node.has("source")) { - JsonNode sourceNode = node.get("source"); - ResourceReferenceChoice source = objectMapper.treeToValue(sourceNode, ResourceReferenceChoice.class); - inputType.setSource(source); - } - - if(node.has("target")) { - JsonNode targetNode = node.get("target"); - ResourceReferenceChoice target = objectMapper.treeToValue(targetNode, ResourceReferenceChoice.class); - inputType.setTarget(target); - } + setReference(node, "source", inputType); + setReference(node, "target", inputType); createInputDataInfo(node, inputType, deserializationContext, jsonParser); @@ -83,17 +68,7 @@ private void createInputDataInfo(JsonNode node, InputType inputType, Deserializa List parameters = objectMapper.convertValue(parametersNode, new TypeReference>() {}); inputType.setParameters(parameters); } else if (node.has("environmentVars")) { - JsonNode nodes = node.get("environmentVars"); - List environmentVars = new ArrayList<>(); - - ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes)); - - for (JsonNode envVarNode : environmentVarsNode) { - JsonParser nodeParser = envVarNode.traverse(jsonParser.getCodec()); - EnvVariableChoice envVar = envVariableDeserializer.deserialize(nodeParser, ctxt); - environmentVars.add(envVar); - } - inputType.setEnvironmentVars(environmentVars); + setEnvironmentVars(node, inputType); } else if (node.has("data")) { JsonNode dataNode = node.get("data"); AttachmentText data = objectMapper.treeToValue(dataNode, AttachmentText.class); diff --git a/src/main/java/org/cyclonedx/util/deserializer/OutputTypeDeserializer.java b/src/main/java/org/cyclonedx/util/deserializer/OutputTypeDeserializer.java index 4cd445aa7..1cb02f6b8 100644 --- a/src/main/java/org/cyclonedx/util/deserializer/OutputTypeDeserializer.java +++ b/src/main/java/org/cyclonedx/util/deserializer/OutputTypeDeserializer.java @@ -19,27 +19,21 @@ package org.cyclonedx.util.deserializer; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; import org.cyclonedx.model.AttachmentText; import org.cyclonedx.model.Property; -import org.cyclonedx.model.formulation.common.EnvVariableChoice; import org.cyclonedx.model.formulation.common.OutputType; import org.cyclonedx.model.formulation.common.OutputType.OutputTypeEnum; import org.cyclonedx.model.formulation.common.ResourceReferenceChoice; public class OutputTypeDeserializer - extends JsonDeserializer { - private final ObjectMapper objectMapper = new ObjectMapper(); + extends AbstractDataTypeDeserializer { @Override public OutputType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) @@ -48,17 +42,8 @@ public OutputType deserialize(JsonParser jsonParser, DeserializationContext dese JsonNode node = jsonParser.getCodec().readTree(jsonParser); OutputType outputType = new OutputType(); - if(node.has("source")) { - JsonNode sourceNode = node.get("source"); - ResourceReferenceChoice source = objectMapper.treeToValue(sourceNode, ResourceReferenceChoice.class); - outputType.setSource(source); - } - - if(node.has("target")) { - JsonNode targetNode = node.get("target"); - ResourceReferenceChoice target = objectMapper.treeToValue(targetNode, ResourceReferenceChoice.class); - outputType.setTarget(target); - } + setReference(node, "source", outputType); + setReference(node, "target", outputType); createOutputDataInfo(node, outputType); @@ -82,18 +67,11 @@ private void createOutputDataInfo(JsonNode node, OutputType outputType) throws J JsonNode resourceNode = node.get("resource"); ResourceReferenceChoice resource = objectMapper.treeToValue(resourceNode, ResourceReferenceChoice.class); outputType.setResource(resource); - } else if (node.has("environmentVars")) { - JsonNode nodes = node.get("environmentVars"); - List environmentVars = new ArrayList<>(); - - ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes)); - - for (JsonNode envVarNode : environmentVarsNode) { - EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class); - environmentVars.add(envVar); - } - outputType.setEnvironmentVars(environmentVars); - } else if (node.has("data")) { + } + else if (node.has("environmentVars")) { + setEnvironmentVars(node, outputType); + } + else if (node.has("data")) { JsonNode dataNode = node.get("data"); AttachmentText data = objectMapper.treeToValue(dataNode, AttachmentText.class); outputType.setData(data); diff --git a/src/main/java/org/cyclonedx/util/serializer/AbstractDataTypeSerializer.java b/src/main/java/org/cyclonedx/util/serializer/AbstractDataTypeSerializer.java new file mode 100644 index 000000000..d0fb5e15d --- /dev/null +++ b/src/main/java/org/cyclonedx/util/serializer/AbstractDataTypeSerializer.java @@ -0,0 +1,38 @@ +package org.cyclonedx.util.serializer; + +import java.io.IOException; +import java.util.List; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import org.cyclonedx.model.formulation.common.AbstractType; +import org.cyclonedx.model.formulation.common.EnvVariableChoice; + + +public abstract class AbstractDataTypeSerializer extends StdSerializer +{ + + public AbstractDataTypeSerializer(Class t) { + super(t); + } + + protected void parseEnvironmentVars(final JsonGenerator jsonGenerator, final List vars) + throws IOException + { + jsonGenerator.writeArrayFieldStart("environmentVars"); + for (EnvVariableChoice envVarChoice : vars) { + if (envVarChoice.getEnvironmentVar() != null) { + jsonGenerator.writeStartObject(); + jsonGenerator.writeObjectField("environmentVar", envVarChoice.getEnvironmentVar()); + jsonGenerator.writeEndObject(); + } + else if (envVarChoice.getValue() != null) { + jsonGenerator.writeStartObject(); + jsonGenerator.writeObjectField("value", envVarChoice.getValue()); + jsonGenerator.writeEndObject(); + } + } + jsonGenerator.writeEndArray(); + } + +} diff --git a/src/main/java/org/cyclonedx/util/serializer/InputTypeSerializer.java b/src/main/java/org/cyclonedx/util/serializer/InputTypeSerializer.java index e080e0269..70834798c 100644 --- a/src/main/java/org/cyclonedx/util/serializer/InputTypeSerializer.java +++ b/src/main/java/org/cyclonedx/util/serializer/InputTypeSerializer.java @@ -4,13 +4,11 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; -import org.cyclonedx.model.formulation.common.EnvVariableChoice; import org.cyclonedx.model.formulation.common.InputType; public class InputTypeSerializer - extends StdSerializer + extends AbstractDataTypeSerializer { private final boolean isXml; @@ -48,40 +46,27 @@ else if (input.getParameters() != null && !input.getParameters().isEmpty()) { jsonGenerator.writeObject( input.getParameters()); } else if (input.getEnvironmentVars() != null && !input.getEnvironmentVars().isEmpty()) { - jsonGenerator.writeArrayFieldStart("environmentVars"); - for (EnvVariableChoice envVarChoice : input.getEnvironmentVars()) { - if (envVarChoice.getEnvironmentVar() != null) { - jsonGenerator.writeStartObject(); - jsonGenerator.writeObjectField("environmentVar", envVarChoice.getEnvironmentVar()); - jsonGenerator.writeEndObject(); - } else if (envVarChoice.getValue() != null) { - jsonGenerator.writeStartObject(); - jsonGenerator.writeObjectField("value", envVarChoice.getValue()); - jsonGenerator.writeEndObject(); - } - } - jsonGenerator.writeEndArray(); + parseEnvironmentVars(jsonGenerator, input.getEnvironmentVars()); } else if (input.getData() != null) { jsonGenerator.writeFieldName("data"); jsonGenerator.writeObject( input.getData()); } - if (input.getSource() != null) { - jsonGenerator.writeFieldName("source"); - jsonGenerator.writeObject(input.getSource()); - } - if (input.getTarget() != null) { - jsonGenerator.writeFieldName("target"); - jsonGenerator.writeObject(input.getTarget()); - } - if (input.getProperties() != null) { - jsonGenerator.writeFieldName("properties"); - jsonGenerator.writeObject( input.getProperties()); - } + writeField(jsonGenerator, "source", input.getSource()); + writeField(jsonGenerator, "target", input.getTarget()); + writeField(jsonGenerator, "properties", input.getProperties()); + jsonGenerator.writeEndObject(); } + private void writeField(JsonGenerator jsonGenerator, String fieldName, Object fieldValue) throws IOException { + if (fieldValue != null) { + jsonGenerator.writeFieldName(fieldName); + jsonGenerator.writeObject(fieldValue); + } + } + @Override public Class handledType() { return InputType.class; diff --git a/src/main/java/org/cyclonedx/util/serializer/OutputTypeSerializer.java b/src/main/java/org/cyclonedx/util/serializer/OutputTypeSerializer.java index 62ba2acbf..ebfe6a2f8 100644 --- a/src/main/java/org/cyclonedx/util/serializer/OutputTypeSerializer.java +++ b/src/main/java/org/cyclonedx/util/serializer/OutputTypeSerializer.java @@ -4,13 +4,11 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; -import org.cyclonedx.model.formulation.common.EnvVariableChoice; import org.cyclonedx.model.formulation.common.OutputType; public class OutputTypeSerializer - extends StdSerializer + extends AbstractDataTypeSerializer { private final boolean isXml; @@ -47,44 +45,27 @@ private void createOutputChoice(final OutputType output, final JsonGenerator jso jsonGenerator.writeObject( output.getResource()); } else if (output.getEnvironmentVars() != null && !output.getEnvironmentVars().isEmpty()) { - jsonGenerator.writeArrayFieldStart("environmentVars"); - for (EnvVariableChoice envVarChoice : output.getEnvironmentVars()) { - if (envVarChoice.getEnvironmentVar() != null) { - jsonGenerator.writeStartObject(); - jsonGenerator.writeObjectField("environmentVar", envVarChoice.getEnvironmentVar()); - jsonGenerator.writeEndObject(); - } else if (envVarChoice.getValue() != null) { - jsonGenerator.writeStartObject(); - jsonGenerator.writeObjectField("value", envVarChoice.getValue()); - jsonGenerator.writeEndObject(); - } - } - jsonGenerator.writeEndArray(); + parseEnvironmentVars(jsonGenerator, output.getEnvironmentVars()); } else if (output.getData() != null) { jsonGenerator.writeFieldName("data"); jsonGenerator.writeObject( output.getData()); } - if (output.getType() != null) { - jsonGenerator.writeFieldName("type"); - jsonGenerator.writeObject(output.getType()); - } - if (output.getSource() != null) { - jsonGenerator.writeFieldName("source"); - jsonGenerator.writeObject(output.getSource()); - } - if (output.getTarget() != null) { - jsonGenerator.writeFieldName("target"); - jsonGenerator.writeObject(output.getTarget()); - } - if (output.getProperties() != null) { - jsonGenerator.writeFieldName("properties"); - jsonGenerator.writeObject( output.getProperties()); - } + writeField(jsonGenerator, "type", output.getType()); + writeField(jsonGenerator, "source", output.getSource()); + writeField(jsonGenerator, "target", output.getTarget()); + writeField(jsonGenerator, "properties", output.getProperties()); jsonGenerator.writeEndObject(); } + private void writeField(JsonGenerator jsonGenerator, String fieldName, Object fieldValue) throws IOException { + if (fieldValue != null) { + jsonGenerator.writeFieldName(fieldName); + jsonGenerator.writeObject(fieldValue); + } + } + @Override public Class handledType() { return OutputType.class; diff --git a/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java b/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java index 37bd258e3..a92ddc5cd 100644 --- a/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java +++ b/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java @@ -46,6 +46,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.stream.Stream; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -95,7 +96,8 @@ public void schema12JsonObjectGenerationTest() throws Exception { @Test public void schema12MultipleDependenciesJsonTest() throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/bom-1.2.json")); + final byte[] bomBytes = IOUtils.toByteArray( + Objects.requireNonNull(this.getClass().getResourceAsStream("/bom-1.2.json"))); final JsonParser parser = new JsonParser(); final Bom bom = parser.parse(bomBytes); @@ -120,7 +122,8 @@ public void schema13EmptyComponentsJsonTest() throws Exception { @Test public void schema13MultipleDependenciesJsonTest() throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/bom-1.3.json")); + final byte[] bomBytes = IOUtils.toByteArray( + Objects.requireNonNull(this.getClass().getResourceAsStream("/bom-1.3.json"))); final JsonParser parser = new JsonParser(); final Bom bom = parser.parse(bomBytes); @@ -157,7 +160,8 @@ public void testJsonGeneration(Version version, String bomXmlPath) @Test public void schema14MultipleDependenciesJsonTest() throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/bom-1.4.json")); + final byte[] bomBytes = IOUtils.toByteArray( + Objects.requireNonNull(this.getClass().getResourceAsStream("/bom-1.4.json"))); final JsonParser parser = new JsonParser(); final Bom bom = parser.parse(bomBytes); diff --git a/src/test/java/org/cyclonedx/BomParserFactoryTest.java b/src/test/java/org/cyclonedx/BomParserFactoryTest.java index 263eb8f75..9bcd261b7 100644 --- a/src/test/java/org/cyclonedx/BomParserFactoryTest.java +++ b/src/test/java/org/cyclonedx/BomParserFactoryTest.java @@ -25,6 +25,7 @@ import org.cyclonedx.parsers.XmlParser; import org.junit.jupiter.api.Test; import java.io.File; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -33,13 +34,15 @@ public class BomParserFactoryTest { @Test public void testXMLFactory() throws Exception { - Parser parser = BomParserFactory.createParser(new File(BomParserFactory.class.getResource("/bom-1.2.xml").getFile())); + Parser parser = BomParserFactory.createParser( + new File(Objects.requireNonNull(BomParserFactory.class.getResource("/bom-1.2.xml")).getFile())); assertTrue(parser instanceof XmlParser); } @Test public void testJSONFactory() throws Exception { - Parser parser = BomParserFactory.createParser(new File(BomParserFactory.class.getResource("/bom-1.2.json").getFile())); + Parser parser = BomParserFactory.createParser(new File( + Objects.requireNonNull(BomParserFactory.class.getResource("/bom-1.2.json")).getFile())); assertTrue(parser instanceof JsonParser); } diff --git a/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java b/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java index 921d36511..b515a8faf 100644 --- a/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java +++ b/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java @@ -47,6 +47,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.stream.Stream; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.*; @@ -140,7 +141,8 @@ public void schema12GenerationTest() throws Exception { @Test public void schema12MultipleDependenciesXmlTest() throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/bom-1.2.json")); + final byte[] bomBytes = IOUtils.toByteArray( + Objects.requireNonNull(this.getClass().getResourceAsStream("/bom-1.2.json"))); final JsonParser parser = new JsonParser(); final Bom bom = parser.parse(bomBytes); @@ -189,7 +191,8 @@ public void testXmlGeneration(Version version, String bomXmlPath) @Test public void schema13MultipleDependenciesXmlTest() throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/bom-1.3.json")); + final byte[] bomBytes = IOUtils.toByteArray( + Objects.requireNonNull(this.getClass().getResourceAsStream("/bom-1.3.json"))); final JsonParser parser = new JsonParser(); final Bom bom = parser.parse(bomBytes); @@ -202,7 +205,8 @@ public void schema13MultipleDependenciesXmlTest() throws Exception { @Test public void schema14MultipleDependenciesXmlTest() throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/bom-1.4.json")); + final byte[] bomBytes = IOUtils.toByteArray( + Objects.requireNonNull(this.getClass().getResourceAsStream("/bom-1.4.json"))); final JsonParser parser = new JsonParser(); final Bom bom = parser.parse(bomBytes); @@ -529,7 +533,8 @@ private File writeToFile(String xmlString) throws Exception { } private Bom createCommonBomXml(String resource) throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream(resource)); + final byte[] bomBytes = IOUtils.toByteArray( + Objects.requireNonNull(this.getClass().getResourceAsStream(resource))); XmlParser parser = new XmlParser(); return parser.parse(bomBytes); } diff --git a/src/test/java/org/cyclonedx/parse/BaseParseTest.java b/src/test/java/org/cyclonedx/parse/BaseParseTest.java index feb204ded..15d31c3f3 100644 --- a/src/test/java/org/cyclonedx/parse/BaseParseTest.java +++ b/src/test/java/org/cyclonedx/parse/BaseParseTest.java @@ -36,6 +36,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; public abstract class BaseParseTest { @@ -50,8 +51,8 @@ List getAllResources() { List getResources(final String resourceDirectory) { final ClassLoader loader = Thread.currentThread().getContextClassLoader(); final URL url = loader.getResource(resourceDirectory); - final String path = url.getPath(); - return Arrays.asList(new File(path).listFiles()); + final String path = Objects.requireNonNull(url).getPath(); + return Arrays.asList(Objects.requireNonNull(new File(path).listFiles())); } Bom parseBom(File file) throws ParseException { diff --git a/src/test/java/org/cyclonedx/parse/JsonParseTest.java b/src/test/java/org/cyclonedx/parse/JsonParseTest.java index abbcbf5ee..e1625223f 100644 --- a/src/test/java/org/cyclonedx/parse/JsonParseTest.java +++ b/src/test/java/org/cyclonedx/parse/JsonParseTest.java @@ -43,9 +43,9 @@ public Collection dynamicTestsWithCollection() { super.generateBomJson(file.getName(), bom); })); } - else if (file.getName().startsWith("invalid")) { + /*else if (file.getName().startsWith("invalid")) { - } + }*/ } } return dynamicTests; diff --git a/src/test/java/org/cyclonedx/parse/XmlParseTest.java b/src/test/java/org/cyclonedx/parse/XmlParseTest.java index 475fb44d9..84de1896f 100644 --- a/src/test/java/org/cyclonedx/parse/XmlParseTest.java +++ b/src/test/java/org/cyclonedx/parse/XmlParseTest.java @@ -43,9 +43,9 @@ public Collection dynamicTestsWithCollection() { super.generateBomXml(file.getName(), bom); })); } - else if (file.getName().startsWith("invalid")) { + /*else if (file.getName().startsWith("invalid")) { - } + }*/ } } return dynamicTests; diff --git a/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java b/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java index ced7bcc81..0304af3ce 100644 --- a/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java +++ b/src/test/java/org/cyclonedx/schema/JsonSchemaVerificationTest.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -70,7 +71,7 @@ else if (file.endsWith("-1.6.json")) { } private boolean isValidJson(Version version, String resource) throws Exception { - final File file = new File(this.getClass().getResource(resource).getFile()); + final File file = new File(Objects.requireNonNull(this.getClass().getResource(resource)).getFile()); final JsonParser parser = new JsonParser(); return parser.isValid(file, version); diff --git a/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java b/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java index bbc19c8a7..03ff737b1 100644 --- a/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java +++ b/src/test/java/org/cyclonedx/schema/XmlSchemaVerificationTest.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -77,7 +78,7 @@ else if (file.endsWith("-1.6.xml")) { } private boolean isValid(Version version, String resource) throws Exception { - final File file = new File(this.getClass().getResource(resource).getFile()); + final File file = new File(Objects.requireNonNull(this.getClass().getResource(resource)).getFile()); final XmlParser parser = new XmlParser(); return parser.isValid(file, version); } diff --git a/src/test/java/org/cyclonedx/util/BomUtilsTest.java b/src/test/java/org/cyclonedx/util/BomUtilsTest.java index d28f71a75..873075b88 100644 --- a/src/test/java/org/cyclonedx/util/BomUtilsTest.java +++ b/src/test/java/org/cyclonedx/util/BomUtilsTest.java @@ -28,6 +28,7 @@ import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.List; +import java.util.Objects; import static org.apache.commons.io.FileUtils.ONE_KB; import static org.assertj.core.api.Assertions.assertThat; @@ -44,7 +45,7 @@ public class BomUtilsTest { @Test public void calculateHashesTest() throws Exception { - final File file = new File(this.getClass().getResource("/hashtest.txt").toURI()); + final File file = new File(Objects.requireNonNull(this.getClass().getResource("/hashtest.txt")).toURI()); final List hashes = BomUtils.calculateHashes(file, Version.VERSION_12); From 110a8926d583aed53ee67e7f16b71b59c28dc6b3 Mon Sep 17 00:00:00 2001 From: Alex Alzate Date: Tue, 14 May 2024 14:08:05 -0500 Subject: [PATCH 2/2] Remove unused fields Signed-off-by: Alex Alzate --- .../org/cyclonedx/CycloneDxMediaType.java | 2 - .../org/cyclonedx/model/ExtensibleType.java | 2 +- .../org/cyclonedx/model/LicenseChoice.java | 4 -- .../attestation/affirmation/Signatory.java | 1 - .../model/attestation/evidence/Contents.java | 1 - .../model/attestation/evidence/Data.java | 2 - .../crypto/enums/CertificationLevel.java | 1 - .../model/definition/Definition.java | 1 - .../model/definition/Requirement.java | 2 - .../formulation/workspace/Workspace.java | 54 ------------------- .../model/vulnerability/Vulnerability.java | 1 - .../org/cyclonedx/parsers/JsonParser.java | 2 - .../ComponentWrapperDeserializer.java | 12 +++-- .../deserializer/InputTypeDeserializer.java | 6 +-- .../deserializer/LicenseDeserializer.java | 2 +- .../org/cyclonedx/BomJsonGeneratorTest.java | 6 ++- .../org/cyclonedx/BomParserFactoryTest.java | 6 +-- .../org/cyclonedx/BomXmlGeneratorTest.java | 3 +- .../org/cyclonedx/parsers/JsonParserTest.java | 2 +- .../org/cyclonedx/parsers/XmlParserTest.java | 3 +- 20 files changed, 22 insertions(+), 91 deletions(-) diff --git a/src/main/java/org/cyclonedx/CycloneDxMediaType.java b/src/main/java/org/cyclonedx/CycloneDxMediaType.java index c596f7976..d8eb7075d 100644 --- a/src/main/java/org/cyclonedx/CycloneDxMediaType.java +++ b/src/main/java/org/cyclonedx/CycloneDxMediaType.java @@ -24,14 +24,12 @@ private CycloneDxMediaType() { } /** * Official CycloneDX XML media type assigned by IANA. - * * https://www.iana.org/assignments/media-types/application/vnd.cyclonedx+xml */ public static final String APPLICATION_CYCLONEDX_XML = "application/vnd.cyclonedx+xml"; /** * Official CycloneDX JSON media type assigned by IANA. - * * https://www.iana.org/assignments/media-types/application/vnd.cyclonedx+json */ public static final String APPLICATION_CYCLONEDX_JSON = "application/vnd.cyclonedx+json"; diff --git a/src/main/java/org/cyclonedx/model/ExtensibleType.java b/src/main/java/org/cyclonedx/model/ExtensibleType.java index 62ec53f55..71192c60b 100644 --- a/src/main/java/org/cyclonedx/model/ExtensibleType.java +++ b/src/main/java/org/cyclonedx/model/ExtensibleType.java @@ -68,7 +68,7 @@ public List getAttributes() { } public String getValue() { - if (super.getExtensibleTypes() != null && super.getExtensibleTypes().size() > 0) { + if (super.getExtensibleTypes() != null && !super.getExtensibleTypes().isEmpty()) { return null; } else { return value; diff --git a/src/main/java/org/cyclonedx/model/LicenseChoice.java b/src/main/java/org/cyclonedx/model/LicenseChoice.java index b82778a1c..c3618d095 100644 --- a/src/main/java/org/cyclonedx/model/LicenseChoice.java +++ b/src/main/java/org/cyclonedx/model/LicenseChoice.java @@ -23,15 +23,11 @@ import java.util.Objects; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import org.cyclonedx.model.license.Expression; import org.cyclonedx.util.deserializer.LicenseDeserializer; -import org.cyclonedx.util.serializer.LicenseChoiceSerializer; @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_EMPTY) diff --git a/src/main/java/org/cyclonedx/model/attestation/affirmation/Signatory.java b/src/main/java/org/cyclonedx/model/attestation/affirmation/Signatory.java index a50fbb6f7..cd5467d8e 100644 --- a/src/main/java/org/cyclonedx/model/attestation/affirmation/Signatory.java +++ b/src/main/java/org/cyclonedx/model/attestation/affirmation/Signatory.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonTypeName; -import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.cyclonedx.model.ExternalReference; import org.cyclonedx.model.OrganizationalEntity; diff --git a/src/main/java/org/cyclonedx/model/attestation/evidence/Contents.java b/src/main/java/org/cyclonedx/model/attestation/evidence/Contents.java index 9ad1310bb..6eba20905 100644 --- a/src/main/java/org/cyclonedx/model/attestation/evidence/Contents.java +++ b/src/main/java/org/cyclonedx/model/attestation/evidence/Contents.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import org.cyclonedx.model.AttachmentText; @JsonIgnoreProperties(ignoreUnknown = true) diff --git a/src/main/java/org/cyclonedx/model/attestation/evidence/Data.java b/src/main/java/org/cyclonedx/model/attestation/evidence/Data.java index b9b29116f..892e89865 100644 --- a/src/main/java/org/cyclonedx/model/attestation/evidence/Data.java +++ b/src/main/java/org/cyclonedx/model/attestation/evidence/Data.java @@ -5,11 +5,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import org.cyclonedx.model.component.modelCard.data.Governance; -import org.cyclonedx.util.deserializer.StringListDeserializer; @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_EMPTY) diff --git a/src/main/java/org/cyclonedx/model/component/crypto/enums/CertificationLevel.java b/src/main/java/org/cyclonedx/model/component/crypto/enums/CertificationLevel.java index 374cfe6c6..d71801e47 100644 --- a/src/main/java/org/cyclonedx/model/component/crypto/enums/CertificationLevel.java +++ b/src/main/java/org/cyclonedx/model/component/crypto/enums/CertificationLevel.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import org.cyclonedx.model.LifecycleChoice.Phase; public enum CertificationLevel { diff --git a/src/main/java/org/cyclonedx/model/definition/Definition.java b/src/main/java/org/cyclonedx/model/definition/Definition.java index d38edb3bf..fb6c2fc9d 100644 --- a/src/main/java/org/cyclonedx/model/definition/Definition.java +++ b/src/main/java/org/cyclonedx/model/definition/Definition.java @@ -7,7 +7,6 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_EMPTY) diff --git a/src/main/java/org/cyclonedx/model/definition/Requirement.java b/src/main/java/org/cyclonedx/model/definition/Requirement.java index 2b70b27e4..dbf49e765 100644 --- a/src/main/java/org/cyclonedx/model/definition/Requirement.java +++ b/src/main/java/org/cyclonedx/model/definition/Requirement.java @@ -6,12 +6,10 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import org.cyclonedx.model.ExternalReference; import org.cyclonedx.model.Property; -import org.cyclonedx.util.deserializer.StringListDeserializer; @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_EMPTY) diff --git a/src/main/java/org/cyclonedx/model/formulation/workspace/Workspace.java b/src/main/java/org/cyclonedx/model/formulation/workspace/Workspace.java index 639965310..680f603eb 100644 --- a/src/main/java/org/cyclonedx/model/formulation/workspace/Workspace.java +++ b/src/main/java/org/cyclonedx/model/formulation/workspace/Workspace.java @@ -8,9 +8,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; -import org.cyclonedx.model.Property; import org.cyclonedx.model.formulation.common.BasicDataAbstract; -import org.cyclonedx.model.formulation.common.ResourceReferenceChoice; @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_EMPTY) @@ -56,30 +54,6 @@ public String getAccessMode() { } } - public String getBomRef() { - return bomRef; - } - - public void setBomRef(final String bomRef) { - this.bomRef = bomRef; - } - - public String getUid() { - return uid; - } - - public void setUid(final String uid) { - this.uid = uid; - } - - public String getName() { - return name; - } - - public void setName(final String name) { - this.name = name; - } - @JacksonXmlElementWrapper(localName = "aliases") @JacksonXmlProperty(localName = "alias") public List getAliases() { @@ -90,24 +64,6 @@ public void setAliases(final List aliases) { this.aliases = aliases; } - public String getDescription() { - return description; - } - - public void setDescription(final String description) { - this.description = description; - } - - @JacksonXmlElementWrapper(localName = "resourceReferences") - @JacksonXmlProperty(localName = "resourceReference") - public List getResourceReferences() { - return resourceReferences; - } - - public void setResourceReferences(final List resourceReferences) { - this.resourceReferences = resourceReferences; - } - public AccessMode getAccessMode() { return accessMode; } @@ -147,14 +103,4 @@ public Volume getVolume() { public void setVolume(final Volume volume) { this.volume = volume; } - - @JacksonXmlElementWrapper(localName = "properties") - @JacksonXmlProperty(localName = "property") - public List getProperties() { - return properties; - } - - public void setProperties(final List properties) { - this.properties = properties; - } } diff --git a/src/main/java/org/cyclonedx/model/vulnerability/Vulnerability.java b/src/main/java/org/cyclonedx/model/vulnerability/Vulnerability.java index e8d1ef2ac..b9e0dd78b 100644 --- a/src/main/java/org/cyclonedx/model/vulnerability/Vulnerability.java +++ b/src/main/java/org/cyclonedx/model/vulnerability/Vulnerability.java @@ -35,7 +35,6 @@ import org.cyclonedx.model.Tool; import org.cyclonedx.model.VersionFilter; import org.cyclonedx.util.serializer.CustomDateSerializer; -import org.cyclonedx.Version; /** * @since 6.0.0 diff --git a/src/main/java/org/cyclonedx/parsers/JsonParser.java b/src/main/java/org/cyclonedx/parsers/JsonParser.java index 8102f8b17..7445503a0 100644 --- a/src/main/java/org/cyclonedx/parsers/JsonParser.java +++ b/src/main/java/org/cyclonedx/parsers/JsonParser.java @@ -155,7 +155,6 @@ public List validate(final InputStream inputStream, final Versio * Verifies a CycloneDX BOM conforms to the specification through JSON validation. * @param bomString the CycloneDX BOM to validate * @param schemaVersion the schema version to validate against - * @return true is the file is a valid BOM, false if not * @throws IOException when errors are encountered * @since 3.0.0 */ @@ -167,7 +166,6 @@ public List validate(final String bomString, final Version schem * Verifies a CycloneDX BOM conforms to the specification through JSON validation. * @param bomJson the CycloneDX BOM to validate * @param schemaVersion the schema version to validate against - * @return true is the file is a valid BOM, false if not * @throws IOException when errors are encountered * @since 3.0.0 */ diff --git a/src/main/java/org/cyclonedx/util/deserializer/ComponentWrapperDeserializer.java b/src/main/java/org/cyclonedx/util/deserializer/ComponentWrapperDeserializer.java index 39b757a19..0f79cde79 100644 --- a/src/main/java/org/cyclonedx/util/deserializer/ComponentWrapperDeserializer.java +++ b/src/main/java/org/cyclonedx/util/deserializer/ComponentWrapperDeserializer.java @@ -86,11 +86,13 @@ public ComponentWrapper deserialize( ObjectNode node = parser.readValueAs(ObjectNode.class); if (node.has("component")) { JsonNode component = node.get("component"); - JsonParser componentsParser = component.traverse(parser.getCodec()); - if (component.isArray()) { - components = Arrays.asList(componentsParser.readValueAs(Component[].class)); - } else { - components = Collections.singletonList(componentsParser.readValueAs(Component.class)); + try (JsonParser componentsParser = component.traverse(parser.getCodec())) { + if (component.isArray()) { + components = Arrays.asList(componentsParser.readValueAs(Component[].class)); + } + else { + components = Collections.singletonList(componentsParser.readValueAs(Component.class)); + } } } } diff --git a/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java b/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java index 06b721336..0f5a78252 100644 --- a/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java +++ b/src/main/java/org/cyclonedx/util/deserializer/InputTypeDeserializer.java @@ -33,8 +33,6 @@ public class InputTypeDeserializer extends AbstractDataTypeDeserializer { - private final EnvVariableChoiceDeserializer envVariableDeserializer = new EnvVariableChoiceDeserializer(); - @Override public InputType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException @@ -45,7 +43,7 @@ public InputType deserialize(JsonParser jsonParser, DeserializationContext deser setReference(node, "source", inputType); setReference(node, "target", inputType); - createInputDataInfo(node, inputType, deserializationContext, jsonParser); + createInputDataInfo(node, inputType); if(node.has("properties")) { JsonNode propertiesNode = node.get("properties"); @@ -56,7 +54,7 @@ public InputType deserialize(JsonParser jsonParser, DeserializationContext deser return inputType; } - private void createInputDataInfo(JsonNode node, InputType inputType, DeserializationContext ctxt, JsonParser jsonParser) + private void createInputDataInfo(JsonNode node, InputType inputType) throws IOException { if (node.has("resource")) { diff --git a/src/main/java/org/cyclonedx/util/deserializer/LicenseDeserializer.java b/src/main/java/org/cyclonedx/util/deserializer/LicenseDeserializer.java index b1fb8f7d4..02fbb322a 100644 --- a/src/main/java/org/cyclonedx/util/deserializer/LicenseDeserializer.java +++ b/src/main/java/org/cyclonedx/util/deserializer/LicenseDeserializer.java @@ -34,7 +34,7 @@ public class LicenseDeserializer extends JsonDeserializer { - ExpressionDeserializer expressionDeserializer = new ExpressionDeserializer(); + final ExpressionDeserializer expressionDeserializer = new ExpressionDeserializer(); @Override public LicenseChoice deserialize( diff --git a/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java b/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java index a92ddc5cd..9169ce147 100644 --- a/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java +++ b/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java @@ -408,13 +408,15 @@ private File writeToFile(String jsonString) throws Exception { } private Bom createCommonXmlBom(String resource) throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream(resource)); + final byte[] bomBytes = + IOUtils.toByteArray(Objects.requireNonNull(this.getClass().getResourceAsStream(resource))); XmlParser parser = new XmlParser(); return parser.parse(bomBytes); } private Bom createCommonJsonBom(String resource) throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream(resource)); + final byte[] bomBytes = + IOUtils.toByteArray(Objects.requireNonNull(this.getClass().getResourceAsStream(resource))); JsonParser parser = new JsonParser(); return parser.parse(bomBytes); } diff --git a/src/test/java/org/cyclonedx/BomParserFactoryTest.java b/src/test/java/org/cyclonedx/BomParserFactoryTest.java index 9bcd261b7..fcf763b07 100644 --- a/src/test/java/org/cyclonedx/BomParserFactoryTest.java +++ b/src/test/java/org/cyclonedx/BomParserFactoryTest.java @@ -27,8 +27,8 @@ import java.io.File; import java.util.Objects; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; public class BomParserFactoryTest { @@ -36,14 +36,14 @@ public class BomParserFactoryTest { public void testXMLFactory() throws Exception { Parser parser = BomParserFactory.createParser( new File(Objects.requireNonNull(BomParserFactory.class.getResource("/bom-1.2.xml")).getFile())); - assertTrue(parser instanceof XmlParser); + assertInstanceOf(XmlParser.class, parser); } @Test public void testJSONFactory() throws Exception { Parser parser = BomParserFactory.createParser(new File( Objects.requireNonNull(BomParserFactory.class.getResource("/bom-1.2.json")).getFile())); - assertTrue(parser instanceof JsonParser); + assertInstanceOf(JsonParser.class, parser); } @Test() diff --git a/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java b/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java index b515a8faf..d7d7fd9bd 100644 --- a/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java +++ b/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java @@ -540,7 +540,8 @@ private Bom createCommonBomXml(String resource) throws Exception { } private Bom createCommonJsonBom(String resource) throws Exception { - final byte[] bomBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream(resource)); + final byte[] bomBytes = + IOUtils.toByteArray(Objects.requireNonNull(this.getClass().getResourceAsStream(resource))); JsonParser parser = new JsonParser(); return parser.parse(bomBytes); } diff --git a/src/test/java/org/cyclonedx/parsers/JsonParserTest.java b/src/test/java/org/cyclonedx/parsers/JsonParserTest.java index 35be9b6a4..0c8e84154 100644 --- a/src/test/java/org/cyclonedx/parsers/JsonParserTest.java +++ b/src/test/java/org/cyclonedx/parsers/JsonParserTest.java @@ -534,7 +534,7 @@ public void schema16_attestation() throws Exception { Signatory s1 = affirmation.getSignatories().get(0); assertEquals("Tom", s1.getName()); assertEquals("CEO", s1.getRole()); - assertEquals(null, s1.getSignature()); + assertNull(s1.getSignature()); assertNull(s1.getOrganization()); assertNull(s1.getExternalReference()); diff --git a/src/test/java/org/cyclonedx/parsers/XmlParserTest.java b/src/test/java/org/cyclonedx/parsers/XmlParserTest.java index 763b5c6b9..4d976ff8c 100644 --- a/src/test/java/org/cyclonedx/parsers/XmlParserTest.java +++ b/src/test/java/org/cyclonedx/parsers/XmlParserTest.java @@ -18,7 +18,6 @@ */ package org.cyclonedx.parsers; -import org.cyclonedx.CycloneDxSchema; import org.cyclonedx.Version; import org.cyclonedx.model.Bom; import org.cyclonedx.model.Component; @@ -701,7 +700,7 @@ public void schema16_attestation() throws Exception { Signatory s1 = affirmation.getSignatories().get(0); assertEquals("Tom", s1.getName()); assertEquals("CEO", s1.getRole()); - assertEquals(null, s1.getSignature()); + assertNull(s1.getSignature()); assertNull(s1.getOrganization()); assertNull(s1.getExternalReference());