Convert tabs to spaces basic refactoring
[policy/engine.git] / ONAP-REST / src / test / java / org / onap / policy / rest / util / MSModelUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.rest.util;
21
22 import static org.junit.Assert.*;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.junit.Test;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
37
38 public class MSModelUtilsTest {
39     private static Logger logger = FlexLogger.getLogger(MSModelUtilsTest.class);
40     @Test
41     public void testMSModelUtils(){
42         HashMap<String, MSAttributeObject> classMap = new HashMap<>();
43         ClassLoader classLoader = getClass().getClassLoader();
44         File file = new File(classLoader.getResource("DKaTVESPolicy-v1802.xmi").getFile());
45         MSModelUtils utils = new MSModelUtils("http://org.onap", "http://org.onap.policy");
46         Map<String, MSAttributeObject> tempMap = utils.processEpackage(file.getAbsolutePath().toString(), MODEL_TYPE.XMI);
47         classMap.putAll(tempMap);
48         MSAttributeObject mainClass = classMap.get("StandardDeviationThreshold");
49         String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
50         List<String> dependency = new ArrayList<String>(Arrays.asList(dependTemp.split(",")));
51         dependency = utils.getFullDependencyList(dependency, classMap);
52         String subAttribute = utils.createSubAttributes(dependency, classMap, "StandardDeviationThreshold");
53         assertTrue(subAttribute != null);
54     }
55
56
57     /**
58      * Run the void stringBetweenDots(String, String) method test
59      */
60
61      @Test
62     public void testStringBetweenDots() {
63
64         //expect: uniqueKeys should contain a string value
65          MSModelUtils controllerA = new MSModelUtils();
66         String str = "testing\\.byCorrectWay\\.OfDATA";
67         assertEquals(1, controllerA.stringBetweenDots(str));
68
69         //expect: uniqueKeys should not contain a string value
70         str = "testing\byWrongtWay.\\OfDATA";
71         MSModelUtils controllerB = new MSModelUtils();
72         assertEquals(0, controllerB.stringBetweenDots(str));
73     }
74
75     /**
76      * Run the Map<String,String> load(String) method test
77      */
78
79     @Test
80     public void testLoad() {
81
82         boolean isLocalTesting = true;
83         MSModelUtils controller = new MSModelUtils();
84         String fileName = null;
85         Map<String,String> result = null;
86         try {
87             ClassLoader classLoader = getClass().getClassLoader();
88             fileName = new File(classLoader.getResource("policy_tosca_tca-v1707.yml").getFile()).getAbsolutePath();
89         } catch (Exception e1) {
90             logger.error("Exception Occured while loading file"+e1);
91         }
92         if(isLocalTesting){
93             try {
94                 result = controller.load(fileName);
95             } catch (IOException e) {
96                 logger.error("testLoad", e);
97                 result = null;
98             }catch(ParserException e){
99                 logger.error("testLoad", e);
100             }
101
102             assertTrue(result != null && !result.isEmpty());
103             logger.debug("result : " + result);
104         }
105
106         logger.debug("testLoad: exit");
107     }
108
109     /**
110      * Run the void parseTosca(String) method test
111      */
112
113     @Test
114     public void testParseTosca() {
115
116         logger.debug("testParseTosca: enter");
117         boolean isLocalTesting = true;
118         String fileName = null;
119         try {
120             ClassLoader classLoader = getClass().getClassLoader();
121             fileName = new File(classLoader.getResource("policy_tosca_tca-v1707.yml").getFile()).getAbsolutePath();
122         } catch (Exception e1) {
123             logger.error("Exception Occured while loading file"+e1);
124         }
125
126         MSModelUtils controller = new MSModelUtils();
127         if(isLocalTesting){
128             try {
129                 controller.parseTosca(fileName);
130             }catch (Exception e) {
131                 fail("parseTosca caused error: " + e);
132             }
133         }
134         logger.debug("testParseTosca: exit");
135     }
136
137 }