2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.universalvesadapter.adapter;
22 import static org.junit.Assert.*;
23 import static org.mockito.Mockito.when;
25 import java.io.FileNotFoundException;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Collections;
31 import java.util.concurrent.ConcurrentHashMap;
33 import javax.annotation.Resource;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.milyn.Smooks;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.dcaegen2.ves.domain.VesEvent;
45 import org.onap.universalvesadapter.Application;
46 import org.onap.universalvesadapter.adapter.UniversalEventAdapter;
47 import org.onap.universalvesadapter.exception.ConfigFileReadException;
48 import org.onap.universalvesadapter.exception.ConfigFileSmooksConversionException;
49 import org.onap.universalvesadapter.exception.MapperConfigException;
50 import org.onap.universalvesadapter.exception.VesException;
51 import org.onap.universalvesadapter.service.ConfigFileService;
52 import org.onap.universalvesadapter.utils.MapperConfigUtils;
53 import org.onap.universalvesadapter.utils.SmooksUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.beans.factory.annotation.Value;
58 import org.springframework.boot.test.context.SpringBootTest;
59 import org.springframework.test.context.junit4.SpringRunner;
60 import org.springframework.util.FileCopyUtils;
62 import com.fasterxml.jackson.core.JsonProcessingException;
63 import com.fasterxml.jackson.databind.ObjectMapper;
65 @RunWith(SpringRunner.class)
66 @SpringBootTest(classes=Application.class)
67 public class UniversalEventAdapterTest {
69 private final Logger eLOGGER = LoggerFactory.getLogger(this.getClass());
72 private ConfigFileService configFileService;
76 private UniversalEventAdapter universalVesAdapter;
81 MockitoAnnotations.initMocks(this);
86 public void testtransform() {
87 StringBuffer incomingJsonString = new StringBuffer("{ ")
88 .append("\"protocol version\":\"v2c\", ")
89 .append("\"notify OID\":\".1.3.6.1.4.1.74.2.46.12.1.1AAA\", ")
90 .append("\"cambria.partition\":\"dcae-snmp.client.research.att.com\", ")
91 .append("\"trap category\":\"UCSNMP-HEARTBEAT\", ")
92 .append("\"epoch_serno\": 15161177410000, ")
93 .append("\"community\":\"public\", ")
94 .append("\"time received\": 1516117741, ")
95 .append("\"agent name\":\"localhost\", ")
96 .append("\"agent address\":\"127.0.0.1\", ")
97 .append("\"community len\": 6, ")
98 .append("\"notify OID len\": 12, ")
99 .append("\"varbinds\": [{ ")
100 .append(" \"varbind_type\":\"octet\", ")
101 .append(" \"varbind_oid\":\".1.3.6.1.4.1.74.2.46.12.1.1.1\", ")
102 .append(" \"varbind_value\":\"ucsnmp heartbeat - ignore\" ")
104 .append(" \"varbind_type\":\"octet\", ")
105 .append(" \"varbind_oid\":\".1.3.6.1.4.1.74.2.46.12.1.1.2\", ")
106 .append(" \"varbind_value\":\"Tue Jan 16 10:49:01 EST 2018\" ")
110 StringBuffer configFileData = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ")
111 .append("<smooks-resource-list xmlns=\"http://www.milyn.org/xsd/smooks-1.1.xsd\" ")
112 .append("xmlns:json=\"http://www.milyn.org/xsd/smooks/json-1.1.xsd\" ")
113 .append(" xmlns:jb=\"http://www.milyn.org/xsd/smooks/javabean-1.2.xsd\"> ")
114 .append(" <json:reader rootName=\"simple\" keyWhitspaceReplacement=\"-\"> ")
115 .append(" </json:reader> ")
116 .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.VesEvent\" beanId=\"vesEvent\" ")
117 .append(" createOnElement=\"simple\">")
118 .append(" <jb:wiring property=\"event\" beanIdRef=\"event\"/>")
119 .append(" </jb:bean> ")
120 .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.Event\" beanId=\"event\" ")
121 .append(" createOnElement=\"simple\"> ")
122 .append(" <jb:wiring property=\"commonEventHeader\" beanIdRef=\"commonEventHeader\"/>")
123 .append(" <jb:wiring property=\"faultFields\" beanIdRef=\"faultFields\"/> ")
124 .append(" </jb:bean> ")
125 .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.CommonEventHeader\" ")
126 .append(" beanId=\"commonEventHeader\" createOnElement=\"simple\"> ")
127 .append(" <jb:value property=\"eventId\" data=\"#/community\" /> ")
128 .append(" <jb:value property=\"eventName\" data=\"#/protocol-version\" /> ")
129 .append(" <jb:value property=\"domain\" data=\"#/trap-category\" /> ")
130 .append(" <jb:value property=\"sequence\" data=\"#/time-received\" decoder=\"Long\"/> ")
131 .append(" <jb:value property=\"lastEpochMicrosec\" data=\"#/community-len\" decoder=\"Double\" /> ")
132 .append(" <jb:value property=\"startEpochMicrosec\" data=\"#/notify-OID-len\" /> ")
133 .append(" </jb:bean> ")
134 .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.FaultFields\" beanId=\"faultFields\"")
135 .append(" createOnElement=\"simple\"> <jb:value property=\"alarmCondition\" data=\"#/cambria.partition\" /> ")
136 .append(" <jb:value property=\"eventSeverity\" data=\"#/notify-OID\" /> ")
137 .append(" <jb:value property=\"eventSourceType\" data=\"#/agent-name\" /> ")
138 .append(" <jb:value property=\"specificProblem\" data=\"#/agent-address\" /> ")
139 .append(" <jb:value property=\"faultFieldsVersion\" data=\"#/epoch_serno\" decoder=\"Double\" /> ")
140 .append(" </jb:bean> ")
141 .append("</smooks-resource-list>");
144 Mockito.when(configFileService.readConfigFile(Mockito.anyString())).thenReturn(configFileData.toString());
145 } catch (Exception e) {
146 eLOGGER.error("Error occurred : " + e.getMessage());
151 String actualResult = universalVesAdapter.transform(incomingJsonString.toString(), "snmp");
152 assertNotNull(actualResult);
153 assertNotEquals("", actualResult);
154 } catch (VesException exception) {
155 eLOGGER.error("Error occurred : ", exception );