From 20db9552ccb2fce44c6adad996692c45c5c0f6cc Mon Sep 17 00:00:00 2001 From: Brian Bazurto <124537281+ba-00001@users.noreply.github.com> Date: Sat, 27 Jun 2026 17:04:22 -0500 Subject: [PATCH] Fix confirmout.json handling for missing/malformed files (#16) The confirmout.json loader logged "JSON is in wrong format" whenever the file was simply absent, and would NPE if the file existed but failed to parse (the IOException was caught but rootDevNode was still dereferenced). Report a clear "not found" message when the file is missing, return early after a parse failure instead of dereferencing null, and extract the loading logic into a testable method. Add unit tests for the missing, null, malformed, and valid cases. --- .../entities/DeviceErrorStatusResponse.java | 37 +++++++----- .../DeviceErrorStatusResponseTest.java | 58 +++++++++++++++++++ 2 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponseTest.java diff --git a/src/main/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponse.java b/src/main/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponse.java index bcc5ae2..7a721fb 100644 --- a/src/main/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponse.java +++ b/src/main/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponse.java @@ -20,24 +20,29 @@ public class DeviceErrorStatusResponse { private static List deviceErrorStatuses; private DeviceErrorStatusResponse(){ - deviceErrorStatuses = new CopyOnWriteArrayList<>(); - ObjectMapper objectMapper = new ObjectMapper(); - File jsonConfirm = new File("/var/tmp/CONFIRMOUT/confirmout.json"); - if(jsonConfirm.exists() && jsonConfirm.isFile()){ - JsonNode rootDevNode = null; - try { - rootDevNode = objectMapper.readTree(jsonConfirm); - } catch (IOException ioException) { - log.failure("Error in parsing confirmout", 17, ioException); - } - Iterator fieldNames = rootDevNode.fieldNames(); + deviceErrorStatuses = loadDeviceErrorStatuses(new File("/var/tmp/CONFIRMOUT/confirmout.json")); + } - while(fieldNames.hasNext()){ - deviceErrorStatuses.add(new DeviceErrorStatus(fieldNames.next(), false, null)); - } - } else { - log.failure("JSON is in wrong format", 17, null); + static List loadDeviceErrorStatuses(File jsonConfirm){ + List statuses = new CopyOnWriteArrayList<>(); + if(jsonConfirm == null || !jsonConfirm.exists() || !jsonConfirm.isFile()){ + log.failure("confirmout.json not found at " + (jsonConfirm == null ? "null" : jsonConfirm.getPath()), 17, null); + return statuses; + } + + JsonNode rootDevNode; + try { + rootDevNode = new ObjectMapper().readTree(jsonConfirm); + } catch (IOException ioException) { + log.failure("JSON is in wrong format", 17, ioException); + return statuses; + } + + Iterator fieldNames = rootDevNode.fieldNames(); + while(fieldNames.hasNext()){ + statuses.add(new DeviceErrorStatus(fieldNames.next(), false, null)); } + return statuses; } public static List getDeviceErrorStatusResponse(){ diff --git a/src/test/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponseTest.java b/src/test/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponseTest.java new file mode 100644 index 0000000..1edf94a --- /dev/null +++ b/src/test/java/com/target/devicemanager/common/entities/DeviceErrorStatusResponseTest.java @@ -0,0 +1,58 @@ +package com.target.devicemanager.common.entities; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class DeviceErrorStatusResponseTest { + + @TempDir + Path tempDir; + + @Test + public void loadDeviceErrorStatuses_WhenFileMissing_ReturnsEmptyList() { + File missing = tempDir.resolve("confirmout.json").toFile(); + + List result = DeviceErrorStatusResponse.loadDeviceErrorStatuses(missing); + + assertTrue(result.isEmpty()); + } + + @Test + public void loadDeviceErrorStatuses_WhenNull_ReturnsEmptyList() { + List result = DeviceErrorStatusResponse.loadDeviceErrorStatuses(null); + + assertTrue(result.isEmpty()); + } + + @Test + public void loadDeviceErrorStatuses_WhenMalformedJson_ReturnsEmptyListWithoutThrowing() throws IOException { + File bad = tempDir.resolve("confirmout.json").toFile(); + Files.writeString(bad.toPath(), "{ this is not valid json "); + + List result = DeviceErrorStatusResponse.loadDeviceErrorStatuses(bad); + + assertTrue(result.isEmpty()); + } + + @Test + public void loadDeviceErrorStatuses_WhenValidJson_ReturnsADeviceStatusPerTopLevelField() throws IOException { + File good = tempDir.resolve("confirmout.json").toFile(); + Files.writeString(good.toPath(), "{\"scanner\":{},\"printer\":{}}"); + + List result = DeviceErrorStatusResponse.loadDeviceErrorStatuses(good); + + assertEquals(2, result.size()); + assertEquals("scanner", result.get(0).deviceName); + assertEquals("printer", result.get(1).deviceName); + assertTrue(result.stream().noneMatch(status -> status.faultPresent)); + } +}