aboutsummaryrefslogtreecommitdiffstats
path: root/UniversalVesAdapter/src/test/java/org/onap/universalvesadapter/utils/SmooksUtilsTest.java
blob: 41431f70217bc248b1a176d81dab36b2cc15307a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* ============LICENSE_START=======================================================
* ONAP : DCAE
* ================================================================================
* Copyright 2018 TechMahindra
*=================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.universalvesadapter.utils;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.beans.beancontext.BeanContext;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.milyn.Smooks;
import org.milyn.container.ExecutionContext;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.dcaegen2.ves.domain.VesEvent;
import org.onap.universalvesadapter.exception.MapperConfigException;
import org.onap.universalvesadapter.utils.SmooksUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.xml.sax.SAXException;

public class SmooksUtilsTest {

    private final Logger eLOGGER = LoggerFactory.getLogger(this.getClass());    
    
    @Test
    public void testGetTransformedObjectForInput() {
        StringBuffer incomingJsonString = new StringBuffer("{ ")
                .append("\"protocol version\":\"v2c\", ")
                .append("\"notify OID\":\".1.3.6.1.4.1.74.2.46.12.1.1AAA\", ")
                .append("\"cambria.partition\":\"dcae-snmp.client.research.att.com\", ")
                .append("\"trap category\":\"UCSNMP-HEARTBEAT\", ")
                .append("\"epoch_serno\": 15161177410000, ")
                .append("\"community\":\"public\", ")
                .append("\"time received\": 1516117741, ")
                .append("\"agent name\":\"localhost\", ")
                .append("\"agent address\":\"127.0.0.1\", ")
                .append("\"community len\": 6, ")
                .append("\"notify OID len\": 12, ")
                .append("\"varbinds\": [{ ")
                .append("    \"varbind_type\":\"octet\", ")
                .append("    \"varbind_oid\":\".1.3.6.1.4.1.74.2.46.12.1.1.1\", ")
                .append("    \"varbind_value\":\"ucsnmp heartbeat - ignore\" ")
                .append(" }, { ")
                .append("    \"varbind_type\":\"octet\", ")
                .append("    \"varbind_oid\":\".1.3.6.1.4.1.74.2.46.12.1.1.2\", ")
                .append("    \"varbind_value\":\"Tue Jan 16 10:49:01 EST 2018\" ")
                .append(" }] ")
                .append("}");
        StringBuffer configFileData = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ")
                .append("<smooks-resource-list    xmlns=\"http://www.milyn.org/xsd/smooks-1.1.xsd\"   ")
                .append("xmlns:json=\"http://www.milyn.org/xsd/smooks/json-1.1.xsd\"    ")
                .append(" xmlns:jb=\"http://www.milyn.org/xsd/smooks/javabean-1.2.xsd\">     ")
                .append(" <json:reader rootName=\"simple\"  keyWhitspaceReplacement=\"-\">    ")
                .append(" </json:reader>     ")
                .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.VesEvent\" beanId=\"vesEvent\" ")
                .append(" createOnElement=\"simple\">")
                .append("        <jb:wiring property=\"event\" beanIdRef=\"event\"/>")
                .append(" </jb:bean>       ")
                .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.Event\" beanId=\"event\" ")
                .append(" createOnElement=\"simple\">         ")
                .append("     <jb:wiring property=\"commonEventHeader\" beanIdRef=\"commonEventHeader\"/>")
                .append("     <jb:wiring property=\"faultFields\" beanIdRef=\"faultFields\"/>             ")
                .append(" </jb:bean>       ")
                .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.CommonEventHeader\" ")
                .append(" beanId=\"commonEventHeader\" createOnElement=\"simple\">         ")
                .append("     <jb:value property=\"eventId\" data=\"#/community\" />      ")
                .append("     <jb:value property=\"eventName\" data=\"#/protocol-version\" />             ")
                .append("     <jb:value property=\"domain\" data=\"#/trap-category\" />       ")
                .append("     <jb:value property=\"sequence\" data=\"#/time-received\" decoder=\"Long\"/>        ")
                .append("     <jb:value property=\"lastEpochMicrosec\" data=\"#/community-len\"  decoder=\"Double\" />       ")
                .append("     <jb:value property=\"startEpochMicrosec\" data=\"#/notify-OID-len\"   />    ")
                .append(" </jb:bean>           ")
                .append(" <jb:bean class=\"org.onap.dcaegen2.ves.domain.FaultFields\" beanId=\"faultFields\"")
                .append(" createOnElement=\"simple\">        <jb:value property=\"alarmCondition\" data=\"#/cambria.partition\" />  ")
                .append("     <jb:value property=\"eventSeverity\" data=\"#/notify-OID\" />    ")
                .append("     <jb:value property=\"eventSourceType\" data=\"#/agent-name\" />       ")
                .append("     <jb:value property=\"specificProblem\" data=\"#/agent-address\" />       ")
                .append("     <jb:value property=\"faultFieldsVersion\" data=\"#/epoch_serno\" decoder=\"Double\" /> ")
                .append(" </jb:bean>                   ")
                .append("</smooks-resource-list>");    
        
        Smooks smooks = null;
        VesEvent vesEvent = new VesEvent();
        try {
        //reading config file.. for now, looking at it as just one time operation
            if(null == smooks){
                
                smooks = new Smooks(new ByteArrayInputStream(
                        configFileData.toString().getBytes(StandardCharsets.UTF_8)));
            }
        
            vesEvent = SmooksUtils.getTransformedObjectForInput(smooks,
                    incomingJsonString.toString());
        } catch (IOException | SAXException exception) {
            eLOGGER.error("Error occurred : ", exception );
        }
        
        assertEquals(vesEvent.getEvent().getCommonEventHeader().getDomain(), "UCSNMP-HEARTBEAT");
        
        
    }

}