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