Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDownloadManagerVnfcTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
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 package org.onap.aai.modelloader.notification;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.when;
30 import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
36
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.aai.babel.service.data.BabelArtifact;
44 import org.onap.aai.modelloader.babel.BabelArtifactService;
45 import org.onap.aai.modelloader.entity.Artifact;
46 import org.onap.aai.modelloader.entity.ArtifactType;
47 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact;
48 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
49 import org.onap.aai.modelloader.entity.model.ModelArtifact;
50 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
51 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
52 import org.onap.aai.modelloader.restclient.BabelServiceClient;
53 import org.onap.aai.modelloader.restclient.BabelServiceClientException;
54 import org.onap.aai.modelloader.service.BabelServiceClientFactory;
55 import org.onap.aai.modelloader.util.ArtifactTestUtils;
56 import org.onap.sdc.api.IDistributionClient;
57 import org.onap.sdc.api.notification.IArtifactInfo;
58 import org.onap.sdc.api.notification.INotificationData;
59 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
60 import org.onap.sdc.impl.DistributionClientDownloadResultImpl;
61 import org.onap.sdc.utils.DistributionActionResultEnum;
62
63 /**
64  * Tests {@link ArtifactDownloadManager} with VNF Catalog Artifacts.
65  */
66 public class ArtifactDownloadManagerVnfcTest {
67
68     @Mock private ArtifactDownloadManager downloadManager;
69     @Mock private BabelServiceClient mockBabelClient;
70     @Mock private IDistributionClient mockDistributionClient;
71     @Mock private NotificationPublisher mockNotificationPublisher;
72     @Mock private BabelArtifactConverter mockBabelArtifactConverter;
73     @Mock private BabelServiceClientFactory mockClientFactory;
74     @Mock private VnfCatalogExtractor mockVnfCatalogExtractor;
75     @InjectMocks private BabelArtifactService babelArtifactService;
76
77     @BeforeEach
78     public void setup() throws Exception {
79         MockitoAnnotations.openMocks(this);
80         when(mockClientFactory.create(Mockito.any())).thenReturn(mockBabelClient);
81
82         Properties configProperties = new Properties();
83         configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties"));
84         downloadManager = new ArtifactDownloadManager(mockDistributionClient,
85                 mockNotificationPublisher, mockVnfCatalogExtractor, babelArtifactService);
86     }
87
88     @Test
89     public void downloadArtifacts_validToscaVnfcCsarFile()
90             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
91         INotificationData data = getNotificationDataWithToscaCsarFile();
92         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
93
94         setupValidDownloadCsarMocks(data, artifactInfo);
95         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts());
96         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
97
98         List<Artifact> modelArtifacts = new ArrayList<>();
99         List<Artifact> catalogFiles = new ArrayList<>();
100         assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
101                 is(true));
102
103         assertEquals(1, catalogFiles.size(), "There should be a catalog file");
104     }
105
106     @Test
107     public void downloadArtifacts_validXmlVnfcCsarFile()
108             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
109         INotificationData data = getNotificationDataWithToscaCsarFile();
110         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
111
112         setupValidDownloadCsarMocks(data, artifactInfo);
113         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifactsNoVnfc());
114         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
115
116         List<Artifact> modelArtifacts = new ArrayList<>();
117         List<Artifact> catalogFiles = new ArrayList<>();
118         assertTrue(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles));
119
120         assertEquals(3, catalogFiles.size(), "There should be three catalog artifacts");
121         assertEquals(1, modelArtifacts.size(), "There should be a model artifact");
122     }
123
124     @Test
125     public void downloadArtifacts_validNoVnfcCsarFile()
126             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
127         INotificationData data = getNotificationDataWithToscaCsarFile();
128         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
129
130         setupValidDownloadCsarMocks(data, artifactInfo);
131         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifactsNoVnfc());
132         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(new ArrayList<>());
133
134         List<Artifact> modelArtifacts = new ArrayList<>();
135         List<Artifact> catalogFiles = new ArrayList<>();
136         assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
137                 is(true));
138
139         assertEquals(0, catalogFiles.size(), "There should not have been any catalog files");
140     }
141
142     @Test
143     public void downloadArtifacts_invalidXmlAndToscaVnfcCsarFile()
144             throws IOException, BabelServiceClientException, BabelArtifactParsingException, InvalidArchiveException {
145         INotificationData data = getNotificationDataWithToscaCsarFile();
146         IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0);
147
148         setupValidDownloadCsarMocks(data, artifactInfo);
149         when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts());
150         when(mockVnfCatalogExtractor.extract(any(), any())).thenReturn(createXmlVnfcArtifacts());
151         doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
152
153         List<Artifact> modelArtifacts = new ArrayList<>();
154         List<Artifact> catalogFiles = new ArrayList<>();
155         assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles),
156                 is(false));
157
158         Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo);
159     }
160
161     private IDistributionClientDownloadResult createDistributionClientDownloadResult(
162             DistributionActionResultEnum status, String message, byte[] payload) {
163         IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message);
164
165         ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload);
166
167         return downloadResult;
168     }
169
170     private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo)
171             throws IOException, BabelServiceClientException, BabelArtifactParsingException {
172         when(mockDistributionClient.download(artifactInfo))
173                 .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null,
174                         new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar")));
175         when(mockBabelArtifactConverter.convertToModel(Mockito.any(BabelArtifact.class))).thenReturn(createModelArtifacts());
176         when(mockBabelArtifactConverter.convertToCatalog(Mockito.any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("Some VNFC payload"));
177     }
178
179     private List<BabelArtifact> createBabelArtifacts() {
180         List<BabelArtifact> artifactList = new ArrayList<>();
181         artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
182         artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload"));
183         return artifactList;
184     }
185
186     private List<BabelArtifact> createBabelArtifactsNoVnfc() {
187         List<BabelArtifact> artifactList = new ArrayList<>();
188         artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload"));
189         return artifactList;
190     }
191
192     private List<Artifact> createModelArtifacts() {
193         List<Artifact> modelArtifacts = new ArrayList<>();
194         modelArtifacts.add(new ModelArtifact());
195         return modelArtifacts;
196     }
197
198     private List<Artifact> createXmlVnfcArtifacts() {
199         List<Artifact> vnfcArtifacts = new ArrayList<>();
200         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
201         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
202         vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload"));
203         return vnfcArtifacts;
204     }
205 }