Skip to content

Commit

Permalink
Code checked in from the last spotbugs PR push caused issues with the
Browse files Browse the repository at this point in the history
DeviceTest unit test.  The issue revolved around the equals and hashCode
mehtods that were initially be done by lombok.  Auto generated by an IDE
also failed (using Objects).  The issue came up because the methods all
called super.equals().  I took this out and all issues were resolved.
Lastly the null timestamp unit test was changed because the value will
never be null.
  • Loading branch information
cyrus-dev committed Jan 23, 2024
1 parent 2e1ac19 commit d6af9fd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Objects;

@Entity
@Table(name = "Device")
Expand Down Expand Up @@ -112,9 +113,33 @@ public void setLastReportTimestamp(final Timestamp lastReportTimestamp) {
}

public String toString() {
return String.format("Device Name: %s%nStatus: %s%nSummary: %s",
return String.format("Device Name: %s%nStatus: %s%nSummary: %s%n",
name, healthStatus.getStatus(),
supplyChainValidationStatus.toString(),
summaryId);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Device)) {
return false;
}

Device device = (Device) o;
return isStateOverridden == device.isStateOverridden
&& Objects.equals(name, device.name)
&& healthStatus == device.healthStatus
&& supplyChainValidationStatus == device.supplyChainValidationStatus
&& Objects.equals(lastReportTimestamp, device.lastReportTimestamp)
&& Objects.equals(overrideReason, device.overrideReason)
&& Objects.equals(summaryId, device.summaryId);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), name, healthStatus,
supplyChainValidationStatus, lastReportTimestamp,
isStateOverridden, overrideReason, summaryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

import java.io.Serializable;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Objects;

/**
* This class is used to represent the network info of a device.
*/
@EqualsAndHashCode
@Log4j2
@Embeddable
public class NetworkInfo implements Serializable {
Expand Down Expand Up @@ -112,4 +113,23 @@ private void setMacAddress(final byte[] macAddress) {
log.debug("setting MAC address to: {}", sb);
this.macAddress = macAddress;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof NetworkInfo)) {
return false;
}
NetworkInfo that = (NetworkInfo) o;
return Objects.equals(hostname, that.hostname)
&& Objects.equals(ipAddress, that.ipAddress)
&& Arrays.equals(macAddress, that.macAddress);
}

@Override
public int hashCode() {
int result = Objects.hash(hostname, ipAddress);
result = 31 * result + Arrays.hashCode(macAddress);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.Serializable;
import java.net.InetAddress;
import java.util.Objects;

/**
* A <code>DeviceInfoReport</code> is a <code>Report</code> used to transfer the
Expand Down Expand Up @@ -230,4 +231,27 @@ private void setHardwareInfo(HardwareInfo hardwareInfo) {
private void setTPMInfo(TPMInfo tpmInfo) {
this.tpmInfo = tpmInfo;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DeviceInfoReport)) {
return false;
}
DeviceInfoReport that = (DeviceInfoReport) o;
return Objects.equals(networkInfo, that.networkInfo)
&& Objects.equals(osInfo, that.osInfo)
&& Objects.equals(firmwareInfo, that.firmwareInfo)
&& Objects.equals(hardwareInfo, that.hardwareInfo)
&& Objects.equals(tpmInfo, that.tpmInfo)
&& Objects.equals(clientApplicationVersion, that.clientApplicationVersion)
&& Objects.equals(paccorOutputString, that.paccorOutputString);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), networkInfo, osInfo,
firmwareInfo, hardwareInfo, tpmInfo,
clientApplicationVersion, paccorOutputString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public void testSetNullDeviceInfo() {
* Tests that retrieving a null LastReportTimestamp will not trigger an exception.
*/
@Test
public void testNullLastReportTimeStamp() {
public void testNotNullLastReportTimeStamp() {
final String name = "my-laptop";
final DeviceInfoReport deviceInfo = DeviceInfoReportTest.getTestReport();
final Device device = new Device(name, deviceInfo, HealthStatus.UNKNOWN, AppraisalStatus.Status.UNKNOWN, null, false, null, null);
assertNull(device.getLastReportTimestamp());
assertNotNull(device.getLastReportTimestamp());
}

/**
Expand Down

0 comments on commit d6af9fd

Please sign in to comment.