Update chart repo url from installation script
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / service / DeploymentArtifactsServiceTest.java
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END========================================================================
17  *
18  */
19
20 package com.oransc.rappmanager.service;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
27 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
28 import static org.springframework.test.web.client.response.MockRestResponseCreators.withException;
29 import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
30 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withTooManyRequests;
32
33 import com.oransc.rappmanager.models.csar.DeploymentItem;
34 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
35 import com.oransc.rappmanager.models.exception.RappHandlerException;
36 import com.oransc.rappmanager.models.rapp.Rapp;
37 import com.oransc.rappmanager.models.rapp.RappState;
38 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
39 import java.io.IOException;
40 import java.util.List;
41 import java.util.UUID;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44 import org.junit.jupiter.params.ParameterizedTest;
45 import org.junit.jupiter.params.provider.EnumSource;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.mock.mockito.MockBean;
49 import org.springframework.boot.test.mock.mockito.SpyBean;
50 import org.springframework.http.HttpMethod;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.test.web.client.ExpectedCount;
53 import org.springframework.test.web.client.MockRestServiceServer;
54 import org.springframework.web.client.RestTemplate;
55
56 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
57 class DeploymentArtifactsServiceTest {
58
59     @SpyBean
60     DeploymentArtifactsService deploymentArtifactsService;
61     @Autowired
62     RestTemplate restTemplate;
63     @Autowired
64     RappCsarConfigurationHandler rappCsarConfigurationHandler;
65     @MockBean
66     SmeLifecycleManager smeLifecycleManager;
67
68     MockRestServiceServer mockServer;
69
70     String validCsarFileLocation = "src/test/resources/";
71
72     private final String validRappFile = "valid-rapp-package.csar";
73
74     @BeforeEach
75     public void init() {
76         mockServer = MockRestServiceServer.createServer(restTemplate);
77     }
78
79     @ParameterizedTest
80     @EnumSource(value = HttpStatus.class, names = {"CREATED", "CONFLICT"})
81     void testChartUpload(HttpStatus status) {
82         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
83                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
84         List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
85         deploymentItems.forEach(deploymentItem -> mockServer.expect(ExpectedCount.once(),
86                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
87                                                           .andRespond(withStatus(status)));
88         assertTrue(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
89         mockServer.verify();
90     }
91
92     @Test
93     void testChartUploadNoArtifacts() {
94         String invalidRappFile = "invalid-rapp-package.csar";
95         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(invalidRappFile)
96                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
97         assertTrue(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
98     }
99
100     @Test
101     void testChartUploadFailure() {
102         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
103                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
104         List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
105         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
106                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
107                                                                                  .andRespond(withServerError()));
108         RappHandlerException exception = assertThrows(RappHandlerException.class,
109                 () -> deploymentArtifactsService.configureDeploymentArtifacts(rapp));
110         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
111         mockServer.verify();
112     }
113
114     @Test
115     void testChartUploadFailureWithNotFound() {
116         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
117                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
118         List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
119         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
120                 requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
121                 withStatus(HttpStatus.NOT_FOUND)));
122         assertFalse(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
123         mockServer.verify();
124     }
125
126     @Test
127     void testChartUploadFailureWithException() {
128         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
129                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
130         List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
131         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
132                 requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
133                 withException(new IOException())));
134         RappHandlerException exception = assertThrows(RappHandlerException.class,
135                 () -> deploymentArtifactsService.configureDeploymentArtifacts(rapp));
136         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
137         mockServer.verify();
138     }
139
140     @Test
141     void testChartUploadFailureWithTooManyRequests() {
142         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
143                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
144         List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
145         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
146                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
147                                                                                  .andRespond(withTooManyRequests()));
148         assertFalse(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
149         mockServer.verify();
150     }
151 }