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