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