Skip to content
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 6 commits into from Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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 src/test/java/edu/harvard/iq/dataverse/mydata/MyDataUtilTest.java
@@ -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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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/

Copy link
Contributor

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.


@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));
}

}
@@ -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);
}
}