2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.modelloader.notification;
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;
32 import java.util.List;
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;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.github.tomakehurst.wiremock.client.WireMock;
55 import java.util.ArrayList;
58 @AutoConfigureWireMock(port = 0)
59 public class ModelArtifactHandlerTest {
61 @Value("${wiremock.server.port}")
62 private int wiremockPort;
64 final ObjectMapper objectMapper = new ObjectMapper();
66 ModelLoaderConfig config;
68 ModelArtifactHandler modelArtifactHandler;
71 AaiRestClient restClient;
75 when(config.getAaiBaseUrl()).thenReturn("http://localhost:" + wiremockPort);
76 when(config.getAaiModelUrl(any())).thenReturn("/aai/v28/service-design-and-creation/models/model/");
80 public void thatArtifactsCanBeCreated() {
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"))
88 .withStatus(HttpStatus.NOT_FOUND.value())));
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"))
98 .withStatus(HttpStatus.CREATED.value())));
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<>();
106 boolean result = modelArtifactHandler.pushArtifacts(artifacts,
107 "someId", completedArtifacts, restClient);
110 WireMock.putRequestedFor(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId")));
114 public void thatArtifactsCanBeUpdated() {
115 // Checks if model exists in resources service
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"))
123 .withStatus(HttpStatus.OK.value())));
125 // Checks if specific version of model exists in aai-resources
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"))
134 .withStatus(HttpStatus.NOT_FOUND.value())));
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"))
145 .withStatus(HttpStatus.CREATED.value())));
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<>();
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")));
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")));
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())));
177 ModelArtifact modelArtifact = new ModelArtifact();
178 modelArtifact.setModelInvariantId("3a40ab73-6694-4e75-bb6d-9a4a86ce35b3");
179 modelArtifact.setModelVerId("modelVersionId");
180 // modelArtifact.setModelVer("2.0");
182 List<Artifact> completedArtifacts = new ArrayList<>();
183 completedArtifacts.add(modelArtifact);
185 mockModelCreation(modelArtifact);
187 modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
191 urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974")));
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.
200 * @param modelArtifact
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<>());
214 public void thatModelVersionCanBeRolledBack() {
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"))
224 .withHeader("Content-Type", MediaType.APPLICATION_XML_VALUE)
225 .withBodyFile("modelVersion.xml")));
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())));
232 ModelArtifact modelArtifact = new ModelArtifact();
233 modelArtifact.setModelInvariantId("modelInvariantId");
234 modelArtifact.setModelVerId("modelVersionId");
236 List<Artifact> completedArtifacts = new ArrayList<>();
237 completedArtifacts.add(modelArtifact);
239 modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
243 urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId?resource-version=1708937324692")));