Catalog alignment
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / impl / validator / tasks / artifacts / ArtifactValidationUtilsTest.java
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     public void initReportManager() {
95         String resourcePath = new File(Objects
96             .requireNonNull(ArtifactValidationUtilsTest.class.getClassLoader().getResource(""))
97             .getFile()).getAbsolutePath();
98         ValidationConfigManager.setOutputFullFilePath(resourcePath);
99         new ReportManager();
100     }
101
102     @Before
103     public void setup() {
104         initReportManager();
105         when(artifactCassandraDao.getCountOfArtifactById(ES_ID)).thenReturn(Either.left(1L));
106         when(artifactCassandraDao.getCountOfArtifactById(ES_ID_NOT_IN_CASS))
107             .thenReturn(Either.right(CassandraOperationStatus.NOT_FOUND));
108
109         when(artifactDataDefinition.getEsId()).thenReturn(ES_ID);
110         when(artifactDataDefinitionNotInCassandra.getEsId()).thenReturn(ES_ID_NOT_IN_CASS);
111
112         when(artifactDataDefinitionNotInCassandra.getUniqueId()).thenReturn(UNIQUE_ID);
113         when(vertex.getUniqueId()).thenReturn(UNIQUE_ID_VERTEX);
114     }
115
116     @After
117     public void clean() {
118         ReportManagerHelper.cleanReports();
119     }
120
121     @Test
122     public void testValidateArtifactsAreInCassandra() {
123         // given
124         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
125         artifacts.add(artifactDataDefinition);
126
127         // when
128         ArtifactsVertexResult result =
129             testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts);
130
131         List reportOutputFile = ReportManagerHelper.getReportOutputFileAsList();
132
133         // then
134         assertTrue(result.getStatus());
135         assertEquals(0, result.notFoundArtifacts.size());
136         assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
137     }
138
139     @Test
140     public void testValidateArtifactsNotInCassandra() {
141         // given
142         List<ArtifactDataDefinition> artifacts = new ArrayList<>();
143         artifacts.add(artifactDataDefinition);
144         artifacts.add(artifactDataDefinitionNotInCassandra);
145
146         // when
147         ArtifactsVertexResult result =
148             testSubject.validateArtifactsAreInCassandra(vertex, TASK_NAME, artifacts);
149         ReportManager.reportEndOfToolRun();
150
151         List reportOutputFile = ReportManagerHelper.getReportOutputFileAsList();
152
153         // then
154         assertFalse(result.getStatus());
155         assertEquals(1, result.notFoundArtifacts.size());
156         assertEquals(UNIQUE_ID, result.notFoundArtifacts.iterator().next());
157
158         assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(2));
159         assertEquals("Artifact " + ES_ID_NOT_IN_CASS + " doesn't exist in Cassandra",
160             reportOutputFile.get(3));
161         assertEquals("Task: " + TASK_NAME, reportOutputFile.get(5));
162         assertEquals("FailedVertices: [" + UNIQUE_ID_VERTEX + "]", reportOutputFile.get(6));
163     }
164
165     @Test
166     public void testIsArtifactsInCassandra() {
167         // when
168         boolean notInCass = testSubject.isArtifactInCassandra(ES_ID_NOT_IN_CASS);
169         boolean inCass = testSubject.isArtifactInCassandra(ES_ID);
170
171         // then
172         assertFalse(notInCass);
173         assertTrue(inCass);
174     }
175
176     @Test
177     public void testAddRelevantArtifacts() {
178         // given
179         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
180         artifactsMap.put(ES_ID_NOT_IN_CASS, artifactDataDefinitionNotInCassandra);
181         artifactsMap.put(ES_ID, artifactDataDefinition);
182
183         // when
184         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
185
186         // then
187         result.forEach(Assert::assertNotNull);
188     }
189
190     @Test
191     public void testAddRelevantArtifactsWithNullEsId() {
192         // given
193         Map<String, ArtifactDataDefinition> artifactsMap = new HashMap<>();
194         artifactsMap.put("", artifactDataDefinitionDummy);
195
196         // when
197         List<ArtifactDataDefinition> result = testSubject.addRelevantArtifacts(artifactsMap);
198
199         // then
200         assertEquals(0, result.size());
201     }
202
203     @Test
204     public void testValidateTopologyTemplateArtifacts() {
205         // given
206                 Map<String, ArtifactDataDefinition> artifacts = new HashMap<>();
207                 artifacts.put(ES_ID, artifactDataDefinition);
208
209                 when(topologyTemplate.getDeploymentArtifacts()).thenReturn(artifacts);
210                 when(topologyTemplate.getArtifacts()).thenReturn(artifacts);
211                 when(topologyTemplate.getServiceApiArtifacts()).thenReturn(artifacts);
212
213                 when(mapToscaDataDefinition.getMapToscaDataDefinition()).thenReturn(artifacts);
214                 Map<String, MapArtifactDataDefinition> artifactsMap = new HashMap<>();
215                 artifactsMap.put(ES_ID, mapToscaDataDefinition);
216
217                 when(topologyTemplate.getInstanceArtifacts()).thenReturn(artifactsMap);
218                 when(topologyTemplate.getInstDeploymentArtifacts()).thenReturn(artifactsMap);
219
220         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
221             .thenReturn(Either.left(topologyTemplate));
222
223         // when
224         ArtifactsVertexResult result =
225             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME);
226
227         List reportOutputFile = ReportManagerHelper.getReportOutputFileAsList();
228
229         // then
230         assertTrue(result.getStatus());
231         assertEquals(0, result.notFoundArtifacts.size());
232
233         IntStream.range(2, reportOutputFile.size()).forEach(
234             i -> assertEquals("Artifact " + ES_ID + " is in Cassandra", reportOutputFile.get(i)));
235     }
236
237     @Test
238     public void testValidateTopologyTemplateArtifactsNotFoundToscaElement() {
239         // given
240         when(topologyTemplateOperation.getToscaElement(eq(vertex.getUniqueId()), any()))
241             .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
242
243         // when
244         ArtifactsVertexResult result =
245             testSubject.validateTopologyTemplateArtifacts(vertex, TASK_NAME);
246
247         // then
248         assertFalse(result.getStatus());
249     }
250 }