99733a7bd379f1bd7831f273ea52df0ffc6f06ec
[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 fj.data.Either;
25 import org.junit.After;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.openecomp.sdc.asdctool.impl.validator.config.ValidationConfigManager;
34 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
35 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManagerHelper;
36 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
37 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
38 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
39 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
43 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
47
48 import java.io.File;
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.stream.IntStream;
54
55 import static org.junit.Assert.assertEquals;
56 import static org.junit.Assert.assertFalse;
57 import static org.junit.Assert.assertTrue;
58 import static org.mockito.ArgumentMatchers.any;
59 import static org.mockito.ArgumentMatchers.eq;
60 import static org.mockito.Mockito.when;
61
62 @RunWith(PowerMockRunner.class)
63 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
64 @PrepareForTest({ReportManager.class})
65 public class ArtifactValidationUtilsTest {
66
67     @Mock
68     private ArtifactCassandraDao artifactCassandraDao;
69     @Mock
70     private TopologyTemplateOperation topologyTemplateOperation;
71     @InjectMocks
72     private ArtifactValidationUtils testSubject;
73
74     @Mock
75     private GraphVertex vertex;
76     @Mock
77     private MapArtifactDataDefinition mapToscaDataDefinition;
78     @Mock
79     private ArtifactDataDefinition artifactDataDefinition;
80     @Mock
81     private ArtifactDataDefinition artifactDataDefinitionNotInCassandra;
82     @Mock
83     private ArtifactDataDefinition artifactDataDefinitionDummy;
84     @Mock
85     private TopologyTemplate topologyTemplate;
86
87     private static final String ES_ID = "testEsInCassandra";
88     private static final String ES_ID_NOT_IN_CASS = "testEsNotInCassandra";
89     private static final String TASK_NAME = "testTaskName";
90     private static final String UNIQUE_ID = "4321";
91     private static final String UNIQUE_ID_VERTEX = "321";
92
93     private final static String resourcePath = new File("src/test/resources").getAbsolutePath();
94     private final static String csvReportFilePath = ValidationConfigManager.DEFAULT_CSV_PATH;
95     private final static String txtReportFilePath = ValidationConfigManager.txtReportFilePath(resourcePath);
96
97     public void initReportManager() {
98         ReportManager.make(csvReportFilePath, txtReportFilePath);
99     }
100
101     @Before
102     public void setup() {
103         initReportManager();
104         when(artifactCassandraDao.getCountOfArtifactById(ES_ID)).thenReturn(Either.left(1L));
105         when(artifactCassandraDao.getCountOfArtifactById(ES_ID_NOT_IN_CASS))
106             .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
107
108         when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
109         when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
110
111         when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
112         when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
113     }
114
115     @After
116     public void clean() {
117         ReportManagerHelper.cleanReports(csvReportFilePath, txtReportFilePath);
118     }
119
120     @Test
121     public void testValidateArtifactsAreInCassandra() {
122         // given
123         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
124         artifacts.add(artifactDataDefinition);
125
126         // when
127         ArtifactsVertexResult result =
128             testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts, txtReportFilePath);
129
130         List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
131
132         // then
133         assertTrue(result.getStatus());
134         assertEquals(0, result.notFoundArtifacts.size());
135         assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
136     }
137
138     @Test
139     public void testValidateArtifactsNotInCassandra() {
140         // given
141         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
142         artifacts.add(artifactDataDefinition);
143         artifacts.add(artifactDataDefinitionNotInCassandra);
144
145         // when
146         ArtifactsVertexResult result =
147             testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts, txtReportFilePath);
148         ReportManager.reportEndOfToolRun(csvReportFilePath, txtReportFilePath);
149
150         List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
151
152         // then
153         assertFalse(result.getStatus());
154         assertEquals(1, result.notFoundArtifacts.size());
155         assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
156
157         assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
158         assertEquals("Artifact " + ES_ID_NOT_IN_CASS + " doesn't exist in Cassandra",
159             reportOutputFile.get(3));
160         assertEquals("Task: " + TASK_NAME, reportOutputFile.get(5));
161         assertEquals("FailedVertices: [" + UNIQUE_ID_VERTEX + "]", reportOutputFile.get(6));
162     }
163
164     @Test
165     public void testIsArtifactsInCassandra() {
166         // when
167         boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
168         boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
169
170         // then
171         assertFalse(notInCass);
172         assertTrue(inCass);
173     }
174
175     @Test
176     public void testAddRelevantArtifacts() {
177         // given
178         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
179         artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
180         artifactsMap.put(ES_ID, artifactDataDefinition);
181
182         // when
183         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
184
185         // then
186         result.forEach(Assert::assertNotNull);
187     }
188
189     @Test
190     public void testAddRelevantArtifactsWithNullEsId() {
191         // given
192         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
193         artifactsMap.put("", artifactDataDefinitionDummy);
194
195         // when
196         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
197
198         // then
199         assertEquals(0, result.size());
200     }
201
202     @Test
203     public void testValidateTopologyTemplateArtifacts() {
204         // given
205         Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
206         artifacts.put(ES_ID, artifactDataDefinition);
207
208         when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
209         when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
210         when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
211
212         when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
213         Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
214         artifactsMap.put(ES_ID, mapToscaDataDefinition);
215
216         when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
217         when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
218
219         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
220             .thenReturn(Either.left(topologyTemplate));
221
222         // when
223         ArtifactsVertexResult result =
224             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME, txtReportFilePath);
225
226         List<String> reportOutputFile = ReportManagerHelper.getReportOutputFileAsList(txtReportFilePath);
227
228         // then
229         assertTrue(result.getStatus());
230         assertEquals(0, result.notFoundArtifacts.size());
231
232         IntStream.range(2, reportOutputFile.size()).forEach(
233             i -> assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(i)));
234     }
235
236     @Test
237     public void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
238         // given
239         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
240             .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
241
242         // when
243         ArtifactsVertexResult result =
244             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME, txtReportFilePath);
245
246         // then
247         assertFalse(result.getStatus());
248     }
249 }