53a2cfa650f992c8749ade8ba82b401fa6bf54c0
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 Nokia 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.validation.impl.validators;
21
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import java.util.stream.Stream;
27 import org.junit.jupiter.api.Test;
28
29 class FileExtensionUtilsTest {
30
31     private static final Set<String> VALID_YAML_EXTENSIONS = Set.of(
32         ".yaml",
33         ".yml",
34         ".env"
35     );
36     private static final Set<String> INVALID_YAML_EXTENSIONS = Set.of(
37         ".txt",
38         ".java",
39         ".properties"
40     );
41
42     private static final Set<String> VALID_PM_DICTIONARY_EXTENSIONS = Set.of(
43         "pmdict.yml",
44         "pmdict.yaml",
45         "pm_dict.yml",
46         "pm_dict.yaml",
47         "pmdictionary.yml",
48         "pmdictionary.yaml",
49         "pm_dictionary.yml",
50         "pm_dictionary.yaml"
51     );
52     private static final Set<String> INVALID_PM_DICTIONARY_EXTENSIONS = Set.of(
53         "pmdict.txt",
54         "pmdict.java",
55         "pm.yml"
56     );
57
58     private static final Set<String> TEST_FILE_PREFIXES = Set.of(
59         "test",
60         "test_file"
61     );
62
63     @Test
64     void shouldMatchProperYamlExtensions() {
65         final boolean allValidFilesMatched = constructTestFilenamesWithExtensions(VALID_YAML_EXTENSIONS)
66             .allMatch(FileExtensionUtils::isYaml);
67
68         assertTrue(allValidFilesMatched);
69     }
70
71     @Test
72     void shouldNotMatchImproperYamlExtensions() {
73         final boolean allInvalidFilesNotMatched = constructTestFilenamesWithExtensions(INVALID_YAML_EXTENSIONS)
74             .noneMatch(FileExtensionUtils::isYaml);
75
76         assertTrue(allInvalidFilesNotMatched);
77     }
78
79     @Test
80     void shouldMatchProperPmDictionaryExtensions() {
81         final boolean allValidFilesMatched = constructTestFilenamesWithExtensions(VALID_PM_DICTIONARY_EXTENSIONS)
82             .allMatch(FileExtensionUtils::isPmDictionary);
83
84         assertTrue(allValidFilesMatched);
85     }
86
87     @Test
88     void shouldNotMatchImproperPmDictionaryExtensions() {
89         final boolean allInvalidFilesNotMatched = constructTestFilenamesWithExtensions(INVALID_PM_DICTIONARY_EXTENSIONS)
90             .noneMatch(FileExtensionUtils::isPmDictionary);
91
92         assertTrue(allInvalidFilesNotMatched);
93     }
94
95     private Stream<String> constructTestFilenamesWithExtensions(Set<String> extensions) {
96         return extensions.stream()
97             .flatMap(ext -> prepareFilenamesWithExtension(ext).stream());
98     }
99
100     private Set<String> prepareFilenamesWithExtension(String extension) {
101         return Stream.concat(
102             joinTestNamesWithExtension(extension.toLowerCase()),
103             joinTestNamesWithExtension(extension.toUpperCase())
104         ).collect(Collectors.toSet());
105     }
106
107     private Stream<String> joinTestNamesWithExtension(String extension) {
108         return TEST_FILE_PREFIXES.stream()
109             .map(prefix -> prefix + extension);
110     }
111 }