Skip to content

Commit

Permalink
Merge pull request #577 from nsacyber/v3_issue_576_pt1-unittest
Browse files Browse the repository at this point in the history
[#546] (Part 1) Migrating tests in /data/persist/ folder in HIRS_Utils
  • Loading branch information
iadgovuser26 authored Sep 1, 2023
2 parents 7833978 + d5af444 commit c1917f0
Show file tree
Hide file tree
Showing 8 changed files with 1,276 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@
import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.NonNull;

/**
* An abstract archivable entity that can be given a user-defined name and description.
*/
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@MappedSuperclass
public abstract class UserDefinedEntity extends ArchivableEntity {

@Column(nullable = false, unique = true)
private String name;
@NonNull private String name;

@ToString.Exclude
@EqualsAndHashCode.Exclude
@Column(nullable = false, unique = false)
private String description = "";
@NonNull private String description = "";

/**
* Default empty constructor is required for Hibernate. It is protected to
Expand All @@ -42,5 +40,40 @@ protected UserDefinedEntity() {
public UserDefinedEntity(final String name) {
this(name, "");
}

/**
* Returns a boolean if other is equal to this. <code>UserDefinedEntity</code>s are
* identified by their name, so this returns true if <code>other</code> is
* an instance of <code>UserDefinedEntity</code> and its name is the same as this
* <code>UserDefinedEntity</code>. Otherwise this returns false.
*
* @param other
* other object to test for equals
* @return true if other is <code>Baseline</code> and has same name
*/
@Override
public boolean equals(final Object other) {
if (this == other) {
return true;
}
if (!(other instanceof UserDefinedEntity)) {
return false;
}

final UserDefinedEntity entity = (UserDefinedEntity) other;
return this.getName().equals(entity.getName());
}

/**
* Returns the hash code for this <code>UserDefinedEntity</code>.
* <code>UserDefinedEntity</code>s are identified by their name, so the returned hash
* is the hash of the name.
*
* @return hash
*/
@Override
public int hashCode() {
return name.hashCode();
}
}

1 change: 1 addition & 0 deletions HIRS_Utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
testImplementation 'org.junit.platform:junit-platform-launcher:1.9.3'
testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation project(path: ':HIRS_AttestationCA')

compileOnly libs.lombok
annotationProcessor libs.lombok
Expand Down
207 changes: 207 additions & 0 deletions HIRS_Utils/src/test/java/hirs/data/persist/FirmwareInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package hirs.data.persist;

import hirs.attestationca.persist.entity.userdefined.info.FirmwareInfo;
import org.apache.commons.lang3.StringUtils;
import static hirs.utils.enums.DeviceInfoEnums.NOT_SPECIFIED;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* FirmwareInfoTest is a unit test class for FirmwareInfo.
*/
public class FirmwareInfoTest {

private static final String BIOS_VENDOR = "test bios vendor";
private static final String BIOS_VERSION = "test bios version";
private static final String BIOS_RELEASE_DATE = "test bios release date";

private static final String LONG_BIOS_VENDOR = StringUtils.rightPad(
"test bios vendor",
257
);
private static final String LONG_BIOS_VERSION = StringUtils.rightPad(
"test bios version",
257
);
private static final String LONG_BIOS_RELEASE_DATE = StringUtils.rightPad(
"test bios release date",
33
);

/**
* Tests instantiation of a FirmwareInfo object.
*/
@Test
public final void firmwareInfo() {
new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
}

/**
* Tests that the no-parameter constructor for FirmwareInfo contains expected values.
*/
@Test
public final void firmwareInfoNoParams() {
FirmwareInfo firmwareInfo = new FirmwareInfo();
Assertions.assertEquals(NOT_SPECIFIED, firmwareInfo.getBiosVendor());
Assertions.assertEquals(NOT_SPECIFIED,firmwareInfo.getBiosVersion());
Assertions.assertEquals(NOT_SPECIFIED,firmwareInfo.getBiosReleaseDate());
}

/**
* Tests that the getters for FirmwareInfo return the expected values.
*/
@Test
public final void firmwareInfoGetters() {
FirmwareInfo firmwareInfo = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
Assertions.assertEquals(BIOS_VENDOR, firmwareInfo.getBiosVendor());
Assertions.assertEquals(BIOS_VERSION, firmwareInfo.getBiosVersion());
Assertions.assertEquals(BIOS_RELEASE_DATE, firmwareInfo.getBiosReleaseDate());
}

/**
* Tests that an IllegalArgumentException is thrown if BIOS vendor is null.
*/
@Test
public final void biosVendorNullTest() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new FirmwareInfo(null, BIOS_VERSION, BIOS_RELEASE_DATE));
}

/**
* Tests that an IllegalArgumentException is thrown if BIOS version is null.
*/
@Test
public final void biosVersionNullTest() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new FirmwareInfo(BIOS_VENDOR, null, BIOS_RELEASE_DATE));
}

/**
* Tests that an IllegalArgumentException is thrown if BIOS release date is null.
*/
@Test
public final void biosReleaseDateNullTest() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, null));
}

/**
* Tests that an IllegalArgumentException is thrown if BIOS vendor is longer than allowed.
*/
@Test
public final void biosVendorLongTest() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new FirmwareInfo(LONG_BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE));
}

/**
* Tests that an IllegalArgumentException is thrown if BIOS version is longer than allowed.
*/
@Test
public final void biosVersionLongTest() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new FirmwareInfo(BIOS_VENDOR, LONG_BIOS_VERSION, BIOS_RELEASE_DATE));
}

/**
* Tests that an IllegalArgumentException is thrown if BIOS release date is longer than allowed.
*/
@Test
public final void biosReleaseDateLongTest() {
Assertions.assertThrows(IllegalArgumentException.class,
() -> new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, LONG_BIOS_RELEASE_DATE));
}

/**
* Tests that two FirmwareInfo objects with the same BIOS vendor, version, and release date
* create hash codes that are equal.
*/
@Test
public final void testEqualHashCode() {
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
Assertions.assertEquals(fi2.hashCode(), fi1.hashCode());
}

/**
* Tests that two FirmwareInfo objects with different BIOS vendor information will generate
* different hash codes.
*/
@Test
public final void testNotEqualHashCodeBiosVendor() {
String biosVendor2 = "test bios vendor 2";
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(biosVendor2, BIOS_VERSION, BIOS_RELEASE_DATE);
Assertions.assertNotEquals(fi2.hashCode(), fi1.hashCode());
}

/**
* Tests that two FirmwareInfo objects with different BIOS version information will
* generate different hash codes.
*/
@Test
public final void testNotEqualHashCodeBiosVersion() {
String biosVersion2 = "test bios version 2";
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, biosVersion2, BIOS_RELEASE_DATE);
Assertions.assertNotEquals(fi2.hashCode(), fi1.hashCode());
}

/**
* Tests that two FirmwareInfo objects with different BIOS release date information will
* generate different hash codes.
*/
@Test
public final void testNotEqualHashCodeBiosReleaseDate() {
String biosReleaseDate2 = "test bios release date 2";
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, biosReleaseDate2);
Assertions.assertNotEquals(fi2.hashCode(), fi1.hashCode());
}

/**
* Tests that two FirmwareInfo objects with the same BIOS vendor, version, and release date
* information are equal.
*/
@Test
public final void testEqual() {
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
Assertions.assertEquals(fi2, fi1);
}

/**
* Tests that two FirmwareInfo objects with different BIOS vendor information are not equal.
*/
@Test
public final void testNotEqualBiosVendor() {
String biosVendor2 = "test bios vendor 2";
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(biosVendor2, BIOS_VERSION, BIOS_RELEASE_DATE);
Assertions.assertNotEquals(fi2, fi1);
}

/**
* Tests that two FirmwareInfo objects with different BIOS version information are not equal.
*/
@Test
public final void testNotEqualBiosVersion() {
String biosVersion2 = "test bios version 2";
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, biosVersion2, BIOS_RELEASE_DATE);
Assertions.assertNotEquals(fi2, fi1);
}

/**
* Tests that two FirmwareInfo objects with different BIOS release date information are not
* equal.
*/
@Test
public final void testNotEqualBiosReleaseDate() {
String biosReleaseDate2 = "test bios release date 2";
FirmwareInfo fi1 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, BIOS_RELEASE_DATE);
FirmwareInfo fi2 = new FirmwareInfo(BIOS_VENDOR, BIOS_VERSION, biosReleaseDate2);
Assertions.assertNotEquals(fi2, fi1);
}
}
Loading

0 comments on commit c1917f0

Please sign in to comment.