aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/java/org/openecomp/sdc/validation/impl/validators/ValidatorBaseTest.java
blob: f9c5bf423b25802c05c9c96c9322281737a62fac (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
package org.openecomp.sdc.validation.impl.validators;

import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
import org.openecomp.core.validation.interfaces.Validator;
import org.openecomp.core.validation.types.GlobalValidationContext;
import org.openecomp.core.validation.types.MessageContainer;
import org.testng.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public abstract class ValidatorBaseTest {

  private static GlobalValidationContext createGlobalContextFromPath(String path) {
    GlobalValidationContext globalValidationContext = new GlobalValidationContext();
    Map<String, byte[]> contentMap = getContentMapByPath(path);
    if (contentMap == null) {
      return null;
    }
    contentMap.entrySet().stream()
        .forEach(entry -> globalValidationContext.addFileContext(entry.getKey(), entry.getValue()));

    return globalValidationContext;
  }


  // New test base implementation

  private static Map<String, byte[]> getContentMapByPath(String path) {
    Map<String, byte[]> contentMap = new HashMap<>();
    byte[] fileContent;
    FileInputStream fis;
    URL url = ValidatorBaseTest.class.getResource(path);
    File pathFile = new File(url.getFile());
    File[] files;
    if (pathFile.isDirectory()) {
      files = pathFile.listFiles();
    } else {
      files = new File[]{pathFile};
    }

    if (files == null || files.length == 0) {
      return null;
    }

    for (File file : files) {
      try {
        fis = new FileInputStream(file);
        fileContent = FileUtils.toByteArray(fis);
        contentMap.put(file.getName(), fileContent);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return contentMap;
  }

  public abstract Map<String, MessageContainer> runValidation(String path);

  protected Map<String, MessageContainer> testValidator(Validator validator, String path) {

    GlobalValidationContext globalValidationContext = createGlobalContextFromPath(path);
    validator.validate(globalValidationContext);

    assert globalValidationContext != null;
    return globalValidationContext.getContextMessageContainers();


  }

  protected void validateErrorMessage(String actualMessage, String expected, String... params) {
    Assert.assertEquals(actualMessage.replace("\n", "").replace("\r", ""),
        ErrorMessagesFormatBuilder.getErrorWithParameters(expected, params).replace("\n", "")
            .replace("\r", ""));

  }


}