2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.rest.util;
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;
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;
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;
47 public class MsModelUtilsTest {
48 private static Logger logger = FlexLogger.getLogger(MsModelUtilsTest.class);
49 private static CommonClassDao commonClassDao;
54 * @throws Exception on setup failures
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);
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(),
77 classMap.putAll(tempMap);
78 MsAttributeObject mainClass = classMap.get("StandardDeviationThreshold");
79 String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]
80 { "[", "]", " " }, new String[]
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);
89 * Run the void stringBetweenDots(String, String) method test.
93 public void testStringBetweenDots() {
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));
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));
107 * Run the load method test.
110 public void testLoad() {
112 boolean isLocalTesting = true;
113 MsModelUtils controller = new MsModelUtils();
114 String fileName = null;
115 Map<String, String> result = null;
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);
122 if (isLocalTesting) {
124 result = controller.load(fileName);
125 } catch (IOException e) {
126 logger.error("testLoad", e);
128 } catch (ParserException e) {
129 logger.error("testLoad", e);
132 assertTrue(result != null && !result.isEmpty());
133 logger.debug("result : " + result);
136 logger.debug("testLoad: exit");
140 * Run the void parseTosca(String) method test.
144 public void testParseTosca() {
146 logger.debug("testParseTosca: enter");
147 boolean isLocalTesting = true;
148 String fileName = null;
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);
156 MsModelUtils controller = new MsModelUtils(commonClassDao);
157 if (isLocalTesting) {
159 controller.parseTosca(fileName);
160 } catch (Exception e) {
161 fail("parseTosca caused error: " + e);
164 logger.debug("testParseTosca: exit");