Skip to content

Commit

Permalink
Adding PolicyTest and associated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iadgovuser59 committed Sep 1, 2023
1 parent 8386864 commit 0e6450d
Show file tree
Hide file tree
Showing 3 changed files with 279 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();
}
}

204 changes: 204 additions & 0 deletions HIRS_Utils/src/test/java/hirs/data/persist/PolicyTest.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <code>PolicyTest</code> is a unit test class for the <code>Policy</code>
* class.
*/
public final class PolicyTest {

private static final Logger LOGGER = LogManager.getLogger(PolicyTest.class);

/**
* Empty constructor that does nothing.
*/
public PolicyTest() {
/* do nothing */
}

/**
* Tests <code>Policy</code> constructor with valid name.
*/
@Test
public void testPolicy() {
LOGGER.debug("testPolicy test started");
final String name = "myPolicy";
new TestPolicy(name);
}

/**
* Tests that <code>Policy</code> constructor throws
* <code>NullPointerException</code> with null name.
*/
@Test
public void testPolicyNullName() {
LOGGER.debug("testPolicyNullName test started");
Assertions.assertThrows(NullPointerException.class, () -> new TestPolicy(null));
}

/**
* Tests that <code>getName()</code> 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 <code>Policy</code> constructor throws
* <code>NullPointerException</code> 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 <code>getDescription()()</code> 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 <code>Policy</code> 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 <code>Policy</code> 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 <code>Policy</code> 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 <code>Policy</code> 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 <code>Policy</code>.
*/
@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 <code>Policy</code>.
*/
@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 <code>Policy</code>.
*/
@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 <code>Policy</code>.
*/
@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));
}
}

37 changes: 37 additions & 0 deletions HIRS_Utils/src/test/java/hirs/data/persist/TestPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hirs.data.persist;

import hirs.attestationca.persist.entity.Policy;
import jakarta.persistence.Entity;

/**
* This <code>Policy</code> class is used exclusively for testing purposes.
*/
@Entity
public class TestPolicy extends Policy {

/**
* Creates a new <code>TestPolicy</code> with the set name.
*
* @param name name
*/
public TestPolicy(final String name) {
super(name);
}

/**
* Creates a new <code>TestPolicy</code> 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();
}
}

0 comments on commit 0e6450d

Please sign in to comment.