34d555062511a287a1db645ac0d24339bc3ed573
[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-2019 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
21 package org.onap.policy.rest.util;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.apache.commons.lang.StringUtils;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.DictionaryData;
42 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
43
44 public class MSModelUtilsTest {
45     private static Logger logger = FlexLogger.getLogger(MSModelUtilsTest.class);
46     private static CommonClassDao commonClassDao;
47
48     @Before
49     public void setUp() throws Exception {
50         List<Object> dictionaryData = new ArrayList<Object>();
51         DictionaryData testData = new DictionaryData();
52         testData.setDictionaryName("dictionaryName");
53         testData.setDictionaryDataByName("dictionaryDataByName");
54         dictionaryData.add(testData);
55         logger.info("setUp: Entering");
56         commonClassDao = mock(CommonClassDao.class);
57         when(commonClassDao.getDataById(DictionaryData.class, "dictionaryName", "GocVNFType"))
58                 .thenReturn(dictionaryData);
59     }
60
61     @Test
62     public void testMSModelUtils() {
63         HashMap<String, MSAttributeObject> classMap = new HashMap<>();
64         ClassLoader classLoader = getClass().getClassLoader();
65         File file = new File(classLoader.getResource("DKaTVESPolicy-v1802.xmi").getFile());
66         MSModelUtils utils = new MSModelUtils("http://org.onap", "http://org.onap.policy");
67         Map<String, MSAttributeObject> tempMap =
68                 utils.processEpackage(file.getAbsolutePath().toString(), MODEL_TYPE.XMI);
69         classMap.putAll(tempMap);
70         MSAttributeObject mainClass = classMap.get("StandardDeviationThreshold");
71         String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[] {"[", "]", " "},
72                 new String[] {"", "", ""});
73         List<String> dependency = new ArrayList<String>(Arrays.asList(dependTemp.split(",")));
74         dependency = utils.getFullDependencyList(dependency, classMap);
75         String subAttribute = utils.createSubAttributes(dependency, classMap, "StandardDeviationThreshold");
76         assertTrue(subAttribute != null);
77     }
78
79
80     /**
81      * Run the void stringBetweenDots(String, String) method test.
82      */
83
84     @Test
85     public void testStringBetweenDots() {
86
87         // expect: uniqueKeys should contain a string value
88         MSModelUtils controllerA = new MSModelUtils();
89         String str = "testing\\.byCorrectWay\\.OfDATA";
90         assertEquals(1, controllerA.stringBetweenDots(str));
91
92         // expect: uniqueKeys should not contain a string value
93         str = "testing\byWrongtWay.\\OfDATA";
94         MSModelUtils controllerB = new MSModelUtils();
95         assertEquals(0, controllerB.stringBetweenDots(str));
96     }
97
98     /**
99      * Run the Map<String,String> load(String) method test.
100      */
101
102     @Test
103     public void testLoad() {
104
105         boolean isLocalTesting = true;
106         MSModelUtils controller = new MSModelUtils();
107         String fileName = null;
108         Map<String, String> result = null;
109         try {
110             ClassLoader classLoader = getClass().getClassLoader();
111             fileName = new File(classLoader.getResource("TESTMODEL-v1806.yml").getFile()).getAbsolutePath();
112         } catch (Exception e1) {
113             logger.error("Exception Occured while loading file" + e1);
114         }
115         if (isLocalTesting) {
116             try {
117                 result = controller.load(fileName);
118             } catch (IOException e) {
119                 logger.error("testLoad", e);
120                 result = null;
121             } catch (ParserException e) {
122                 logger.error("testLoad", e);
123             }
124
125             assertTrue(result != null && !result.isEmpty());
126             logger.debug("result : " + result);
127         }
128
129         logger.debug("testLoad: exit");
130     }
131
132     /**
133      * Run the void parseTosca(String) method test.
134      */
135
136     @Test
137     public void testParseTosca() {
138
139         logger.debug("testParseTosca: enter");
140         boolean isLocalTesting = true;
141         String fileName = null;
142         try {
143             ClassLoader classLoader = getClass().getClassLoader();
144             fileName = new File(classLoader.getResource("TESTMODEL-v1806.yml").getFile()).getAbsolutePath();
145         } catch (Exception e1) {
146             logger.error("Exception Occured while loading file" + e1);
147         }
148
149         MSModelUtils controller = new MSModelUtils(commonClassDao);
150         if (isLocalTesting) {
151             try {
152                 controller.parseTosca(fileName);
153             } catch (Exception e) {
154                 fail("parseTosca caused error: " + e);
155             }
156         }
157         logger.debug("testParseTosca: exit");
158     }
159
160 }