Fix SonarQube issues (2) (#2205)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -18,12 +18,14 @@ import java.util.Objects;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.eclipse.hawkbit.artifact.repository.model.AbstractDbArtifact;
|
||||
import org.eclipse.hawkbit.artifact.repository.model.DbArtifactHash;
|
||||
|
||||
/**
|
||||
* {@link AbstractDbArtifact} implementation which dynamically creates a {@link FileInputStream} on calling {@link #getFileInputStream()}.
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class ArtifactFilesystem extends AbstractDbArtifact {
|
||||
|
||||
private final File file;
|
||||
|
||||
@@ -49,6 +49,6 @@ class ArtifactFilesystemTest {
|
||||
final ArtifactFilesystem underTest = new ArtifactFilesystem(
|
||||
createTempFile, ArtifactFilesystemTest.class.getSimpleName(), new DbArtifactHash("1", "2", "3"), 0L, null);
|
||||
final byte[] buffer = new byte[1024];
|
||||
assertThat(IOUtils.read(underTest.getFileInputStream(), buffer)).isEqualTo(0);
|
||||
assertThat(IOUtils.read(underTest.getFileInputStream(), buffer)).isZero();
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
@@ -33,7 +32,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback;
|
||||
import org.eclipse.hawkbit.ddi.json.model.DdiAssignedVersion;
|
||||
import org.eclipse.hawkbit.ddi.json.model.DdiConfirmationFeedback;
|
||||
@@ -288,7 +286,7 @@ public abstract class AbstractDDiApiIntegrationTest extends AbstractRestIntegrat
|
||||
protected String getJsonActionFeedback(
|
||||
final DdiStatus.ExecutionStatus executionStatus, final DdiResult.FinalResult finalResult) throws JsonProcessingException {
|
||||
return getJsonActionFeedback(
|
||||
executionStatus, finalResult, Collections.singletonList(RandomStringUtils.randomAlphanumeric(1000)));
|
||||
executionStatus, finalResult, Collections.singletonList(randomString(1000)));
|
||||
}
|
||||
|
||||
protected String getJsonActionFeedback(
|
||||
|
||||
@@ -24,25 +24,25 @@ import org.springframework.test.context.TestPropertySource;
|
||||
"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());
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ public final class AmqpErrorMessageComposer {
|
||||
*/
|
||||
public static String constructErrorMessage(final Throwable throwable) {
|
||||
final String mainErrorMsg = throwable.getCause().getMessage();
|
||||
if (throwable instanceof ListenerExecutionFailedException) {
|
||||
Collection<Message> failedMessages = ((ListenerExecutionFailedException) throwable).getFailedMessages();
|
||||
if (throwable instanceof ListenerExecutionFailedException listenerExecutionFailedException) {
|
||||
Collection<Message> failedMessages = listenerExecutionFailedException.getFailedMessages();
|
||||
// since the intended message content is always on top of the collection, we only extract the first one
|
||||
final Message failedMessage = failedMessages.iterator().next();
|
||||
final byte[] amqpFailedMsgBody = failedMessage.getBody();
|
||||
|
||||
@@ -370,7 +370,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
|
||||
// Handling remote request in parallel streams
|
||||
return StreamSupport.stream(partitions.spliterator(), true) //
|
||||
.flatMap(partition -> withSecurityContext(() -> loadingFunction.apply(partition), context).stream())
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
}
|
||||
return loadingFunction.apply(controllerIds);
|
||||
}
|
||||
@@ -436,7 +436,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}).collect(Collectors.toList()));
|
||||
}).toList());
|
||||
}
|
||||
|
||||
private void sendUpdateMessageToTargets(
|
||||
@@ -570,7 +570,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
|
||||
}
|
||||
|
||||
private List<DmfMetadata> convertMetadata(final List<SoftwareModuleMetadata> metadata) {
|
||||
return metadata.stream().map(md -> new DmfMetadata(md.getKey(), md.getValue())).collect(Collectors.toList());
|
||||
return metadata.stream().map(md -> new DmfMetadata(md.getKey(), md.getValue())).toList();
|
||||
}
|
||||
|
||||
private List<DmfArtifact> convertArtifacts(final Target target, final List<Artifact> localArtifacts) {
|
||||
@@ -579,7 +579,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
|
||||
}
|
||||
|
||||
return localArtifacts.stream().map(localArtifact -> convertArtifact(target, localArtifact))
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
}
|
||||
|
||||
private DmfArtifact convertArtifact(final Target target, final Artifact localArtifact) {
|
||||
@@ -619,7 +619,7 @@ public class AmqpMessageDispatcherService extends BaseAmqpService {
|
||||
final List<DmfTarget> dmfTargets = targets.stream()
|
||||
.filter(target -> IpUtil.isAmqpUri(target.getAddress()))
|
||||
.map(t -> convertToDmfTarget(t, actions.get(t.getControllerId()).getId()))
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
final DmfBatchDownloadAndUpdateRequest batchRequest = new DmfBatchDownloadAndUpdateRequest();
|
||||
batchRequest.setTimestamp(System.currentTimeMillis());
|
||||
|
||||
@@ -23,6 +23,8 @@ import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.hawkbit.dmf.amqp.api.EventTopic;
|
||||
import org.eclipse.hawkbit.dmf.amqp.api.MessageHeaderKey;
|
||||
@@ -215,7 +217,7 @@ public class AmqpMessageHandlerService extends BaseAmqpService {
|
||||
|
||||
// Exception squid:MethodCyclomaticComplexity - false positive, is a simple mapping
|
||||
@SuppressWarnings("squid:MethodCyclomaticComplexity")
|
||||
private static Status mapStatus(final Message message, final DmfActionUpdateStatus actionUpdateStatus, final Action action) {
|
||||
private static @NotNull Status mapStatus(final Message message, final DmfActionUpdateStatus actionUpdateStatus, final Action action) {
|
||||
Status status = null;
|
||||
switch (actionUpdateStatus.getActionStatus()) {
|
||||
case DOWNLOAD: {
|
||||
@@ -226,8 +228,7 @@ public class AmqpMessageHandlerService extends BaseAmqpService {
|
||||
status = Status.RETRIEVED;
|
||||
break;
|
||||
}
|
||||
case RUNNING:
|
||||
case CONFIRMED: {
|
||||
case RUNNING, CONFIRMED: {
|
||||
status = Status.RUNNING;
|
||||
break;
|
||||
}
|
||||
@@ -373,7 +374,7 @@ public class AmqpMessageHandlerService extends BaseAmqpService {
|
||||
}
|
||||
|
||||
private Map<SoftwareModule, List<SoftwareModuleMetadata>> getSoftwareModulesWithMetadata(final DistributionSet distributionSet) {
|
||||
final List<Long> smIds = distributionSet.getModules().stream().map(SoftwareModule::getId).collect(Collectors.toList());
|
||||
final List<Long> smIds = distributionSet.getModules().stream().map(SoftwareModule::getId).toList();
|
||||
final Map<Long, List<SoftwareModuleMetadata>> metadata = controllerManagement.findTargetVisibleMetaDataBySoftwareModuleId(smIds);
|
||||
return distributionSet.getModules().stream().collect(Collectors.toMap(
|
||||
Function.identity(), sm -> metadata.getOrDefault(sm.getId(), Collections.emptyList())));
|
||||
|
||||
@@ -83,7 +83,7 @@ class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
|
||||
private Target testTarget;
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEach() throws Exception {
|
||||
public void beforeEach() {
|
||||
testTarget = targetManagement.create(entityFactory.target().create().controllerId(CONTROLLER_ID)
|
||||
.securityToken(TEST_TOKEN).address(AMQP_URI.toString()));
|
||||
|
||||
@@ -135,8 +135,7 @@ class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
|
||||
assertThat(downloadAndUpdateRequest.getTargetSecurityToken()).isEqualTo(TEST_TOKEN);
|
||||
for (final org.eclipse.hawkbit.dmf.json.model.DmfSoftwareModule softwareModule : downloadAndUpdateRequest
|
||||
.getSoftwareModules()) {
|
||||
assertThat(softwareModule.getArtifacts().isEmpty()).as("Artifact list for softwaremodule should be empty")
|
||||
.isTrue();
|
||||
assertThat(softwareModule.getArtifacts()).as("Artifact list for softwaremodule should be empty").isEmpty();
|
||||
|
||||
assertThat(softwareModule.getMetadata()).allSatisfy(metadata -> {
|
||||
assertThat(metadata.getKey()).isEqualTo(TestdataFactory.VISIBLE_SM_MD_KEY);
|
||||
@@ -202,7 +201,7 @@ class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
|
||||
assertThat(found.get().getSize()).isEqualTo(dbArtifact.getSize());
|
||||
assertThat(found.get().getHashes().getMd5()).isEqualTo(dbArtifact.getMd5Hash());
|
||||
assertThat(found.get().getHashes().getSha1()).isEqualTo(dbArtifact.getSha1Hash());
|
||||
assertThat(found.get().getLastModified()).isGreaterThan(0L);
|
||||
assertThat(found.get().getLastModified()).isPositive();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ import org.springframework.amqp.support.converter.MessageConverter;
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@Feature("Component Tests - Device Management Federation API")
|
||||
@Story("AmqpMessage Handler Service Test")
|
||||
public class AmqpMessageHandlerServiceTest {
|
||||
class AmqpMessageHandlerServiceTest {
|
||||
|
||||
private static final String FAIL_MESSAGE_AMQP_REJECT_REASON = AmqpRejectAndDontRequeueException.class
|
||||
.getSimpleName() + " was expected, ";
|
||||
@@ -128,7 +128,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void before() {
|
||||
void before() {
|
||||
messageConverter = new Jackson2JsonMessageConverter();
|
||||
lenient().when(rabbitTemplate.getMessageConverter()).thenReturn(messageConverter);
|
||||
final TenantConfigurationValue multiAssignmentConfig = TenantConfigurationValue.builder().value(Boolean.FALSE)
|
||||
@@ -146,19 +146,20 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests not allowed content-type in message")
|
||||
public void wrongContentType() {
|
||||
void wrongContentType() {
|
||||
final MessageProperties messageProperties = new MessageProperties();
|
||||
messageProperties.setContentType("xml");
|
||||
final Message message = new Message(new byte[0], messageProperties);
|
||||
final String type = MessageType.THING_CREATED.name();
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to wrong content type")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT,
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT,
|
||||
VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a target/thing by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void createThing() {
|
||||
void createThing() {
|
||||
final String knownThingId = "1";
|
||||
|
||||
processThingCreatedMessage(knownThingId, null);
|
||||
@@ -169,7 +170,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a target/thing with specified name by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void createThingWithName() {
|
||||
void createThingWithName() {
|
||||
final String knownThingId = "2";
|
||||
final String knownThingName = "NonDefaultTargetName";
|
||||
|
||||
@@ -185,7 +186,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a target/thing with specified type name by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void createThingWithType() {
|
||||
void createThingWithType() {
|
||||
final String knownThingId = "2";
|
||||
final String knownThingTypeName = "TargetTypeName";
|
||||
|
||||
@@ -201,19 +202,17 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests not allowed body in message")
|
||||
public void createThingWithWrongBody() {
|
||||
final String knownThingId = "3";
|
||||
|
||||
void createThingWithWrongBody() {
|
||||
final Message message = createMessage("Not allowed Body".getBytes(), getThingCreatedMessageProperties("3"));
|
||||
final String type = MessageType.THING_CREATED.name();
|
||||
assertThatExceptionOfType(MessageConversionException.class)
|
||||
.as("MessageConversionException was excepeted due to wrong body")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(
|
||||
createMessage("Not allowed Body".getBytes(), getThingCreatedMessageProperties(knownThingId)),
|
||||
MessageType.THING_CREATED.name(), TENANT, VIRTUAL_HOST));
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a target/thing with specified attributes by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void createThingWithAttributes() {
|
||||
void createThingWithAttributes() {
|
||||
final String knownThingId = "4";
|
||||
|
||||
final DmfAttributeUpdate attributeUpdate = new DmfAttributeUpdate();
|
||||
@@ -232,7 +231,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a target/thing with specified name and attributes by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void createThingWithNameAndAttributes() {
|
||||
void createThingWithNameAndAttributes() {
|
||||
final String knownThingId = "5";
|
||||
final String knownThingName = "NonDefaultTargetName";
|
||||
|
||||
@@ -255,7 +254,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the target attribute update by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void updateAttributes() {
|
||||
void updateAttributes() {
|
||||
final String knownThingId = "1";
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
|
||||
messageProperties.setHeader(MessageHeaderKey.THING_ID, knownThingId);
|
||||
@@ -277,7 +276,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that the update mode is retrieved from the UPDATE_ATTRIBUTES message and passed to the controller management.")
|
||||
public void attributeUpdateModes() {
|
||||
void attributeUpdateModes() {
|
||||
final String knownThingId = "1";
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
|
||||
messageProperties.setHeader(MessageHeaderKey.THING_ID, knownThingId);
|
||||
@@ -320,32 +319,30 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a thing without a 'reply to' header in message.")
|
||||
public void createThingWithoutReplyTo() {
|
||||
void createThingWithoutReplyTo() {
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.THING_CREATED, null);
|
||||
messageProperties.setHeader(MessageHeaderKey.THING_ID, "1");
|
||||
final Message message = createMessage("", messageProperties);
|
||||
|
||||
final String type = MessageType.THING_CREATED.name();
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no replyTo header was set")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT,
|
||||
VIRTUAL_HOST));
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests the creation of a target/thing without a thingID by calling the same method that incoming RabbitMQ messages would access.")
|
||||
public void createThingWithoutID() {
|
||||
void createThingWithoutID() {
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.THING_CREATED);
|
||||
final Message message = createMessage(new byte[0], messageProperties);
|
||||
|
||||
final String type = MessageType.THING_CREATED.name();
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no thingId was set")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_CREATED.name(), TENANT,
|
||||
VIRTUAL_HOST));
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests the call of the same method that incoming RabbitMQ messages would access with an unknown message type.")
|
||||
public void unknownMessageType() {
|
||||
void unknownMessageType() {
|
||||
final String type = "bumlux";
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.THING_CREATED);
|
||||
messageProperties.setHeader(MessageHeaderKey.THING_ID, "");
|
||||
@@ -358,7 +355,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests a invalid message without event topic")
|
||||
public void invalidEventTopic() {
|
||||
void invalidEventTopic() {
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
|
||||
final Message message = new Message(new byte[0], messageProperties);
|
||||
|
||||
@@ -366,52 +363,51 @@ public class AmqpMessageHandlerServiceTest {
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to unknown message type").isThrownBy(
|
||||
() -> amqpMessageHandlerService.onMessage(message, "unknownMessageType", TENANT, VIRTUAL_HOST));
|
||||
|
||||
final String type = MessageType.EVENT.name();
|
||||
messageProperties.setHeader(MessageHeaderKey.TOPIC, "wrongTopic");
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to unknown topic").isThrownBy(() -> amqpMessageHandlerService
|
||||
.onMessage(message, MessageType.EVENT.name(), TENANT, VIRTUAL_HOST));
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "due to unknown topic")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
|
||||
messageProperties.setHeader(MessageHeaderKey.TOPIC, EventTopic.CANCEL_DOWNLOAD.name());
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "because there was no event topic")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT,
|
||||
VIRTUAL_HOST));
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests the update of an action of a target without a exist action id")
|
||||
public void updateActionStatusWithoutActionId() {
|
||||
void updateActionStatusWithoutActionId() {
|
||||
when(controllerManagementMock.findActionWithDetails(anyLong())).thenReturn(Optional.empty());
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
|
||||
messageProperties.setHeader(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ACTION_STATUS.name());
|
||||
final DmfActionUpdateStatus actionUpdateStatus = new DmfActionUpdateStatus(1L, DmfActionStatus.DOWNLOAD);
|
||||
final Message message = createMessage(actionUpdateStatus, messageProperties);
|
||||
|
||||
final String type = MessageType.EVENT.name();
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no action id was set")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT,
|
||||
VIRTUAL_HOST));
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests the update of an action of a target without a exist action id")
|
||||
public void updateActionStatusWithoutExistActionId() {
|
||||
void updateActionStatusWithoutExistActionId() {
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
|
||||
messageProperties.setHeader(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ACTION_STATUS.name());
|
||||
when(controllerManagementMock.findActionWithDetails(anyLong())).thenReturn(Optional.empty());
|
||||
|
||||
final DmfActionUpdateStatus actionUpdateStatus = createActionUpdateStatus(DmfActionStatus.DOWNLOAD);
|
||||
final Message message = createMessage(actionUpdateStatus, messageProperties);
|
||||
|
||||
final String type = MessageType.EVENT.name();
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no action id was set")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT,
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT,
|
||||
VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Tests that messages which cause quota violations are not re-added to message queue so they would block other communication.")
|
||||
public void quotaExceeded() {
|
||||
void quotaExceeded() {
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.EVENT);
|
||||
messageProperties.setHeader(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ACTION_STATUS.name());
|
||||
|
||||
@@ -429,14 +425,14 @@ public class AmqpMessageHandlerServiceTest {
|
||||
when(controllerManagementMock.findActionWithDetails(anyLong())).thenReturn(Optional.of(action));
|
||||
when(controllerManagementMock.addUpdateActionStatus(any())).thenThrow(new AssignmentQuotaExceededException());
|
||||
|
||||
final String type = MessageType.EVENT.name();
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.EVENT.name(), TENANT,
|
||||
VIRTUAL_HOST));
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Test next update is provided on finished action")
|
||||
public void lookupNextUpdateActionAfterFinished() throws IllegalAccessException {
|
||||
void lookupNextUpdateActionAfterFinished() throws IllegalAccessException {
|
||||
|
||||
// Mock
|
||||
final Action action = createActionWithTarget(22L);
|
||||
@@ -473,7 +469,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Test feedback code is persisted in messages when provided with DmfActionUpdateStatus")
|
||||
public void feedBackCodeIsPersistedInMessages() throws IllegalAccessException {
|
||||
void feedBackCodeIsPersistedInMessages() throws IllegalAccessException {
|
||||
|
||||
// Mock
|
||||
final Action action = createActionWithTarget(22L);
|
||||
@@ -509,7 +505,7 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the deletion of a target/thing, requested by the target itself.")
|
||||
public void deleteThing() {
|
||||
void deleteThing() {
|
||||
// prepare valid message
|
||||
final String knownThingId = "1";
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.THING_REMOVED);
|
||||
@@ -525,15 +521,15 @@ public class AmqpMessageHandlerServiceTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the deletion of a target/thing with missing thingId")
|
||||
public void deleteThingWithoutThingId() {
|
||||
void deleteThingWithoutThingId() {
|
||||
// prepare invalid message
|
||||
final MessageProperties messageProperties = createMessageProperties(MessageType.THING_REMOVED);
|
||||
final Message message = createMessage(new byte[0], messageProperties);
|
||||
|
||||
final String type = MessageType.THING_REMOVED.name();
|
||||
assertThatExceptionOfType(AmqpRejectAndDontRequeueException.class)
|
||||
.as(FAIL_MESSAGE_AMQP_REJECT_REASON + "since no thingId was set")
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, MessageType.THING_REMOVED.name(), TENANT,
|
||||
VIRTUAL_HOST));
|
||||
.isThrownBy(() -> amqpMessageHandlerService.onMessage(message, type, TENANT, VIRTUAL_HOST));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -382,9 +382,9 @@ abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpIntegratio
|
||||
|
||||
createConditionFactory().untilAsserted(() -> {
|
||||
try {
|
||||
final Map<String, String> controllerAttributes = SecurityContextSwitch
|
||||
.runAsPrivileged(() -> targetManagement.getControllerAttributes(controllerId));
|
||||
assertThat(controllerAttributes.size()).isEqualTo(attributes.size());
|
||||
final Map<String, String> controllerAttributes = SecurityContextSwitch.runAsPrivileged(
|
||||
() -> targetManagement.getControllerAttributes(controllerId));
|
||||
assertThat(controllerAttributes).hasSameSizeAs(attributes);
|
||||
assertThat(controllerAttributes).containsAllEntriesOf(attributes);
|
||||
} catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -227,12 +227,12 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
registerAndAssertTargetWithExistingTenant(controllerId);
|
||||
|
||||
final Long actionId1 = assignNewDsToTarget(controllerId, 450);
|
||||
final Entry<Long, EventTopic> action1Install = new SimpleEntry<>(actionId1, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
final SimpleEntry<Long, EventTopic> action1Install = new SimpleEntry<>(actionId1, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
waitUntilEventMessagesAreDispatchedToTarget(EventTopic.MULTI_ACTION);
|
||||
assertLatestMultiActionMessage(controllerId, Collections.singletonList(action1Install));
|
||||
|
||||
final Long actionId2 = assignNewDsToTarget(controllerId, 111);
|
||||
final Entry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
final SimpleEntry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
waitUntilEventMessagesAreDispatchedToTarget(EventTopic.MULTI_ACTION);
|
||||
assertLatestMultiActionMessage(controllerId, Arrays.asList(action1Install, action2Install));
|
||||
}
|
||||
@@ -315,8 +315,8 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
deploymentManagement.cancelAction(actionId1);
|
||||
waitUntilEventMessagesAreDispatchedToTarget(EventTopic.MULTI_ACTION);
|
||||
|
||||
final Entry<Long, EventTopic> action1Cancel = new SimpleEntry<>(actionId1, EventTopic.CANCEL_DOWNLOAD);
|
||||
final Entry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
final SimpleEntry<Long, EventTopic> action1Cancel = new SimpleEntry<>(actionId1, EventTopic.CANCEL_DOWNLOAD);
|
||||
final SimpleEntry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
|
||||
assertLatestMultiActionMessage(controllerId, Arrays.asList(action1Cancel, action2Install));
|
||||
updateActionViaDmfClient(controllerId, actionId1, DmfActionStatus.CANCELED);
|
||||
@@ -349,7 +349,7 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
|
||||
final long actionId1 = assignNewDsToTarget(controllerId, 66);
|
||||
final long actionId2 = assignNewDsToTarget(controllerId, 767);
|
||||
final Entry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
final SimpleEntry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
waitUntilEventMessagesAreDispatchedToTarget(EventTopic.MULTI_ACTION, EventTopic.MULTI_ACTION);
|
||||
|
||||
updateActionViaDmfClient(controllerId, actionId1, DmfActionStatus.FINISHED);
|
||||
@@ -385,8 +385,8 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
final Long actionId2 = getFirstAssignedAction(assignDistributionSet(dsId, controllerId, 775)).getId();
|
||||
waitUntilEventMessagesAreDispatchedToTarget(EventTopic.MULTI_ACTION);
|
||||
|
||||
final Entry<Long, EventTopic> action1Install = new SimpleEntry<>(actionId1, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
final Entry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
final SimpleEntry<Long, EventTopic> action1Install = new SimpleEntry<>(actionId1, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
final SimpleEntry<Long, EventTopic> action2Install = new SimpleEntry<>(actionId2, EventTopic.DOWNLOAD_AND_INSTALL);
|
||||
assertLatestMultiActionMessage(controllerId, Arrays.asList(action1Install, action2Install));
|
||||
}
|
||||
|
||||
@@ -567,9 +567,10 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
|
||||
assertThat(message).isNotNull();
|
||||
final Map<String, Object> headers = message.getMessageProperties().getHeaders();
|
||||
assertThat(headers).containsEntry("thingId", controllerId);
|
||||
assertThat(headers).containsEntry("type", EVENT.toString());
|
||||
assertThat(headers).containsEntry("topic", DOWNLOAD.toString());
|
||||
assertThat(headers)
|
||||
.containsEntry("thingId", controllerId)
|
||||
.containsEntry("type", EVENT.toString())
|
||||
.containsEntry("topic", DOWNLOAD.toString());
|
||||
|
||||
final Optional<Target> target = controllerManagement.getByControllerId(controllerId);
|
||||
assertThat(target).isPresent();
|
||||
@@ -596,11 +597,11 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
final List<String> controllerIds) {
|
||||
assertSoftwareModules(softwareModules, request.getSoftwareModules());
|
||||
|
||||
final List<Object> tokens = controllerIds.stream().map(controllerId -> {
|
||||
final List<String> tokens = controllerIds.stream().map(controllerId -> {
|
||||
final Optional<Target> target = controllerManagement.getByControllerId(controllerId);
|
||||
assertThat(target).isPresent();
|
||||
return target.get().getSecurityToken();
|
||||
}).collect(Collectors.toList());
|
||||
}).toList();
|
||||
|
||||
final List<DmfTarget> requestTargets = request.getTargets();
|
||||
|
||||
@@ -700,7 +701,7 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
private static List<DmfDownloadAndUpdateRequest> getDownloadAndUpdateRequests(final DmfMultiActionRequest request) {
|
||||
return request.getElements().stream()
|
||||
.filter(AmqpMessageDispatcherServiceIntegrationTest::isDownloadAndUpdateRequest)
|
||||
.map(multiAction -> (DmfDownloadAndUpdateRequest) multiAction.getAction()).collect(Collectors.toList());
|
||||
.map(multiAction -> (DmfDownloadAndUpdateRequest) multiAction.getAction()).toList();
|
||||
}
|
||||
|
||||
private static boolean isDownloadAndUpdateRequest(final DmfMultiActionElement multiActionElement) {
|
||||
@@ -767,20 +768,20 @@ class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpServiceInt
|
||||
.fromMessage(multiactionMessage);
|
||||
|
||||
final List<Set<Long>> smIdsOfActionsFound = getDownloadAndUpdateRequests(multiActionRequest).stream()
|
||||
.map(AmqpMessageDispatcherServiceIntegrationTest::getSmIds).collect(Collectors.toList());
|
||||
.map(AmqpMessageDispatcherServiceIntegrationTest::getSmIds).toList();
|
||||
assertThat(smIdsOfActionsFound).containsExactlyInAnyOrderElementsOf(smIdsOfActionsExpected);
|
||||
}
|
||||
|
||||
private void assertLatestMultiActionMessage(final String controllerId,
|
||||
final List<Entry<Long, EventTopic>> actionsExpected) {
|
||||
final List<Entry<Long, EventTopic>> actionsFromMessage = getLatestMultiActionMessageActions(controllerId);
|
||||
final List<SimpleEntry<Long, EventTopic>> actionsExpected) {
|
||||
final List<SimpleEntry<Long, EventTopic>> actionsFromMessage = getLatestMultiActionMessageActions(controllerId);
|
||||
assertThat(actionsFromMessage).containsExactlyInAnyOrderElementsOf(actionsExpected);
|
||||
}
|
||||
|
||||
private List<Entry<Long, EventTopic>> getLatestMultiActionMessageActions(final String expectedControllerId) {
|
||||
private List<SimpleEntry<Long, EventTopic>> getLatestMultiActionMessageActions(final String expectedControllerId) {
|
||||
final List<DmfMultiActionElement> multiActionRequest = getLatestMultiActionMessages(expectedControllerId);
|
||||
return multiActionRequest.stream()
|
||||
.map(request -> new SimpleEntry<>(request.getAction().getActionId(), request.getTopic()))
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
@@ -32,7 +31,6 @@ import io.qameta.allure.Description;
|
||||
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.amqp.AmqpMessageHandlerService;
|
||||
import org.eclipse.hawkbit.amqp.AmqpProperties;
|
||||
import org.eclipse.hawkbit.dmf.amqp.api.EventTopic;
|
||||
@@ -199,7 +197,7 @@ class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServiceIntegr
|
||||
@Description("Tests register invalid target with too long controller id")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
|
||||
void registerInvalidTargetWithTooLongControllerId() {
|
||||
createAndSendThingCreated(RandomStringUtils.randomAlphabetic(Target.CONTROLLER_ID_MAX_SIZE + 1));
|
||||
createAndSendThingCreated(randomString(Target.CONTROLLER_ID_MAX_SIZE + 1));
|
||||
assertAllTargetsCount(0);
|
||||
verifyOneDeadLetterMessage();
|
||||
}
|
||||
@@ -1171,13 +1169,13 @@ class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServiceIntegr
|
||||
.filter(Objects::nonNull)
|
||||
.filter(message -> message
|
||||
.startsWith(RepositoryConstants.SERVER_MESSAGE_PREFIX + "DMF message"))
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
assertThat(messagesFromServer).hasSize(messages)
|
||||
.allMatch(message -> message.endsWith(CORRELATION_ID));
|
||||
|
||||
final List<Status> status = actionStatusList.stream().map(ActionStatus::getStatus)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
assertThat(status).containsOnly(expectedActionStates);
|
||||
|
||||
return null;
|
||||
@@ -1204,7 +1202,7 @@ class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServiceIntegr
|
||||
assertThat(actionStatusList).hasSize(statusListCount);
|
||||
|
||||
final List<Status> status = actionStatusList.stream().map(ActionStatus::getStatus)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
assertThat(status).containsOnly(expectedActionStates);
|
||||
|
||||
return null;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -11,18 +11,21 @@ package org.eclipse.hawkbit.repository.exception;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import org.eclipse.hawkbit.exception.AbstractServerRtException;
|
||||
import org.eclipse.hawkbit.exception.SpServerError;
|
||||
|
||||
/**
|
||||
* Exception being thrown in case of error while generating encryption secrets,
|
||||
* encrypting or decrypting artifacts.
|
||||
* Exception being thrown in case of error while generating encryption secrets, encrypting or decrypting artifacts.
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public final class ArtifactEncryptionFailedException extends AbstractServerRtException {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Getter
|
||||
private final EncryptionOperation encryptionOperation;
|
||||
|
||||
public ArtifactEncryptionFailedException(final EncryptionOperation encryptionOperation) {
|
||||
@@ -33,21 +36,15 @@ public final class ArtifactEncryptionFailedException extends AbstractServerRtExc
|
||||
this(encryptionOperation, message, null);
|
||||
}
|
||||
|
||||
public ArtifactEncryptionFailedException(final EncryptionOperation encryptionOperation, final String message,
|
||||
final Throwable cause) {
|
||||
public ArtifactEncryptionFailedException(final EncryptionOperation encryptionOperation, final String message, final Throwable cause) {
|
||||
super(message, SpServerError.SP_ARTIFACT_ENCRYPTION_FAILED, cause);
|
||||
this.encryptionOperation = encryptionOperation;
|
||||
}
|
||||
|
||||
public EncryptionOperation getEncryptionOperation() {
|
||||
return encryptionOperation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encryption operation that caused the exception.
|
||||
*/
|
||||
public enum EncryptionOperation {
|
||||
GENERATE_SECRETS, ENCRYPT, DECRYPT;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,4 +19,8 @@ public abstract class AbstractBaseEntityBuilder implements Identifiable<Long> {
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
static String strip(final String value) {
|
||||
return value == null ? null : value.strip();
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,8 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -25,6 +25,7 @@ public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNam
|
||||
@ValidString
|
||||
protected String version;
|
||||
protected Boolean requiredMigrationStep;
|
||||
@Getter
|
||||
protected Collection<Long> modules;
|
||||
|
||||
public T modules(final Collection<Long> modules) {
|
||||
@@ -32,10 +33,6 @@ public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNam
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public Collection<Long> getModules() {
|
||||
return modules;
|
||||
}
|
||||
|
||||
public T requiredMigrationStep(final Boolean requiredMigrationStep) {
|
||||
this.requiredMigrationStep = requiredMigrationStep;
|
||||
return (T) this;
|
||||
@@ -46,7 +43,7 @@ public abstract class AbstractDistributionSetUpdateCreate<T> extends AbstractNam
|
||||
}
|
||||
|
||||
public T version(final String version) {
|
||||
this.version = StringUtils.trimWhitespace(version);
|
||||
this.version = strip(version);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ import java.util.Optional;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -29,12 +28,12 @@ public abstract class AbstractMetadataUpdateCreate<T> {
|
||||
protected String value;
|
||||
|
||||
public T key(final String key) {
|
||||
this.key = StringUtils.trimWhitespace(key);
|
||||
this.key = AbstractBaseEntityBuilder.strip(key);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public T value(final String value) {
|
||||
this.value = StringUtils.trimWhitespace(value);
|
||||
this.value = AbstractBaseEntityBuilder.strip(value);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public abstract class AbstractNamedEntityBuilder<T> extends AbstractBaseEntityBuilder {
|
||||
|
||||
@@ -22,12 +21,12 @@ public abstract class AbstractNamedEntityBuilder<T> extends AbstractBaseEntityBu
|
||||
protected String description;
|
||||
|
||||
public T name(final String name) {
|
||||
this.name = StringUtils.trimWhitespace(name);
|
||||
this.name = AbstractBaseEntityBuilder.strip(name);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
public T description(final String description) {
|
||||
this.description = StringUtils.trimWhitespace(description);
|
||||
this.description = AbstractBaseEntityBuilder.strip(description);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.model.RolloutGroupConditions;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create builder DTO.
|
||||
@@ -27,7 +26,7 @@ public abstract class AbstractRolloutGroupCreate<T> extends AbstractNamedEntityB
|
||||
protected boolean confirmationRequired;
|
||||
|
||||
public T targetFilterQuery(final String targetFilterQuery) {
|
||||
this.targetFilterQuery = StringUtils.trimWhitespace(targetFilterQuery);
|
||||
this.targetFilterQuery = AbstractBaseEntityBuilder.strip(targetFilterQuery);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -29,7 +28,7 @@ public abstract class AbstractSoftwareModuleUpdateCreate<T> extends AbstractName
|
||||
protected String type;
|
||||
|
||||
public T type(final String type) {
|
||||
this.type = StringUtils.trimWhitespace(type);
|
||||
this.type = AbstractBaseEntityBuilder.strip(type);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -38,7 +37,7 @@ public abstract class AbstractSoftwareModuleUpdateCreate<T> extends AbstractName
|
||||
}
|
||||
|
||||
public T vendor(final String vendor) {
|
||||
this.vendor = StringUtils.trimWhitespace(vendor);
|
||||
this.vendor = AbstractBaseEntityBuilder.strip(vendor);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -47,7 +46,7 @@ public abstract class AbstractSoftwareModuleUpdateCreate<T> extends AbstractName
|
||||
}
|
||||
|
||||
public T version(final String version) {
|
||||
this.version = StringUtils.trimWhitespace(version);
|
||||
this.version = AbstractBaseEntityBuilder.strip(version);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public class AbstractTagUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
|
||||
protected String colour;
|
||||
|
||||
public T colour(final String colour) {
|
||||
this.colour = StringUtils.trimWhitespace(colour);
|
||||
this.colour = AbstractBaseEntityBuilder.strip(colour);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.model.Action;
|
||||
import org.eclipse.hawkbit.repository.model.Action.ActionType;
|
||||
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -91,7 +90,7 @@ public abstract class AbstractTargetFilterQueryUpdateCreate<T> extends AbstractB
|
||||
* @return this builder
|
||||
*/
|
||||
public T name(final String name) {
|
||||
this.name = StringUtils.trimWhitespace(name);
|
||||
this.name = AbstractBaseEntityBuilder.strip(name);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -106,7 +105,7 @@ public abstract class AbstractTargetFilterQueryUpdateCreate<T> extends AbstractB
|
||||
* @return this builder
|
||||
*/
|
||||
public T query(final String query) {
|
||||
this.query = StringUtils.trimWhitespace(query);
|
||||
this.query = AbstractBaseEntityBuilder.strip(query);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ import lombok.ToString;
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.eclipse.hawkbit.repository.exception.InvalidTargetAddressException;
|
||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -37,7 +36,7 @@ public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T>
|
||||
protected Long targetTypeId;
|
||||
|
||||
protected AbstractTargetUpdateCreate(final String controllerId) {
|
||||
this.controllerId = StringUtils.trimWhitespace(controllerId);
|
||||
this.controllerId = AbstractBaseEntityBuilder.strip(controllerId);
|
||||
}
|
||||
|
||||
public T status(final TargetUpdateStatus status) {
|
||||
@@ -49,7 +48,7 @@ public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T>
|
||||
// check if this is a real URI
|
||||
if (address != null) {
|
||||
try {
|
||||
URI.create(StringUtils.trimWhitespace(address));
|
||||
URI.create(AbstractBaseEntityBuilder.strip(address));
|
||||
} catch (final IllegalArgumentException e) {
|
||||
throw new InvalidTargetAddressException(
|
||||
"The given address " + address + " violates the RFC-2396 specification", e);
|
||||
@@ -60,7 +59,7 @@ public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T>
|
||||
}
|
||||
|
||||
public T securityToken(final String securityToken) {
|
||||
this.securityToken = StringUtils.trimWhitespace(securityToken);
|
||||
this.securityToken = AbstractBaseEntityBuilder.strip(securityToken);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -75,7 +74,7 @@ public class AbstractTargetUpdateCreate<T> extends AbstractNamedEntityBuilder<T>
|
||||
}
|
||||
|
||||
public TargetCreate controllerId(final String controllerId) {
|
||||
this.controllerId = StringUtils.trimWhitespace(controllerId);
|
||||
this.controllerId = AbstractBaseEntityBuilder.strip(controllerId);
|
||||
return (TargetCreate) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.hawkbit.repository.ValidString;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Create and update builder DTO.
|
||||
@@ -27,7 +26,7 @@ public abstract class AbstractTypeUpdateCreate<T> extends AbstractNamedEntityBui
|
||||
protected String key;
|
||||
|
||||
public T colour(final String colour) {
|
||||
this.colour = StringUtils.trimWhitespace(colour);
|
||||
this.colour = AbstractBaseEntityBuilder.strip(colour);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
@@ -36,7 +35,7 @@ public abstract class AbstractTypeUpdateCreate<T> extends AbstractNamedEntityBui
|
||||
}
|
||||
|
||||
public T key(final String key) {
|
||||
this.key = StringUtils.trimWhitespace(key);
|
||||
this.key = AbstractBaseEntityBuilder.strip(key);
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,8 +85,6 @@ public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest
|
||||
protected static final String NOT_EXIST_ID = "12345678990";
|
||||
protected static final long NOT_EXIST_IDL = Long.parseLong(NOT_EXIST_ID);
|
||||
|
||||
protected static final RandomStringUtils RANDOM_STRING_UTILS = RandomStringUtils.insecure();
|
||||
|
||||
private static final List<String> REPOSITORY_AND_TARGET_PERMISSIONS = List.of(SpPermission.READ_REPOSITORY, SpPermission.CREATE_REPOSITORY, SpPermission.UPDATE_REPOSITORY, SpPermission.DELETE_REPOSITORY, SpPermission.READ_TARGET, SpPermission.CREATE_TARGET, SpPermission.UPDATE_TARGET, SpPermission.DELETE_TARGET);
|
||||
|
||||
@PersistenceContext
|
||||
@@ -177,14 +175,6 @@ public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest
|
||||
((JpaSoftwareModule) module).setOptLockRevision(module.getOptLockRevision() + 1);
|
||||
}
|
||||
|
||||
protected static String randomString(final int len) {
|
||||
return RANDOM_STRING_UTILS.next(len, true, false);
|
||||
}
|
||||
|
||||
protected static byte[] randomBytes(final int len) {
|
||||
return randomString(len).getBytes();
|
||||
}
|
||||
|
||||
protected Database getDatabase() {
|
||||
return jpaProperties.getDatabase();
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import io.qameta.allure.Feature;
|
||||
import io.qameta.allure.Step;
|
||||
import io.qameta.allure.Story;
|
||||
import org.eclipse.hawkbit.repository.DeploymentManagement;
|
||||
import org.eclipse.hawkbit.repository.builder.AutoAssignDistributionSetUpdate;
|
||||
import org.eclipse.hawkbit.repository.exception.IncompleteDistributionSetException;
|
||||
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
|
||||
import org.eclipse.hawkbit.repository.jpa.specifications.ActionSpecifications;
|
||||
@@ -248,15 +249,13 @@ class AutoAssignCheckerIntTest extends AbstractJpaIntegrationTest {
|
||||
final String targetDsAIdPref = "targA";
|
||||
final String targetDsFIdPref = "targB";
|
||||
|
||||
// target filter query that matches first bunch of targets, that should
|
||||
// fail
|
||||
assertThatExceptionOfType(IncompleteDistributionSetException.class).isThrownBy(() -> {
|
||||
final Long filterId = targetFilterQueryManagement.create(
|
||||
entityFactory.targetFilterQuery().create().name("filterA").query("id==" + targetDsFIdPref + "*"))
|
||||
.getId();
|
||||
targetFilterQueryManagement
|
||||
.updateAutoAssignDS(entityFactory.targetFilterQuery().updateAutoAssign(filterId).ds(setF.getId()));
|
||||
});
|
||||
final Long filterId = targetFilterQueryManagement.create(
|
||||
entityFactory.targetFilterQuery().create().name("filterA").query("id==" + targetDsFIdPref + "*"))
|
||||
.getId();
|
||||
final AutoAssignDistributionSetUpdate targetFilterQuery = entityFactory.targetFilterQuery().updateAutoAssign(filterId).ds(setF.getId());
|
||||
// target filter query that matches first bunch of targets, that should fail
|
||||
assertThatExceptionOfType(IncompleteDistributionSetException.class).isThrownBy(
|
||||
() -> targetFilterQueryManagement.updateAutoAssignDS(targetFilterQuery));
|
||||
// target filter query that matches failed bunch of targets
|
||||
targetFilterQueryManagement.create(entityFactory.targetFilterQuery().create().name("filterB")
|
||||
.query("id==" + targetDsAIdPref + "*").autoAssignDistributionSet(setA.getId()));
|
||||
|
||||
@@ -30,7 +30,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.eclipse.hawkbit.artifact.repository.model.DbArtifact;
|
||||
import org.eclipse.hawkbit.artifact.repository.model.DbArtifactHash;
|
||||
import org.eclipse.hawkbit.im.authentication.SpPermission;
|
||||
|
||||
@@ -116,6 +116,8 @@ 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
|
||||
@@ -292,6 +294,14 @@ public abstract class AbstractIntegrationTest {
|
||||
return Comparator.comparing(Target::getControllerId);
|
||||
}
|
||||
|
||||
protected static String randomString(final int len) {
|
||||
return RANDOM_STRING_UTILS.next(len, true, false);
|
||||
}
|
||||
|
||||
protected static byte[] randomBytes(final int len) {
|
||||
return randomString(len).getBytes();
|
||||
}
|
||||
|
||||
protected DistributionSetAssignmentResult assignDistributionSet(final long dsID, final String controllerId) {
|
||||
return assignDistributionSet(dsID, controllerId, ActionType.FORCED);
|
||||
}
|
||||
@@ -477,7 +487,7 @@ public abstract class AbstractIntegrationTest {
|
||||
|
||||
private static String createTempDir() {
|
||||
try {
|
||||
return Files.createTempDirectory(null).toString() + "/" + RandomStringUtils.randomAlphanumeric(20);
|
||||
return Files.createTempDirectory(null).toString() + "/" + randomString(20);
|
||||
} catch (final IOException e) {
|
||||
throw new IllegalStateException("Failed to create temp directory");
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public abstract class AbstractSqlTestDatabase extends AbstractTestExecutionListe
|
||||
|
||||
protected final DatasourceContext context;
|
||||
|
||||
public AbstractSqlTestDatabase(final DatasourceContext context) {
|
||||
protected AbstractSqlTestDatabase(final DatasourceContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ public abstract class AbstractRestIntegrationTest extends AbstractIntegrationTes
|
||||
private CharacterEncodingFilter characterEncodingFilter;
|
||||
|
||||
@BeforeEach
|
||||
public void before() throws Exception {
|
||||
public void before() {
|
||||
mvc = createMvcWebAppContext(webApplicationContext).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -58,11 +58,9 @@ public class Amqp {
|
||||
}
|
||||
}
|
||||
connectionFactory.setUsername(
|
||||
dmf == null || ObjectUtils.isEmpty(dmf.getUsername()) ?
|
||||
rabbitProperties.getUsername() : dmf.getUsername());
|
||||
dmf == null || ObjectUtils.isEmpty(dmf.getUsername()) ? rabbitProperties.getUsername() : dmf.getUsername());
|
||||
connectionFactory.setPassword(
|
||||
dmf == null || ObjectUtils.isEmpty(dmf.getPassword()) ?
|
||||
rabbitProperties.getPassword() : dmf.getPassword());
|
||||
dmf == null || ObjectUtils.isEmpty(dmf.getPassword()) ? rabbitProperties.getPassword() : dmf.getPassword());
|
||||
connectionFactory.setVirtualHost(vHost);
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user