8f17f0f35774ccd8027a4da70c84e38df4e386ff
[sdc.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.validation.types;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.openecomp.core.validation.ErrorMessageCode;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.function.BiPredicate;
33
34 public class GlobalValidationContextTest {
35   private static String filename = "testName";
36   private static String yaml1 = "one.yaml";
37   private static String yaml2 = "two.yaml";
38   private static String text1 = "one.txt";
39   private static String content = "testContent";
40   private static String message = "The file is corrupted";
41
42   @Test
43   public void testAddMessageCode() {
44     GlobalValidationContext globalValidationContext = new GlobalValidationContext();
45     ErrorMessageCode error = new ErrorMessageCode("Error");
46     globalValidationContext.setMessageCode(error);
47
48     Assert.assertEquals(error, globalValidationContext.getMessageCode());
49   }
50
51   @Test
52   public void testAddFileContext() {
53     GlobalValidationContext globalValidationContext = new GlobalValidationContext();
54     globalValidationContext.addFileContext(filename, content.getBytes());
55     Map<String, FileValidationContext> fileContextMap = globalValidationContext.getFileContextMap();
56
57     Assert.assertTrue(MapUtils.isNotEmpty(fileContextMap));
58     Assert.assertTrue(fileContextMap.containsKey(filename));
59   }
60
61   @Test
62   public void testGetContextMessageContainers() {
63     GlobalValidationContext globalValidationContext = new GlobalValidationContext();
64     globalValidationContext.addMessage(filename, ErrorLevel.ERROR, message);
65
66     Map<String, MessageContainer> messageContainers =
67         globalValidationContext.getContextMessageContainers();
68
69     testIfFileHasMessageContainer(messageContainers);
70   }
71
72   @Test
73   public void testAddMessage() {
74     GlobalValidationContext globalValidationContext = new GlobalValidationContext();
75     globalValidationContext.addMessage(filename, ErrorLevel.ERROR, message);
76
77     Map<String, MessageContainer> messageContainers =
78         globalValidationContext.getContextMessageContainers();
79
80     testIfFileHasMessageContainer(messageContainers);
81     testIfFileHasErrorMessage(messageContainers, 1);
82   }
83
84   @Test
85   public void testGetFiles() {
86     GlobalValidationContext globalValidationContext = new GlobalValidationContext();
87     byte[] bytes = content.getBytes();
88     globalValidationContext.addFileContext(yaml1, bytes);
89     globalValidationContext.addFileContext(yaml2, bytes);
90     globalValidationContext.addFileContext(text1, bytes);
91
92     testGetFilesByFileType((fileName, globalContext) -> fileName.endsWith(".yaml"),
93         2, Arrays.asList(yaml1, yaml2), globalValidationContext);
94     testGetFilesByFileType((fileName, globalContext) -> fileName.endsWith(".txt"),
95         1, Collections.singletonList(text1), globalValidationContext);
96
97
98   }
99
100   private void testGetFilesByFileType(BiPredicate<String, GlobalValidationContext> func,
101                                       int expectedFilesNumberToFind,
102                                       List<String> expectedFileNames,
103                                       GlobalValidationContext globalValidationContext) {
104     Collection<String> files = globalValidationContext.files(func);
105     Assert.assertTrue(CollectionUtils.isNotEmpty(files));
106     Assert.assertEquals(files.size(), expectedFilesNumberToFind);
107     expectedFileNames.forEach(filename -> Assert.assertTrue(files.contains(filename)));
108   }
109
110   private void testIfFileHasMessageContainer(Map<String, MessageContainer> messageContainers) {
111     Assert.assertNotNull(messageContainers);
112     Assert.assertTrue(messageContainers.containsKey(filename));
113   }
114
115   private void testIfFileHasErrorMessage(Map<String, MessageContainer> messageContainers,
116                                          int expectedErrorsNumber) {
117     MessageContainer messageContainer = messageContainers.get(filename);
118     Assert.assertEquals(messageContainer.getErrorMessageList().size(), expectedErrorsNumber);
119
120     ErrorMessage errorMessage =
121         new ErrorMessage(ErrorLevel.ERROR, ErrorLevel.ERROR.toString() + ": " + message);
122     Assert.assertTrue(messageContainer.getErrorMessageList().contains(errorMessage));
123   }
124 }