From 0e6450da7f680576c6c6fbb45e203f1052a9be89 Mon Sep 17 00:00:00 2001 From: iadgovuser59 <133057011+iadgovuser59@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:30:09 -0400 Subject: [PATCH] Adding PolicyTest and associated changes --- .../persist/entity/UserDefinedEntity.java | 43 +++- .../java/hirs/data/persist/PolicyTest.java | 204 ++++++++++++++++++ .../java/hirs/data/persist/TestPolicy.java | 37 ++++ 3 files changed, 279 insertions(+), 5 deletions(-) create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java create mode 100644 HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java index 651d32526..ca38680dc 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/UserDefinedEntity.java @@ -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 @@ -42,5 +40,40 @@ protected UserDefinedEntity() { public UserDefinedEntity(final String name) { this(name, ""); } + + /** + * Returns a boolean if other is equal to this. UserDefinedEntitys are + * identified by their name, so this returns true if other is + * an instance of UserDefinedEntity and its name is the same as this + * UserDefinedEntity. Otherwise this returns false. + * + * @param other + * other object to test for equals + * @return true if other is Baseline 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 UserDefinedEntity. + * UserDefinedEntitys are identified by their name, so the returned hash + * is the hash of the name. + * + * @return hash + */ + @Override + public int hashCode() { + return name.hashCode(); + } } diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java b/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java new file mode 100644 index 000000000..93f1f4d3d --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java @@ -0,0 +1,204 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.Policy; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * PolicyTest is a unit test class for the Policy + * class. + */ +public final class PolicyTest { + + private static final Logger LOGGER = LogManager.getLogger(PolicyTest.class); + + /** + * Empty constructor that does nothing. + */ + public PolicyTest() { + /* do nothing */ + } + + /** + * Tests Policy constructor with valid name. + */ + @Test + public void testPolicy() { + LOGGER.debug("testPolicy test started"); + final String name = "myPolicy"; + new TestPolicy(name); + } + + /** + * Tests that Policy constructor throws + * NullPointerException with null name. + */ + @Test + public void testPolicyNullName() { + LOGGER.debug("testPolicyNullName test started"); + Assertions.assertThrows(NullPointerException.class, () -> new TestPolicy(null)); + } + + /** + * Tests that getName() returns the name. + */ + @Test + public void testGetName() { + LOGGER.debug("testGetName test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + Assertions.assertEquals(name, p.getName()); + } + + /** + * Tests that Policy constructor throws + * NullPointerException with null name. + */ + @Test + public void testPolicyNullDescription() { + LOGGER.debug("testPolicyNullDescription test started"); + final String name = "myPolicy"; + Assertions.assertThrows(NullPointerException.class, () -> new TestPolicy(name, null)); + } + + /** + * Tests that getDescription()() returns the description. + */ + @Test + public void testGetDescription() { + LOGGER.debug("testGetDescription test started"); + final String name = "myPolicy"; + final String description = "myDescription"; + Policy p = new TestPolicy(name, description); + Assertions.assertEquals(description, p.getDescription()); + } + + /** + * Tests that two Policy objects are equal if they have the + * same name. + */ + @Test + public void testEquals() { + LOGGER.debug("testEquals test started"); + final String name1 = "myPolicy"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy"; + Policy p2 = new TestPolicy(name2); + Assertions.assertEquals(p2, p1); + Assertions.assertEquals(p2, p1); + Assertions.assertEquals(p2, p1); + Assertions.assertEquals(p2, p1); + } + + /** + * Tests that two Policy objects are not equal if the names are + * different. + */ + @Test + public void testNotEquals() { + LOGGER.debug("testNotEquals test started"); + final String name1 = "myPolicy1"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy2"; + Policy p2 = new TestPolicy(name2); + Assertions.assertNotEquals(p2, p1); + Assertions.assertNotEquals(p2, p1); + Assertions.assertEquals(p1, p1); + Assertions.assertEquals(p2, p2); + } + + /** + * Tests that hash code is that of the name. + */ + @Test + public void testHashCode() { + LOGGER.debug("testHashCode test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + Assertions.assertEquals(p.hashCode(), name.hashCode()); + } + + /** + * Tests that the hash code of two Policy objects are the same + * if the names are the same. + */ + @Test + public void testHashCodeEquals() { + LOGGER.debug("testHashCodeEquals test started"); + final String name1 = "myPolicy"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy"; + Policy p2 = new TestPolicy(name2); + Assertions.assertEquals(p2.hashCode(), p1.hashCode()); + } + + /** + * Tests that the hash codes of two Policy objects are + * different if they have different names. + */ + @Test + public void testHashCodeNotEquals() { + LOGGER.debug("testHashCodeNotEquals test started"); + final String name1 = "myPolicy1"; + Policy p1 = new TestPolicy(name1); + final String name2 = "myPolicy2"; + Policy p2 = new TestPolicy(name2); + Assertions.assertNotEquals(p2.hashCode(), p1.hashCode()); + } + + /** + * Tests that the name can be set for a Policy. + */ + @Test + public void setName() { + LOGGER.debug("setName test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + final String newName = "newPolicy"; + p.setName(newName); + Assertions.assertEquals(p.getName(), newName); + Assertions.assertEquals(p.hashCode(), newName.hashCode()); + } + + /** + * Tests that the description can be set for a Policy. + */ + @Test + public void setDescription() { + LOGGER.debug("setDescription test started"); + final String name = "myPolicy"; + final String description = "myDescription"; + Policy p = new TestPolicy(name, description); + final String newDescription = "newDescription"; + p.setDescription(newDescription); + Assertions.assertEquals(p.getDescription(), newDescription); + } + + /** + * Tests that a name cannot be null for a Policy. + */ + @Test + public void setNameNull() { + LOGGER.debug("setNameNull test started"); + final String name = "myPolicy"; + Policy p = new TestPolicy(name); + Assertions.assertThrows(NullPointerException.class, () -> p.setName(null)); + } + + /** + * Tests that a description cannot be null for a Policy. + */ + @Test + public void setDescriptionNull() { + LOGGER.debug("setDescriptionNull test started"); + final String name = "myPolicy"; + final String description = "myDescription"; + Policy p = new TestPolicy(name, description); + Assertions.assertThrows(NullPointerException.class, () -> p.setDescription(null)); + } +} + diff --git a/HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java b/HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java new file mode 100644 index 000000000..a8682eab2 --- /dev/null +++ b/HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java @@ -0,0 +1,37 @@ +package hirs.data.persist; + +import hirs.attestationca.persist.entity.Policy; +import jakarta.persistence.Entity; + +/** + * This Policy class is used exclusively for testing purposes. + */ +@Entity +public class TestPolicy extends Policy { + + /** + * Creates a new TestPolicy with the set name. + * + * @param name name + */ + public TestPolicy(final String name) { + super(name); + } + + /** + * Creates a new TestPolicy with the set name and description. + * + * @param name name + * @param description description + */ + public TestPolicy(final String name, final String description) { + super(name, description); + } + + /** + * Default constructor necessary for Hibernate. + */ + protected TestPolicy() { + super(); + } +}