Skip to content

Commit

Permalink
Added Junit tests for broker module
Browse files Browse the repository at this point in the history
Added Junit test for classes in:

broker/api module:
BrokerDomain class
BrokerDomains class

broker/core/message package:
CamelKapuaMessage class
CamelUtil class
MessageConstants class
DefaultSystemMessageCreator class

broker/core/plugin/metric package:
ClientMetric class
LoginMetric class
PublishMetric class
SecurityMetrics class
SubscribeMetric class

Signed-off-by: Sonja <sonja.matic@endava.com>
  • Loading branch information
sonja-ct committed Jan 14, 2021
1 parent c90a699 commit e485540
Show file tree
Hide file tree
Showing 13 changed files with 694 additions and 0 deletions.
10 changes: 10 additions & 0 deletions broker/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-qa-markers</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.broker;

import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(JUnitTests.class)
public class BrokerDomainTest extends Assert {

BrokerDomain brokerDomain;
BrokerDomain secondBrokerDomain;
Object[] objects;

@Before
public void initialize() {
brokerDomain = new BrokerDomain();
secondBrokerDomain = new BrokerDomain();
objects = new Object[]{0, 10, 100000, "String", 'c', -10, -1000000000, -100000000000L, 10L, 10.0f, null, 10.10d, true, false};
}

@Test
public void getNameTest() {
assertEquals("Expected and actual values should be the same.", "broker", brokerDomain.getName());
}

@Test
public void getActionsTest() {
assertEquals("Expected and actual values should be the same.", "[connect]", brokerDomain.getActions().toString());
}

@Test
public void getGroupableTest() {
assertFalse("False expected.", brokerDomain.getGroupable());
}

@Test
public void equalsTest() {
assertTrue("True expected.", brokerDomain.equals(secondBrokerDomain));
for (Object object : objects) {
assertFalse("False expected.", brokerDomain.equals(object));
}
}

@Test
public void hashCodeTest() {
assertNotNull("NotNull expected.", brokerDomain.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.broker;

import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

@Category(JUnitTests.class)
public class BrokerDomainsTest extends Assert {

@Test
public void brokerDomainsTest() throws Exception {
Constructor<BrokerDomains> brokerDomains = BrokerDomains.class.getDeclaredConstructor();
assertTrue("True expected.", Modifier.isPrivate(brokerDomains.getModifiers()));
brokerDomains.setAccessible(true);
brokerDomains.newInstance();
}

@Test
public void brokerDomainTest() {
BrokerDomain brokerDomain = BrokerDomains.BROKER_DOMAIN;
assertNotNull("NotNull expected.", brokerDomain);
}
}
5 changes: 5 additions & 0 deletions broker/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@
<artifactId>kapua-locator-guice</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.broker.core.message;

import org.eclipse.kapua.broker.core.plugin.ConnectorDescriptor;
import org.eclipse.kapua.message.KapuaMessage;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;

@Category(JUnitTests.class)
public class CamelKapuaMessageTest extends Assert {

CamelKapuaMessage camelKapuaMessage;

@Before
public void initialize() {
camelKapuaMessage = new CamelKapuaMessage(null, null, null);
}

@Test
public void setAndGetMessageTest() {
KapuaMessage kapuaMessage = Mockito.mock(KapuaMessage.class);

assertNull("Null expected.", camelKapuaMessage.getMessage());
camelKapuaMessage.setMessage(kapuaMessage);
assertEquals("Expected and actual values should be the same.", kapuaMessage, camelKapuaMessage.getMessage());
camelKapuaMessage.setMessage(null);
assertNull("Null expected.", camelKapuaMessage.getMessage());
}

@Test
public void setAndGetConnectionIdTest() {
KapuaId connectionId = KapuaId.ONE;

assertNull("Null expected.", camelKapuaMessage.getConnectionId());
camelKapuaMessage.setConnectionId(connectionId);
assertEquals("Expected and actual values should be the same.", connectionId, camelKapuaMessage.getConnectionId());
camelKapuaMessage.setConnectionId(null);
assertNull("Null expected.", camelKapuaMessage.getConnectionId());
}

@Test
public void setAndGetConnectorDescriptorTest() {
ConnectorDescriptor descriptor = Mockito.mock(ConnectorDescriptor.class);

assertNull("Null expected.", camelKapuaMessage.getConnectorDescriptor());
camelKapuaMessage.setConnectorDescriptor(descriptor);
assertEquals("Expected and actual values should be the same.", descriptor, camelKapuaMessage.getConnectorDescriptor());
camelKapuaMessage.setConnectorDescriptor(null);
assertNull("Null expected.", camelKapuaMessage.getConnectorDescriptor());
}

@Test
public void setAndGetDatastoreIdTest() {
String[] dataStoreIds = {null, "", "1", "awsd", "123123123", "-123", "ASDa12$%~", "~ˇ#$%&/()=?*\\", "DSUrCucsvb2qUwbdD5yyiCC5v1wwF5FmBIcWa2oXafNw5bqV2RAcyyN0gCHQTdL2JedME5A4PXsXt7iHekhys52GZVmiCNcA065RxFEsDasCcaH1dzeRioRvA14NoJPGmScHdHf8Mfzv03vWrs7n2W59f1In9NdUtnaxY1Wp5TjknB6X8U5ZRczLntQZNX3MSu5f4OzF29oTtuDcuPIUal6OBTKnm8qLVhsiu3oMK3YnFhoHuiATBk3Pl0q9LaL"};

assertNull("Null expected.", camelKapuaMessage.getDatastoreId());
for (String id : dataStoreIds) {
camelKapuaMessage.setDatastoreId(id);
assertEquals("Expected and actual values should be the same.", id, camelKapuaMessage.getDatastoreId());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.broker.core.message;

import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.camel.impl.DefaultMessage;
import org.eclipse.kapua.broker.core.listener.CamelConstants;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;

import javax.jms.JMSException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

@Category(JUnitTests.class)
public class CamelUtilTest extends Assert {

@Test
public void camelUtilTest() throws Exception {
Constructor<CamelUtil> camelUtil = CamelUtil.class.getDeclaredConstructor();
assertTrue("True expected.", Modifier.isPrivate(camelUtil.getModifiers()));
camelUtil.setAccessible(true);
camelUtil.newInstance();
}

@Test(expected = NullPointerException.class)
public void getTopicNullTest() throws JMSException {
CamelUtil.getTopic(null);
}

@Test
public void getTopicTest() throws JMSException {
org.apache.camel.Message message = new DefaultMessage();
Map<String, Object> map = new HashMap();
map.put("originalTopic", "testTopic");
message.setHeaders(map);
String topic = CamelUtil.getTopic(message);
assertEquals("Expected and actual values should be the same.", "testTopic", topic);
}

@Test
public void getTopicWithActiveMQTopicTest() throws JMSException {
org.apache.camel.Message message = new DefaultMessage();
Map<String, Object> map = new HashMap();
map.put(CamelConstants.JMS_HEADER_DESTINATION, new ActiveMQTopic("VirtualTopic.Topic"));
message.setHeaders(map);
String topic = CamelUtil.getTopic(message);
assertEquals("Expected and actual values should be the same.", "Topic", topic);
}

@Test
public void getTopicWithActiveMQDestinationTest() {
org.apache.camel.Message message = new DefaultMessage();
Map<String, Object> map = new HashMap();
map.put(MessageConstants.PROPERTY_ORIGINAL_TOPIC, null);
map.put(CamelConstants.JMS_HEADER_DESTINATION, Mockito.mock(ActiveMQDestination.class));
message.setHeaders(map);
try {
CamelUtil.getTopic(message);
fail("JMSException expected.");
} catch (Exception e) {
assertTrue("True expected.", e.getMessage().contains("Unable to extract the destination. Wrong destination Mock for ActiveMQDestination"));
}
}

@Test
public void getTopicNullDestinationTest() {
org.apache.camel.Message message = new DefaultMessage();
Map<String, Object> map = new HashMap();
map.put(MessageConstants.PROPERTY_ORIGINAL_TOPIC, null);
map.put(CamelConstants.JMS_HEADER_DESTINATION, null);
message.setHeaders(map);
try {
CamelUtil.getTopic(message);
fail("JMSException expected.");
} catch (Exception e) {
assertEquals("Expected and actual values should be the same.", new JMSException("Unable to extract the destination. Wrong destination null").toString(), e.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.broker.core.message;

import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

@Category(JUnitTests.class)
public class MessageConstantsTest extends Assert {

@Test
public void messageConstantsTest() throws Exception {
Constructor<MessageConstants> messageConstants = MessageConstants.class.getDeclaredConstructor();
assertTrue("True expected.", Modifier.isPrivate(messageConstants.getModifiers()));
messageConstants.setAccessible(true);
messageConstants.newInstance();
}

@Test
public void constantsTest() {
assertEquals("Expected and actual values should be the same.", "username", MessageConstants.METRIC_USERNAME);
assertEquals("Expected and actual values should be the same.", "account", MessageConstants.METRIC_ACCOUNT);
assertEquals("Expected and actual values should be the same.", "clientId", MessageConstants.METRIC_CLIENT_ID);
assertEquals("Expected and actual values should be the same.", "ip", MessageConstants.METRIC_IP);
assertEquals("Expected and actual values should be the same.", "originalTopic", MessageConstants.PROPERTY_ORIGINAL_TOPIC);
assertEquals("Expected and actual values should be the same.", "enqueuedTimestamp", MessageConstants.PROPERTY_ENQUEUED_TIMESTAMP);
assertEquals("Expected and actual values should be the same.", "brokerId", MessageConstants.PROPERTY_BROKER_ID);
assertEquals("Expected and actual values should be the same.", "clientId", MessageConstants.PROPERTY_CLIENT_ID);
assertEquals("Expected and actual values should be the same.", "scopeId", MessageConstants.PROPERTY_SCOPE_ID);
assertEquals("Expected and actual values should be the same.", "userId", MessageConstants.METRIC_USER_ID);
assertEquals("Expected and actual values should be the same.", "nodeId", MessageConstants.METRIC_NODE_ID);
assertEquals("Expected and actual values should be the same.", "KAPUA_CONNECTION_ID", MessageConstants.HEADER_KAPUA_CONNECTION_ID);
assertEquals("Expected and actual values should be the same.", "KAPUA_RECEIVED_TIMESTAMP", MessageConstants.HEADER_KAPUA_RECEIVED_TIMESTAMP);
assertEquals("Expected and actual values should be the same.", "KAPUA_CLIENT_ID", MessageConstants.HEADER_KAPUA_CLIENT_ID);
assertEquals("Expected and actual values should be the same.", "KAPUA_DEVICE_PROTOCOL", MessageConstants.HEADER_KAPUA_CONNECTOR_DEVICE_PROTOCOL);
assertEquals("Expected and actual values should be the same.", "KAPUA_SESSION", MessageConstants.HEADER_KAPUA_SESSION);
assertEquals("Expected and actual values should be the same.", "KAPUA_BROKER_CONTEXT", MessageConstants.HEADER_KAPUA_BROKER_CONTEXT);
assertEquals("Expected and actual values should be the same.", "KAPUA_PROCESSING_EXCEPTION", MessageConstants.HEADER_KAPUA_PROCESSING_EXCEPTION);
}
}
Loading

0 comments on commit e485540

Please sign in to comment.