Skip to content

Commit

Permalink
Move random string generation to one place
Browse files Browse the repository at this point in the history
  • Loading branch information
henning-gerhardt committed May 13, 2024
1 parent 9e93112 commit 6ace832
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
23 changes: 16 additions & 7 deletions Kitodo/src/main/java/org/kitodo/production/helper/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -535,19 +536,27 @@ public static String getNormalizedTitle(String title) {
}

/**
* Generate random string.
* Generate random string with only letters.
*
* @param length
* of random string to be created
* @return random string
*/
public static String generateRandomString(int length) {
final String AB = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return generateRandomString(length, true, false);
}

StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
sb.append(AB.charAt(secureRandom.nextInt(AB.length())));
}
return sb.toString();
/**
* Generate random string with and without letters and / or numbers.
*
* @param length of string to genertate
* @param letters should letter used
* @param numbers should number used
* @return random string with and without letters and / or numbers.
*/
public static String generateRandomString(int length, boolean letters, boolean numbers) {
// RandomStringUtils is using a non-secure random generator by default
// call random method with all parameters to set a secure random generator
return RandomStringUtils.random(length, 0, 0, letters, numbers, null, secureRandom);
}
}
38 changes: 38 additions & 0 deletions Kitodo/src/test/java/org/kitodo/production/helper/HelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.production.helper;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

class HelperTest {

@Test
void generateRandomStringOnlyLetters() {
String randomString = Helper.generateRandomString(10);
assertTrue(StringUtils.isAlpha(randomString), "Generated string contains not only letters");
}

@Test
void generateRandomStringOnlyNumbers() {
String randomString = Helper.generateRandomString(10, false, true);
assertTrue(StringUtils.isNumeric(randomString), "Generated string contains not only numbers");
}

@Test
void generateRandomStringLettersAndNumbers() {
String randomString = Helper.generateRandomString(10, true, true);
assertTrue(StringUtils.isAlphanumeric(randomString), "Generated string contains not only letters and numbers");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,27 @@

package org.kitodo.selenium.testframework.generators;

import java.security.SecureRandom;

import org.apache.commons.lang3.RandomStringUtils;
import org.kitodo.data.database.beans.User;
import org.kitodo.production.helper.Helper;

public class UserGenerator {

private static final SecureRandom secureRandom = new SecureRandom();

/**
* Create user with random name, surname, login and password.
*
* @return Created user.
*/
public static User generateUser() {
String suffix = generateRandomString(5);
String suffix = Helper.generateRandomString(5, true, true);

User user = new User();
// sometimes generated password doesn't contain number so it is added here explicitly
user.setPassword("P1_" + generateRandomString(10));
user.setPassword("P1_" + Helper.generateRandomString(10, true, true));
user.setName("Name" + suffix);
user.setSurname("Surname" + suffix);
user.setLogin("UserLogin" + suffix);
user.setLocation("MockLocation");

return user;
}

/**
* Create a random string with a defined length.
*
* @param length How long the to be created string should be
* @return Created string with random values.
*/
private static String generateRandomString(int length) {
// RandomStringUtils is using a non-secure random generator by default
// call random method with all parameters to set a secure random generator
return RandomStringUtils.random(length, 0, 0, true, true, null, secureRandom);
}
}

0 comments on commit 6ace832

Please sign in to comment.