848632b310ba2b15b97129bcd17a1ee92b1f6623
[appc.git] / appc-config / appc-config-generator / provider / src / test / java / org / onap / sdnc / config / generator / pattern / TestPatternNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2018 IBM.
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.sdnc.config.generator.pattern;
27
28 import static org.junit.Assert.assertEquals;
29 import java.util.HashMap;
30 import java.util.Map;
31 import org.apache.commons.io.IOUtils;
32 import org.junit.Test;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
36
37 public class TestPatternNode {
38
39     @Test
40     public void parseErrorLog() throws Exception {
41         PatternNode patternNode = new PatternNode();
42         Map<String, String> inParams = new HashMap<String, String>();
43         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
44         String logData = IOUtils.toString(
45                 TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/errorlog.txt"),
46                 ConfigGeneratorConstant.STRING_ENCODING);
47         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_LOG_DATA, logData);
48         SvcLogicContext ctx = new SvcLogicContext();
49         patternNode.parseErrorLog(inParams, ctx);
50         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
51                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
52
53     }
54     
55     @Test(expected= SvcLogicException.class)
56     public void testParseErrorLogForEmptyLogData() throws Exception {
57         PatternNode patternNode = new PatternNode();
58         Map<String, String> inParams = new HashMap<String, String>();
59         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
60         String logData = IOUtils.toString(
61                 TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/errorlog.txt"),
62                 ConfigGeneratorConstant.STRING_ENCODING);
63         SvcLogicContext ctx = new SvcLogicContext();
64         patternNode.parseErrorLog(inParams, ctx);
65        
66     }
67
68
69     // @Test(expected=Exception.class)
70     public void checkXMLData() throws Exception {
71         PatternNode patternNode = new PatternNode();
72         Map<String, String> inParams = new HashMap<String, String>();
73         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
74
75         String xmlData = IOUtils.toString(
76                 TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/xml_data.xml"),
77                 ConfigGeneratorConstant.STRING_ENCODING);
78         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_CHECK_DATA, xmlData);
79         SvcLogicContext ctx = new SvcLogicContext();
80         patternNode.checkDataType(inParams, ctx);
81         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
82                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
83     }
84
85     // @Test
86     public void checkJsonData() throws Exception {
87         PatternNode patternNode = new PatternNode();
88         Map<String, String> inParams = new HashMap<String, String>();
89         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
90         String xmlData =
91                 IOUtils.toString(TestPatternNode.class.getClassLoader().getResourceAsStream(
92                         "pattern/json_data.json"), ConfigGeneratorConstant.STRING_ENCODING);
93         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_CHECK_DATA, xmlData);
94         SvcLogicContext ctx = new SvcLogicContext();
95         patternNode.checkDataType(inParams, ctx);
96         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
97                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
98     }
99
100     @Test(expected = Exception.class)
101     public void checStringData() throws Exception {
102         PatternNode patternNode = new PatternNode();
103         Map<String, String> inParams = new HashMap<String, String>();
104         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
105         String stringData = IOUtils.toString(
106                 TestPatternNode.class.getClassLoader().getResourceAsStream("pattern/text_data.txt"),
107                 ConfigGeneratorConstant.STRING_ENCODING);
108         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_CHECK_DATA, stringData);
109         SvcLogicContext ctx = new SvcLogicContext();
110         patternNode.checkDataType(inParams, ctx);
111         assertEquals(ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS),
112                 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
113     }
114     
115     @Test
116     public void testCheckDataType() throws Exception {
117         PatternNode patternNode = new PatternNode();
118         Map<String, String> inParams = new HashMap<String, String>();
119         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX, "test");
120         inParams.put(ConfigGeneratorConstant.INPUT_PARAM_CHECK_DATA, "testData");
121         SvcLogicContext ctx = new SvcLogicContext();
122         patternNode.checkDataType(inParams, ctx);
123         assertEquals(ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
124     }
125 }