Skip to content

Commit

Permalink
Merge pull request #23 from openepcis/resolves_sonarqube_issues
Browse files Browse the repository at this point in the history
add autoclose support to address sonarqube issues
  • Loading branch information
sboeckelmann authored Nov 6, 2024
2 parents 086a8ca + e46812d commit b0e0bbc
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 87 deletions.
26 changes: 13 additions & 13 deletions core/src/main/java/io/openepcis/converter/VersionTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ public final InputStream performConversion(
private InputStream toXml(final InputStream inputDocument) {
try {
final PipedOutputStream xmlOutputStream = new PipedOutputStream();
final EventHandler<? extends XmlEPCISEventCollector> handler =
new EventHandler(new XmlEPCISEventCollector(xmlOutputStream));

final PipedInputStream convertedDocument = new PipedInputStream(xmlOutputStream);

executorService.execute(
() -> {
try {
try (
final EventHandler<? extends XmlEPCISEventCollector> handler =
new EventHandler(new XmlEPCISEventCollector(xmlOutputStream));
) {
jsonToXmlConverter.convert(inputDocument, handler);
xmlOutputStream.close();
} catch (Exception e) {
Expand Down Expand Up @@ -302,14 +302,14 @@ private InputStream toXml(final InputStream inputDocument) {
private InputStream toJson(final InputStream inputDocument) {
try {
final PipedOutputStream jsonOutputStream = new PipedOutputStream();
final EventHandler<? extends JsonEPCISEventCollector> handler =
new EventHandler(new JsonEPCISEventCollector(jsonOutputStream));

final InputStream convertedDocument = new PipedInputStream(jsonOutputStream);

executorService.execute(
() -> {
try {
try ( final EventHandler<? extends JsonEPCISEventCollector> handler =
new EventHandler(new JsonEPCISEventCollector(jsonOutputStream));
) {
xmlToJsonConverter.convert(inputDocument, handler);
} catch (Exception e) {
try {
Expand All @@ -333,14 +333,14 @@ private InputStream toJson(final InputStream inputDocument) {
private InputStream fromJsonToJson(final InputStream inputDocument) {
try {
final PipedOutputStream jsonOutputStream = new PipedOutputStream();
final EventHandler<? extends JsonEPCISEventCollector> handler =
new EventHandler(new JsonEPCISEventCollector(jsonOutputStream));

final InputStream convertedDocument = new PipedInputStream(jsonOutputStream);

executorService.execute(
() -> {
try {
try ( final EventHandler<? extends JsonEPCISEventCollector> handler =
new EventHandler(new JsonEPCISEventCollector(jsonOutputStream));
) {
jsonEventValueTransformer.convert(inputDocument, handler);
} catch (Exception e) {
try {
Expand All @@ -364,14 +364,14 @@ private InputStream fromJsonToJson(final InputStream inputDocument) {
private InputStream fromXmlToXml(final InputStream inputDocument) {
try {
final PipedOutputStream xmlOutputStream = new PipedOutputStream();
final EventHandler<? extends XmlEPCISEventCollector> handler =
new EventHandler(new XmlEPCISEventCollector(xmlOutputStream));

final PipedInputStream convertedDocument = new PipedInputStream(xmlOutputStream);

executorService.execute(
() -> {
try {
try ( final EventHandler<? extends XmlEPCISEventCollector> handler =
new EventHandler(new XmlEPCISEventCollector(xmlOutputStream));
) {
xmlEventValueTransformer.convert(inputDocument, handler);
xmlOutputStream.close();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Interface to collect each of the converted events into the desired container based on User
* provided OutputStream/List Type
*/
public interface EPCISEventCollector<R> {
public interface EPCISEventCollector<R> extends AutoCloseable {

// Method to store the event
void collect(Object event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* Class to delegate the incoming request to appropriate class to perform either XSD/Schema
* validation or create final XML/JSON file after conversion
*/
public class EventHandler<R> implements EPCISEventValidator, EPCISEventCollector<R> {
public class EventHandler<R> implements EPCISEventValidator, EPCISEventCollector<R>, AutoCloseable {

private EPCISEventValidator validator;
private final EPCISEventCollector<R> collector;
Expand Down Expand Up @@ -139,4 +139,10 @@ public void setQueryName(String queryName){
public boolean isEPCISDocument() {
return collector != null && collector.isEPCISDocument();
}

@Override
public void close() throws Exception {
if (collector != null)
collector.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public void collectSingleEvent(Object event) {
public boolean isEPCISDocument() {
return false;
}

@Override
public void close() throws Exception {
// not implemented for this simple list collector
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class JsonEPCISEventCollector implements EPCISEventCollector<OutputStream
private final DefaultJsonSchemaNamespaceURIResolver namespaceResolver =
DefaultJsonSchemaNamespaceURIResolver.getContext();

private static final JsonFactory JSON_FACTORY = new JsonFactory();

public JsonEPCISEventCollector(OutputStream stream) {
this.stream = stream;

Expand Down Expand Up @@ -277,4 +279,11 @@ public void setQueryName(String queryName) {
public boolean isEPCISDocument() {
return this.isEPCISDocument;
}

@Override
public void close() throws Exception {
if (jsonGenerator != null && !jsonGenerator.isClosed()) {
jsonGenerator.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class XmlEPCISEventCollector implements EPCISEventCollector<OutputStream>

private String queryName;

private boolean closed = false;

private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();

private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();
Expand Down Expand Up @@ -185,6 +187,7 @@ public void end() {
null));
xmlEventWriter.add(events.createEndDocument());
xmlEventWriter.close();
closed = true;
} catch (XMLStreamException e) {
throw new FormatConverterException(
"Exception during JSON-XML conversion, Error occurred during the closing of xmlEventWriter:"
Expand Down Expand Up @@ -227,4 +230,12 @@ public void setQueryName(String queryName) {
public boolean isEPCISDocument() {
return this.isEPCISDocument;
}

@Override
public void close() throws Exception {
if (!closed && xmlEventWriter != null) {
xmlEventWriter.close();
closed = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ public class JsonToXmlConverter extends JsonEventParser implements EventsConvert

private final JAXBContext jaxbContext;

// To read the JSON-LD events using the Jackson
private static final ObjectMapper objectMapper =
new ObjectMapper()
.registerModule(
new SimpleModule()
.addDeserializer(JsonNode.class, new JsonNodeDupeFieldHandlingDeserializer()))
.registerModule(new JavaTimeModule());

private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();

public JsonToXmlConverter(final JAXBContext jaxbContext) {
Expand Down
63 changes: 35 additions & 28 deletions core/src/test/java/com/io/openepcis/convert/JsonToXmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ void jsonToXmlObjectEventTest() throws Exception {
.getClassLoader()
.getResourceAsStream(
"2.0/EPCIS/JSON/Capture/Documents/ObjectEvent_all_possible_fields.json");
final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream));
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
try (final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream))) {
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
}
}

@Test
Expand All @@ -60,10 +61,11 @@ void jsonToXmlAggregationEventTest() throws Exception {
.getClassLoader()
.getResourceAsStream(
"2.0/EPCIS/JSON/Capture/Documents/AggregationEvent_all_possible_fields.json");
final EventHandler handler =
new EventHandler(null, new XmlEPCISEventCollector(byteArrayOutputStream));
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
try (final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream))) {
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
}
}

// Test to only validate the converted XML events
Expand All @@ -75,10 +77,11 @@ void jsonToXmlTransactionEventTest() throws Exception {
.getClassLoader()
.getResourceAsStream(
"2.0/EPCIS/JSON/Capture/Documents/TransactionEvent_all_possible_fields.json");
final EventHandler handler =
new EventHandler(null, new XmlEPCISEventCollector(byteArrayOutputStream));
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
try (final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream))) {
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
}
}

@Test
Expand All @@ -89,10 +92,11 @@ void jsonToXmlTransformationEventTest() throws Exception {
.getClassLoader()
.getResourceAsStream(
"2.0/EPCIS/JSON/Capture/Documents/TransformationEvent_all_possible_fields.json");
final EventHandler handler =
new EventHandler(null, new XmlEPCISEventCollector(byteArrayOutputStream));
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.size() > 0);
try (final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream))) {
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.size() > 0);
}
}

// Test the conversion of single EPCIS event in JSON -> XML
Expand All @@ -103,10 +107,11 @@ void jsonToXmlTestSingleEvent() throws Exception {
getClass()
.getClassLoader()
.getResourceAsStream("2.0/EPCIS/JSON/Capture/Events/AssociationEvent.json");
final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream));
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.size() > 0);
try (final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream))) {
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.size() > 0);
}
}

@Test
Expand Down Expand Up @@ -195,10 +200,11 @@ void combinationOfDifferentEventsTest() throws Exception {
getClass()
.getClassLoader()
.getResourceAsStream("2.0/EPCIS/JSON/Query/Combination_of_different_event.json");
final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream));
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
try (final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream))) {
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
}
}

@Test
Expand All @@ -208,10 +214,11 @@ void jumbledFieldsOrderTest() throws Exception {
getClass()
.getClassLoader()
.getResourceAsStream("2.0/EPCIS/JSON/Query/JumbledFieldsOrder.json");
final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream));
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
try (final EventHandler handler =
new EventHandler(new EventValidator(), new XmlEPCISEventCollector(byteArrayOutputStream))) {
new JsonToXmlConverter().convert(inputStream, handler);
assertTrue(byteArrayOutputStream.toString().length() > 0);
}
}

@Test
Expand Down
Loading

0 comments on commit b0e0bbc

Please sign in to comment.