New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
5634 new unit tests #5643
Merged
Merged
5634 new unit tests #5643
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e27af54
create Test Class for ConfirmEmailDataTest IQSS#5634
nikzaugg 4995045
create Test Class for MyDataUtil IQSS#5634
nikzaugg 93801d0
create Test class for PasswordValidator in edu.harvard.iq.dataverse.p…
nikzaugg b4cb5f7
Merge branch 'develop' into 5634-new-unit-tests
nikzaugg 50c4c78
fix typo in PasswordValidatorTest IQSS#5634
nikzaugg 31f0d38
added a comment about JUnit Theories IQSS#5634
nikzaugg File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/test/java/edu/harvard/iq/dataverse/confirmemail/ConfirmEmailDataTest.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,66 @@ | ||
package edu.harvard.iq.dataverse.confirmemail; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; | ||
|
||
public class ConfirmEmailDataTest { | ||
|
||
private ConfirmEmailData instance; | ||
private AuthenticatedUser user; | ||
|
||
@Before | ||
public void setUp() { | ||
this.user = new AuthenticatedUser(); | ||
this.instance = new ConfirmEmailData(user, 60); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
this.instance = null; | ||
this.user = null; | ||
} | ||
|
||
@Test | ||
public void testConfirmEmailDataNotNull() { | ||
assertTrue(instance != null); | ||
} | ||
|
||
@Test | ||
public void testTokenNotNull() { | ||
assertTrue(instance.getToken() != null); | ||
} | ||
|
||
@Test | ||
public void testTokenCreationTimestampNotNull () { | ||
assertTrue(instance.getCreated() != null); | ||
} | ||
|
||
@Test | ||
public void testTokenExpirationTimestampNotNull () { | ||
assertTrue(instance.getExpires() != null); | ||
} | ||
|
||
@Test | ||
public void testTokenNotExpired() { | ||
assertFalse(instance.isExpired()); | ||
} | ||
|
||
@Test | ||
public void testAuthenticatedUserAssigned() { | ||
assertTrue(user == instance.getAuthenticatedUser()); | ||
} | ||
|
||
@Test | ||
public void testIdAssigned() { | ||
long id = 42; | ||
instance.setId(id); | ||
assertTrue(42 == instance.getId()); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/test/java/edu/harvard/iq/dataverse/mydata/MyDataUtilTest.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,88 @@ | ||
package edu.harvard.iq.dataverse.mydata; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assume.assumeTrue; | ||
|
||
import org.junit.experimental.theories.DataPoints; | ||
import org.junit.experimental.theories.Theories; | ||
import org.junit.experimental.theories.Theory; | ||
import org.junit.runner.RunWith; | ||
|
||
/** | ||
* Theories allows to add more formal tests to our code. In a way JUnit Theories behave | ||
* much like mathematical theories that hold for every element of a large (infinite) set. | ||
* JUnit will combine every possible combination (cartesian product) of datapoints and | ||
* pass these to the tests annotated with @Theory. The assume statements make sure, only | ||
* valid datapoints are tested in each Theory. | ||
* | ||
* @Datapoints - defines an array of values to test on | ||
* @Datapoint - stores one single value | ||
* | ||
* JUnit will no longer maintain a JUnit 4 Theories equivalent in the JUnit 5 codebase, as | ||
* mentioned in a discussion here: https://github.com/junit-team/junit5/pull/1422#issuecomment-389644868 | ||
*/ | ||
|
||
@RunWith(Theories.class) | ||
public class MyDataUtilTest { | ||
|
||
@DataPoints | ||
public static String[] userIdentifier = { | ||
"@nzaugg", "nzaugg@", "nzaugg", "123nzaugg", "", " ", null, "@", "n" }; | ||
|
||
@Theory | ||
public void testFormatUserIdentifierAsAssigneeIdentifierNull(String userIdentifier) { | ||
assumeTrue(userIdentifier == null); | ||
String formattedUserIdentifier = MyDataUtil.formatUserIdentifierAsAssigneeIdentifier(userIdentifier); | ||
assertTrue(formattedUserIdentifier == null); | ||
} | ||
|
||
@Theory | ||
public void testFormatUserIdentifierAsAssigneeIdentifierOneCharString(String userIdentifier) { | ||
assumeTrue(userIdentifier != null); | ||
assumeTrue(userIdentifier.startsWith("@")); | ||
String formattedUserIdentifier = MyDataUtil.formatUserIdentifierAsAssigneeIdentifier(userIdentifier); | ||
assertTrue(formattedUserIdentifier.equals(userIdentifier)); | ||
} | ||
|
||
@Theory | ||
public void testFormatUserIdentifierAsAssigneeIdentifier(String userIdentifier) { | ||
assumeTrue(userIdentifier != null); | ||
assumeTrue(!userIdentifier.startsWith("@")); | ||
String formattedUserIdentifier = MyDataUtil.formatUserIdentifierAsAssigneeIdentifier(userIdentifier); | ||
assertTrue(formattedUserIdentifier.equals("@" + userIdentifier)); | ||
} | ||
|
||
@Theory | ||
public void testFormatUserIdentifierForMyDataFormNull(String userIdentifier) { | ||
assumeTrue(userIdentifier == null); | ||
String formattedUserIdentifier = MyDataUtil.formatUserIdentifierForMyDataForm(userIdentifier); | ||
assertTrue(formattedUserIdentifier == null); | ||
} | ||
|
||
@Theory | ||
public void testFormatUserIdentifierForMyDataFormOneCharString(String userIdentifier) { | ||
assumeTrue(userIdentifier != null); | ||
assumeTrue(userIdentifier.startsWith("@")); | ||
assumeTrue(userIdentifier.length() == 1); | ||
String formattedUserIdentifier = MyDataUtil.formatUserIdentifierForMyDataForm(userIdentifier); | ||
assertTrue(formattedUserIdentifier == null); | ||
} | ||
|
||
@Theory | ||
public void testFormatUserIdentifierForMyDataFormLongerString(String userIdentifier) { | ||
assumeTrue(userIdentifier != null); | ||
assumeTrue(userIdentifier.startsWith("@")); | ||
assumeTrue(userIdentifier.length() > 1); | ||
String formattedUserIdentifier = MyDataUtil.formatUserIdentifierForMyDataForm(userIdentifier); | ||
assertTrue(formattedUserIdentifier.equals(userIdentifier.substring(1))); | ||
} | ||
|
||
@Theory | ||
public void testFormatUserIdentifierForMyDataForm(String userIdentifier) { | ||
assumeTrue(userIdentifier != null); | ||
assumeTrue(!userIdentifier.startsWith("@")); | ||
String formattedUserIdentifier = MyDataUtil.formatUserIdentifierForMyDataForm(userIdentifier); | ||
assertTrue(formattedUserIdentifier.equals(userIdentifier)); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordValidatorTest.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,75 @@ | ||
package edu.harvard.iq.dataverse.passwordreset; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
@RunWith(Parameterized.class) | ||
public class PasswordValidatorTest { | ||
|
||
public String password; | ||
public boolean expected; | ||
public boolean mustContainSpecialCharacters; | ||
public boolean mustContainCapitalLetters; | ||
public boolean mustContainNumbers; | ||
public int minLength; | ||
public int maxLength; | ||
|
||
public PasswordValidatorTest(String password, boolean expected, boolean mustContainSpecialCharacters, | ||
boolean mustContainCapitalLetters, boolean mustContainNumbers, int minLength, int maxLength) { | ||
this.password = password; | ||
this.expected = expected; | ||
this.mustContainSpecialCharacters = mustContainSpecialCharacters; | ||
this.mustContainCapitalLetters = mustContainCapitalLetters; | ||
this.mustContainNumbers = mustContainNumbers; | ||
this.minLength = minLength; | ||
this.maxLength = maxLength; | ||
} | ||
|
||
@Parameters | ||
public static Collection<Object[]> parameters() { | ||
return Arrays.asList( | ||
new Object[][] { | ||
// Check if PasswordValidator correctly validates correct passwords | ||
// with all combinations of Special Characters, | ||
// Capital Letters and Numbers | ||
{"abcdefghabcdefgh", true, false, false, false, 8, 30}, | ||
{"@bcdefgh@bcdefgh", true, true, false, false, 8, 30}, | ||
{"@bAdefgh@bAdefgh", true, true, true, false, 8, 30}, | ||
{"abAdefghabAdefgh", true, false, true, false, 8, 30}, | ||
{"a1Adefgha1Adefgh", true, false, true, true, 8, 30}, | ||
{"ab1defghab1defgh", true, false, false, true, 8, 30}, | ||
{"@1cdefgh@1cdefgh", true, true, false, true, 8, 30}, | ||
{"@1Adefgh@1Adefgh", true, true, true, true, 8, 30}, | ||
// Check if PasswordValidator correctly rejects wrong passwords | ||
// with all combinations of Special Characters, | ||
// Capital Letters and Numbers | ||
{"abcabc", false, false, false, false, 8, 30}, | ||
{"abcdabcd", false, true, false, false, 8, 30}, | ||
{"@bcd@bcd", false, true, true, false, 8, 30}, | ||
{"@bc1@bc1", false, false, true, false, 8, 30}, | ||
{"a1cda1cd", false, false, true, true, 8, 30}, | ||
{"AbcdAbcd", false, false, false, true, 8, 30}, | ||
{"@Bcd@Bcd", false, true, false, true, 8, 30}, | ||
{"a1Ada1Ad", false, true, true, true, 8, 30}, | ||
{"", false, false, false, false, 1, 30}, | ||
{" ", false, false, false, false, 1, 30}, | ||
{"?!abcdef", false, true, false, false, 8, 30} | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testValidatePassword() { | ||
PasswordValidator validator = PasswordValidator.buildValidator(mustContainSpecialCharacters, | ||
mustContainCapitalLetters, mustContainNumbers, minLength, maxLength); | ||
boolean isValidPassword = validator.validatePassword(password); | ||
assertEquals(expected, isValidPassword); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't seen JUnit theories before. They look really powerful and I like how you've used them here. They are experimental though and it looks like JUnit 5 is steering away from this sort of functionality junit-team/junit5#1422 (comment)
I'm ok with accepting this test. Can you leave a comment in the code about this experimentally for us to refer to when we decide to migrate tests? Also maybe a line about Theories in general?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @matthew-a-dunlap
Thanks for the review. I added a comment about JUnit Theories in a new commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be replaced with the upstream-supported Dynamic Testing in JUnit5?
Promising blog article on that: https://blog.codefx.org/libraries/junit-5-dynamic-tests/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@poikilotherm This does look really useful! I don't think I'll ask for it in this PR but if we want more dynamic tests this is the way to go.