Fix SonarQube issues (2) (#2205)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-01-20 16:29:45 +02:00
committed by GitHub
parent 385023d8b6
commit 5dabe9117a
33 changed files with 162 additions and 185 deletions

View File

@@ -20,10 +20,10 @@ import org.junit.jupiter.api.Test;
@Feature("Unit Tests - Management API")
@Story("Serialization")
public class AuditFieldSerializationTest {
class AuditFieldSerializationTest {
@Test
public void assertAuditingFields() throws JsonProcessingException {
void assertAuditingFields() throws JsonProcessingException {
final MgmtTarget mgmtTarget = new MgmtTarget();
mgmtTarget.setCreatedBy("user");
mgmtTarget.setCreatedAt(System.currentTimeMillis() - 1_000_000);

View File

@@ -43,7 +43,6 @@ import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.awaitility.Awaitility;
import org.eclipse.hawkbit.exception.SpServerError;
import org.eclipse.hawkbit.mgmt.json.model.artifact.MgmtArtifact;
@@ -142,14 +141,13 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
public void getArtifactsWithParameters() throws Exception {
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
final byte[] random = RandomStringUtils.random(5).getBytes();
final byte[] random = randomBytes(5);
artifactManagement.create(new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, 0));
mvc.perform(
get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts", sm.getId())
.param("representation", MgmtRepresentationMode.FULL.toString())
.param("useartifacturlhandler", Boolean.TRUE.toString()))
mvc.perform(get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts", sm.getId())
.param("representation", MgmtRepresentationMode.FULL.toString())
.param("useartifacturlhandler", Boolean.TRUE.toString()))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaTypes.HAL_JSON));
@@ -933,7 +931,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
final int artifactSize = 5 * 1024;
final byte[] random = RandomStringUtils.random(artifactSize).getBytes();
final byte[] random = randomBytes(artifactSize);
// Create 2 artifacts
final Artifact artifact = artifactManagement.create(
@@ -994,7 +992,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(status().isBadRequest());
final SoftwareModule toLongName = entityFactory.softwareModule().create().type(osType)
.name(RandomStringUtils.randomAlphanumeric(80)).build();
.name(randomString(80)).build();
mvc.perform(post("/rest/v1/softwaremodules").content(JsonBuilder.softwareModules(Collections.singletonList(toLongName)))
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print())
@@ -1298,7 +1296,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
final int artifactSize = 5 * 1024;
final byte[] random = RandomStringUtils.random(artifactSize).getBytes();
final byte[] random = randomBytes(artifactSize);
artifactManagement.create(
new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, artifactSize));
@@ -1321,7 +1319,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final DistributionSet ds1 = testdataFactory.createDistributionSet("a");
final int artifactSize = 5 * 1024;
final byte[] random = RandomStringUtils.random(artifactSize).getBytes();
final byte[] random = randomBytes(artifactSize);
final Long appTypeSmId = ds1.findFirstModuleByType(appType).get().getId();
@@ -1506,10 +1504,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(jsonPath("content[0].value", equalTo("knownValue1")));
}
private static byte[] randomBytes(final int len) {
return RandomStringUtils.randomAlphanumeric(len).getBytes();
}
private void assertArtifact(final SoftwareModule sm, final byte[] random) throws IOException {
// check result in db...
// repo

View File

@@ -19,27 +19,28 @@ import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.TestPropertySource;
@TestPropertySource(properties = { "hawkbit.server.security.allowedHostNames=localhost",
@TestPropertySource(properties = {
"hawkbit.server.security.allowedHostNames=localhost",
"hawkbit.server.security.httpFirewallIgnoredPaths=/index.html" })
@Feature("Integration Test - Security")
@Story("Allowed Host Names")
public class AllowedHostNamesTest extends AbstractSecurityTest {
class AllowedHostNamesTest extends AbstractSecurityTest {
@Test
@Description("Tests whether a RequestRejectedException is thrown when not allowed host is used")
public void allowedHostNameWithNotAllowedHost() throws Exception {
void allowedHostNameWithNotAllowedHost() throws Exception {
mvc.perform(get("/").header(HttpHeaders.HOST, "www.google.com")).andExpect(status().isBadRequest());
}
@Test
@Description("Tests whether request is redirected when allowed host is used")
public void allowedHostNameWithAllowedHost() throws Exception {
void allowedHostNameWithAllowedHost() throws Exception {
mvc.perform(get("/").header(HttpHeaders.HOST, "localhost")).andExpect(status().is3xxRedirection());
}
@Test
@Description("Tests whether request without allowed host name and with ignored path end up with a client error")
public void notAllowedHostnameWithIgnoredPath() throws Exception {
void notAllowedHostnameWithIgnoredPath() throws Exception {
mvc.perform(get("/index.html").header(HttpHeaders.HOST, "www.google.com"))
.andExpect(status().is4xxClientError());
}