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