Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/main/java/de/fva_net/maxs/logger/MaxsLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public static void logMessage(final MaxsLoggableRoutine routine, final RexsCompo
* @param message the log message
* @param messageType the severity of the message
*/
public static void logMessage(final MaxsLoggableRoutine routine, final int componentId, final String message, final MaxsMessageType messageType) {
public static void logMessage(final MaxsLoggableRoutine routine, final Integer componentId, final String message, final MaxsMessageType messageType) {
// Create the log message
final Notification notification = new Notification();
notification.setRoutine(routine != null ? routine.getMaxsId() : null);
notification.setMessage(message);
notification.setType(messageType);
if (componentId > 0) {
if (componentId != null && componentId > 0) {
notification.setCompId(componentId);
}

Expand All @@ -126,9 +126,9 @@ public static void logMessage(final MaxsLoggableRoutine routine, final int compo
/**
* Logs a new message.
*
* @param routine the routine for which the messasge shall be logged
* @param message the log message
* @param messageType the severity of the message
* @param routine the routine for which the messasge shall be logged
* @param message the log message
* @param messageType the severity of the message
*/
public static void logMessage(final MaxsLoggableRoutine routine, final String message, final MaxsMessageType messageType) {
// Create the log message
Expand All @@ -150,14 +150,16 @@ public static void logMessage(final MaxsLoggableRoutine routine, final String me
* @param value the value
* @param attribute the attribute name
*/
private static void logMissingAttribute(final MaxsLoggableRoutine routine, final int componentId, final Object value, final String attribute) {
private static void logMissingAttribute(final MaxsLoggableRoutine routine, final Integer componentId, final Object value, final String attribute) {
final Notification notification = new Notification();
final Item item = new Item();

notification.setRoutine(routine != null ? routine.getMaxsId() : null);
notification.setMessage(attribute + " is required to perform the calculation but is missing.");
notification.setType(MaxsMessageType.DEBUG_ERROR);
notification.setCompId(componentId);
if (componentId != null) {
notification.setCompId(componentId);
}

if (value == null) {
item.setValue(Double.NaN);
Expand All @@ -176,7 +178,9 @@ private static void logMissingAttribute(final MaxsLoggableRoutine routine, final
log.error("Unsupported Data Type");
}
item.setAttrId(attribute);
item.setCompId(componentId);
if (componentId != null) {
item.setCompId(componentId);
}
notification.getData().add(item);

// store it in the list and write it to file
Expand All @@ -192,7 +196,7 @@ private static void logMissingAttribute(final MaxsLoggableRoutine routine, final
* @param value the double field
* @param attribute the name of the attribute
*/
public static void requireNonNull(final MaxsLoggableRoutine routine, final int componentId, final double value, final String attribute) {
public static void requireNonNull(final MaxsLoggableRoutine routine, final Integer componentId, final double value, final String attribute) {
if (Double.isNaN(value)) {
logMissingAttribute(routine, componentId, value, attribute);
}
Expand All @@ -207,7 +211,7 @@ public static void requireNonNull(final MaxsLoggableRoutine routine, final int c
* @param value the value to check for null or "UNKNOWN"
* @param attribute the name of the attribute being checked
*/
public static void requireNonNull(final MaxsLoggableRoutine routine, final int componentId, final Object value, final String attribute) {
public static void requireNonNull(final MaxsLoggableRoutine routine, final Integer componentId, final Object value, final String attribute) {
// Check if the value is null and log the missing attribute if true
if (value == null) {
logMissingAttribute(routine, componentId, null, attribute);
Expand All @@ -232,7 +236,7 @@ else if (value instanceof Double d && Double.isNaN(d)) {
* @param value the double value to check
* @param attribute the name of the attribute
*/
public static void requireNonZero(final MaxsLoggableRoutine routine, final int componentId, final double value, final String attribute) {
public static void requireNonZero(final MaxsLoggableRoutine routine, final Integer componentId, final double value, final String attribute) {
if (Double.isNaN(value) || Precision.equalsWithRelativeTolerance(value, 0, 1e-7)) {
logMissingAttribute(routine, componentId, value, attribute);
}
Expand All @@ -247,7 +251,7 @@ public static void requireNonZero(final MaxsLoggableRoutine routine, final int c
* @param attribute the name of the attribute
* @param <Q> the type of the quantity
*/
public static <Q extends Quantity<Q>> void requireNonZero(final MaxsLoggableRoutine routine, final int componentId, final Quantity<Q> quantity, final String attribute) {
public static <Q extends Quantity<Q>> void requireNonZero(final MaxsLoggableRoutine routine, final Integer componentId, final Quantity<Q> quantity, final String attribute) {
if (quantity == null) {
logMissingAttribute(routine, componentId, null, attribute);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/fva_net/maxs/logger/xml/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Item {
* The component ID of the item.
*/
@XmlAttribute(name = "compId", required = true)
private int compId;
private Integer compId;

/**
* The value of the item.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/fva_net/maxs/logger/xml/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Notification {
* The component ID associated with this notification.
*/
@XmlAttribute
private int compId;
private Integer compId;

/**
* The list of items (attributes and values) associated with this notification.
Expand Down
95 changes: 43 additions & 52 deletions src/test/java/de/fva_net/maxs/logger/MaxsLoggerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -80,12 +81,12 @@ void deactivateFileLogging_deactivatesLogging(@TempDir Path tempDir) {
*/
@Test
void logMessage_withPart_logsMessage() {
MaxsLogger.logMessage(IsoRoutine.ISO21771_2007, 42, "Test message", MaxsMessageType.INFO);
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(42, MaxsLogger.getAllNotifications().get(0).getCompId());
assertEquals("Test message", MaxsLogger.getAllNotifications().get(0).getMessage());
assertEquals(MaxsMessageType.INFO, MaxsLogger.getAllNotifications().get(0).getType());
assertEquals("iso21771_2007", MaxsLogger.getAllNotifications().get(0).getRoutine());
MaxsLogger.logMessage(IsoRoutine.ISO21771_2007, 42, "Test message", MaxsMessageType.INFO);
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(42, MaxsLogger.getAllNotifications().get(0).getCompId());
assertEquals("Test message", MaxsLogger.getAllNotifications().get(0).getMessage());
assertEquals(MaxsMessageType.INFO, MaxsLogger.getAllNotifications().get(0).getType());
assertEquals("iso21771_2007", MaxsLogger.getAllNotifications().get(0).getRoutine());
}

/**
Expand All @@ -104,37 +105,27 @@ void logMessage_withString_logsMessage() {
* Logs a NaN double value and verifies that a missing attribute notification is created.
*/
@Test
void requireNonNull_doubleValueIsNaN_logsMissingAttribute() {
MaxsLogger.requireNonNull(IsoRoutine.ISO21771_2007, 1, Double.NaN, "attr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
void requireNonNull_ValueIsNull_logsMissingAttribute() {
MaxsLogger.requireNonNull(IsoRoutine.ISO21771_2007, 1, null, "attr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}

/**
* Logs a NaN object double value and verifies that a missing attribute notification is created.
*/
@Test
void requireNonNull_objectDoubleValueIsNaN_logsMissingAttribute() {
MaxsLogger.requireNonNull(IsoRoutine.ISO21771_2007, 1, Double.valueOf(Double.NaN), "attr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}

/**
* Logs a non-NaN double value and verifies that no notifications are created.
*/
@Test
void requireNonNull_doubleValueIsNotNaN_doesNotLog() {
MaxsLogger.requireNonNull(IsoRoutine.ISO21771_2007, 1, 1.23, "attr");
assertEquals(0, MaxsLogger.getAllNotifications().size());
MaxsLogger.requireNonNull(IsoRoutine.ISO21771_2007, 1, Optional.of(1.23), "attr");
assertEquals(0, MaxsLogger.getAllNotifications().size());
}

/**
* Logs a non-NaN object double value and verifies that no notifications are created.
*/
@Test
void requireNonNull_objectDoubleValueIsNotNaN_doesNotLog() {
MaxsLogger.requireNonNull(IsoRoutine.ISO21771_2007, 1, Double.valueOf(1.0), "attr");
MaxsLogger.requireNonNull(IsoRoutine.ISO21771_2007, 1, Optional.of(1.0), "attr");
assertEquals(0, MaxsLogger.getAllNotifications().size());
}

Expand Down Expand Up @@ -172,48 +163,48 @@ void requireNonNull_enumValueIsValid_doesNotLog() {
*/
@Test
void requireNonZero_doubleValueIsNaN_logsMissingAttribute() {
MaxsLogger.requireNonZero(IsoRoutine.ISO21771_2007, 3, Double.NaN, "attr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}
MaxsLogger.requireNonZero(IsoRoutine.ISO21771_2007, 3, Double.NaN, "attr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}

/**
* Logs a zero double value and verifies that a missing attribute notification is created.
*/
@Test
void requireNonZero_doubleValueIsZero_logsMissingAttribute() {
MaxsLogger.requireNonZero(IsoRoutine.ISO21771_2007, 3, 0.0, "attr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}
MaxsLogger.requireNonZero(IsoRoutine.ISO21771_2007, 3, 0.0, "attr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}

/**
* Logs a non-zero double value and verifies that no notifications are created.
*/
@Test
void requireNonZero_doubleValueIsNonZero_doesNotLog() {
MaxsLogger.requireNonZero(IsoRoutine.ISO21771_2007, 3, 2.0, "attr");
assertEquals(0, MaxsLogger.getAllNotifications().size());
}
MaxsLogger.requireNonZero(IsoRoutine.ISO21771_2007, 3, 2.0, "attr");
assertEquals(0, MaxsLogger.getAllNotifications().size());
}

/**
* Logs a zero int value and verifies that a missing attribute notification is created.
*/
@Test
void requireNonZero_intValueIsZero_logsMissingAttribute() {
MaxsLogger.requireNonZero(IsoRoutine.ISO6336_2019, 4, 0, "intAttr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}
MaxsLogger.requireNonZero(IsoRoutine.ISO6336_2019, 4, 0, "intAttr");
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(MaxsMessageType.DEBUG_ERROR, MaxsLogger.getAllNotifications().get(0).getType());
}

/**
* Logs a non-zero int value and verifies that no notifications are created.
*/
@Test
void requireNonZero_intValueIsNonZero_doesNotLog() {
MaxsLogger.requireNonZero(IsoRoutine.ISO6336_2019, 4, 5, "intAttr");
assertEquals(0, MaxsLogger.getAllNotifications().size());
}
MaxsLogger.requireNonZero(IsoRoutine.ISO6336_2019, 4, 5, "intAttr");
assertEquals(0, MaxsLogger.getAllNotifications().size());
}

/**
* Verifies that resetting the logger clears notifications and deactivates logging.
Expand All @@ -222,13 +213,13 @@ void requireNonZero_intValueIsNonZero_doesNotLog() {
void reset_clearsNotificationsAndDeactivatesLogging(@TempDir Path tempDir) {
File validFile = new File(tempDir.toFile(), "logfile.maxs");
MaxsLogger.activateFileLogging(validFile);
MaxsLogger.logMessage(IsoRoutine.ISO21771_2007, 5, "msg", MaxsMessageType.INFO);
MaxsLogger.logMessage(IsoRoutine.ISO21771_2007, 5, "msg", MaxsMessageType.INFO);
assertTrue(MaxsLogger.isLoggingToFileActivated());
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(1, MaxsLogger.getAllNotifications().size());
MaxsLogger.reset();
assertFalse(MaxsLogger.isLoggingToFileActivated());
assertEquals(0, MaxsLogger.getAllNotifications().size());
}
assertEquals(0, MaxsLogger.getAllNotifications().size());
}

/**
* Sets the application information and verifies the app ID and version.
Expand All @@ -248,12 +239,12 @@ void setAppInformation_setsAppIdAndVersion() {
*/
@Test
void logMessage_withRexsComponent() {
MaxsLogger.logMessage(IsoRoutine.ISO21771_2007, rexsGear, "Info message", MaxsMessageType.INFO);
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(12, MaxsLogger.getAllNotifications().get(0).getCompId());
assertEquals("Info message", MaxsLogger.getAllNotifications().get(0).getMessage());
assertEquals(MaxsMessageType.INFO, MaxsLogger.getAllNotifications().get(0).getType());
assertEquals("iso21771_2007", MaxsLogger.getAllNotifications().get(0).getRoutine());
assertTrue(MaxsLogger.getAllNotifications().get(0).getMessage().contains("Info"));
MaxsLogger.logMessage(IsoRoutine.ISO21771_2007, rexsGear, "Info message", MaxsMessageType.INFO);
assertEquals(1, MaxsLogger.getAllNotifications().size());
assertEquals(12, MaxsLogger.getAllNotifications().get(0).getCompId());
assertEquals("Info message", MaxsLogger.getAllNotifications().get(0).getMessage());
assertEquals(MaxsMessageType.INFO, MaxsLogger.getAllNotifications().get(0).getType());
assertEquals("iso21771_2007", MaxsLogger.getAllNotifications().get(0).getRoutine());
assertTrue(MaxsLogger.getAllNotifications().get(0).getMessage().contains("Info"));
}
}