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
9 * http://www.apache.org/licenses/LICENSE-2.0
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========================================================================
20 package com.oransc.rappmanager.service;
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;
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;
56 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
57 class DeploymentArtifactsServiceTest {
60 DeploymentArtifactsService deploymentArtifactsService;
62 RestTemplate restTemplate;
64 RappCsarConfigurationHandler rappCsarConfigurationHandler;
66 SmeLifecycleManager smeLifecycleManager;
68 MockRestServiceServer mockServer;
70 String validCsarFileLocation = "src/test/resources/";
72 private final String validRappFile = "valid-rapp-package.csar";
76 mockServer = MockRestServiceServer.createServer(restTemplate);
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));
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));
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());
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));
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());
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));