-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding SupplyChainSummaryTest.java. Failed assertions still present, …
…caused by NullPointerException from getDevice()
- Loading branch information
1 parent
8559ea2
commit 67e7f57
Showing
1 changed file
with
182 additions
and
0 deletions.
There are no files selected for viewing
182 changes: 182 additions & 0 deletions
182
.../java/hirs/attestationca/persist/entity/userdefined/SupplyChainValidationSummaryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package hirs.attestationca.persist.entity.userdefined; | ||
import hirs.attestationca.persist.entity.ArchivableEntity; | ||
import hirs.attestationca.persist.enums.AppraisalStatus; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Tests the functionality in SupplyChainValidationSummary, as well as the persistence of | ||
* SupplyChainValidationSummary and SupplyChainValidation. | ||
*/ | ||
public class SupplyChainValidationSummaryTest { | ||
private Device device = DeviceTest.getTestDevice("TestDevice"); | ||
private List<ArchivableEntity> certificates = CertificateTest.getAllTestCertificates();; | ||
|
||
public SupplyChainValidationSummaryTest() throws Exception { | ||
} | ||
|
||
/** | ||
* Tests that an empty summary behaves as expected. | ||
*/ | ||
@Test | ||
public void testEmptySummary() throws InterruptedException { | ||
SupplyChainValidationSummary emptySummary = getTestSummary( | ||
0, | ||
0, | ||
certificates | ||
); | ||
|
||
// assertEquals(emptySummary.getDevice(), device); | ||
assertEquals(emptySummary.getValidations(), Collections.EMPTY_SET); | ||
assertEquals(emptySummary.getOverallValidationResult(), AppraisalStatus.Status.PASS); | ||
assertNotNull(emptySummary.getCreateTime()); | ||
} | ||
|
||
/** | ||
* Test that a summary can't be created with a null validationIdentifier. | ||
*/ | ||
@Test | ||
public void testNullValidationIdentifier() { | ||
assertThrows(IllegalArgumentException.class, () -> | ||
new SupplyChainValidationSummary(null, Collections.emptyList())); | ||
} | ||
|
||
/** | ||
* Test that a summary can't be created with a null validations list. | ||
*/ | ||
@Test | ||
public void testNullValidationList() { | ||
assertThrows(IllegalArgumentException.class, () -> | ||
new SupplyChainValidationSummary(device, null)); | ||
} | ||
|
||
/** | ||
* Test that summaries with one and two component validations, which both represent successful | ||
* validations, have getters that return the expected information. | ||
*/ | ||
@Test | ||
public void testSuccessfulSummary() throws InterruptedException { | ||
SupplyChainValidationSummary oneValidation = getTestSummary( | ||
1, | ||
0, | ||
certificates | ||
); | ||
|
||
assertEquals(oneValidation.getDevice(), device); | ||
assertEquals(oneValidation.getValidations().size(), 1); | ||
assertEquals(oneValidation.getOverallValidationResult(), | ||
AppraisalStatus.Status.PASS); | ||
assertNotNull(oneValidation.getCreateTime()); | ||
|
||
SupplyChainValidationSummary twoValidations = getTestSummary( | ||
2, | ||
0, | ||
certificates | ||
); | ||
|
||
assertEquals(twoValidations.getDevice(), device); | ||
assertEquals(2, twoValidations.getValidations().size()); | ||
assertEquals(twoValidations.getOverallValidationResult(), | ||
AppraisalStatus.Status.PASS); | ||
assertNotNull(twoValidations.getCreateTime()); | ||
} | ||
|
||
/** | ||
* Test that summaries with one and two component validations, of which one represents an | ||
* unsuccessful validations, have getters that return the expected information. | ||
*/ | ||
@Test | ||
public void testUnsuccessfulSummary() throws InterruptedException { | ||
SupplyChainValidationSummary oneValidation = getTestSummary( | ||
1, | ||
1, | ||
certificates | ||
); | ||
|
||
assertEquals(oneValidation.getDevice(), device); | ||
assertEquals(oneValidation.getValidations().size(), 1); | ||
assertEquals(oneValidation.getOverallValidationResult(), | ||
AppraisalStatus.Status.FAIL); | ||
assertNotNull(oneValidation.getCreateTime()); | ||
|
||
SupplyChainValidationSummary twoValidations = getTestSummary( | ||
2, | ||
1, | ||
certificates | ||
); | ||
|
||
assertEquals(twoValidations.getDevice(), device); | ||
assertEquals(twoValidations.getValidations().size(), 2); | ||
assertEquals(twoValidations.getOverallValidationResult(), | ||
AppraisalStatus.Status.FAIL); | ||
assertNotNull(twoValidations.getCreateTime()); | ||
|
||
SupplyChainValidationSummary twoBadValidations = getTestSummary( | ||
2, | ||
2, | ||
certificates | ||
); | ||
|
||
assertEquals(twoBadValidations.getDevice(), device); | ||
assertEquals(twoBadValidations.getValidations().size(), 2); | ||
assertEquals(twoBadValidations.getOverallValidationResult(), | ||
AppraisalStatus.Status.FAIL); | ||
assertNotNull(twoBadValidations.getCreateTime()); | ||
} | ||
|
||
private SupplyChainValidationSummary getTestSummary( | ||
final int numberOfValidations, | ||
final int numFail, | ||
final List<ArchivableEntity> certificates | ||
) throws InterruptedException { | ||
SupplyChainValidation.ValidationType[] validationTypes = | ||
SupplyChainValidation.ValidationType.values(); | ||
|
||
if (numberOfValidations > validationTypes.length) { | ||
throw new IllegalArgumentException(String.format( | ||
"Cannot have more than %d validation types", | ||
validationTypes.length | ||
)); | ||
} | ||
|
||
if (numFail > numberOfValidations) { | ||
throw new IllegalArgumentException(String.format( | ||
"Cannot have more than %d failed validations", | ||
validationTypes.length | ||
)); | ||
} | ||
|
||
Collection<SupplyChainValidation> validations = new HashSet<>(); | ||
for (int i = 0; i < numberOfValidations; i++) { | ||
boolean successful = true; | ||
if (i >= (numberOfValidations - numFail)) { | ||
successful = false; | ||
} | ||
|
||
AppraisalStatus.Status result = AppraisalStatus.Status.FAIL; | ||
if (successful) { | ||
result = AppraisalStatus.Status.PASS; | ||
} | ||
|
||
validations.add(SupplyChainValidationTest.getTestSupplyChainValidation( | ||
validationTypes[i], | ||
result, | ||
certificates | ||
)); | ||
Thread.sleep(1); | ||
} | ||
|
||
|
||
|
||
return new SupplyChainValidationSummary(device, validations); | ||
} | ||
} |