2e2b48b8d043614ace0f752dde47a18ad5e62d8d
[aai/schema-service.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2025 Deutsche Telekom. 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.onap.aai.schemaservice.nodeschema.validation;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.mockito.*;
25 import org.onap.aai.schemaservice.config.ConfigTranslator;
26 import org.onap.aai.schemaservice.nodeschema.SchemaVersion;
27 import java.util.*;
28 import static org.mockito.Mockito.*;
29
30 import static org.junit.jupiter.api.Assertions.*;
31
32 class NodeValidatorTest {
33
34     @Mock
35     private ConfigTranslator mockTranslator;
36
37     @Mock
38     private SchemaErrorStrategy mockErrorStrategy;
39
40     @Mock
41     private DuplicateNodeDefinitionValidationModule mockDupChecker;
42
43     private NodeValidator nodeValidator;
44
45     @BeforeEach
46     void setUp() {
47         MockitoAnnotations.openMocks(this);
48         nodeValidator = new NodeValidator(mockTranslator, mockErrorStrategy, mockDupChecker);
49     }
50
51     @Test
52     void testValidate_NoDuplicates_ShouldReturnTrue() {
53         SchemaVersion schemaVersion = mock(SchemaVersion.class);
54         List<String> nodeFiles = Arrays.asList("file1", "file2", "file3");
55
56         Map<SchemaVersion, List<String>> mockNodeFiles = new HashMap<>();
57         mockNodeFiles.put(schemaVersion, nodeFiles);
58
59         when(mockTranslator.getNodeFiles()).thenReturn(mockNodeFiles);
60         when(mockDupChecker.findDuplicates(nodeFiles, schemaVersion)).thenReturn("");
61
62         boolean result = nodeValidator.validate();
63         assertFalse(result);
64
65         verify(mockErrorStrategy, never()).notifyOnError(anyString());
66     }
67
68     @Test
69     void testValidate_WithDuplicates_ShouldReturnFalse() {
70         SchemaVersion schemaVersion = mock(SchemaVersion.class);
71         List<String> nodeFiles = Arrays.asList("file1", "file2", "file3");
72
73         Map<SchemaVersion, List<String>> mockNodeFiles = new HashMap<>();
74         mockNodeFiles.put(schemaVersion, nodeFiles);
75
76         when(mockTranslator.getNodeFiles()).thenReturn(mockNodeFiles);
77         when(mockDupChecker.findDuplicates(nodeFiles, schemaVersion)).thenReturn("Duplicate found!");
78         boolean result = nodeValidator.validate();
79
80         assertFalse(result);
81         verify(mockErrorStrategy).notifyOnError("Duplicate found!");
82     }
83
84     @Test
85     void testGetErrorMsg_WithoutError_ShouldReturnEmptyString() {
86         when(mockErrorStrategy.getErrorMsg()).thenReturn("");
87
88         String errorMsg = nodeValidator.getErrorMsg();
89
90         assertEquals("", errorMsg);
91     }
92
93     @Test
94     void testGetErrorMsg_WithError_ShouldReturnErrorMessage() {
95         when(mockErrorStrategy.getErrorMsg()).thenReturn("Some error occurred");
96
97         String errorMsg = nodeValidator.getErrorMsg();
98
99         assertEquals("Some error occurred", errorMsg);
100     }
101 }