2dca89f2fa63b3ffc1fc8685748ee419e23e7113
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (c) 2019 Samsung
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.asdctool.impl.validator.tasks.artifacts;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.when;
31
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Objects;
38
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
48 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
49 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
50 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
51
52 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
53 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
54 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
55 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
56 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58
59 import fj.data.Either;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
63
64 @RunWith(PowerMockRunner.class)
65 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
66 @PrepareForTest({ReportManager.class})
67 public class ArtifactValidationUtilsTest {
68     
69     @Mock
70     private ArtifactCassandraDao artifactCassandraDao;
71     @Mock
72     private TopologyTemplateOperation topologyTemplateOperation;
73     @InjectMocks
74     private ArtifactValidationUtils testSubject;
75
76     @Mock
77     private GraphVertex vertex;
78     @Mock
79     private MapArtifactDataDefinition mapToscaDataDefinition;
80     @Mock
81     private ArtifactDataDefinition artifactDataDefinition;
82     @Mock
83     private ArtifactDataDefinition artifactDataDefinitionNotInCassandra;
84     @Mock
85     private ArtifactDataDefinition artifactDataDefinitionDummy;
86     @Mock
87     private TopologyTemplate topologyTemplate;
88
89     private static final String ES_ID = "testEsInCassandra";
90     private static final String ES_ID_NOT_IN_CASS = "testEsNotInCassandra";
91     private static final String TASK_NAME = "testTaskName";
92     private static final String UNIQUE_ID = "4321";
93     private static final String UNIQUE_ID_VERTEX = "321";
94
95     public void initReportManager() {
96         String resourcePath = new File(Objects
97             .requireNonNull(ArtifactValidationUtilsTest.class.getClassLoader().getResource(""))
98             .getFile()).getAbsolutePath();
99         ValidationConfigManager.setOutputFullFilePath(resourcePath);
100         new ReportManager();
101     }
102
103     @Before
104     public void setup() {
105         initReportManager();
106         when(artifactCassandraDao.getCountOfArtifactById(ES_ID)).thenReturn(Either.left(1L));
107         when(artifactCassandraDao.getCountOfArtifactById(ES_ID_NOT_IN_CASS))
108             .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
109
110         when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
111         when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
112
113         when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
114         when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
115     }
116
117     @Test
118     public void testValidateArtifactsAreInCassandra() {
119         // given
120         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
121         artifacts.add(artifactDataDefinition);
122
123         // when
124         ArtifactsVertexResult result =
125             testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts);
126
127         // then
128         assertTrue(result.getStatus());
129         assertEquals(0, result.notFoundArtifacts.size());
130     }
131
132     @Test
133     public void testValidateArtifactsNotInCassandra() {
134         // given
135         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
136         artifacts.add(artifactDataDefinition);
137         artifacts.add(artifactDataDefinitionNotInCassandra);
138
139         // when
140         ArtifactsVertexResult result =
141             testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts);
142
143         // then
144         assertFalse(result.getStatus());
145         assertEquals(1, result.notFoundArtifacts.size());
146         assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
147     }
148
149     @Test
150     public void testIsArtifactsInCassandra() {
151         // when
152         boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
153         boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
154
155         // then
156         assertFalse(notInCass);
157         assertTrue(inCass);
158     }
159
160     @Test
161     public void testAddRelevantArtifacts() {
162         // given
163         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
164         artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
165         artifactsMap.put(ES_ID, artifactDataDefinition);
166
167         // when
168         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
169
170         // then
171         result.forEach(Assert::assertNotNull);
172     }
173
174     @Test
175     public void testAddRelevantArtifactsWithNullEsId() {
176         // given
177         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
178         artifactsMap.put("", artifactDataDefinitionDummy);
179
180         // when
181         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
182
183         // then
184         assertEquals(0, result.size());
185     }
186
187     @Test
188     public void testValidateTopologyTemplateArtifacts() {
189         // given
190                 Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
191                 artifacts.put(ES_ID, artifactDataDefinition);
192
193                 when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
194                 when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
195                 when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
196
197                 when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
198                 Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
199                 artifactsMap.put(ES_ID, mapToscaDataDefinition);
200
201                 when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
202                 when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
203
204         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
205             .thenReturn(Either.left(topologyTemplate));
206
207         // when
208         ArtifactsVertexResult result =
209             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME);
210
211         // then
212                 assertTrue(result.getStatus());
213                 assertEquals(0, result.notFoundArtifacts.size());
214     }
215
216     @Test
217     public void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
218         // given
219         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
220             .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
221
222         // when
223         ArtifactsVertexResult result =
224             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME);
225
226         // then
227         assertFalse(result.getStatus());
228     }
229 }