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;
12 import static org.junit.Assert.assertEquals;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.lang.reflect.Field;
19 import java.util.Properties;
21 import org.apache.commons.io.FileUtils;
23 import org.junit.Before;
24 import org.junit.Test;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
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" +
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" +
60 " \"related-to\": \"complex\",\r\n" +
61 " \"relationship-data\": [\r\n" +
63 " \"relationship-value\": \"L1L2L3\",\r\n" +
64 " \"relationship-key\": \"complex.physical-location-id\"\r\n" +
67 " \"related-link\": \"https://aai.com:8443/aai/v11/cloud-infrastructure/complexes/complex/cmpl1\"\r\n" +
71 " \"p-interfaces\": {\r\n" +
72 " \"p-interface\": []\r\n" +
74 " \"lag-interfaces\": {\r\n" +
75 " \"lag-interface\": []\r\n" +
81 public void setUp() throws Exception {
85 public void test() throws Exception {
86 Properties props = new Properties();
88 String rpcMsgbody = new SdncAaiDmaapConsumer(props).publish("src/main/resources/template-pserver.vt", aaiInput);
90 ObjectMapper oMapper = new ObjectMapper();
93 aaiRootNode = oMapper.readTree(rpcMsgbody);
94 } catch (Exception e) {
95 throw new InvalidMessageException("Cannot parse json object", e);
98 assertTrue(aaiRootNode.get("input").get("payload") != null);
99 assertTrue(aaiRootNode.get("input").get("common-header") != null);
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");
106 public void testProcessMsgFieldMap() throws Exception {
109 String DMAAPLISTENERROOT = "DMAAPLISTENERROOT";
110 File directory = new File("lib");
112 if (! directory.exists()){
116 File source = new File("src/main/resources");
117 File dest = new File("lib/");
119 FileUtils.copyDirectory(source, dest);
120 } catch (IOException e) {
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);
134 Properties props = new Properties();
136 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
138 InputStream propStr = TestSdncPserverDmaapReceiver.class.getResourceAsStream("/dmaap-consumer-pserver.properties");
143 consumer.init(props, "src/test/resources/dmaap-consumer-pserver.properties");
144 consumer.processMsg(aaiInput);
147 @Test(expected = InvalidMessageException.class)
148 public void testProcessMsgNullMessage() throws Exception {
149 Properties props = new Properties();
151 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
152 consumer.processMsg(null);
155 @Test(expected = InvalidMessageException.class)
156 public void testProcessMsgInvalidMessage() throws Exception {
157 Properties props = new Properties();
159 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
160 consumer.processMsg("test");
164 public void testProcessMsgMissingEventHeader() throws Exception {
165 Properties props = new Properties();
167 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
168 consumer.processMsg("{\n" +
169 " \"input\" : { \n" +
175 public void testProcessMsgInvalidEventType() throws Exception {
176 Properties props = new Properties();
178 String msg = "{\"cambria.partition\": \"AAI\",\r\n" +
179 " \"event-header\": {\"event-type\": \"TEST-EVENT\"}}";
181 SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
182 consumer.processMsg(msg);