2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.core.validation.types;
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;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
32 import java.util.function.BiPredicate;
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";
43 public void testAddMessageCode() {
44 GlobalValidationContext globalValidationContext = new GlobalValidationContext();
45 ErrorMessageCode error = new ErrorMessageCode("Error");
46 globalValidationContext.setMessageCode(error);
48 Assert.assertEquals(error, globalValidationContext.getMessageCode());
52 public void testAddFileContext() {
53 GlobalValidationContext globalValidationContext = new GlobalValidationContext();
54 globalValidationContext.addFileContext(filename, content.getBytes());
55 Map<String, FileValidationContext> fileContextMap = globalValidationContext.getFileContextMap();
57 Assert.assertTrue(MapUtils.isNotEmpty(fileContextMap));
58 Assert.assertTrue(fileContextMap.containsKey(filename));
62 public void testGetContextMessageContainers() {
63 GlobalValidationContext globalValidationContext = new GlobalValidationContext();
64 globalValidationContext.addMessage(filename, ErrorLevel.ERROR, message);
66 Map<String, MessageContainer> messageContainers =
67 globalValidationContext.getContextMessageContainers();
69 testIfFileHasMessageContainer(messageContainers);
73 public void testAddMessage() {
74 GlobalValidationContext globalValidationContext = new GlobalValidationContext();
75 globalValidationContext.addMessage(filename, ErrorLevel.ERROR, message);
77 Map<String, MessageContainer> messageContainers =
78 globalValidationContext.getContextMessageContainers();
80 testIfFileHasMessageContainer(messageContainers);
81 testIfFileHasErrorMessage(messageContainers, 1);
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);
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);
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)));
110 private void testIfFileHasMessageContainer(Map<String, MessageContainer> messageContainers) {
111 Assert.assertNotNull(messageContainers);
112 Assert.assertTrue(messageContainers.containsKey(filename));
115 private void testIfFileHasErrorMessage(Map<String, MessageContainer> messageContainers,
116 int expectedErrorsNumber) {
117 MessageContainer messageContainer = messageContainers.get(filename);
118 Assert.assertEquals(messageContainer.getErrorMessageList().size(), expectedErrorsNumber);
120 ErrorMessage errorMessage =
121 new ErrorMessage(ErrorLevel.ERROR, ErrorLevel.ERROR.toString() + ": " + message);
122 Assert.assertTrue(messageContainer.getErrorMessageList().contains(errorMessage));