Add validation for asd descriptor and invariant id
[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)
84                             .state(RappState.COMMISSIONED).build();
85         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
86         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
87         deploymentItems.forEach(deploymentItem -> mockServer.expect(ExpectedCount.once(),
88                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
89                                                           .andRespond(withStatus(status)));
90         assertTrue(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
91         mockServer.verify();
92     }
93
94     @Test
95     void testChartUploadNoArtifacts() {
96         String invalidRappFile = "invalid-rapp-package.csar";
97         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(invalidRappFile)
98                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
99         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
100         assertTrue(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
101     }
102
103     @Test
104     void testChartUploadFailure() {
105         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
106                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
107         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
108         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
109         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
110                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
111                                                                                  .andRespond(withServerError()));
112         RappHandlerException exception = assertThrows(RappHandlerException.class,
113                 () -> deploymentArtifactsService.configureDeploymentArtifacts(rapp));
114         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
115         mockServer.verify();
116     }
117
118     @Test
119     void testChartUploadFailureWithNotFound() {
120         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
121                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
122         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
123         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
124         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
125                 requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
126                 withStatus(HttpStatus.NOT_FOUND)));
127         assertFalse(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
128         mockServer.verify();
129     }
130
131     @Test
132     void testChartUploadFailureWithException() {
133         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
134                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
135         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
136         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
137         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
138                 requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
139                 withException(new IOException())));
140         RappHandlerException exception = assertThrows(RappHandlerException.class,
141                 () -> deploymentArtifactsService.configureDeploymentArtifacts(rapp));
142         assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
143         mockServer.verify();
144     }
145
146     @Test
147     void testChartUploadFailureWithTooManyRequests() {
148         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
149                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
150         rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
151         List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
152         deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
153                         requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
154                                                                                  .andRespond(withTooManyRequests()));
155         assertFalse(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
156         mockServer.verify();
157     }
158 }