summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/test/java/org/openecomp/sdc/healing/healers/util/TestUtil.java
blob: da44ec422f707ac7f3ae41df9400f31f4c54a1a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package org.openecomp.sdc.healing.healers.util;

import org.junit.Assert;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
import org.openecomp.sdc.tosca.services.ToscaUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.NotDirectoryException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class TestUtil {
  private final static Logger log = (Logger) LoggerFactory.getLogger
      (TestUtil.class.getName());

  public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
                                                        String globalServiceTemplatesPath,
                                                        String entryDefinitionServiceTemplate)
      throws IOException {
    ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
    Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
    if (entryDefinitionServiceTemplate == null) {
      entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
    }

    loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
    if (globalServiceTemplatesPath != null) {
      loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates);
    }

    return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
  }

  private static void loadServiceTemplates(String serviceTemplatesPath,
                                           ToscaExtensionYamlUtil toscaExtensionYamlUtil,
                                           Map<String, ServiceTemplate> serviceTemplates)
      throws IOException {
    URL urlFile = TestUtil.class.getResource(serviceTemplatesPath);
    if (urlFile != null) {
      File pathFile = new File(urlFile.getFile());
      Collection<File> files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true);
      if (files != null) {
        addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
      } else {
        throw new NotDirectoryException(serviceTemplatesPath);
      }
    } else {
      throw new NotDirectoryException(serviceTemplatesPath);
    }
  }

  private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
                                              Collection<File> files,
                                              ToscaExtensionYamlUtil toscaExtensionYamlUtil)
      throws IOException {
    for (File file : files) {
      try (InputStream yamlFile = new FileInputStream(file)) {
        ServiceTemplate serviceTemplateFromYaml =
            toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
        serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml);
        try {
          yamlFile.close();
        } catch (IOException ignore) {
          log.debug("",ignore);
        }
      } catch (FileNotFoundException exception) {
        throw exception;
      } catch (IOException exception) {
        throw exception;
      }
    }
  }

  public static void compareToscaServiceModels(String expectedServiceModelPath,
                                                ToscaServiceModel actualServiceModel)
      throws IOException {
    ToscaServiceModel expectedServiceModel =
        loadToscaServiceModel(expectedServiceModelPath, null, null);

    Map<String, ServiceTemplate> expectedServiceTemplates =
        new HashMap<>(expectedServiceModel.getServiceTemplates());
    Map<String, ServiceTemplate> actualServiceTemplates =
        new HashMap<>(actualServiceModel.getServiceTemplates());

    for (Map.Entry<String, ServiceTemplate> expectedServiceTemplateEntry : expectedServiceTemplates.entrySet()) {
      String serviceTemplateName = expectedServiceTemplateEntry.getKey();
      ServiceTemplate actualServiceTemplate =
          actualServiceTemplates.get(serviceTemplateName);

      Assert.assertNotNull("Missing service template in service model : " + serviceTemplateName, actualServiceTemplate);
      org.junit.Assert.assertEquals("Difference in file " + serviceTemplateName,
          JsonUtil.object2Json(expectedServiceTemplateEntry.getValue()),
          JsonUtil.object2Json(actualServiceTemplate));
    }
  }
}