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