d1978b2d38586baf361f0cf8433d103ec9883ae9
[nonrtric/plt/rappmanager.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2024-2025 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 org.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 java.io.IOException;
34 import java.util.List;
35 import java.util.UUID;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.params.ParameterizedTest;
39 import org.junit.jupiter.params.provider.EnumSource;
40 import org.oransc.rappmanager.models.csar.DeploymentItem;
41 import org.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
42 import org.oransc.rappmanager.models.exception.RappHandlerException;
43 import org.oransc.rappmanager.models.rapp.Rapp;
44 import org.oransc.rappmanager.models.rapp.RappState;
45 import org.oransc.rappmanager.sme.service.SmeLifecycleManager;
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     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         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
85         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
86         deploymentItems.forEach(deploymentItem -> mockServer.expect(ExpectedCount.once(),
87                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
88                                                           .andRespond(withStatus(status)));
89         assertTrue(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
90         mockServer.verify();
91     }
92
93     @Test
94     void testChartUploadNoArtifacts() {
95         String invalidRappFile = "valid-rapp-package-no-artifacts.csar";
96         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(invalidRappFile)
97                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
98         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
99         assertTrue(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
100     }
101
102     @Test
103     void testChartUploadFailure() {
104         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
105                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
106         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
107         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
108         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
109                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
110                                                                                  .andRespond(withServerError()));
111         RappHandlerException exception = assertThrows(RappHandlerException.class,
112                 () -> deploymentArtifactsService.configureDeploymentArtifacts(rapp));
113         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
114         mockServer.verify();
115     }
116
117     @Test
118     void testChartUploadFailureWithNotFound() {
119         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
120                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
121         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
122         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
123         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
124                 requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
125                 withStatus(HttpStatus.NOT_FOUND)));
126         assertFalse(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
127         mockServer.verify();
128     }
129
130     @Test
131     void testChartUploadFailureWithException() {
132         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
133                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
134         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
135         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
136         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
137                 requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
138                 withException(new IOException())));
139         RappHandlerException exception = assertThrows(RappHandlerException.class,
140                 () -> deploymentArtifactsService.configureDeploymentArtifacts(rapp));
141         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
142         mockServer.verify();
143     }
144
145     @Test
146     void testChartUploadFailureWithTooManyRequests() {
147         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
148                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
149         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
150         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
151         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
152                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
153                                                                                  .andRespond(withTooManyRequests()));
154         assertFalse(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
155         mockServer.verify();
156     }
157 }