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