From 6c74c1186dbd95bc57ac1661ffc3e863d81c1b9f Mon Sep 17 00:00:00 2001 From: SirWayne Date: Tue, 8 Mar 2016 12:23:31 +0100 Subject: [PATCH] Create unit test for the json amqp message conversion Signed-off-by: SirWayne --- .../eclipse/hawkbit/amqp/BaseAmqpService.java | 8 +- .../amqp/AmqpMessageHandlerServiceTest.java | 1 + .../hawkbit/amqp/BaseAmqpServiceTest.java | 103 ++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/BaseAmqpServiceTest.java diff --git a/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java b/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java index 897f1ae62..8a054165b 100644 --- a/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java +++ b/hawkbit-dmf-amqp/src/main/java/org/eclipse/hawkbit/amqp/BaseAmqpService.java @@ -58,8 +58,8 @@ public class BaseAmqpService { * @return the converted object */ @SuppressWarnings("unchecked") - protected T convertMessage(final Message message, final Class clazz) { - if (message == null) { + public T convertMessage(final Message message, final Class clazz) { + if (message == null || message.getBody() == null) { return null; } message.getMessageProperties().getHeaders().put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME, @@ -78,8 +78,8 @@ public class BaseAmqpService { * @return the list of converted objects */ @SuppressWarnings("unchecked") - protected List convertMessageList(final Message message, final Class clazz) { - if (message == null) { + public List convertMessageList(final Message message, final Class clazz) { + if (message == null || message.getBody() == null) { return Collections.emptyList(); } message.getMessageProperties().getHeaders().put(AbstractJavaTypeMapper.DEFAULT_CLASSID_FIELD_NAME, diff --git a/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerServiceTest.java b/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerServiceTest.java index 79a39a40a..9d6ae3ba7 100644 --- a/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerServiceTest.java +++ b/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/AmqpMessageHandlerServiceTest.java @@ -116,6 +116,7 @@ public class AmqpMessageHandlerServiceTest { } + @Test @Description("Tests not allowed content-type in message") public void testWrongContentType() { final MessageProperties messageProperties = new MessageProperties(); diff --git a/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/BaseAmqpServiceTest.java b/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/BaseAmqpServiceTest.java new file mode 100644 index 000000000..0bd8c164b --- /dev/null +++ b/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/amqp/BaseAmqpServiceTest.java @@ -0,0 +1,103 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.amqp; + +import static org.fest.assertions.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.hawkbit.dmf.json.model.ActionUpdateStatus; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.core.MessageProperties; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; + +import ru.yandex.qatools.allure.annotations.Description; +import ru.yandex.qatools.allure.annotations.Features; +import ru.yandex.qatools.allure.annotations.Stories; + +@RunWith(MockitoJUnitRunner.class) +@Features("Component Tests - Device Management Federation API") +@Stories("Base Amqp Service Test") +public class BaseAmqpServiceTest { + + @Mock + private RabbitTemplate rabbitTemplate; + + private BaseAmqpService baseAmqpService; + + @Before + public void setup() { + when(rabbitTemplate.getMessageConverter()).thenReturn(new Jackson2JsonMessageConverter()); + baseAmqpService = new BaseAmqpService(rabbitTemplate); + + } + + @Test + @Description("Verify that the message conversion works") + public void convertMessageTest() { + final ActionUpdateStatus actionUpdateStatus = new ActionUpdateStatus(); + actionUpdateStatus.setActionId(1L); + actionUpdateStatus.setSoftwareModuleId(2L); + + final Message message = rabbitTemplate.getMessageConverter().toMessage(actionUpdateStatus, + new MessageProperties()); + ActionUpdateStatus convertedActionUpdateStatus = baseAmqpService.convertMessage(message, + ActionUpdateStatus.class); + + assertThat(convertedActionUpdateStatus).as("Converted Action Status is wrong") + .isEqualsToByComparingFields(actionUpdateStatus); + + convertedActionUpdateStatus = baseAmqpService.convertMessage(null, ActionUpdateStatus.class); + assertThat(convertedActionUpdateStatus).as("Converted Object should be null when message is null").isNull(); + + convertedActionUpdateStatus = baseAmqpService.convertMessage(new Message(null, new MessageProperties()), + ActionUpdateStatus.class); + assertThat(convertedActionUpdateStatus).as("Converted Object should be null when message body is null") + .isNull(); + } + + @Test + @Description("Verify that a conversion of a list from a message works") + public void convertMessageListTest() { + final List actionUpdateStatusList = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + final ActionUpdateStatus actionUpdateStatus = new ActionUpdateStatus(); + actionUpdateStatus.setActionId(Long.valueOf(i)); + actionUpdateStatus.setSoftwareModuleId(Long.valueOf(i)); + actionUpdateStatusList.add(actionUpdateStatus); + } + + final Message message = rabbitTemplate.getMessageConverter().toMessage(actionUpdateStatusList, + new MessageProperties()); + List convertedActionUpdateStatus = baseAmqpService.convertMessageList(message, + ActionUpdateStatus.class); + + assertThat(convertedActionUpdateStatus).as("Converted Action Status list is wrong") + .hasSameClassAs(actionUpdateStatusList); + assertThat(convertedActionUpdateStatus).as("Converted Action Status list is wrong") + .hasSameSizeAs(actionUpdateStatusList); + + convertedActionUpdateStatus = baseAmqpService.convertMessageList(null, ActionUpdateStatus.class); + assertThat(convertedActionUpdateStatus).as("Converted list should be empty when message is null").isEmpty(); + + convertedActionUpdateStatus = baseAmqpService.convertMessageList(new Message(null, new MessageProperties()), + ActionUpdateStatus.class); + assertThat(convertedActionUpdateStatus).as("Converted list should be empty when message body is null") + .isEmpty(); + } + +}