Skip to content

Commit

Permalink
Remove use of deprecated RandomStringUtils method
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm committed Jan 21, 2025
1 parent e317a38 commit 4b5f082
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.qameta.allure.Story;
import org.apache.commons.lang3.RandomStringUtils;
import org.awaitility.Awaitility;
import org.eclipse.hawkbit.exception.SpServerError;
import org.eclipse.hawkbit.im.authentication.SpPermission;
Expand Down Expand Up @@ -554,7 +553,7 @@ void updateTargetDescription() throws Exception {
@Description("Ensures that target update request fails is updated value fails against a constraint.")
void updateTargetDescriptionFailsIfInvalidLength() throws Exception {
final String knownControllerId = "123";
final String knownNewDescription = RandomStringUtils.randomAlphabetic(513);
final String knownNewDescription = randomString(513);
final String knownNameNotModify = "nameNotModify";
final String body = new JSONObject().put("description", knownNewDescription).toString();

Expand Down Expand Up @@ -869,7 +868,7 @@ void getInstalledDistributionSetOfTargetIsEmpty() throws Exception {
@Test
@Description("Ensures that a target creation with empty name and a controllerId that exceeds the name length limitation is successful and the name gets truncated.")
void createTargetWithEmptyNameAndLongControllerId() throws Exception {
final String randomString = RandomStringUtils.randomAlphanumeric(JpaTarget.CONTROLLER_ID_MAX_SIZE);
final String randomString = randomString(JpaTarget.CONTROLLER_ID_MAX_SIZE);

final Target target = entityFactory.target().create().controllerId(randomString).build();

Expand Down Expand Up @@ -950,7 +949,7 @@ void createTargetWithMissingMandatoryPropertyBadRequest() throws Exception {
@Description("Verifies that a properties of new targets are validated as in allowed size range.")
void createTargetWithInvalidPropertyBadRequest() throws Exception {
final Target test1 = entityFactory.target().create().controllerId("id1")
.name(RandomStringUtils.randomAlphanumeric(NamedEntity.NAME_MAX_SIZE + 1)).build();
.name(randomString(NamedEntity.NAME_MAX_SIZE + 1)).build();

final MvcResult mvcResult = mvc
.perform(post(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.qameta.allure.Story;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.hawkbit.exception.SpServerError;
import org.eclipse.hawkbit.im.authentication.SpPermission;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
Expand Down Expand Up @@ -510,7 +509,7 @@ void invalidRequestsOnTargetTypesResource() throws Exception {
.andExpect(status().isBadRequest());

final TargetType tooLongName = entityFactory.targetType().create()
.name(RandomStringUtils.randomAlphanumeric(NamedEntity.NAME_MAX_SIZE + 1)).build();
.name(randomString(NamedEntity.NAME_MAX_SIZE + 1)).build();
mvc.perform(post(TARGETTYPES_ENDPOINT).content(JsonBuilder.targetTypes(Collections.singletonList(tooLongName)))
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.eclipse.hawkbit.im.authentication.SpPermission;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.qameta.allure.Story;
import org.apache.commons.lang3.RandomStringUtils;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.Condition;
import org.awaitility.Awaitility;
Expand Down Expand Up @@ -1869,7 +1868,7 @@ void deleteRolloutWhichHasBeenStartedBeforeIsSoftDeleted() {
@Test
@Description("Verifies that returned result considers provided sort parameter.")
void findAllRolloutsConsidersSorting() {
final String randomString = RandomStringUtils.randomAlphanumeric(5);
final String randomString = randomString(5);
final DistributionSet testDs = testdataFactory.createDistributionSet(randomString + "-testDs");
testdataFactory.createTargets(10, randomString + "-testTarget-");
final RolloutGroupConditions conditions = new RolloutGroupConditionBuilder().withDefaults().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.qameta.allure.Story;
import org.apache.commons.lang3.RandomStringUtils;
import org.awaitility.Awaitility;
import org.eclipse.hawkbit.im.authentication.SpPermission;
import org.eclipse.hawkbit.im.authentication.SpRole;
Expand Down Expand Up @@ -1231,7 +1230,7 @@ private void createAndUpdateTargetWithInvalidDescription(final Target target) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long description should not be created")
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId("a")
.description(RandomStringUtils.randomAlphanumeric(513))));
.description(randomString(513))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with invalid description should not be created").isThrownBy(() -> targetManagement
Expand All @@ -1240,7 +1239,7 @@ private void createAndUpdateTargetWithInvalidDescription(final Target target) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long description should not be updated")
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.description(RandomStringUtils.randomAlphanumeric(513))));
.description(randomString(513))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with invalid description should not be updated").isThrownBy(() -> targetManagement.update(
Expand All @@ -1253,7 +1252,7 @@ private void createAndUpdateTargetWithInvalidName(final Target target) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long name should not be created")
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId("a")
.name(RandomStringUtils.randomAlphanumeric(NamedEntity.NAME_MAX_SIZE + 1))));
.name(randomString(NamedEntity.NAME_MAX_SIZE + 1))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with invalid name should not be created").isThrownBy(() -> targetManagement
Expand All @@ -1262,7 +1261,7 @@ private void createAndUpdateTargetWithInvalidName(final Target target) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long name should not be updated")
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.name(RandomStringUtils.randomAlphanumeric(NamedEntity.NAME_MAX_SIZE + 1))));
.name(randomString(NamedEntity.NAME_MAX_SIZE + 1))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with invalid name should not be updated").isThrownBy(() -> targetManagement
Expand All @@ -1280,7 +1279,7 @@ private void createAndUpdateTargetWithInvalidSecurityToken(final Target target)
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long token should not be created")
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId("a")
.securityToken(RandomStringUtils.randomAlphanumeric(Target.SECURITY_TOKEN_MAX_SIZE + 1))));
.securityToken(randomString(Target.SECURITY_TOKEN_MAX_SIZE + 1))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with invalid token should not be created").isThrownBy(() -> targetManagement
Expand All @@ -1289,7 +1288,7 @@ private void createAndUpdateTargetWithInvalidSecurityToken(final Target target)
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long token should not be updated")
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.securityToken(RandomStringUtils.randomAlphanumeric(Target.SECURITY_TOKEN_MAX_SIZE + 1))));
.securityToken(randomString(Target.SECURITY_TOKEN_MAX_SIZE + 1))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with invalid token should not be updated").isThrownBy(() -> targetManagement.update(
Expand All @@ -1306,7 +1305,7 @@ private void createAndUpdateTargetWithInvalidAddress(final Target target) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long address should not be created")
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId("a")
.address(RandomStringUtils.randomAlphanumeric(513))));
.address(randomString(513))));

assertThatExceptionOfType(InvalidTargetAddressException.class).as("target with invalid should not be created")
.isThrownBy(() -> targetManagement
Expand All @@ -1315,7 +1314,7 @@ private void createAndUpdateTargetWithInvalidAddress(final Target target) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long address should not be updated")
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.address(RandomStringUtils.randomAlphanumeric(513))));
.address(randomString(513))));

assertThatExceptionOfType(InvalidTargetAddressException.class)
.as("target with invalid address should not be updated").isThrownBy(() -> targetManagement
Expand All @@ -1335,7 +1334,7 @@ private void createTargetWithInvalidControllerId() {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with too long controller id should not be created")
.isThrownBy(() -> targetManagement.create(entityFactory.target().create()
.controllerId(RandomStringUtils.randomAlphanumeric(Target.CONTROLLER_ID_MAX_SIZE + 1))));
.controllerId(randomString(Target.CONTROLLER_ID_MAX_SIZE + 1))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("target with invalid controller id should not be created").isThrownBy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.hawkbit.artifact.repository.ArtifactRepository;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.ArtifactManagement;
Expand Down Expand Up @@ -117,8 +116,6 @@ public abstract class AbstractIntegrationTest {
protected static final URI LOCALHOST = URI.create("http://127.0.0.1");
protected static final int DEFAULT_TEST_WEIGHT = 500;

protected static final RandomStringUtils RANDOM_STRING_UTILS = RandomStringUtils.secure();

/**
* Number of {@link DistributionSetType}s that exist in every test case. One
* generated by using
Expand Down Expand Up @@ -296,11 +293,11 @@ protected static Comparator<Target> controllerIdComparator() {
}

protected static String randomString(final int len) {
return RANDOM_STRING_UTILS.next(len, true, false);
return TestdataFactory.randomString(len);
}

protected static byte[] randomBytes(final int len) {
return randomString(len).getBytes();
return TestdataFactory.randomBytes(len);
}

protected DistributionSetAssignmentResult assignDistributionSet(final long dsID, final String controllerId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
*/
package org.eclipse.hawkbit.repository.test.util;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;

/**
* Holds all database related configuration
*/
@Getter
@Slf4j
public class DatasourceContext {

Expand All @@ -23,19 +24,19 @@ public class DatasourceContext {
public static final String SPRING_DATABASE_USERNAME_KEY = "spring.datasource.username";
public static final String SPRING_DATABASE_PASSWORD_KEY = "spring.datasource.password";
public static final String DATABASE_PREFIX_KEY = "spring.database.random.prefix";

private static final String RANDOM_DB_PREFIX = System.getProperty(DATABASE_PREFIX_KEY, "HAWKBIT_TEST_");

private final String database;
private final String datasourceUrl;
private final String username;
private final String password;
private final String randomSchemaName = RANDOM_DB_PREFIX + RandomStringUtils.randomAlphanumeric(10);
private final String randomSchemaName = RANDOM_DB_PREFIX + TestdataFactory.randomString(10);

/**
* Constructor
*/
public DatasourceContext(final String database, final String datasourceUrl, final String username,
final String password) {
public DatasourceContext(final String database, final String datasourceUrl, final String username, final String password) {
this.database = database;
this.datasourceUrl = datasourceUrl;
this.username = username;
Expand All @@ -47,36 +48,14 @@ public DatasourceContext(final String database, final String datasourceUrl, fina
*/
public DatasourceContext() {
database = System.getProperty(SPRING_DATABASE_KEY, System.getProperty(upperCaseVariant(SPRING_DATABASE_KEY)));
datasourceUrl = System.getProperty(SPRING_DATASOURCE_URL_KEY,
System.getProperty(upperCaseVariant(SPRING_DATASOURCE_URL_KEY)));
username = System.getProperty(SPRING_DATABASE_USERNAME_KEY,
System.getProperty(upperCaseVariant(SPRING_DATABASE_USERNAME_KEY)));
password = System.getProperty(SPRING_DATABASE_PASSWORD_KEY,
System.getProperty(upperCaseVariant(SPRING_DATABASE_PASSWORD_KEY)));
}

public String getDatabase() {
return database;
}

public String getDatasourceUrl() {
return datasourceUrl;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public String getRandomSchemaName() {
return randomSchemaName;
datasourceUrl = System.getProperty(SPRING_DATASOURCE_URL_KEY, System.getProperty(upperCaseVariant(SPRING_DATASOURCE_URL_KEY)));
username = System.getProperty(SPRING_DATABASE_USERNAME_KEY, System.getProperty(upperCaseVariant(SPRING_DATABASE_USERNAME_KEY)));
password = System.getProperty(SPRING_DATABASE_PASSWORD_KEY, System.getProperty(upperCaseVariant(SPRING_DATABASE_PASSWORD_KEY)));
}

public boolean isNotProperlyConfigured() {
log.debug("Datasource environment variables: [database: {}, username: {}, password: {}, datasourceUrl: {}]",
log.debug(
"Datasource environment variables: [database: {}, username: {}, password: {}, datasourceUrl: {}]",
database, username, password, datasourceUrl);

return database == null || datasourceUrl == null || username == null || password == null;
Expand All @@ -85,4 +64,4 @@ public boolean isNotProperlyConfigured() {
private static String upperCaseVariant(final String key) {
return key.toUpperCase().replace('.', '_');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public class TestdataFactory {

public static final String DEFAULT_COLOUR = "#000000";

public static final RandomStringUtils RANDOM_STRING_UTILS = RandomStringUtils.secure();

private static final String SPACE_AND_DESCRIPTION = " description";

@Autowired
Expand Down Expand Up @@ -191,8 +193,16 @@ public class TestdataFactory {
@Autowired
private QuotaManagement quotaManagement;

public static String randomString(final int len) {
return RANDOM_STRING_UTILS.next(len, true, false);
}

public static byte[] randomBytes(final int len) {
return randomString(len).getBytes();
}

public Action performAssignment(final DistributionSet distributionSet) {
final Target target = createTarget(RandomStringUtils.randomAlphanumeric(5));
final Target target = createTarget(randomString(5));
final DeploymentRequest deploymentRequest = new DeploymentRequest(target.getControllerId(),
distributionSet.getId(), ActionType.FORCED, 0, null, null, null, null, false);
deploymentManagement.assignDistributionSets(Collections.singletonList(deploymentRequest));
Expand Down Expand Up @@ -1090,7 +1100,7 @@ public Rollout createRollout(final String prefix) {
* @return created {@link Rollout}
*/
public Rollout createRollout() {
final String prefix = RandomStringUtils.randomAlphanumeric(5);
final String prefix = randomString(5);
createTargets(quotaManagement.getMaxTargetsPerRolloutGroup() * quotaManagement.getMaxRolloutGroupsPerRollout(),
prefix);
return createRolloutByVariables(prefix, prefix + SPACE_AND_DESCRIPTION,
Expand Down Expand Up @@ -1158,7 +1168,7 @@ public Rollout createSimpleTestRolloutWithTargetsAndDistributionSet(final int am
public Rollout createSimpleTestRolloutWithTargetsAndDistributionSet(final int amountTargetsForRollout,
final int amountOtherTargets, final int amountOfGroups, final String successCondition,
final String errorCondition, final ActionType actionType, final Integer weight) {
final String suffix = RandomStringUtils.randomAlphanumeric(5);
final String suffix = randomString(5);
final DistributionSet rolloutDS = createDistributionSet("rolloutDS-" + suffix);
createTargets(amountTargetsForRollout, "rollout-" + suffix + "-", "rollout");
createTargets(amountOtherTargets, "others-" + suffix + "-", "rollout");
Expand Down Expand Up @@ -1256,15 +1266,11 @@ public DistributionSet createIncompleteDistributionSet() {
}

private static String randomDescriptionShort() {
return randomText(100);
return randomString(100);
}

private static String randomDescriptionLong() {
return randomText(200);
}

private static String randomText(final int len) {
return RandomStringUtils.randomAlphanumeric(len);
return randomString(200);
}

private void addTestModuleMetadata(final SoftwareModule module) {
Expand Down

0 comments on commit 4b5f082

Please sign in to comment.