11201265d1ea8dea2f902b832e78244f8492cc9f
[ccsdk/sli.git] /
1 /*
2  * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
3  * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
4  * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
5  * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
6  * Vestibulum commodo. Ut rhoncus gravida arcu.
7  */
8
9 package org.onap.ccsdk.sli.northbound.dmaapclient;
10
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.lang.reflect.Field;
17 import java.util.Map;
18 import java.util.Properties;
19
20 import org.apache.commons.io.FileUtils;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27
28 public class TestSdncPserverDmaapReceiver {
29     private static final String aaiInput = "{\r\n" + 
30             "    \"cambria.partition\": \"AAI\",\r\n" + 
31             "    \"event-header\": {\r\n" + 
32             "        \"severity\": \"NORMAL\",\r\n" + 
33             "        \"entity-type\": \"pserver\",\r\n" + 
34             "        \"top-entity-type\": \"pserver\",\r\n" + 
35             "        \"entity-link\": \"https://aai.com:8443/aai/v11/cloud-infrastructure/pservers/pserver/a3d3d3d3/\",\r\n" + 
36             "        \"event-type\": \"AAI-EVENT\",\r\n" + 
37             "        \"domain\": \"e2e\",\r\n" + 
38             "        \"action\": \"UPDATE\",\r\n" + 
39             "        \"sequence-number\": \"0\",\r\n" + 
40             "        \"id\": \"20170415000111-1234\",\r\n" + 
41             "        \"source-name\": \"testclient\",\r\n" + 
42             "        \"version\": \"v11\",\r\n" + 
43             "        \"timestamp\": \"20170415-00:01:11:979\"\r\n" + 
44             "    },\r\n" + 
45             "    \"entity\": {\r\n" + 
46             "        \"hostname\": \"host1\",\r\n" + 
47             "        \"ptnii-equip-name\": \"lat111\",\r\n" + 
48             "        \"equip-type\": \"server\",\r\n" + 
49             "        \"equip-vendor\": \"HP\",\r\n" + 
50             "        \"equip-model\": \"model1\",\r\n" + 
51             "        \"fqdn\": \"l.global.net\",\r\n" + 
52             "        \"ipv4-oam-address\": \"12.12.12.12\",\r\n" + 
53             "        \"in-maint\": false,\r\n" + 
54             "        \"resource-version\": \"11111111111\",\r\n" + 
55             "        \"purpose\": \"Gamma\",\r\n" + 
56             "        \"relationship-list\": {\r\n" + 
57             "            \"relationship\": [\r\n" + 
58             "                {\r\n" + 
59             "                    \"related-to\": \"complex\",\r\n" + 
60             "                    \"relationship-data\": [\r\n" + 
61             "                        {\r\n" + 
62             "                            \"relationship-value\": \"L1L2L3\",\r\n" + 
63             "                            \"relationship-key\": \"complex.physical-location-id\"\r\n" + 
64             "                        }\r\n" + 
65             "                    ],\r\n" + 
66             "                    \"related-link\": \"https://aai.com:8443/aai/v11/cloud-infrastructure/complexes/complex/cmpl1\"\r\n" + 
67             "                }\r\n" + 
68             "            ]\r\n" + 
69             "        },\r\n" + 
70             "        \"p-interfaces\": {\r\n" + 
71             "            \"p-interface\": []\r\n" + 
72             "        },\r\n" + 
73             "        \"lag-interfaces\": {\r\n" + 
74             "            \"lag-interface\": []\r\n" + 
75             "        }\r\n" + 
76             "    }\r\n" + 
77             "}";
78     
79         @Before
80         public void setUp() throws Exception {
81         }
82
83         @Test
84         public void test() throws Exception {
85                 Properties props = new Properties();
86  
87             String rpcMsgbody = new SdncAaiDmaapConsumer(props).publish("src/main/resources/template-pserver.vt", aaiInput);
88         
89             ObjectMapper oMapper = new ObjectMapper();
90         JsonNode aaiRootNode;
91         try {
92             aaiRootNode = oMapper.readTree(rpcMsgbody);
93         } catch (Exception e) {
94             throw new InvalidMessageException("Cannot parse json object", e);
95         }       
96
97         assertTrue(aaiRootNode.get("input").get("payload") != null); 
98         assertTrue(aaiRootNode.get("input").get("common-header") != null); 
99         
100         }
101
102         @Test
103         public void testProcessMsgFieldMap() throws Exception {
104
105
106                 String DMAAPLISTENERROOT = "DMAAPLISTENERROOT";
107                 File directory = new File("lib");
108
109                 if (! directory.exists()){
110                         directory.mkdir();
111                 }
112
113                 File source = new File("src/main/resources");
114                 File dest = new File("lib/");
115                 try {
116                         FileUtils.copyDirectory(source, dest);
117                 } catch (IOException e) {
118                         e.printStackTrace();
119                 }
120
121                 try {
122                         Map<String, String> env = System.getenv();
123                         Class<?> cl = env.getClass();
124                         Field field = cl.getDeclaredField("m");
125                         field.setAccessible(true);
126                         Map<String, String> writableEnv = (Map<String, String>) field.get(env);
127                         writableEnv.put(DMAAPLISTENERROOT, ".");
128                 } catch (Exception e) {
129                         throw new IllegalStateException("Failed to set environment variable", e);
130                 }
131                 Properties props = new Properties();
132
133                 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
134
135                 InputStream propStr = TestSdncPserverDmaapReceiver.class.getResourceAsStream("/dmaap-consumer-pserver.properties");
136
137
138                 props.load(propStr);
139
140                 consumer.init(props, "src/test/resources/dmaap-consumer-pserver.properties");
141                 consumer.processMsg(aaiInput);
142         }
143
144         @Test(expected = InvalidMessageException.class)
145         public void testProcessMsgNullMessage() throws Exception {
146                 Properties props = new Properties();
147
148                 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
149                 consumer.processMsg(null);
150         }
151
152         @Test(expected = InvalidMessageException.class)
153         public void testProcessMsgInvalidMessage() throws Exception {
154                 Properties props = new Properties();
155
156                 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
157                 consumer.processMsg("test");
158         }
159
160         @Test
161         public void testProcessMsgMissingEventHeader() throws Exception {
162                 Properties props = new Properties();
163
164                 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
165                 consumer.processMsg("{\n" +
166                                 "    \"input\" : {        \n" +
167                                 "    }\n" +
168                                 "}");
169         }
170
171         @Test
172         public void testProcessMsgInvalidEventType() throws Exception {
173                 Properties props = new Properties();
174
175                 String msg = "{\"cambria.partition\": \"AAI\",\r\n" +
176                                 "    \"event-header\": {\"event-type\": \"TEST-EVENT\"}}";
177
178                 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
179                 consumer.processMsg(msg);
180         }
181
182
183
184
185
186 }