aa41429b925ecd4d2951a05ab940c47fa7376558
[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                         }
99                         
100                         assertTrue(result != null && !result.isEmpty());                                
101                         logger.debug("result : " + result);
102                 }
103
104                 logger.debug("testLoad: exit");
105         }
106         
107         /**
108          * Run the void parseTosca(String) method test
109          */
110         
111         @Test
112         public void testParseTosca() {
113                 
114                 logger.debug("testParseTosca: enter");
115                 boolean isLocalTesting = true;
116                 String fileName = null;
117                 try {
118                         ClassLoader classLoader = getClass().getClassLoader();
119                         fileName = new File(classLoader.getResource("policy_tosca_tca-v1707.yml").getFile()).getAbsolutePath();
120                 } catch (Exception e1) {
121                         logger.error("Exception Occured while loading file"+e1);
122                 }
123                 
124                 MSModelUtils controller = new MSModelUtils();
125         if(isLocalTesting){
126                         try {
127                             controller.parseTosca(fileName);
128                         }catch (Exception e) {
129                                 fail("parseTosca caused error: " + e);
130                         }
131         }
132                 logger.debug("testParseTosca: exit");
133         }
134
135 }