Merge "Update model-loader dependencies"
[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 javax.ws.rs.core.Response;
33
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.onap.aai.modelloader.babel.BabelArtifactService;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40 import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
41 import org.onap.aai.modelloader.notification.ArtifactDownloadManager;
42 import org.onap.aai.modelloader.notification.BabelArtifactConverter;
43 import org.onap.aai.modelloader.notification.NotificationPublisher;
44 import org.onap.aai.modelloader.restclient.BabelServiceClient;
45 import org.onap.aai.modelloader.restclient.BabelServiceClientException;
46 import org.onap.aai.modelloader.util.ArtifactTestUtils;
47 import org.onap.sdc.api.IDistributionClient;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.test.context.SpringBootTest;
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 BabelServiceClientFactory clientFactory;
68     @Mock BabelServiceClient babelServiceClient;
69     @InjectMocks BabelArtifactService babelArtifactService;
70     
71     private ModelController modelController;
72
73     @BeforeEach
74     public void init() throws BabelServiceClientException {
75         when(clientFactory.create(any())).thenReturn(babelServiceClient);
76         when(babelServiceClient.postArtifact(any(), any())).thenReturn(Collections.emptyList());
77         ArtifactDownloadManager artifactDownloadManager = new ArtifactDownloadManager(iDistributionClient, notificationPublisher, vnfCatalogExtractor, babelArtifactService);
78         this.modelController = new ModelController(iDistributionClient, modelLoaderConfig, artifactDeploymentManager, artifactDownloadManager);
79     }
80
81     @Test
82     public void testLoadModel() {
83         Response response = modelController.loadModel("");
84         assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
85     }
86
87     @Test
88     public void testSaveModel() {
89         Response response = modelController.saveModel("", "");
90         assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
91     }
92
93     @Test
94     public void testIngestModel() throws IOException {
95         byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
96         Response response = modelController.ingestModel("model-name", "", Base64.getEncoder().encodeToString(csarPayload));
97         assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
98     }
99
100     @Test
101     public void testIngestModelMissingName() throws IOException {
102         byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
103         Response response = modelController.ingestModel("", "", Base64.getEncoder().encodeToString(csarPayload));
104         assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
105     }
106
107 }