2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.sdc.tosca;
23 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
24 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
25 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
33 import java.nio.file.NotDirectoryException;
34 import java.util.HashMap;
37 public class TestUtil {
39 public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
40 String globalServiceTemplatesPath,
41 String entryDefinitionServiceTemplate)
43 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
44 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
45 if (entryDefinitionServiceTemplate == null) {
46 entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
49 loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
50 if (globalServiceTemplatesPath != null) {
51 loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
54 return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
57 private static void loadServiceTemplates(String serviceTemplatesPath,
58 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
59 Map<String, ServiceTemplate> serviceTemplates)
61 URL urlFile = TestUtil.class.getResource(serviceTemplatesPath);
62 if (urlFile != null) {
63 File pathFile = new File(urlFile.getFile());
64 File[] files = pathFile.listFiles();
66 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
68 throw new NotDirectoryException(serviceTemplatesPath);
71 throw new NotDirectoryException(serviceTemplatesPath);
75 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
77 ToscaExtensionYamlUtil toscaExtensionYamlUtil)
79 for (File file : files) {
80 try (InputStream yamlFile = new FileInputStream(file)) {
81 ServiceTemplate serviceTemplateFromYaml =
82 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
83 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
86 } catch (IOException ignore) {
88 } catch (FileNotFoundException e) {
90 } catch (IOException e) {