Feature/fix sonar warnings (#1226)
* Fixed sonar warnings - "Cognitive Complexity" - "Do not use replaceAll when not using a regex" - java:S5869 - Character classes in regular expressions should not contain the same character twice - Improved bad name - Typos - reduced code duplications - Replaced hand-made wait-utility with Awaitility - Log messages - Duplicate code - Typos - Removed Thread.sleep, instead relaxed check condition - Removed use of deprecated API - Removed use of deprecated API - Added supress-warnings as I do not see a better way to write the tests - Removed Thread.sleep / redundant functionality to Awaitility - Fixed other warnings (use isZero, isEmpty, hasToString) - Removed/Reduced duplicate code - Added generics - Fixed asserts - removed: field.setAccessible(true) actually should not be needed for public static fields! - Too long constructor passes arguments in wrong order - how surprisingly... - Clean-up use of varargs arguments - Fixed regex - Fixed typos and other minor stuff - Making public constructors protected in abstract classes - Swapped expected and asserted argument - volatile not enough for syncing threads - volatile not enough for syncing threads - out-commented code - Made regex not-greedy, added tests for verification - Avoid exposure of thread-local member var Signed-off-by: Peter Vigier <Peter.Vigier@bosch.io> * Fixed Sonar warnings * License header fix Signed-off-by: Peter Vigier <Peter.Vigier@bosch.io> * License header fix #2 Signed-off-by: Peter Vigier <Peter.Vigier@bosch.io> * Fixing review findings Signed-off-by: Peter Vigier <Peter.Vigier@bosch.io> * Fixing tests - Fixed '&' usage in javadoc and typos - Fixing some warnings Signed-off-by: Peter Vigier <Peter.Vigier@bosch.io>
This commit is contained in:
@@ -17,11 +17,10 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
@@ -59,7 +58,6 @@ import org.eclipse.hawkbit.repository.model.MetaData;
|
||||
import org.eclipse.hawkbit.repository.model.RepositoryModelConstants;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetMetadata;
|
||||
import org.eclipse.hawkbit.repository.test.TestConfiguration;
|
||||
import org.eclipse.hawkbit.repository.test.matcher.EventVerifier;
|
||||
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
||||
@@ -103,7 +101,7 @@ import org.springframework.test.context.TestPropertySource;
|
||||
// Cleaning repository will fire "delete" events. We won't count them to the
|
||||
// test execution. So, the order execution between EventVerifier and Cleanup is
|
||||
// important!
|
||||
@TestExecutionListeners(inheritListeners = true, listeners = { EventVerifier.class, CleanupTestExecutionListener.class,
|
||||
@TestExecutionListeners(listeners = { EventVerifier.class, CleanupTestExecutionListener.class,
|
||||
MySqlTestDatabase.class, MsSqlTestDatabase.class,
|
||||
PostgreSqlTestDatabase.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
|
||||
@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
|
||||
@@ -302,7 +300,7 @@ public abstract class AbstractIntegrationTest {
|
||||
}
|
||||
|
||||
protected DistributionSetAssignmentResult assignDistributionSet(final DistributionSet pset, final Target target) {
|
||||
return assignDistributionSet(pset, Arrays.asList(target));
|
||||
return assignDistributionSet(pset, Collections.singletonList(target));
|
||||
}
|
||||
|
||||
protected DistributionSetAssignmentResult assignDistributionSet(final long dsId, final String targetId,
|
||||
@@ -322,16 +320,16 @@ public abstract class AbstractIntegrationTest {
|
||||
return distributionSetManagement.createMetaData(dsId, md);
|
||||
}
|
||||
|
||||
protected TargetMetadata createTargetMetadata(final String controllerId, final MetaData md) {
|
||||
return createTargetMetadata(controllerId, Collections.singletonList(md)).get(0);
|
||||
protected void createTargetMetadata(final String controllerId, final MetaData md) {
|
||||
createTargetMetadata(controllerId, Collections.singletonList(md));
|
||||
}
|
||||
|
||||
protected List<TargetMetadata> createTargetMetadata(final String controllerId, final List<MetaData> md) {
|
||||
return targetManagement.createMetaData(controllerId, md);
|
||||
private void createTargetMetadata(final String controllerId, final List<MetaData> md) {
|
||||
targetManagement.createMetaData(controllerId, md);
|
||||
}
|
||||
|
||||
protected Long getOsModule(final DistributionSet ds) {
|
||||
return ds.findFirstModuleByType(osType).get().getId();
|
||||
return ds.findFirstModuleByType(osType).orElseThrow(NoSuchElementException::new).getId();
|
||||
}
|
||||
|
||||
protected Action prepareFinishedUpdate() {
|
||||
@@ -386,7 +384,7 @@ public abstract class AbstractIntegrationTest {
|
||||
|
||||
}
|
||||
|
||||
private static String artifactDirectory = createTempDir();
|
||||
private static final String ARTIFACT_DIRECTORY = createTempDir();
|
||||
|
||||
private static String createTempDir() {
|
||||
try {
|
||||
@@ -398,9 +396,9 @@ public abstract class AbstractIntegrationTest {
|
||||
|
||||
@AfterEach
|
||||
public void cleanUp() {
|
||||
if (new File(artifactDirectory).exists()) {
|
||||
if (new File(ARTIFACT_DIRECTORY).exists()) {
|
||||
try {
|
||||
FileUtils.cleanDirectory(new File(artifactDirectory));
|
||||
FileUtils.cleanDirectory(new File(ARTIFACT_DIRECTORY));
|
||||
} catch (final IOException | IllegalArgumentException e) {
|
||||
LOG.warn("Cannot cleanup file-directory", e);
|
||||
}
|
||||
@@ -409,14 +407,14 @@ public abstract class AbstractIntegrationTest {
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeClass() {
|
||||
System.setProperty("org.eclipse.hawkbit.repository.file.path", artifactDirectory);
|
||||
System.setProperty("org.eclipse.hawkbit.repository.file.path", ARTIFACT_DIRECTORY);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void afterClass() {
|
||||
if (new File(artifactDirectory).exists()) {
|
||||
if (new File(ARTIFACT_DIRECTORY).exists()) {
|
||||
try {
|
||||
FileUtils.deleteDirectory(new File(artifactDirectory));
|
||||
FileUtils.deleteDirectory(new File(ARTIFACT_DIRECTORY));
|
||||
} catch (final IOException | IllegalArgumentException e) {
|
||||
LOG.warn("Cannot delete file-directory", e);
|
||||
}
|
||||
@@ -449,20 +447,6 @@ public abstract class AbstractIntegrationTest {
|
||||
return currentTime.getOffset().getId().replace("Z", "+00:00");
|
||||
}
|
||||
|
||||
protected static String generateRandomStringWithLength(final int length) {
|
||||
final StringBuilder randomStringBuilder = new StringBuilder(length);
|
||||
final Random rand = new Random();
|
||||
final int lowercaseACode = 97;
|
||||
final int lowercaseZCode = 122;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
final char randomCharacter = (char) (rand.nextInt(lowercaseZCode - lowercaseACode + 1) + lowercaseACode);
|
||||
randomStringBuilder.append(randomCharacter);
|
||||
}
|
||||
|
||||
return randomStringBuilder.toString();
|
||||
}
|
||||
|
||||
protected static Action getFirstAssignedAction(
|
||||
final DistributionSetAssignmentResult distributionSetAssignmentResult) {
|
||||
return distributionSetAssignmentResult.getAssignedEntity().stream().findFirst()
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2022 Bosch.IO GmbH and others.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.test.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
|
||||
public class TargetTestData {
|
||||
public static final String ATTRIBUTE_KEY_TOO_LONG;
|
||||
public static final String ATTRIBUTE_KEY_VALID;
|
||||
public static final String ATTRIBUTE_VALUE_TOO_LONG;
|
||||
public static final String ATTRIBUTE_VALUE_VALID;
|
||||
|
||||
static {
|
||||
final Random rand = new Random();
|
||||
ATTRIBUTE_KEY_TOO_LONG = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_KEY_SIZE + 1, rand);
|
||||
ATTRIBUTE_KEY_VALID = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_KEY_SIZE, rand);
|
||||
ATTRIBUTE_VALUE_TOO_LONG = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_VALUE_SIZE + 1, rand);
|
||||
ATTRIBUTE_VALUE_VALID = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_VALUE_SIZE, rand);
|
||||
}
|
||||
|
||||
private static String generateRandomStringWithLength(final int length, final Random rand) {
|
||||
final StringBuilder randomStringBuilder = new StringBuilder(length);
|
||||
final int lowercaseACode = 97;
|
||||
final int lowercaseZCode = 122;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
final char randomCharacter = (char) (rand.nextInt(lowercaseZCode - lowercaseACode + 1) + lowercaseACode);
|
||||
randomStringBuilder.append(randomCharacter);
|
||||
}
|
||||
return randomStringBuilder.toString();
|
||||
}
|
||||
|
||||
private TargetTestData() {
|
||||
// nothing to do here
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,7 @@
|
||||
package org.eclipse.hawkbit.repository.test.util;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@@ -29,6 +27,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
public class WithSpringAuthorityRule implements BeforeEachCallback, AfterEachCallback {
|
||||
|
||||
public static final String DEFAULT_TENANT = "default";
|
||||
private SecurityContext oldContext;
|
||||
|
||||
@Override
|
||||
@@ -64,7 +63,7 @@ public class WithSpringAuthorityRule implements BeforeEachCallback, AfterEachCal
|
||||
|
||||
@Override
|
||||
public void setAuthentication(final Authentication authentication) {
|
||||
// nothing todo
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,35 +84,14 @@ public class WithSpringAuthorityRule implements BeforeEachCallback, AfterEachCal
|
||||
}
|
||||
|
||||
private String[] getAllAuthorities(final String[] additionalAuthorities, final String[] notInclude) {
|
||||
final List<String> allPermissions = new ArrayList<>();
|
||||
final Field[] declaredFields = SpPermission.class.getDeclaredFields();
|
||||
for (final Field field : declaredFields) {
|
||||
if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
boolean addPermission = true;
|
||||
final String permissionName = (String) field.get(null);
|
||||
if (notInclude != null) {
|
||||
for (final String notInlcudePerm : notInclude) {
|
||||
if (permissionName.equals(notInlcudePerm)) {
|
||||
addPermission = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (addPermission) {
|
||||
allPermissions.add(permissionName);
|
||||
}
|
||||
// don't want to log this exceptions.
|
||||
} catch (@SuppressWarnings("squid:S1166") IllegalArgumentException | IllegalAccessException e) {
|
||||
// nope
|
||||
}
|
||||
}
|
||||
final List<String> permissions = SpPermission.getAllAuthorities();
|
||||
if (notInclude != null) {
|
||||
permissions.removeAll(Arrays.asList(notInclude));
|
||||
}
|
||||
for (final String authority : additionalAuthorities) {
|
||||
allPermissions.add(authority);
|
||||
if (additionalAuthorities != null) {
|
||||
permissions.addAll(Arrays.asList(additionalAuthorities));
|
||||
}
|
||||
return allPermissions.toArray(new String[allPermissions.size()]);
|
||||
return permissions.toArray(new String[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -146,23 +124,23 @@ public class WithSpringAuthorityRule implements BeforeEachCallback, AfterEachCal
|
||||
}
|
||||
|
||||
public static WithUser withController(final String principal, final String... authorities) {
|
||||
return withUserAndTenant(principal, "default", true, true, true, authorities);
|
||||
return withUserAndTenant(principal, DEFAULT_TENANT, true, true, true, authorities);
|
||||
}
|
||||
|
||||
public static WithUser withUser(final String principal, final String... authorities) {
|
||||
return withUserAndTenant(principal, "default", true, true, false, authorities);
|
||||
return withUserAndTenant(principal, DEFAULT_TENANT, true, true, false, authorities);
|
||||
}
|
||||
|
||||
public static WithUser withUser(final String principal, final boolean allSpPermision, final String... authorities) {
|
||||
return withUserAndTenant(principal, "default", true, allSpPermision, false, authorities);
|
||||
return withUserAndTenant(principal, DEFAULT_TENANT, true, allSpPermision, false, authorities);
|
||||
}
|
||||
|
||||
public static WithUser withUser(final boolean autoCreateTenant) {
|
||||
return withUserAndTenant("bumlux", "default", autoCreateTenant, true, false, new String[] {});
|
||||
return withUserAndTenant("bumlux", DEFAULT_TENANT, autoCreateTenant, true, false);
|
||||
}
|
||||
|
||||
public static WithUser withUserAndTenant(final String principal, final String tenant, final String... authorities) {
|
||||
return withUserAndTenant(principal, tenant, true, true, false, new String[] {});
|
||||
return withUserAndTenant(principal, tenant, true, true, false, authorities);
|
||||
}
|
||||
|
||||
public static WithUser withUserAndTenant(final String principal, final String tenant,
|
||||
@@ -172,8 +150,7 @@ public class WithSpringAuthorityRule implements BeforeEachCallback, AfterEachCal
|
||||
}
|
||||
|
||||
private static WithUser privilegedUser() {
|
||||
return createWithUser("bumlux", "default", true, true, false,
|
||||
new String[] { "ROLE_CONTROLLER", "ROLE_SYSTEM_CODE" });
|
||||
return createWithUser("bumlux", DEFAULT_TENANT, true, true, false, "ROLE_CONTROLLER", "ROLE_SYSTEM_CODE");
|
||||
}
|
||||
|
||||
private static WithUser createWithUser(final String principal, final String tenant, final boolean autoCreateTenant,
|
||||
|
||||
Reference in New Issue
Block a user