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.
9 package org.onap.ccsdk.sli.northbound.dmaapclient;
11 import static org.junit.Assert.assertTrue;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.lang.reflect.Field;
18 import java.util.Properties;
20 import org.apache.commons.io.FileUtils;
22 import org.junit.Before;
23 import org.junit.Test;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
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" +
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" +
59 " \"related-to\": \"complex\",\r\n" +
60 " \"relationship-data\": [\r\n" +
62 " \"relationship-value\": \"L1L2L3\",\r\n" +
63 " \"relationship-key\": \"complex.physical-location-id\"\r\n" +
66 " \"related-link\": \"https://aai.com:8443/aai/v11/cloud-infrastructure/complexes/complex/cmpl1\"\r\n" +
70 " \"p-interfaces\": {\r\n" +
71 " \"p-interface\": []\r\n" +
73 " \"lag-interfaces\": {\r\n" +
74 " \"lag-interface\": []\r\n" +
80 public void setUp() throws Exception {
84 public void test() throws Exception {
85 Properties props = new Properties();
87 String rpcMsgbody = new SdncAaiDmaapConsumer(props).publish("src/main/resources/template-pserver.vt", aaiInput);
89 ObjectMapper oMapper = new ObjectMapper();
92 aaiRootNode = oMapper.readTree(rpcMsgbody);
93 } catch (Exception e) {
94 throw new InvalidMessageException("Cannot parse json object", e);
97 assertTrue(aaiRootNode.get("input").get("payload") != null);
98 assertTrue(aaiRootNode.get("input").get("common-header") != null);
103 public void testProcessMsgFieldMap() throws Exception {
106 String DMAAPLISTENERROOT = "DMAAPLISTENERROOT";
107 File directory = new File("lib");
109 if (! directory.exists()){
113 File source = new File("src/main/resources");
114 File dest = new File("lib/");
116 FileUtils.copyDirectory(source, dest);
117 } catch (IOException e) {
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);
131 Properties props = new Properties();
133 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
135 InputStream propStr = TestSdncPserverDmaapReceiver.class.getResourceAsStream("/dmaap-consumer-pserver.properties");
140 consumer.init(props, "src/test/resources/dmaap-consumer-pserver.properties");
141 consumer.processMsg(aaiInput);
144 @Test(expected = InvalidMessageException.class)
145 public void testProcessMsgNullMessage() throws Exception {
146 Properties props = new Properties();
148 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
149 consumer.processMsg(null);
152 @Test(expected = InvalidMessageException.class)
153 public void testProcessMsgInvalidMessage() throws Exception {
154 Properties props = new Properties();
156 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
157 consumer.processMsg("test");
161 public void testProcessMsgMissingEventHeader() throws Exception {
162 Properties props = new Properties();
164 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
165 consumer.processMsg("{\n" +
166 " \"input\" : { \n" +
172 public void testProcessMsgInvalidEventType() throws Exception {
173 Properties props = new Properties();
175 String msg = "{\"cambria.partition\": \"AAI\",\r\n" +
176 " \"event-header\": {\"event-type\": \"TEST-EVENT\"}}";
178 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
179 consumer.processMsg(msg);