Publish 1.13.5 container release
[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.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.IOException;
30 import java.util.Base64;
31 import java.util.Collections;
32
33 import javax.ws.rs.core.Response;
34
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.Mock;
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.EventCallback;
44 import org.onap.aai.modelloader.notification.NotificationPublisher;
45 import org.onap.aai.modelloader.restclient.BabelServiceClient;
46 import org.onap.aai.modelloader.restclient.BabelServiceClientException;
47 import org.onap.aai.modelloader.util.ArtifactTestUtils;
48 import org.onap.sdc.api.IDistributionClient;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.boot.test.context.SpringBootTest;
51 import org.springframework.test.context.TestPropertySource;
52
53 /**
54  * Tests for the ModelLoaderService class.
55  *
56  */
57 @SpringBootTest
58 @TestPropertySource(properties = {"CONFIG_HOME=src/test/resources",})
59 public class TestModelController {
60
61     @Autowired IDistributionClient iDistributionClient;
62     @Autowired ModelLoaderConfig modelLoaderConfig;
63     @Autowired EventCallback eventCallback;
64     @Autowired ArtifactDeploymentManager artifactDeploymentManager;
65     @Autowired BabelArtifactConverter babelArtifactConverter;
66     @Autowired NotificationPublisher notificationPublisher;
67     @Autowired VnfCatalogExtractor vnfCatalogExtractor;
68
69     @Mock BabelServiceClientFactory clientFactory;
70     @Mock BabelServiceClient babelServiceClient;
71     
72     private ModelController modelController;
73
74     @BeforeEach
75     public void init() throws BabelServiceClientException {
76         when(clientFactory.create(any())).thenReturn(babelServiceClient);
77         when(babelServiceClient.postArtifact(any(), any(), any(), any())).thenReturn(Collections.emptyList());
78         ArtifactDownloadManager artifactDownloadManager = new ArtifactDownloadManager(iDistributionClient, modelLoaderConfig, clientFactory, babelArtifactConverter, notificationPublisher, vnfCatalogExtractor);
79         this.modelController = new ModelController(iDistributionClient, modelLoaderConfig, eventCallback, artifactDeploymentManager, artifactDownloadManager);
80     }
81
82     @AfterEach
83     public void shutdown() {
84         modelController.preShutdownOperations();
85     }
86
87     @Test
88     public void testLoadModel() {
89         Response response = modelController.loadModel("");
90         assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
91     }
92
93     @Test
94     public void testSaveModel() {
95         Response response = modelController.saveModel("", "");
96         assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
97     }
98
99     @Test
100     public void testIngestModel() throws IOException {
101         byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
102         Response response = modelController.ingestModel("model-name", "", Base64.getEncoder().encodeToString(csarPayload));
103         assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
104     }
105
106     @Test
107     public void testIngestModelMissingName() throws IOException {
108         byte[] csarPayload = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
109         Response response = modelController.ingestModel("", "", Base64.getEncoder().encodeToString(csarPayload));
110         assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
111     }
112
113 }