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:
@@ -8,6 +8,8 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.api;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Container for variables available to the {@link ArtifactUrlHandler}.
|
||||
*
|
||||
@@ -107,59 +109,22 @@ public class URLPlaceholder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((artifactId == null) ? 0 : artifactId.hashCode());
|
||||
result = prime * result + ((filename == null) ? 0 : filename.hashCode());
|
||||
result = prime * result + ((sha1Hash == null) ? 0 : sha1Hash.hashCode());
|
||||
result = prime * result + ((softwareModuleId == null) ? 0 : softwareModuleId.hashCode());
|
||||
return result;
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
final SoftwareData that = (SoftwareData) o;
|
||||
return Objects.equals(softwareModuleId, that.softwareModuleId)
|
||||
&& Objects.equals(filename, that.filename)
|
||||
&& Objects.equals(artifactId, that.artifactId)
|
||||
&& Objects.equals(sha1Hash, that.sha1Hash);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final SoftwareData other = (SoftwareData) obj;
|
||||
if (artifactId == null) {
|
||||
if (other.artifactId != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!artifactId.equals(other.artifactId)) {
|
||||
return false;
|
||||
}
|
||||
if (filename == null) {
|
||||
if (other.filename != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!filename.equals(other.filename)) {
|
||||
return false;
|
||||
}
|
||||
if (sha1Hash == null) {
|
||||
if (other.sha1Hash != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!sha1Hash.equals(other.sha1Hash)) {
|
||||
return false;
|
||||
}
|
||||
if (softwareModuleId == null) {
|
||||
if (other.softwareModuleId != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!softwareModuleId.equals(other.softwareModuleId)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
public int hashCode() {
|
||||
return Objects.hash(softwareModuleId, filename, artifactId, sha1Hash);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getTenant() {
|
||||
@@ -183,65 +148,18 @@ public class URLPlaceholder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((controllerId == null) ? 0 : controllerId.hashCode());
|
||||
result = prime * result + ((softwareData == null) ? 0 : softwareData.hashCode());
|
||||
result = prime * result + ((targetId == null) ? 0 : targetId.hashCode());
|
||||
result = prime * result + ((tenant == null) ? 0 : tenant.hashCode());
|
||||
result = prime * result + ((tenantId == null) ? 0 : tenantId.hashCode());
|
||||
return result;
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
final URLPlaceholder that = (URLPlaceholder) o;
|
||||
return tenantId.equals(that.tenantId) && Objects.equals(controllerId, that.controllerId) && Objects.equals(
|
||||
targetId, that.targetId) && Objects.equals(softwareData, that.softwareData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final URLPlaceholder other = (URLPlaceholder) obj;
|
||||
if (controllerId == null) {
|
||||
if (other.controllerId != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!controllerId.equals(other.controllerId)) {
|
||||
return false;
|
||||
}
|
||||
if (softwareData == null) {
|
||||
if (other.softwareData != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!softwareData.equals(other.softwareData)) {
|
||||
return false;
|
||||
}
|
||||
if (targetId == null) {
|
||||
if (other.targetId != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!targetId.equals(other.targetId)) {
|
||||
return false;
|
||||
}
|
||||
if (tenant == null) {
|
||||
if (other.tenant != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!tenant.equals(other.tenant)) {
|
||||
return false;
|
||||
}
|
||||
if (tenantId == null) {
|
||||
if (other.tenantId != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!tenantId.equals(other.tenantId)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
public int hashCode() {
|
||||
return Objects.hash(tenantId, controllerId, targetId, softwareData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@@ -98,25 +99,23 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository {
|
||||
|
||||
protected void deleteTempFile(final String tempFile) {
|
||||
final File file = new File(tempFile);
|
||||
|
||||
if (file.exists() && !file.delete()) {
|
||||
LOG.error("Could not delete temp file {}", file);
|
||||
try {
|
||||
Files.deleteIfExists(file.toPath());
|
||||
} catch (IOException e) {
|
||||
LOG.error("Could not delete temp file {} ({})", file, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected String storeTempFile(final InputStream content) throws IOException {
|
||||
final File file = createTempFile();
|
||||
|
||||
try (final OutputStream outputstream = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||
ByteStreams.copy(content, outputstream);
|
||||
outputstream.flush();
|
||||
}
|
||||
|
||||
return file.getPath();
|
||||
}
|
||||
|
||||
private static File createTempFile() {
|
||||
|
||||
try {
|
||||
return File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);
|
||||
} catch (final IOException e) {
|
||||
@@ -160,5 +159,4 @@ public abstract class AbstractArtifactRepository implements ArtifactRepository {
|
||||
protected static String sanitizeTenant(final String tenant) {
|
||||
return tenant.trim().toUpperCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.cache;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.Cache.ValueWrapper;
|
||||
import org.springframework.cache.CacheManager;
|
||||
@@ -27,7 +28,6 @@ public class DefaultDownloadIdCache implements DownloadIdCache {
|
||||
* @param cacheManager
|
||||
* the underlying cache-manager to store the download-ids
|
||||
*/
|
||||
@Autowired
|
||||
public DefaultDownloadIdCache(final CacheManager cacheManager) {
|
||||
this.cacheManager = cacheManager;
|
||||
}
|
||||
@@ -49,9 +49,9 @@ public class DefaultDownloadIdCache implements DownloadIdCache {
|
||||
}
|
||||
|
||||
private Cache getCache() {
|
||||
if (cacheManager instanceof TenancyCacheManager) {
|
||||
return ((TenancyCacheManager) cacheManager).getDirectCache(DOWNLOAD_ID_CACHE);
|
||||
}
|
||||
return cacheManager.getCache(DOWNLOAD_ID_CACHE);
|
||||
final Cache cache = (cacheManager instanceof TenancyCacheManager)
|
||||
? ((TenancyCacheManager) cacheManager).getDirectCache(DOWNLOAD_ID_CACHE)
|
||||
: cacheManager.getCache(DOWNLOAD_ID_CACHE);
|
||||
return Objects.requireNonNull(cache, "Cache(s) returned by cache-manager must not be null!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public abstract class AbstractServerRtException extends RuntimeException {
|
||||
* @param error
|
||||
* detail
|
||||
*/
|
||||
public AbstractServerRtException(final SpServerError error) {
|
||||
protected AbstractServerRtException(final SpServerError error) {
|
||||
super(error.getMessage());
|
||||
this.error = error;
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public abstract class AbstractServerRtException extends RuntimeException {
|
||||
* @param error
|
||||
* detail
|
||||
*/
|
||||
public AbstractServerRtException(final String message, final SpServerError error) {
|
||||
protected AbstractServerRtException(final String message, final SpServerError error) {
|
||||
super(message);
|
||||
this.error = error;
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public abstract class AbstractServerRtException extends RuntimeException {
|
||||
* @param cause
|
||||
* of the exception
|
||||
*/
|
||||
public AbstractServerRtException(final String message, final SpServerError error, final Throwable cause) {
|
||||
protected AbstractServerRtException(final String message, final SpServerError error, final Throwable cause) {
|
||||
super(message, cause);
|
||||
this.error = error;
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public abstract class AbstractServerRtException extends RuntimeException {
|
||||
* @param cause
|
||||
* of the exception
|
||||
*/
|
||||
public AbstractServerRtException(final SpServerError error, final Throwable cause) {
|
||||
protected AbstractServerRtException(final SpServerError error, final Throwable cause) {
|
||||
super(error.getMessage(), cause);
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@ import io.qameta.allure.Story;
|
||||
|
||||
@Feature("Unit Tests - Artifact URL Handler")
|
||||
@Story("Base62 Utility tests")
|
||||
public class Base62UtilTest {
|
||||
class Base62UtilTest {
|
||||
|
||||
@Test
|
||||
@Description("Convert Base10 numbres to Base62 ASCII strings.")
|
||||
public void fromBase10() {
|
||||
void fromBase10() {
|
||||
assertThat(Base62Util.fromBase10(0L)).isEqualTo("0");
|
||||
assertThat(Base62Util.fromBase10(11L)).isEqualTo("B");
|
||||
assertThat(Base62Util.fromBase10(36L)).isEqualTo("a");
|
||||
@@ -31,8 +31,8 @@ public class Base62UtilTest {
|
||||
|
||||
@Test
|
||||
@Description("Convert Base62 ASCII strings to Base10 numbers.")
|
||||
public void toBase10() {
|
||||
assertThat(Base62Util.toBase10("0")).isEqualTo(0L);
|
||||
void toBase10() {
|
||||
assertThat(Base62Util.toBase10("0")).isZero();
|
||||
assertThat(Base62Util.toBase10("B")).isEqualTo(11);
|
||||
assertThat(Base62Util.toBase10("a")).isEqualTo(36L);
|
||||
assertThat(Base62Util.toBase10("G7")).isEqualTo(999L);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* 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.api;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.qameta.allure.Description;
|
||||
import io.qameta.allure.Feature;
|
||||
import io.qameta.allure.Story;
|
||||
|
||||
@Feature("Unit Tests - Artifact URL Handler")
|
||||
@Story("URL placeholder tests")
|
||||
class URLPlaceholderTest {
|
||||
|
||||
private final URLPlaceholder.SoftwareData softwareData;
|
||||
private final URLPlaceholder placeholder;
|
||||
|
||||
public URLPlaceholderTest() {
|
||||
this.softwareData = new URLPlaceholder.SoftwareData(1L, "file.txt", 123L, "someHash123");
|
||||
this.placeholder = new URLPlaceholder("SuperCorp", 123L, "Super-1", 1L, softwareData);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Same object should be equal")
|
||||
// Exception squid:S5785 - JUnit assertTrue/assertFalse should be simplified to
|
||||
// the corresponding dedicated assertion
|
||||
// Need to test the equals method and need to bypass magic logic in utility
|
||||
// classes
|
||||
@SuppressWarnings({ "squid:S5838" })
|
||||
void sameObjectShouldBeEqual() {
|
||||
assertThat(softwareData.equals(softwareData)).isTrue();
|
||||
assertThat(placeholder.equals(placeholder)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Different object should not be equal")
|
||||
@SuppressWarnings({ "squid:S5838" })
|
||||
void differentObjectShouldNotBeEqual() {
|
||||
final URLPlaceholder.SoftwareData softwareData2 = new URLPlaceholder.SoftwareData(2L, "file.txt", 123L, "someHash123");
|
||||
final URLPlaceholder placeholder2 = new URLPlaceholder("SuperCorp", 123L, "Super-2", 2L, softwareData2);
|
||||
final URLPlaceholder placeholderWithOtherSoftwareData = new URLPlaceholder(placeholder.getTenant(),
|
||||
placeholder.getTenantId(), placeholder.getControllerId(), placeholder.getTargetId(), softwareData2);
|
||||
assertThat(placeholder.equals(placeholder2)).isFalse();
|
||||
assertThat(placeholder2.equals(placeholder)).isFalse();
|
||||
assertThat(softwareData.equals(softwareData2)).isFalse();
|
||||
assertThat(softwareData2.equals(softwareData)).isFalse();
|
||||
assertThat(placeholder.equals(placeholderWithOtherSoftwareData)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Different objects with same properties should be equal")
|
||||
void differentObjectsWithSamePropertiesShouldBeEqual() {
|
||||
final URLPlaceholder placeholderWithSameProperties = new URLPlaceholder(placeholder.getTenant(), placeholder.getTenantId(),
|
||||
placeholder.getControllerId(), placeholder.getTargetId(), softwareData);
|
||||
assertThat(placeholder).isEqualTo(placeholderWithSameProperties);
|
||||
assertThat(placeholderWithSameProperties).isEqualTo(placeholder);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Should not equal null")
|
||||
// Exception squid:S5785 - JUnit assertTrue/assertFalse should be simplified to
|
||||
// the corresponding dedicated assertion
|
||||
// Need to test the equals method and need to bypass magic logic in utility
|
||||
// classes
|
||||
@SuppressWarnings({ "squid:S5838" })
|
||||
void shouldNotEqualNull() {
|
||||
assertThat(placeholder.equals(null)).isFalse();
|
||||
assertThat(softwareData.equals(null)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("HashCode should not change")
|
||||
void hashCodeShouldNotChange() {
|
||||
final URLPlaceholder placeholderWithSameProperties = new URLPlaceholder(placeholder.getTenant(), placeholder.getTenantId(),
|
||||
placeholder.getControllerId(), placeholder.getTargetId(), softwareData);
|
||||
assertThat(placeholder).hasSameHashCodeAs(placeholder).hasSameHashCodeAs(placeholderWithSameProperties);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
@@ -32,7 +31,7 @@ import io.qameta.allure.Story;
|
||||
@Feature("Unit Tests - Cache")
|
||||
@Story("Download ID Cache")
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class DefaultDownloadIdCacheTest {
|
||||
class DefaultDownloadIdCacheTest {
|
||||
|
||||
@Mock
|
||||
private CacheManager cacheManagerMock;
|
||||
@@ -61,7 +60,7 @@ public class DefaultDownloadIdCacheTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that putting key and value is delegated to the CacheManager implementation")
|
||||
public void putKeyAndValueIsDelegatedToCacheManager() {
|
||||
void putKeyAndValueIsDelegatedToCacheManager() {
|
||||
final DownloadArtifactCache value = new DownloadArtifactCache(DownloadType.BY_SHA1, knownKey);
|
||||
|
||||
underTest.put(knownKey, value);
|
||||
@@ -74,7 +73,7 @@ public class DefaultDownloadIdCacheTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that evicting a key is delegated to the CacheManager implementation")
|
||||
public void evictKeyIsDelegatedToCacheManager() {
|
||||
void evictKeyIsDelegatedToCacheManager() {
|
||||
|
||||
underTest.evict(knownKey);
|
||||
|
||||
@@ -85,7 +84,7 @@ public class DefaultDownloadIdCacheTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that retrieving a value for a specific key is delegated to the CacheManager implementation")
|
||||
public void getValueReturnsTheAssociatedValueForKey() {
|
||||
void getValueReturnsTheAssociatedValueForKey() {
|
||||
final String knownKey = "12345";
|
||||
final DownloadArtifactCache knownValue = new DownloadArtifactCache(DownloadType.BY_SHA1, knownKey);
|
||||
|
||||
@@ -98,7 +97,7 @@ public class DefaultDownloadIdCacheTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that retrieving a null value for a specific key is delegated to the CacheManager implementation")
|
||||
public void getValueReturnsNullIfNoKeyIsAssociated() {
|
||||
void getValueReturnsNullIfNoKeyIsAssociated() {
|
||||
|
||||
when(cacheMock.get(knownKey)).thenReturn(new SimpleValueWrapper(null));
|
||||
|
||||
@@ -109,7 +108,7 @@ public class DefaultDownloadIdCacheTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that TenancyCacheManager is using direct cache because download-ids are global unique and don't need to run as tenant aware")
|
||||
public void tenancyCacheManagerIsUsingDirectCache() {
|
||||
void tenancyCacheManagerIsUsingDirectCache() {
|
||||
|
||||
when(tenancyCacheManagerMock.getDirectCache(DefaultDownloadIdCache.DOWNLOAD_ID_CACHE)).thenReturn(cacheMock);
|
||||
underTest = new DefaultDownloadIdCache(tenancyCacheManagerMock);
|
||||
|
||||
Reference in New Issue
Block a user