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:
Peter Vigier
2022-01-31 21:59:46 +01:00
committed by GitHub
parent 5443b5df9c
commit 44a85f20eb
98 changed files with 2583 additions and 2702 deletions

View File

@@ -56,7 +56,7 @@ public class DefaultAmqpMessageSenderService extends BaseAmqpService implements
LOGGER.debug("Sending message to exchange {} with correlationId {}", exchange, correlationId);
}
getRabbitTemplate().send(exchange, null, message, new CorrelationData(correlationId));
getRabbitTemplate().send(exchange, "", message, new CorrelationData(correlationId));
}
protected static boolean isCorrelationIdEmpty(final Message message) {

View File

@@ -68,7 +68,7 @@ import io.qameta.allure.Story;
@Feature("Component Tests - Device Management Federation API")
@Story("AmqpMessage Dispatcher Service Test")
@SpringBootTest(classes = { RepositoryApplicationConfiguration.class })
public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
private static final String TENANT = "default";
private static final Long TENANT_ID = 4711L;
@@ -127,7 +127,7 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
@Test
@Description("Verifies that download and install event with 3 software modules and no artifacts works")
public void testSendDownloadRequestWithSoftwareModulesAndNoArtifacts() {
void testSendDownloadRequestWithSoftwareModulesAndNoArtifacts() {
final DistributionSet createDistributionSet = testdataFactory
.createDistributionSet(UUID.randomUUID().toString());
testdataFactory.addSoftwareModuleMetadata(createDistributionSet);
@@ -154,17 +154,19 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
if (!softwareModule.getModuleId().equals(softwareModule2.getId())) {
continue;
}
assertThat(softwareModule.getModuleType()).isEqualTo(softwareModule2.getType().getKey()).as(
"Software module type in event should be the same as the softwaremodule in the distribution set");
assertThat(softwareModule.getModuleVersion()).isEqualTo(softwareModule2.getVersion()).as(
"Software module version in event should be the same as the softwaremodule in the distribution set");
assertThat(softwareModule.getModuleType())
.as("Software module type in event should be the same as the softwaremodule in the distribution set")
.isEqualTo(softwareModule2.getType().getKey());
assertThat(softwareModule.getModuleVersion())
.as("Software module version in event should be the same as the softwaremodule in the distribution set")
.isEqualTo(softwareModule2.getVersion());
}
}
}
@Test
@Description("Verifies that download and install event with software modules and artifacts works")
public void testSendDownloadRequest() {
void testSendDownloadRequest() {
DistributionSet dsA = testdataFactory.createDistributionSet(UUID.randomUUID().toString());
SoftwareModule module = dsA.getModules().iterator().next();
final List<AbstractDbArtifact> receivedList = new ArrayList<>();
@@ -186,15 +188,18 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
final DmfDownloadAndUpdateRequest downloadAndUpdateRequest = assertDownloadAndInstallMessage(sendMessage,
action.getId());
assertThat(downloadAndUpdateRequest.getSoftwareModules()).hasSize(3)
.as("DownloadAndUpdateRequest event should contains 3 software modules");
assertThat(downloadAndUpdateRequest.getSoftwareModules())
.as("DownloadAndUpdateRequest event should contains 3 software modules")
.hasSize(3);
assertThat(downloadAndUpdateRequest.getTargetSecurityToken()).isEqualTo(TEST_TOKEN);
for (final DmfSoftwareModule softwareModule : downloadAndUpdateRequest.getSoftwareModules()) {
if (!softwareModule.getModuleId().equals(module.getId())) {
continue;
}
assertThat(softwareModule.getArtifacts().size()).isEqualTo(module.getArtifacts().size()).isGreaterThan(0);
assertThat(softwareModule.getArtifacts().size())
.isEqualTo(module.getArtifacts().size())
.isNotZero();
module.getArtifacts().forEach(dbArtifact -> {
final Optional<org.eclipse.hawkbit.dmf.json.model.DmfArtifact> found = softwareModule.getArtifacts()
@@ -211,7 +216,7 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
@Test
@Description("Verifies that sending update controller attributes event works.")
public void sendUpdateAttributesRequest() {
void sendUpdateAttributesRequest() {
final String amqpUri = "amqp://anyhost";
final TargetAttributesRequestedEvent targetAttributesRequestedEvent = new TargetAttributesRequestedEvent(TENANT,
1L, CONTROLLER_ID, amqpUri, Target.class.getName(), serviceMatcher.getServiceId());
@@ -224,7 +229,7 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
@Test
@Description("Verifies that send cancel event works")
public void testSendCancelRequest() {
void testSendCancelRequest() {
final CancelTargetAssignmentEvent cancelTargetAssignmentDistributionSetEvent = new CancelTargetAssignmentEvent(
testTarget, 1L, serviceMatcher.getServiceId());
amqpMessageDispatcherService
@@ -237,7 +242,7 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
@Test
@Description("Verifies that sending a delete message when receiving a delete event works.")
public void sendDeleteRequest() {
void sendDeleteRequest() {
// setup
final String amqpUri = "amqp://anyhost";
@@ -254,7 +259,7 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
@Test
@Description("Verifies that a delete message is not send if the address is not an amqp address.")
public void sendDeleteRequestWithNoAmqpAddress() {
void sendDeleteRequestWithNoAmqpAddress() {
// setup
final String noAmqpUri = "http://anyhost";
@@ -270,7 +275,7 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
@Test
@Description("Verifies that a delete message is not send if the address is null.")
public void sendDeleteRequestWithNullAddress() {
void sendDeleteRequestWithNullAddress() {
// setup
final String noAmqpUri = null;
@@ -287,19 +292,22 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
private void assertCancelMessage(final Message sendMessage) {
assertEventMessage(sendMessage);
final DmfActionRequest actionId = convertMessage(sendMessage, DmfActionRequest.class);
assertThat(actionId.getActionId()).isEqualTo(Long.valueOf(1)).as("Action ID should be 1");
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.TOPIC))
.isEqualTo(EventTopic.CANCEL_DOWNLOAD).as("The topc in the message should be a CANCEL_DOWNLOAD value");
assertThat(actionId.getActionId())
.as("Action ID should be 1")
.isOne();
assertThat(sendMessage.getMessageProperties().getHeaders())
.as("The topc in the message should be a CANCEL_DOWNLOAD value")
.containsEntry(MessageHeaderKey.TOPIC, EventTopic.CANCEL_DOWNLOAD);
}
private void assertDeleteMessage(final Message sendMessage) {
assertThat(sendMessage).isNotNull();
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.THING_ID))
.isEqualTo(CONTROLLER_ID);
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.TENANT)).isEqualTo(TENANT);
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.TYPE))
.isEqualTo(MessageType.THING_DELETED);
assertThat(sendMessage.getMessageProperties().getHeaders())
.containsEntry(MessageHeaderKey.THING_ID, CONTROLLER_ID);
assertThat(sendMessage.getMessageProperties().getHeaders())
.containsEntry(MessageHeaderKey.TENANT, TENANT);
assertThat(sendMessage.getMessageProperties().getHeaders())
.containsEntry(MessageHeaderKey.TYPE,MessageType.THING_DELETED);
}
private DmfDownloadAndUpdateRequest assertDownloadAndInstallMessage(final Message sendMessage, final Long action) {
@@ -307,32 +315,35 @@ public class AmqpMessageDispatcherServiceTest extends AbstractIntegrationTest {
final DmfDownloadAndUpdateRequest downloadAndUpdateRequest = convertMessage(sendMessage,
DmfDownloadAndUpdateRequest.class);
assertThat(downloadAndUpdateRequest.getActionId()).isEqualTo(action);
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.TOPIC))
.isEqualTo(EventTopic.DOWNLOAD_AND_INSTALL)
.as("The topic of the event should contain DOWNLOAD_AND_INSTALL");
assertThat(downloadAndUpdateRequest.getTargetSecurityToken()).isEqualTo(TEST_TOKEN)
.as("Security token of target");
assertThat(sendMessage.getMessageProperties().getHeaders())
.as("The topic of the event should contain DOWNLOAD_AND_INSTALL")
.containsEntry(MessageHeaderKey.TOPIC,EventTopic.DOWNLOAD_AND_INSTALL);
assertThat(downloadAndUpdateRequest.getTargetSecurityToken())
.as("Security token of target")
.isEqualTo(TEST_TOKEN);
return downloadAndUpdateRequest;
}
private void assertUpdateAttributesMessage(final Message sendMessage) {
assertEventMessage(sendMessage);
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.TOPIC))
.isEqualTo(EventTopic.REQUEST_ATTRIBUTES_UPDATE)
.as("The topic of the event should contain REQUEST_ATTRIBUTES_UPDATE");
assertThat(sendMessage.getMessageProperties().getHeaders())
.as("The topic of the event should contain REQUEST_ATTRIBUTES_UPDATE")
.containsEntry(MessageHeaderKey.TOPIC, EventTopic.REQUEST_ATTRIBUTES_UPDATE);
}
private void assertEventMessage(final Message sendMessage) {
assertThat(sendMessage).isNotNull().as("The message should not be null");
assertThat(sendMessage).as("The message should not be null").isNotNull();
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.THING_ID))
.isEqualTo(CONTROLLER_ID).as("The value of the message header THING_ID should be " + CONTROLLER_ID);
assertThat(sendMessage.getMessageProperties().getHeaders().get(MessageHeaderKey.TYPE))
.isEqualTo(MessageType.EVENT).as("The value of the message header TYPE should be EVENT");
assertThat(sendMessage.getMessageProperties().getContentType()).isEqualTo(MessageProperties.CONTENT_TYPE_JSON)
.as("The content type message should be " + MessageProperties.CONTENT_TYPE_JSON);
assertThat(sendMessage.getMessageProperties().getHeaders())
.as("The value of the message header THING_ID should be " + CONTROLLER_ID)
.containsEntry(MessageHeaderKey.THING_ID, CONTROLLER_ID);
assertThat(sendMessage.getMessageProperties().getHeaders())
.as("The value of the message header TYPE should be EVENT")
.containsEntry(MessageHeaderKey.TYPE,MessageType.EVENT);
assertThat(sendMessage.getMessageProperties().getContentType())
.as("The content type message should be " + MessageProperties.CONTENT_TYPE_JSON)
.isEqualTo(MessageProperties.CONTENT_TYPE_JSON);
}
protected Message createArgumentCapture(final URI uri) {

View File

@@ -153,9 +153,9 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
final Message replyMessage = replyToListener.getDeleteMessages().get(target);
assertAllTargetsCount(0);
final Map<String, Object> headers = replyMessage.getMessageProperties().getHeaders();
assertThat(headers.get(MessageHeaderKey.THING_ID)).isEqualTo(target);
assertThat(headers.get(MessageHeaderKey.TENANT)).isEqualTo(TENANT_EXIST);
assertThat(headers.get(MessageHeaderKey.TYPE)).isEqualTo(MessageType.THING_DELETED.toString());
assertThat(headers).containsEntry(MessageHeaderKey.THING_ID, target)
.containsEntry(MessageHeaderKey.TENANT, TENANT_EXIST)
.containsEntry(MessageHeaderKey.TYPE, MessageType.THING_DELETED.toString());
}
protected void assertRequestAttributesUpdateMessage(final String target) {
@@ -173,9 +173,9 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
final Map<String, Object> headers = replyMessage.getMessageProperties().getHeaders();
assertThat(headers.get(MessageHeaderKey.TENANT)).isEqualTo(TENANT_EXIST);
assertThat(headers).containsEntry(MessageHeaderKey.TENANT, TENANT_EXIST);
assertThat(correlationId).isEqualTo(replyMessage.getMessageProperties().getCorrelationId());
assertThat(headers.get(MessageHeaderKey.TYPE)).isEqualTo(MessageType.PING_RESPONSE.toString());
assertThat(headers).containsEntry(MessageHeaderKey.TYPE, MessageType.PING_RESPONSE.toString());
assertThat(Long.valueOf(new String(replyMessage.getBody(), StandardCharsets.UTF_8)))
.isLessThanOrEqualTo(System.currentTimeMillis());
@@ -212,22 +212,17 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
assertAssignmentMessage(dsModules, controllerId, EventTopic.DOWNLOAD);
}
protected void createAndSendThingCreated(final String controllerId, final String tenant) {
createAndSendThingCreated(controllerId, null, null, tenant);
protected void createAndSendThingCreated(final String controllerId) {
createAndSendThingCreated(controllerId, null, null);
}
protected void createAndSendThingCreated(final String controllerId, final String name,
final Map<String, String> attributes, final String tenant) {
final Message message = createTargetMessage(controllerId, name, attributes, tenant);
final Map<String, String> attributes) {
final Message message = createTargetMessage(controllerId, name, attributes,
AbstractAmqpServiceIntegrationTest.TENANT_EXIST);
getDmfClient().send(message);
}
protected Message createAndSendPingMessage(final String correlationId, final String tenant) {
final Message message = createPingMessage(correlationId, tenant);
getDmfClient().send(message);
return message;
}
protected void verifyReplyToListener() {
createConditionFactory()
.untilAsserted(() -> Mockito.verify(replyToListener, Mockito.atLeast(1)).handleMessage(Mockito.any()));
@@ -254,10 +249,11 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
final Message replyMessage = replyToListener.getLatestEventMessage(eventTopic);
assertAllTargetsCount(1);
final Map<String, Object> headers = replyMessage.getMessageProperties().getHeaders();
assertThat(headers.get(MessageHeaderKey.TOPIC)).isEqualTo(eventTopic.toString());
assertThat(headers.get(MessageHeaderKey.THING_ID)).isEqualTo(controllerId);
assertThat(headers.get(MessageHeaderKey.TENANT)).isEqualTo(TENANT_EXIST);
assertThat(headers.get(MessageHeaderKey.TYPE)).isEqualTo(MessageType.EVENT.toString());
assertThat(headers).containsEntry(MessageHeaderKey.TOPIC, eventTopic.toString())
.containsEntry(MessageHeaderKey.THING_ID, controllerId)
.containsEntry(MessageHeaderKey.TENANT, TENANT_EXIST)
.containsEntry(MessageHeaderKey.TYPE, MessageType.EVENT.toString());
return replyMessage;
}
@@ -290,7 +286,7 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
final int existingTargetsAfterCreation, final TargetUpdateStatus expectedTargetStatus,
final String createdBy, final Map<String, String> attributes,
final Callable<Optional<Target>> fetchTarget) {
createAndSendThingCreated(controllerId, name, attributes, TENANT_EXIST);
createAndSendThingCreated(controllerId, name, attributes);
final Target registeredTarget = waitUntilIsPresent(fetchTarget::call);
assertAllTargetsCount(existingTargetsAfterCreation);
assertThat(registeredTarget).isNotNull();
@@ -372,14 +368,15 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
final DmfActionStatus status) {
final DmfActionUpdateStatus dmfActionUpdateStatus = new DmfActionUpdateStatus(actionId, status);
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, dmfActionUpdateStatus);
final Message eventMessage = createUpdateActionEventMessage(dmfActionUpdateStatus);
eventMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.THING_ID, target);
getDmfClient().send(eventMessage);
}
protected Message createUpdateActionEventMessage(final String tenant, final Object payload) {
final MessageProperties messageProperties = createMessagePropertiesWithTenant(tenant);
protected Message createUpdateActionEventMessage(final Object payload) {
final MessageProperties messageProperties = createMessagePropertiesWithTenant(
AbstractAmqpServiceIntegrationTest.TENANT_EXIST);
messageProperties.getHeaders().put(MessageHeaderKey.TYPE, MessageType.EVENT.toString());
messageProperties.getHeaders().put(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ACTION_STATUS.toString());
messageProperties.setCorrelationId(CORRELATION_ID);
@@ -404,8 +401,9 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
}
protected Message createUpdateAttributesMessageWrongBody(final String target, final String tenant) {
final MessageProperties messageProperties = createMessagePropertiesWithTenant(tenant);
protected Message createUpdateAttributesMessageWrongBody(final String target) {
final MessageProperties messageProperties = createMessagePropertiesWithTenant(
AbstractAmqpServiceIntegrationTest.TENANT_EXIST);
messageProperties.getHeaders().put(MessageHeaderKey.THING_ID, target);
messageProperties.getHeaders().put(MessageHeaderKey.TYPE, MessageType.EVENT.toString());
messageProperties.getHeaders().put(MessageHeaderKey.TOPIC, EventTopic.UPDATE_ATTRIBUTES.toString());
@@ -423,19 +421,13 @@ public abstract class AbstractAmqpServiceIntegrationTest extends AbstractAmqpInt
final Map<String, String> controllerAttributes = WithSpringAuthorityRule
.runAsPrivileged(() -> targetManagement.getControllerAttributes(controllerId));
assertThat(controllerAttributes.size()).isEqualTo(attributes.size());
attributes.forEach((k, v) -> assertKeyValueInMap(k, v, controllerAttributes));
assertThat(controllerAttributes).containsAllEntriesOf(attributes);
} catch (final Exception e) {
throw new RuntimeException(e);
}
});
}
private void assertKeyValueInMap(final String key, final String value,
final Map<String, String> controllerAttributes) {
assertThat(controllerAttributes.containsKey(key)).isTrue();
assertThat(controllerAttributes.get(key)).isEqualTo(value);
}
@Override
protected String getExchange() {
return AmqpSettings.DMF_EXCHANGE;

View File

@@ -163,7 +163,7 @@ public class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpSer
assertCancelActionMessage(getFirstAssignedActionId(assignmentResult), controllerId);
// cancelation message is returned upon polling
createAndSendThingCreated(controllerId, TENANT_EXIST);
createAndSendThingCreated(controllerId);
waitUntilEventMessagesAreDispatchedToTarget(EventTopic.CANCEL_DOWNLOAD);
assertCancelActionMessage(getFirstAssignedActionId(assignmentResult), controllerId);
@@ -176,7 +176,7 @@ public class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpSer
assertDownloadAndInstallMessage(distributionSet2.getModules(), controllerId);
// latest action is returned upon polling
createAndSendThingCreated(controllerId, TENANT_EXIST);
createAndSendThingCreated(controllerId);
waitUntilEventMessagesAreDispatchedToTarget(EventTopic.DOWNLOAD_AND_INSTALL);
assertDownloadAndInstallMessage(distributionSet2.getModules(), controllerId);
}
@@ -478,7 +478,7 @@ public class AmqpMessageDispatcherServiceIntegrationTest extends AbstractAmqpSer
final Long actionId = registerTargetAndCancelActionId(controllerId);
createAndSendThingCreated(controllerId, TENANT_EXIST);
createAndSendThingCreated(controllerId);
waitUntilTargetHasStatus(controllerId, TargetUpdateStatus.PENDING);
assertCancelActionMessage(actionId, controllerId);
}

View File

@@ -58,8 +58,12 @@ import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.eclipse.hawkbit.repository.test.matcher.Expect;
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
import org.eclipse.hawkbit.repository.test.util.TargetTestData;
import org.eclipse.hawkbit.repository.test.util.WithSpringAuthorityRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
@@ -75,8 +79,11 @@ import io.qameta.allure.Story;
@Feature("Component Tests - Device Management Federation API")
@Story("Amqp Message Handler Service")
public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServiceIntegrationTest {
class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServiceIntegrationTest {
private static final String TARGET_PREFIX = "Dmf_hand_";
public static final String DMF_REGISTER_TEST_CONTROLLER_ID = "Dmf_hand_registerTargets_1";
public static final String DMF_ATTR_TEST_CONTROLLER_ID = "Dmf_hand_updateAttributes";
public static final String UPDATE_ATTR_TEST_CONTROLLER_ID = "ControllerAttributeTestTarget";
@Autowired
private AmqpProperties amqpProperties;
@@ -86,7 +93,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests DMF PING request and expected response.")
public void pingDmfInterface() {
void pingDmfInterface() {
final Message pingMessage = createPingMessage(CORRELATION_ID, TENANT_EXIST);
getDmfClient().send(pingMessage);
@@ -99,7 +106,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Description("Tests register target")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 2),
@Expect(type = TargetPollEvent.class, count = 3) })
public void registerTargets() {
void registerTargets() {
final String controllerId = TARGET_PREFIX + "registerTargets";
registerAndAssertTargetWithExistingTenant(controllerId, 1);
@@ -114,7 +121,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Description("Tests register target with name")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 2) })
public void registerTargetWithName() {
void registerTargetWithName() {
final String controllerId = TARGET_PREFIX + "registerTargetWithName";
final String name = "NonDefaultTargetName";
registerAndAssertTargetWithExistingTenant(controllerId, name, 1, TargetUpdateStatus.REGISTERED, CREATED_BY,
@@ -130,7 +137,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Description("Tests register target with attributes")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 2), @Expect(type = TargetPollEvent.class, count = 2) })
public void registerTargetWithAttributes() {
void registerTargetWithAttributes() {
final String controllerId = TARGET_PREFIX + "registerTargetWithAttributes";
final Map<String, String> attributes = new HashMap<>();
attributes.put("testKey1", "testValue1");
@@ -149,7 +156,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Description("Tests register target with name and attributes")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 3), @Expect(type = TargetPollEvent.class, count = 2) })
public void registerTargetWithNameAndAttributes() {
void registerTargetWithNameAndAttributes() {
final String controllerId = TARGET_PREFIX + "registerTargetWithAttributes";
final String name = "NonDefaultTargetName";
final Map<String, String> attributes = new HashMap<>();
@@ -166,46 +173,30 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
Mockito.verifyNoInteractions(getDeadletterListener());
}
@Test
@ParameterizedTest
@ValueSource(strings = {"", "Invalid Invalid"})
@NullSource
@Description("Tests register invalid target with empty controller id.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void registerEmptyTarget() {
createAndSendThingCreated("", TENANT_EXIST);
assertAllTargetsCount(0);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests register invalid target with whitespace controller id.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void registerWhitespaceTarget() {
createAndSendThingCreated("Invalid Invalid", TENANT_EXIST);
assertAllTargetsCount(0);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests register invalid target with null controller id.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void registerInvalidNullTarget() {
createAndSendThingCreated(null, TENANT_EXIST);
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void shouldNotRegisterTargetsWithInvalidControllerIds(String controllerId) {
createAndSendThingCreated(controllerId);
assertAllTargetsCount(0);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests register invalid target with too long controller id")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void registerInvalidTargetWithTooLongControllerId() {
createAndSendThingCreated(RandomStringUtils.randomAlphabetic(Target.CONTROLLER_ID_MAX_SIZE + 1), TENANT_EXIST);
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void registerInvalidTargetWithTooLongControllerId() {
createAndSendThingCreated(RandomStringUtils.randomAlphabetic(Target.CONTROLLER_ID_MAX_SIZE + 1));
assertAllTargetsCount(0);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests null reply to property in message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void missingReplyToProperty() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void missingReplyToProperty() {
final String controllerId = TARGET_PREFIX + "missingReplyToProperty";
final Message createTargetMessage = createTargetMessage(controllerId, TENANT_EXIST);
createTargetMessage.getMessageProperties().setReplyTo(null);
@@ -217,8 +208,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests missing reply to property in message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void emptyReplyToProperty() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void emptyReplyToProperty() {
final String controllerId = TARGET_PREFIX + "emptyReplyToProperty";
final Message createTargetMessage = createTargetMessage(controllerId, TENANT_EXIST);
createTargetMessage.getMessageProperties().setReplyTo("");
@@ -230,8 +221,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests missing thing id property in message. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void missingThingIdProperty() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void missingThingIdProperty() {
final Message createTargetMessage = createTargetMessage(null, TENANT_EXIST);
createTargetMessage.getMessageProperties().getHeaders().remove(MessageHeaderKey.THING_ID);
getDmfClient().send(createTargetMessage);
@@ -242,8 +233,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests null thing id property in message. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void nullThingIdProperty() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void nullThingIdProperty() {
final Message createTargetMessage = createTargetMessage(null, TENANT_EXIST);
getDmfClient().send(createTargetMessage);
@@ -253,8 +244,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests missing tenant message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void missingTenantHeader() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void missingTenantHeader() {
final String controllerId = TARGET_PREFIX + "missingTenantHeader";
final Message createTargetMessage = createTargetMessage(controllerId, TENANT_EXIST);
createTargetMessage.getMessageProperties().getHeaders().remove(MessageHeaderKey.TENANT);
@@ -266,8 +257,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests null tenant message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void nullTenantHeader() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void nullTenantHeader() {
final String controllerId = TARGET_PREFIX + "nullTenantHeader";
final Message createTargetMessage = createTargetMessage(controllerId, null);
getDmfClient().send(createTargetMessage);
@@ -278,8 +269,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests empty tenant message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void emptyTenantHeader() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void emptyTenantHeader() {
final String controllerId = TARGET_PREFIX + "emptyTenantHeader";
final Message createTargetMessage = createTargetMessage(controllerId, "");
getDmfClient().send(createTargetMessage);
@@ -290,8 +281,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests tenant not exist. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void tenantNotExist() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void tenantNotExist() {
final String controllerId = TARGET_PREFIX + "tenantNotExist";
final Message createTargetMessage = createTargetMessage(controllerId, "TenantNotExist");
getDmfClient().send(createTargetMessage);
@@ -302,8 +293,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests missing type message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void missingTypeHeader() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void missingTypeHeader() {
final Message createTargetMessage = createTargetMessage(null, TENANT_EXIST);
createTargetMessage.getMessageProperties().getHeaders().remove(MessageHeaderKey.TYPE);
getDmfClient().send(createTargetMessage);
@@ -312,70 +303,28 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
assertAllTargetsCount(0);
}
@Test
@ParameterizedTest
@ValueSource(strings={"", "NotExist"})
@NullSource
@Description("Tests null type message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void nullTypeHeader() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void shouldNotCreateTargetsWithInvalidTypeInHeader(String type) {
final Message createTargetMessage = createTargetMessage(null, TENANT_EXIST);
createTargetMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TYPE, null);
createTargetMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TYPE, type);
getDmfClient().send(createTargetMessage);
verifyOneDeadLetterMessage();
assertAllTargetsCount(0);
}
@Test
@Description("Tests empty type message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void emptyTypeHeader() {
final Message createTargetMessage = createTargetMessage(null, TENANT_EXIST);
createTargetMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TYPE, "");
getDmfClient().send(createTargetMessage);
verifyOneDeadLetterMessage();
assertAllTargetsCount(0);
}
@Test
@Description("Tests invalid type message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void invalidTypeHeader() {
final Message createTargetMessage = createTargetMessage(null, TENANT_EXIST);
createTargetMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TYPE, "NotExist");
getDmfClient().send(createTargetMessage);
verifyOneDeadLetterMessage();
assertAllTargetsCount(0);
}
@Test
@ParameterizedTest
@ValueSource(strings = {"", "NotExist"})
@NullSource
@Description("Tests null topic message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void nullTopicHeader() {
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, "");
eventMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TOPIC, null);
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests null topic message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void emptyTopicHeader() {
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, "");
eventMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TOPIC, "");
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests null topic message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void invalidTopicHeader() {
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, "");
eventMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TOPIC, "NotExist");
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void shouldNotSendMessagesWithInvalidTopic(String topic) {
final Message eventMessage = createUpdateActionEventMessage("");
eventMessage.getMessageProperties().getHeaders().put(MessageHeaderKey.TOPIC, topic);
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
@@ -383,48 +332,32 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Tests missing topic message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void missingTopicHeader() {
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, "");
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void missingTopicHeader() {
final Message eventMessage = createUpdateActionEventMessage("");
eventMessage.getMessageProperties().getHeaders().remove(MessageHeaderKey.TOPIC);
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
}
@Test
@ParameterizedTest
@ValueSource(strings = { "", "Invalid Content"})
@NullSource
@Description("Tests invalid null message content. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void updateActionStatusWithNullContent() {
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, null);
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests invalid empty message content. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void updateActionStatusWithEmptyContent() {
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, "");
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests invalid json message content. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void updateActionStatusWithInvalidJsonContent() {
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, "Invalid Content");
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void shouldMoveUpdateActionStatusWithInvalidPayloadIntoDeadLetter(String payload) {
final Message eventMessage = createUpdateActionEventMessage(payload);
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
}
@Test
@Description("Tests invalid topic message header. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void updateActionStatusWithInvalidActionId() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void updateActionStatusWithInvalidActionId() {
final DmfActionUpdateStatus actionUpdateStatus = new DmfActionUpdateStatus(1L, DmfActionStatus.RUNNING);
final Message eventMessage = createUpdateActionEventMessage(TENANT_EXIST, actionUpdateStatus);
final Message eventMessage = createUpdateActionEventMessage(actionUpdateStatus);
getDmfClient().send(eventMessage);
verifyOneDeadLetterMessage();
}
@@ -439,7 +372,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetAttributesRequestedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 2), @Expect(type = TargetPollEvent.class, count = 1) })
public void finishActionStatus() {
void finishActionStatus() {
final String controllerId = TARGET_PREFIX + "finishActionStatus";
registerTargetAndSendAndAssertUpdateActionStatus(DmfActionStatus.FINISHED, Status.FINISHED, controllerId);
}
@@ -448,12 +381,12 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Description("Register a target and send a update action status (running). Verify if the updated action status is correct.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetAssignDistributionSetEvent.class, count = 1),
@Expect(type = ActionUpdatedEvent.class, count = 0), @Expect(type = ActionCreatedEvent.class, count = 1),
@Expect(type = ActionUpdatedEvent.class), @Expect(type = ActionCreatedEvent.class, count = 1),
@Expect(type = SoftwareModuleCreatedEvent.class, count = 3),
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void runningActionStatus() {
void runningActionStatus() {
final String controllerId = TARGET_PREFIX + "runningActionStatus";
registerTargetAndSendAndAssertUpdateActionStatus(DmfActionStatus.RUNNING, Status.RUNNING, controllerId);
}
@@ -462,12 +395,12 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Description("Register a target and send an update action status (downloaded). Verify if the updated action status is correct.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetAssignDistributionSetEvent.class, count = 1),
@Expect(type = ActionUpdatedEvent.class, count = 0), @Expect(type = ActionCreatedEvent.class, count = 1),
@Expect(type = ActionUpdatedEvent.class), @Expect(type = ActionCreatedEvent.class, count = 1),
@Expect(type = SoftwareModuleCreatedEvent.class, count = 3),
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void downloadedActionStatus() {
void downloadedActionStatus() {
final String controllerId = TARGET_PREFIX + "downloadedActionStatus";
registerTargetAndSendAndAssertUpdateActionStatus(DmfActionStatus.DOWNLOADED, Status.DOWNLOADED, controllerId);
}
@@ -481,7 +414,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void downloadActionStatus() {
void downloadActionStatus() {
final String controllerId = TARGET_PREFIX + "downloadActionStatus";
registerTargetAndSendAndAssertUpdateActionStatus(DmfActionStatus.DOWNLOAD, Status.DOWNLOAD, controllerId);
}
@@ -495,7 +428,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 2), @Expect(type = TargetPollEvent.class, count = 1) })
public void errorActionStatus() {
void errorActionStatus() {
final String controllerId = TARGET_PREFIX + "errorActionStatus";
registerTargetAndSendAndAssertUpdateActionStatus(DmfActionStatus.ERROR, Status.ERROR, controllerId);
}
@@ -509,7 +442,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void warningActionStatus() {
void warningActionStatus() {
final String controllerId = TARGET_PREFIX + "warningActionStatus";
registerTargetAndSendAndAssertUpdateActionStatus(DmfActionStatus.WARNING, Status.WARNING, controllerId);
}
@@ -523,7 +456,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void retrievedActionStatus() {
void retrievedActionStatus() {
final String controllerId = TARGET_PREFIX + "retrievedActionStatus";
registerTargetAndSendAndAssertUpdateActionStatus(DmfActionStatus.RETRIEVED, Status.RETRIEVED, controllerId);
}
@@ -537,7 +470,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void cancelNotAllowActionStatus() {
void cancelNotAllowActionStatus() {
final String controllerId = TARGET_PREFIX + "cancelNotAllowActionStatus";
registerTargetAndSendActionStatus(DmfActionStatus.CANCELED, controllerId);
verifyOneDeadLetterMessage();
@@ -552,7 +485,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 2) })
public void receiveDownLoadAndInstallMessageAfterAssignment() {
void receiveDownLoadAndInstallMessageAfterAssignment() {
final String controllerId = TARGET_PREFIX + "receiveDownLoadAndInstallMessageAfterAssignment";
// setup
@@ -578,7 +511,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 2) })
public void receiveDownloadMessageBeforeMaintenanceWindowStartTime() {
void receiveDownloadMessageBeforeMaintenanceWindowStartTime() {
final String controllerId = TARGET_PREFIX + "receiveDownLoadMessageBeforeMaintenanceWindowStartTime";
// setup
@@ -605,7 +538,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 2) })
public void receiveDownloadAndInstallMessageDuringMaintenanceWindow() {
void receiveDownloadAndInstallMessageDuringMaintenanceWindow() {
final String controllerId = TARGET_PREFIX + "receiveDownLoadAndInstallMessageDuringMaintenanceWindow";
// setup
@@ -633,7 +566,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = CancelTargetAssignmentEvent.class, count = 1),
@Expect(type = ActionUpdatedEvent.class, count = 1), @Expect(type = TargetUpdatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2) })
public void receiveCancelUpdateMessageAfterAssignmentWasCanceled() {
void receiveCancelUpdateMessageAfterAssignmentWasCanceled() {
final String controllerId = TARGET_PREFIX + "receiveCancelUpdateMessageAfterAssignmentWasCanceled";
// Setup
@@ -661,11 +594,11 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = CancelTargetAssignmentEvent.class, count = 1),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void actionNotExists() {
void actionNotExists() {
final String controllerId = TARGET_PREFIX + "actionNotExists";
final Long actionId = registerTargetAndCancelActionId(controllerId);
final Long actionNotExist = actionId + 1;
final long actionNotExist = actionId + 1;
createAndSendActionStatusUpdateMessage(controllerId, actionNotExist, DmfActionStatus.CANCELED);
verifyOneDeadLetterMessage();
@@ -680,7 +613,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void canceledRejectedNotAllowActionStatus() {
void canceledRejectedNotAllowActionStatus() {
final String controllerId = TARGET_PREFIX + "canceledRejectedNotAllowActionStatus";
registerTargetAndSendActionStatus(DmfActionStatus.CANCEL_REJECTED, controllerId);
verifyOneDeadLetterMessage();
@@ -696,7 +629,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) })
public void canceledRejectedActionStatus() {
void canceledRejectedActionStatus() {
final String controllerId = TARGET_PREFIX + "canceledRejectedActionStatus";
final Long actionId = registerTargetAndCancelActionId(controllerId);
@@ -709,35 +642,36 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Description("Verify that sending an update controller attribute message to an existing target works. Verify that different update modes (merge, replace, remove) can be used.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 4), @Expect(type = TargetPollEvent.class, count = 1) })
public void updateAttributesWithDifferentUpdateModes() {
void updateAttributesWithDifferentUpdateModes() {
final String controllerId = TARGET_PREFIX + "updateAttributes";
// setup
registerAndAssertTargetWithExistingTenant(controllerId);
// no update mode specified
updateAttributesWithoutUpdateMode(controllerId);
updateAttributesWithoutUpdateMode();
// update mode REPLACE
updateAttributesWithUpdateModeReplace(controllerId);
updateAttributesWithUpdateModeReplace();
// update mode MERGE
updateAttributesWithUpdateModeMerge(controllerId);
updateAttributesWithUpdateModeMerge();
// update mode REMOVE
updateAttributesWithUpdateModeRemove(controllerId);
updateAttributesWithUpdateModeRemove();
}
@Step
private void updateAttributesWithUpdateModeRemove(final String controllerId) {
private void updateAttributesWithUpdateModeRemove() {
// assemble the expected attributes
final Map<String, String> expectedAttributes = targetManagement.getControllerAttributes(controllerId);
final Map<String, String> expectedAttributes = targetManagement
.getControllerAttributes(DMF_ATTR_TEST_CONTROLLER_ID);
expectedAttributes.remove("k1");
expectedAttributes.remove("k3");
// send a update message with update mode
// send an update message with update mode
final Map<String, String> removeAttributes = new HashMap<>();
removeAttributes.put("k1", "foo");
removeAttributes.put("k3", "bar");
@@ -745,19 +679,19 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
final DmfAttributeUpdate remove = new DmfAttributeUpdate();
remove.setMode(DmfUpdateMode.REMOVE);
remove.getAttributes().putAll(removeAttributes);
sendUpdateAttributeMessage(controllerId, TENANT_EXIST, remove);
sendUpdateAttributeMessage(remove);
// validate
assertUpdateAttributes(controllerId, expectedAttributes);
assertUpdateAttributes(DMF_ATTR_TEST_CONTROLLER_ID, expectedAttributes);
}
@Step
private void updateAttributesWithUpdateModeMerge(final String controllerId) {
private void updateAttributesWithUpdateModeMerge() {
// get the current attributes
final Map<String, String> attributes = new HashMap<>(targetManagement.getControllerAttributes(controllerId));
final Map<String, String> attributes = new HashMap<>(
targetManagement.getControllerAttributes(DMF_ATTR_TEST_CONTROLLER_ID));
// send a update message with update mode MERGE
// send an update message with update mode MERGE
final Map<String, String> mergeAttributes = new HashMap<>();
mergeAttributes.put("k1", "v1_modified_again");
mergeAttributes.put("k4", "v4");
@@ -765,19 +699,18 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
final DmfAttributeUpdate merge = new DmfAttributeUpdate();
merge.setMode(DmfUpdateMode.MERGE);
merge.getAttributes().putAll(mergeAttributes);
sendUpdateAttributeMessage(controllerId, TENANT_EXIST, merge);
sendUpdateAttributeMessage(merge);
// validate
final Map<String, String> expectedAttributes = new HashMap<>();
expectedAttributes.putAll(attributes);
expectedAttributes.putAll(mergeAttributes);
assertUpdateAttributes(controllerId, expectedAttributes);
assertUpdateAttributes(DMF_ATTR_TEST_CONTROLLER_ID, expectedAttributes);
}
@Step
private void updateAttributesWithUpdateModeReplace(final String controllerId) {
// send a update message with update mode REPLACE
private void updateAttributesWithUpdateModeReplace() {
// send an update message with update mode REPLACE
final Map<String, String> expectedAttributes = new HashMap<>();
expectedAttributes.put("k1", "v1_modified");
expectedAttributes.put("k2", "v2");
@@ -786,33 +719,32 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
final DmfAttributeUpdate replace = new DmfAttributeUpdate();
replace.setMode(DmfUpdateMode.REPLACE);
replace.getAttributes().putAll(expectedAttributes);
sendUpdateAttributeMessage(controllerId, TENANT_EXIST, replace);
sendUpdateAttributeMessage(replace);
// validate
assertUpdateAttributes(controllerId, expectedAttributes);
assertUpdateAttributes(DMF_ATTR_TEST_CONTROLLER_ID, expectedAttributes);
}
@Step
private void updateAttributesWithoutUpdateMode(final String controllerId) {
// send a update message which does not specify an update mode
private void updateAttributesWithoutUpdateMode() {
// send an update message which does not specify an update mode
final Map<String, String> expectedAttributes = new HashMap<>();
expectedAttributes.put("k0", "v0");
expectedAttributes.put("k1", "v1");
final DmfAttributeUpdate defaultUpdate = new DmfAttributeUpdate();
defaultUpdate.getAttributes().putAll(expectedAttributes);
sendUpdateAttributeMessage(controllerId, TENANT_EXIST, defaultUpdate);
sendUpdateAttributeMessage(defaultUpdate);
// validate
assertUpdateAttributes(controllerId, expectedAttributes);
assertUpdateAttributes(DMF_ATTR_TEST_CONTROLLER_ID, expectedAttributes);
}
@Test
@Description("Verify that sending an update controller attribute message with no thingid header to an existing target does not work.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 0), @Expect(type = TargetPollEvent.class, count = 1) })
public void updateAttributesWithNoThingId() {
@Expect(type = TargetUpdatedEvent.class), @Expect(type = TargetPollEvent.class, count = 1) })
void updateAttributesWithNoThingId() {
final String controllerId = TARGET_PREFIX + "updateAttributesWithNoThingId";
// setup
@@ -836,17 +768,15 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Verify that sending an update controller attribute message with invalid body to an existing target does not work.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 0), @Expect(type = TargetPollEvent.class, count = 1) })
public void updateAttributesWithWrongBody() {
@Expect(type = TargetUpdatedEvent.class), @Expect(type = TargetPollEvent.class, count = 1) })
void updateAttributesWithWrongBody() {
// setup
final String target = "ControllerAttributeTestTarget";
registerAndAssertTargetWithExistingTenant(target);
registerAndAssertTargetWithExistingTenant(UPDATE_ATTR_TEST_CONTROLLER_ID);
final DmfAttributeUpdate controllerAttribute = new DmfAttributeUpdate();
controllerAttribute.getAttributes().put("test1", "testA");
controllerAttribute.getAttributes().put("test2", "testB");
final Message createUpdateAttributesMessageWrongBody = createUpdateAttributesMessageWrongBody(target,
TENANT_EXIST);
final Message createUpdateAttributesMessageWrongBody = createUpdateAttributesMessageWrongBody(
UPDATE_ATTR_TEST_CONTROLLER_ID);
// test
getDmfClient().send(createUpdateAttributesMessageWrongBody);
@@ -857,20 +787,12 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Test
@Description("Verifies that sending an UPDATE_ATTRIBUTES message with invalid attributes is handled correctly.")
public void updateAttributesWithInvalidValues() {
// setup
final String target = "ControllerAttributeTestTarget";
registerAndAssertTargetWithExistingTenant(target);
final String keyTooLong = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_KEY_SIZE + 1);
final String keyValid = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_KEY_SIZE);
final String valueTooLong = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_VALUE_SIZE + 1);
final String valueValid = generateRandomStringWithLength(Target.CONTROLLER_ATTRIBUTE_VALUE_SIZE);
void updateAttributesWithInvalidValues() {
registerAndAssertTargetWithExistingTenant(UPDATE_ATTR_TEST_CONTROLLER_ID);
sendUpdateAttributesMessageWithGivenAttributes(target, keyTooLong, valueValid);
sendUpdateAttributesMessageWithGivenAttributes(target, keyTooLong, valueTooLong);
sendUpdateAttributesMessageWithGivenAttributes(target, keyValid, valueTooLong);
sendUpdateAttributesMessageWithGivenAttributes(TargetTestData.ATTRIBUTE_KEY_TOO_LONG, TargetTestData.ATTRIBUTE_VALUE_VALID);
sendUpdateAttributesMessageWithGivenAttributes(TargetTestData.ATTRIBUTE_KEY_TOO_LONG, TargetTestData.ATTRIBUTE_VALUE_TOO_LONG);
sendUpdateAttributesMessageWithGivenAttributes(TargetTestData.ATTRIBUTE_KEY_VALID, TargetTestData.ATTRIBUTE_VALUE_TOO_LONG);
verifyNumberOfDeadLetterMessages(3);
}
@@ -885,7 +807,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetAttributesRequestedEvent.class, count = 1),
@Expect(type = TargetUpdatedEvent.class, count = 2), @Expect(type = TargetPollEvent.class, count = 1) })
public void downloadOnlyAssignmentFinishesActionWhenTargetReportsDownloaded() throws IOException {
void downloadOnlyAssignmentFinishesActionWhenTargetReportsDownloaded() throws IOException {
// create target
final String controllerId = TARGET_PREFIX + "registerTargets_1";
final DistributionSet distributionSet = createTargetAndDistributionSetAndAssign(controllerId, DOWNLOAD_ONLY);
@@ -895,14 +817,14 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
Mockito.verifyNoInteractions(getDeadletterListener());
// get actionId from Message
final Long actionId = Long.parseLong(getJsonFieldFromBody(message.getBody(), "actionId"));
final long actionId = Long.parseLong(getActionIdFromBody(message.getBody()));
// Send DOWNLOADED message
createAndSendActionStatusUpdateMessage(controllerId, actionId, DmfActionStatus.DOWNLOADED);
assertAction(actionId, 1, Status.RUNNING, Status.DOWNLOADED);
Mockito.verifyNoInteractions(getDeadletterListener());
verifyAssignedDsAndInstalledDs(controllerId, distributionSet.getId(), null);
verifyAssignedDsAndInstalledDs(distributionSet.getId(), null);
}
@Test
@@ -915,9 +837,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = TargetAttributesRequestedEvent.class, count = 2),
@Expect(type = TargetUpdatedEvent.class, count = 3), @Expect(type = TargetPollEvent.class, count = 1) })
public void downloadOnlyAssignmentAllowsActionStatusUpdatesWhenTargetReportsFinishedAndUpdatesInstalledDS()
void downloadOnlyAssignmentAllowsActionStatusUpdatesWhenTargetReportsFinishedAndUpdatesInstalledDS()
throws IOException {
// create target
final String controllerId = TARGET_PREFIX + "registerTargets_1";
final DistributionSet distributionSet = createTargetAndDistributionSetAndAssign(controllerId, DOWNLOAD_ONLY);
@@ -927,29 +848,28 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
Mockito.verifyNoInteractions(getDeadletterListener());
// get actionId from Message
final Long actionId = Long.parseLong(getJsonFieldFromBody(message.getBody(), "actionId"));
final long actionId = Long.parseLong(getActionIdFromBody(message.getBody()));
// Send DOWNLOADED message, should result in the action being closed
createAndSendActionStatusUpdateMessage(controllerId, actionId, DmfActionStatus.DOWNLOADED);
assertAction(actionId, 1, Status.RUNNING, Status.DOWNLOADED);
Mockito.verifyNoInteractions(getDeadletterListener());
verifyAssignedDsAndInstalledDs(controllerId, distributionSet.getId(), null);
verifyAssignedDsAndInstalledDs(distributionSet.getId(), null);
// Send FINISHED message
createAndSendActionStatusUpdateMessage(controllerId, actionId, DmfActionStatus.FINISHED);
assertAction(actionId, 2, Status.RUNNING, Status.DOWNLOADED, Status.FINISHED);
Mockito.verifyNoInteractions(getDeadletterListener());
verifyAssignedDsAndInstalledDs(controllerId, distributionSet.getId(), distributionSet.getId());
verifyAssignedDsAndInstalledDs(distributionSet.getId(), distributionSet.getId());
}
@Test
@Description("Messages that result into certain exceptions being raised should not be requeued. This message should forwarded to the deadletter queue")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
public void ignoredExceptionTypesShouldNotBeRequeued() {
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
void ignoredExceptionTypesShouldNotBeRequeued() {
final ControllerManagement mockedControllerManagement = Mockito.mock(ControllerManagement.class);
final List<Class<? extends RuntimeException>> exceptionsThatShouldNotBeRequeued = Arrays
.asList(IllegalArgumentException.class, EntityAlreadyExistsException.class);
final String controllerId = "dummy_target";
@@ -960,7 +880,7 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
.findOrRegisterTargetIfItDoesNotExist(eq(controllerId), any());
amqpMessageHandlerService.setControllerManagement(mockedControllerManagement);
createAndSendThingCreated(controllerId, TENANT_EXIST);
createAndSendThingCreated(controllerId);
verifyOneDeadLetterMessage();
assertThat(targetManagement.getByControllerID(controllerId)).isEmpty();
}
@@ -970,9 +890,8 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
}
@Step
private void verifyAssignedDsAndInstalledDs(final String controllerId, final Long assignedDsId,
final Long installedDsId) {
final Optional<Target> target = controllerManagement.getByControllerId(controllerId);
private void verifyAssignedDsAndInstalledDs(final Long assignedDsId, final Long installedDsId) {
final Optional<Target> target = controllerManagement.getByControllerId(DMF_REGISTER_TEST_CONTROLLER_ID);
assertThat(target).isPresent();
// verify the DS was assigned to the Target
@@ -989,11 +908,11 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
}
}
private void sendUpdateAttributesMessageWithGivenAttributes(final String target, final String key,
final String value) {
private void sendUpdateAttributesMessageWithGivenAttributes(final String key, final String value) {
final DmfAttributeUpdate controllerAttribute = new DmfAttributeUpdate();
controllerAttribute.getAttributes().put(key, value);
final Message message = createUpdateAttributesMessage(target, TENANT_EXIST, controllerAttribute);
final Message message = createUpdateAttributesMessage(UPDATE_ATTR_TEST_CONTROLLER_ID, TENANT_EXIST,
controllerAttribute);
getDmfClient().send(message);
}
@@ -1041,9 +960,9 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
});
}
private void sendUpdateAttributeMessage(final String target, final String tenant,
final DmfAttributeUpdate attributeUpdate) {
final Message updateMessage = createUpdateAttributesMessage(target, tenant, attributeUpdate);
private void sendUpdateAttributeMessage(final DmfAttributeUpdate attributeUpdate) {
final Message updateMessage = createUpdateAttributesMessage(DMF_ATTR_TEST_CONTROLLER_ID,
AbstractAmqpServiceIntegrationTest.TENANT_EXIST, attributeUpdate);
getDmfClient().send(updateMessage);
}
@@ -1068,10 +987,10 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AbstractAmqpServic
Mockito.reset(getDeadletterListener());
}
private static String getJsonFieldFromBody(final byte[] body, final String fieldName) throws IOException {
private static String getActionIdFromBody(final byte[] body) throws IOException {
final ObjectMapper objectMapper = new ObjectMapper();
final ObjectNode node = objectMapper.readValue(new String(body, Charset.defaultCharset()), ObjectNode.class);
assertThat(node.has(fieldName)).isTrue();
return node.get(fieldName).asText();
assertThat(node.has("actionId")).isTrue();
return node.get("actionId").asText();
}
}