[SDC] Validate PMDictionary content in Deployment artifacts tab
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / validation / PMDictionaryValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. 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.be.components.impl.validation;
21
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.core.Is.is;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verifyNoInteractions;
27 import static org.mockito.Mockito.when;
28
29 import java.util.List;
30 import java.util.Optional;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.validation.yaml.YamlContentValidator;
34 import org.onap.validation.yaml.error.YamlDocumentValidationError;
35 import org.onap.validation.yaml.exception.YamlProcessingException;
36 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
37
38 class PMDictionaryValidatorTest {
39
40     private YamlContentValidator yamlContentValidator;
41
42     @BeforeEach
43     void setUp() {
44         yamlContentValidator = mock(YamlContentValidator.class);
45     }
46
47     @Test
48     void shouldNotReturnErrors_whenArtifactTypeDoNotMatch() {
49         // when
50         Optional<String> errors = new PMDictionaryValidator(yamlContentValidator)
51             .validateIfPmDictionary(ArtifactTypeEnum.DCAE_INVENTORY_BLUEPRINT.name(), "".getBytes());
52
53         // then
54         assertTrue(errors.isEmpty());
55         verifyNoInteractions(yamlContentValidator);
56     }
57
58     @Test
59     void shouldReturnErrors_whenArtifactTypeIsPmDictionaryAndFileIsInvalid() throws YamlProcessingException {
60         // given
61         byte[] fileContent = "".getBytes();
62         YamlDocumentValidationError validationError = new YamlDocumentValidationError(1, "/", "error");
63         when(yamlContentValidator.validate(fileContent)).thenReturn(List.of(validationError));
64
65         // when
66         Optional<String> errors = new PMDictionaryValidator(yamlContentValidator)
67             .validateIfPmDictionary(ArtifactTypeEnum.PM_DICTIONARY.name(), fileContent);
68
69         // then
70         assertTrue(errors.isPresent());
71         assertThat(errors.get(), is("Line number: 1, Path: /, Message: error"));
72     }
73
74     @Test
75     void shouldNotReturnErrors_whenArtifactTypeIsPmDictionaryAndFileIsValid() throws YamlProcessingException {
76         // given
77         byte[] fileContent = "".getBytes();
78         when(yamlContentValidator.validate(fileContent)).thenReturn(List.of());
79
80         // when
81         Optional<String> errors = new PMDictionaryValidator(yamlContentValidator)
82             .validateIfPmDictionary(ArtifactTypeEnum.PM_DICTIONARY.name(), fileContent);
83
84         // then
85         assertTrue(errors.isEmpty());
86     }
87 }