Return List<Artifact> in ArtifactDownloadManager
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / service / TestModelController.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.service;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.when;
27
28 import java.io.IOException;
29 import java.util.Base64;
30 import java.util.Collections;
31
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.onap.aai.modelloader.babel.BabelArtifactService;
37 import org.onap.aai.modelloader.config.ModelLoaderConfig;
38 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
39 import org.onap.aai.modelloader.notification.ArtifactDownloadManager;
40 import org.onap.aai.modelloader.notification.BabelArtifactConverter;
41 import org.onap.aai.modelloader.notification.NotificationPublisher;
42 import org.onap.aai.modelloader.restclient.BabelServiceClient;
43 import org.onap.aai.modelloader.restclient.BabelServiceClientException;
44 import org.onap.aai.modelloader.util.ArtifactTestUtils;
45 import org.onap.sdc.api.IDistributionClient;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.test.context.TestPropertySource;
51
52 /**
53  * Tests for the ModelLoaderService class.
54  *
55  */
56 @SpringBootTest
57 @TestPropertySource(properties = {"CONFIG_HOME=src/test/resources",})
58 public class TestModelController {
59
60     @Autowired IDistributionClient iDistributionClient;
61     @Autowired ModelLoaderConfig modelLoaderConfig;
62     @Autowired ArtifactDeploymentManager artifactDeploymentManager;
63     @Autowired BabelArtifactConverter babelArtifactConverter;
64     @Autowired NotificationPublisher notificationPublisher;
65     @Autowired VnfCatalogExtractor vnfCatalogExtractor;
66
67     @Mock BabelServiceClient babelServiceClient;
68     @InjectMocks BabelArtifactService babelArtifactService;
69     
70     private ModelController modelController;
71
72     @BeforeEach
73     public void init() throws BabelServiceClientException {
74         when(babelServiceClient.postArtifact(any(), any())).thenReturn(Collections.emptyList());
75         ArtifactDownloadManager artifactDownloadManager = new ArtifactDownloadManager(iDistributionClient, notificationPublisher, vnfCatalogExtractor, babelArtifactService);
76         this.modelController = new ModelController(iDistributionClient, modelLoaderConfig, artifactDeploymentManager, artifactDownloadManager);
77     }
78
79     @Test
80     public void testLoadModel() {
81         ResponseEntity<String> response = modelController.loadModel("");
82         assertThat(response.getStatusCode(), is(HttpStatus.OK));
83     }
84
85     @Test
86     public void testSaveModel() {
87         ResponseEntity<String> response = modelController.saveModel("", "");
88         assertThat(response.getStatusCode(), is(HttpStatus.OK));
89     }
90
91     @Test
92     public void testIngestModel() throws IOException {
93         byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
94         ResponseEntity<String> response = modelController.ingestModel("model-name", "", Base64.getEncoder().encodeToString(csarPayload));
95         assertThat(response.getStatusCode(), is(HttpStatus.OK));
96     }
97
98     @Test
99     public void testIngestModelMissingName() throws IOException {
100         byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
101         ResponseEntity<String> response = modelController.ingestModel("", "", Base64.getEncoder().encodeToString(csarPayload));
102         assertThat(response.getStatusCode(), is(HttpStatus.INTERNAL_SERVER_ERROR));
103     }
104
105 }