8c3a839c2702e5198168b6c2161e705b2d56dc9f
[ccsdk/sli.git] /
1 /**
2  *
3  */
4 package org.onap.ccsdk.sli.northbound.dmaapclient;
5
6 import static org.junit.Assert.*;
7
8 import java.io.InputStream;
9 import java.net.URL;
10 import java.sql.SQLException;
11 import java.util.Properties;
12
13 import javax.sql.rowset.CachedRowSet;
14
15 import org.junit.After;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import ch.vorburger.mariadb4j.DB;
23 import ch.vorburger.mariadb4j.DBConfigurationBuilder;
24
25 /**
26  * @author dt5972
27  *
28  */
29 public class TestSdncDhcpEventConsumer {
30
31         private static final Logger LOG = LoggerFactory.getLogger(TestSdncDhcpEventConsumer.class);
32
33
34         private static final String DHCP_MAP_TABLE = "CREATE TABLE `DHCP_MAP` (\n" +
35                         " mac_addr varchar(80) NOT NULL,\n" +
36                         " ip_addr varchar(80),\n" +
37                         " PRIMARY KEY(`mac_addr`)\n" +
38                         ")";
39
40
41
42         private static final String VALID_DHCP_EVENT =  "{\"msg_name\":\"DHCPACK\"," +
43                                                                                                      "\"macaddr\":\"fa:16:3e:8f:ea:68\"," +
44                                                                                                      "\"yiaddr\":\"10.3.0.2\"}";
45         private static final String MISSING_MSG_NAME_DHCP_EVENT =  "{\"macaddr\":\"fa:16:3e:8f:ea:68\"," +
46                                                                                                               "\"yiaddr\":\"10.3.0.2\"}";
47         private static final String MISSING_MAC_ADDR_DHCP_EVENT =   "{\"msg_name\":\"DHCPACK\"," +
48                                                                                                                         "\"yiaddr\":\"10.3.0.2\"}";
49         private static final String MISSING_IP_ADDR_DHCP_EVENT =  "{\"msg_name\":\"DHCPACK\"," +
50                                                                                                                 "\"macaddr\":\"fa:16:3e:8f:ea:68\"}";
51
52         private static final String GET_DHCP_MAPPING = "SELECT * FROM DHCP_MAP WHERE mac_addr = 'fa:16:3e:8f:ea:68'";
53
54
55         private static DBResourceManager dblibSvc;
56         private static DB db;
57
58         private static SdncDhcpEventConsumer consumer;
59
60         /**
61          * @throws java.lang.Exception
62          */
63         @BeforeClass
64         public static void setUpBeforeClass() throws Exception {
65
66                 LOG.debug("Setting up DHCP event testing");
67
68                 InputStream propStr = TestSdncDhcpEventConsumer.class.getResourceAsStream("/dblib.properties");
69
70                 Properties props = new Properties();
71
72                 props.load(propStr);
73
74
75                 // Start MariaDB4j database
76
77                 LOG.debug("Starting MariaDB instance");
78                 DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
79                 config.setPort(0); // 0 => autom. detect free port
80                 db = DB.newEmbeddedDB(config.build());
81                 db.start();
82
83
84                 // Override jdbc URL and database name
85                 props.setProperty("org.onap.ccsdk.sli.jdbc.database", "test");
86                 props.setProperty("org.onap.ccsdk.sli.jdbc.url", config.getURL("test"));
87
88
89                 // Create dblib connection
90
91                 LOG.debug("Getting DBResourceManager instance");
92                 dblibSvc = new DBResourceManager(props);
93
94                 // Create DHCP_MAP table
95                 dblibSvc.writeData(DHCP_MAP_TABLE, null, null);
96
97                 consumer = new SdncDhcpEventConsumer();
98                 consumer.setJdbcDataSource(dblibSvc);
99                 LOG.debug("Setup complete");
100
101         }
102
103
104         @Test
105         public void testValid() throws InvalidMessageException, SQLException {
106                 consumer.processMsg(VALID_DHCP_EVENT);
107
108                 CachedRowSet results = dblibSvc.getData(GET_DHCP_MAPPING, null, null);
109
110                 if (!results.next()) {
111                         fail("Test query ["+GET_DHCP_MAPPING+"] returned no data");
112                 }
113
114         }
115
116         @Test (expected = InvalidMessageException.class)
117         public void testMissingMsgName() throws InvalidMessageException {
118                 consumer.processMsg(MISSING_MSG_NAME_DHCP_EVENT);
119         }
120
121         @Test (expected = InvalidMessageException.class)
122         public void testMissingMacAddress() throws InvalidMessageException {
123                 consumer.processMsg(MISSING_MAC_ADDR_DHCP_EVENT);
124         }
125
126         @Test (expected = InvalidMessageException.class)
127         public void testMissingIpAddress() throws InvalidMessageException {
128                 consumer.processMsg(MISSING_IP_ADDR_DHCP_EVENT);
129         }
130 }