2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.rest.util;
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;
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;
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;
44 public class MSModelUtilsTest {
45 private static Logger logger = FlexLogger.getLogger(MSModelUtilsTest.class);
46 private static CommonClassDao commonClassDao;
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);
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);
81 * Run the void stringBetweenDots(String, String) method test.
85 public void testStringBetweenDots() {
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));
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));
99 * Run the Map<String,String> load(String) method test.
103 public void testLoad() {
105 boolean isLocalTesting = true;
106 MSModelUtils controller = new MSModelUtils();
107 String fileName = null;
108 Map<String, String> result = null;
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);
115 if (isLocalTesting) {
117 result = controller.load(fileName);
118 } catch (IOException e) {
119 logger.error("testLoad", e);
121 } catch (ParserException e) {
122 logger.error("testLoad", e);
125 assertTrue(result != null && !result.isEmpty());
126 logger.debug("result : " + result);
129 logger.debug("testLoad: exit");
133 * Run the void parseTosca(String) method test.
137 public void testParseTosca() {
139 logger.debug("testParseTosca: enter");
140 boolean isLocalTesting = true;
141 String fileName = null;
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);
149 MSModelUtils controller = new MSModelUtils(commonClassDao);
150 if (isLocalTesting) {
152 controller.parseTosca(fileName);
153 } catch (Exception e) {
154 fail("parseTosca caused error: " + e);
157 logger.debug("testParseTosca: exit");