Merge "Unit/SONAR/Checkstyle in ONAP-REST"
[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  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.rest.util;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.apache.commons.lang.StringUtils;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.logging.flexlogger.FlexLogger;
42 import org.onap.policy.common.logging.flexlogger.Logger;
43 import org.onap.policy.rest.dao.CommonClassDao;
44 import org.onap.policy.rest.jpa.DictionaryData;
45 import org.onap.policy.rest.util.MsModelUtils.ModelType;
46
47 public class MsModelUtilsTest {
48     private static Logger logger = FlexLogger.getLogger(MsModelUtilsTest.class);
49     private static CommonClassDao commonClassDao;
50
51     /**
52      * Set tests up.
53      *
54      * @throws Exception on setup failures
55      */
56     @Before
57     public void setUp() throws Exception {
58         List<Object> dictionaryData = new ArrayList<Object>();
59         DictionaryData testData = new DictionaryData();
60         testData.setDictionaryName("dictionaryName");
61         testData.setDictionaryDataByName("dictionaryDataByName");
62         dictionaryData.add(testData);
63         logger.info("setUp: Entering");
64         commonClassDao = mock(CommonClassDao.class);
65         when(commonClassDao.getDataById(DictionaryData.class, "dictionaryName", "GocVNFType"))
66                         .thenReturn(dictionaryData);
67     }
68
69     @Test
70     public void testMsModelUtils() {
71         HashMap<String, MsAttributeObject> classMap = new HashMap<>();
72         ClassLoader classLoader = getClass().getClassLoader();
73         File file = new File(classLoader.getResource("DKaTVESPolicy-v1802.xmi").getFile());
74         MsModelUtils utils = new MsModelUtils("http://org.onap", "http://org.onap.policy");
75         Map<String, MsAttributeObject> tempMap = utils.processEpackage(file.getAbsolutePath().toString(),
76                         ModelType.XMI);
77         classMap.putAll(tempMap);
78         MsAttributeObject mainClass = classMap.get("StandardDeviationThreshold");
79         String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]
80             { "[", "]", " " }, new String[]
81             { "", "", "" });
82         List<String> dependency = new ArrayList<String>(Arrays.asList(dependTemp.split(",")));
83         dependency = utils.getFullDependencyList(dependency, classMap);
84         String subAttribute = utils.createSubAttributes(dependency, classMap, "StandardDeviationThreshold");
85         assertTrue(subAttribute != null);
86     }
87
88     /**
89      * Run the void stringBetweenDots(String, String) method test.
90      */
91
92     @Test
93     public void testStringBetweenDots() {
94
95         // expect: uniqueKeys should contain a string value
96         MsModelUtils controllerA = new MsModelUtils();
97         String str = "testing\\.byCorrectWay\\.OfDATA";
98         assertEquals(1, controllerA.stringBetweenDots(str));
99
100         // expect: uniqueKeys should not contain a string value
101         str = "testing\byWrongtWay.\\OfDATA";
102         MsModelUtils controllerB = new MsModelUtils();
103         assertEquals(0, controllerB.stringBetweenDots(str));
104     }
105
106     /**
107      * Run the load method test.
108      */
109     @Test
110     public void testLoad() {
111
112         boolean isLocalTesting = true;
113         MsModelUtils controller = new MsModelUtils();
114         String fileName = null;
115         Map<String, String> result = null;
116         try {
117             ClassLoader classLoader = getClass().getClassLoader();
118             fileName = new File(classLoader.getResource("TESTMODEL-v1806.yml").getFile()).getAbsolutePath();
119         } catch (Exception e1) {
120             logger.error("Exception Occured while loading file" + e1);
121         }
122         if (isLocalTesting) {
123             try {
124                 result = controller.load(fileName);
125             } catch (IOException e) {
126                 logger.error("testLoad", e);
127                 result = null;
128             } catch (ParserException e) {
129                 logger.error("testLoad", e);
130             }
131
132             assertTrue(result != null && !result.isEmpty());
133             logger.debug("result : " + result);
134         }
135
136         logger.debug("testLoad: exit");
137     }
138
139     /**
140      * Run the void parseTosca(String) method test.
141      */
142
143     @Test
144     public void testParseTosca() {
145
146         logger.debug("testParseTosca: enter");
147         boolean isLocalTesting = true;
148         String fileName = null;
149         try {
150             ClassLoader classLoader = getClass().getClassLoader();
151             fileName = new File(classLoader.getResource("TESTMODEL-v1806.yml").getFile()).getAbsolutePath();
152         } catch (Exception e1) {
153             logger.error("Exception Occured while loading file" + e1);
154         }
155
156         MsModelUtils controller = new MsModelUtils(commonClassDao);
157         if (isLocalTesting) {
158             try {
159                 controller.parseTosca(fileName);
160             } catch (Exception e) {
161                 fail("parseTosca caused error: " + e);
162             }
163         }
164         logger.debug("testParseTosca: exit");
165     }
166
167 }