Return List<Artifact> in ArtifactDownloadManager
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ModelArtifactHandlerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2024 Deutsche Telekom AG Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.modelloader.notification;
21
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.when;
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
26 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
29 import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor;
30 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
31
32 import java.util.List;
33
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40 import org.onap.aai.modelloader.entity.Artifact;
41 import org.onap.aai.modelloader.entity.model.ModelArtifact;
42 import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
43 import org.onap.aai.modelloader.restclient.AaiRestClient;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.MediaType;
50 import org.springframework.http.ResponseEntity;
51
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.github.tomakehurst.wiremock.client.WireMock;
54
55 import java.util.ArrayList;
56
57 @SpringBootTest
58 @AutoConfigureWireMock(port = 0)
59 public class ModelArtifactHandlerTest {
60
61   @Value("${wiremock.server.port}")
62   private int wiremockPort;
63
64   final ObjectMapper objectMapper = new ObjectMapper();
65   @Mock
66   ModelLoaderConfig config;
67   @InjectMocks
68   ModelArtifactHandler modelArtifactHandler;
69
70   @Autowired
71   AaiRestClient restClient;
72
73   @BeforeEach
74   public void setUp() {
75     when(config.getAaiBaseUrl()).thenReturn("http://localhost:" + wiremockPort);
76     when(config.getAaiModelUrl(any())).thenReturn("/aai/v28/service-design-and-creation/models/model/");
77   }
78
79   @Test
80   public void thatArtifactsCanBeCreated() {
81     WireMock.stubFor(
82         WireMock.get(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId"))
83             .withHeader("Accept", equalTo("application/xml"))
84             .withHeader("X-TransactionId", equalTo("someId"))
85             .withHeader("X-FromAppId", equalTo("ModelLoader"))
86             .willReturn(
87                 WireMock.aResponse()
88                     .withStatus(HttpStatus.NOT_FOUND.value())));
89
90     WireMock.stubFor(
91         WireMock.put(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId"))
92             .withHeader("Accept", equalTo(MediaType.APPLICATION_XML_VALUE))
93             .withHeader("Content-Type", equalTo(MediaType.APPLICATION_XML_VALUE))
94             .withHeader("X-TransactionId", equalTo("someId"))
95             .withHeader("X-FromAppId", equalTo("ModelLoader"))
96             .willReturn(
97                 WireMock.aResponse()
98                     .withStatus(HttpStatus.CREATED.value())));
99
100     ModelArtifact modelArtifact = new ModelArtifact();
101     modelArtifact.setModelInvariantId("modelInvariantId");
102     modelArtifact.setPayload("");
103     List<Artifact> artifacts = List.of(modelArtifact);
104     List<Artifact> completedArtifacts = new ArrayList<>();
105
106     boolean result = modelArtifactHandler.pushArtifacts(artifacts,
107         "someId", completedArtifacts, restClient);
108     assertTrue(result);
109     WireMock.verify(
110         WireMock.putRequestedFor(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId")));
111   }
112
113   @Test
114   public void thatArtifactsCanBeUpdated() {
115     // Checks if model exists in resources service
116     WireMock.stubFor(
117         WireMock.get(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId"))
118             .withHeader("Accept", equalTo("application/xml"))
119             .withHeader("X-TransactionId", equalTo("distributionId"))
120             .withHeader("X-FromAppId", equalTo("ModelLoader"))
121             .willReturn(
122                 WireMock.aResponse()
123                     .withStatus(HttpStatus.OK.value())));
124
125     // Checks if specific version of model exists in aai-resources
126     WireMock.stubFor(
127         WireMock.get(urlEqualTo(
128             "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"))
129             .withHeader("Accept", equalTo("application/xml"))
130             .withHeader("X-TransactionId", equalTo("distributionId"))
131             .withHeader("X-FromAppId", equalTo("ModelLoader"))
132             .willReturn(
133                 WireMock.aResponse()
134                     .withStatus(HttpStatus.NOT_FOUND.value())));
135
136     WireMock.stubFor(
137         WireMock.put(urlEqualTo(
138             "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"))
139             .withHeader("Accept", equalTo(MediaType.APPLICATION_XML_VALUE))
140             .withHeader("Content-Type", equalTo(MediaType.APPLICATION_XML_VALUE))
141             .withHeader("X-TransactionId", equalTo("distributionId"))
142             .withHeader("X-FromAppId", equalTo("ModelLoader"))
143             .willReturn(
144                 WireMock.aResponse()
145                     .withStatus(HttpStatus.CREATED.value())));
146
147     ModelArtifact modelArtifact = new ModelArtifact();
148     modelArtifact.setModelInvariantId("modelInvariantId");
149     modelArtifact.setModelVerId("modelVersionId");
150     modelArtifact.setPayload("");
151     modelArtifact.setModelVer("2.0");
152     List<Artifact> artifacts = List.of(modelArtifact);
153     List<Artifact> completedArtifacts = new ArrayList<>();
154
155     boolean result = modelArtifactHandler.pushArtifacts(artifacts,
156         "distributionId", completedArtifacts, restClient);
157     verify(WireMock.putRequestedFor(urlEqualTo(
158         "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId")));
159     assertTrue(result);
160   }
161
162   @Test
163   public void thatModelCanBeRolledBack() {
164     stubFor(WireMock.get(urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3"))
165         .withHeader("Accept", equalTo(MediaType.APPLICATION_XML_VALUE))    
166         .withHeader("X-FromAppId", equalTo("ModelLoader"))
167         .withHeader("X-TransactionId", equalTo("distributionId"))
168         .willReturn(aResponse()
169             .withHeader("Content-Type", MediaType.APPLICATION_XML_VALUE)
170             .withBodyFile("modelResponse.xml")));
171
172     stubFor(WireMock.delete(urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974"))
173         .withHeader("X-FromAppId", equalTo("ModelLoader"))
174         .withHeader("X-TransactionId", equalTo("distributionId"))
175         .willReturn(aResponse().withStatus(HttpStatus.OK.value())));
176
177     ModelArtifact modelArtifact = new ModelArtifact();
178     modelArtifact.setModelInvariantId("3a40ab73-6694-4e75-bb6d-9a4a86ce35b3");
179     modelArtifact.setModelVerId("modelVersionId");
180     // modelArtifact.setModelVer("2.0");
181     
182     List<Artifact> completedArtifacts = new ArrayList<>();
183     completedArtifacts.add(modelArtifact);
184
185     mockModelCreation(modelArtifact);
186
187     modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
188
189     verify(
190         deleteRequestedFor(
191             urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974")));
192   }
193
194   /**
195    * To test the rollback of the full model, the ModelArtifact must have the
196    * firstVersionOfModel = true state.
197    * This flag is set during the model creation and thus needs to run before
198    * testing this particular aspect.
199    *
200    * @param modelArtifact
201    */
202   private void mockModelCreation(ModelArtifact modelArtifact) {
203     ResponseEntity createdResult = Mockito.mock(ResponseEntity.class);
204     when(createdResult.getStatusCode()).thenReturn(HttpStatus.CREATED);
205     ResponseEntity notFoundResult = Mockito.mock(ResponseEntity.class);
206     when(notFoundResult.getStatusCode()).thenReturn(HttpStatus.NOT_FOUND);
207     AaiRestClient aaiClient = Mockito.mock(AaiRestClient.class);
208     when(aaiClient.putResource(any(), any(), any(), any(), any())).thenReturn(createdResult);
209     when(aaiClient.getResource(any(), any(), any(), any())).thenReturn(notFoundResult);
210     modelArtifact.push(aaiClient, config, null, new ArrayList<>());
211   }
212
213   @Test
214   public void thatModelVersionCanBeRolledBack() {
215
216     WireMock.stubFor(
217         WireMock.get(urlEqualTo(
218             "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"))
219             .withHeader("Accept", equalTo("application/xml"))
220             .withHeader("X-TransactionId", equalTo("distributionId"))
221             .withHeader("X-FromAppId", equalTo("ModelLoader"))
222             .willReturn(
223                 WireMock.aResponse()
224                     .withHeader("Content-Type", MediaType.APPLICATION_XML_VALUE)
225                     .withBodyFile("modelVersion.xml")));
226
227         stubFor(WireMock.delete(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId?resource-version=1708937324692"))
228             .withHeader("X-FromAppId", equalTo("ModelLoader"))
229             .withHeader("X-TransactionId", equalTo("distributionId"))
230             .willReturn(aResponse().withStatus(HttpStatus.OK.value())));
231
232     ModelArtifact modelArtifact = new ModelArtifact();
233     modelArtifact.setModelInvariantId("modelInvariantId");
234     modelArtifact.setModelVerId("modelVersionId");
235     
236     List<Artifact> completedArtifacts = new ArrayList<>();
237     completedArtifacts.add(modelArtifact);
238
239     modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
240
241     verify(
242         deleteRequestedFor(
243             urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId?resource-version=1708937324692")));
244   }
245 }