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;
31 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
33 import java.util.List;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.onap.aai.modelloader.config.ModelLoaderConfig;
41 import org.onap.aai.modelloader.entity.Artifact;
42 import org.onap.aai.modelloader.entity.model.ModelArtifact;
43 import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
44 import org.onap.aai.modelloader.restclient.AaiRestClient;
45 import org.onap.aai.restclient.client.OperationResult;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.web.client.RestTemplate;
52 import org.w3c.dom.Node;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.github.tomakehurst.wiremock.client.WireMock;
57 import java.util.ArrayList;
60 @AutoConfigureWireMock(port = 0)
61 public class ModelArtifactHandlerTest {
63 @Value("${wiremock.server.port}")
64 private int wiremockPort;
66 final ObjectMapper objectMapper = new ObjectMapper();
68 ModelLoaderConfig config;
70 ModelArtifactHandler modelArtifactHandler;
73 AaiRestClient restClient;
77 when(config.getAaiBaseUrl()).thenReturn("http://localhost:" + wiremockPort);
78 when(config.getAaiModelUrl(any())).thenReturn("/aai/v28/service-design-and-creation/models/model/");
82 public void thatArtifactsCanBeCreated() {
84 WireMock.get(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId"))
85 .withHeader("Accept", equalTo("application/xml"))
86 .withHeader("X-TransactionId", equalTo("someId"))
87 .withHeader("X-FromAppId", equalTo("ModelLoader"))
90 .withStatus(HttpStatus.NOT_FOUND.value())));
93 WireMock.put(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId"))
94 .withHeader("Content-Type", equalTo("application/xml"))
95 .withHeader("X-TransactionId", equalTo("someId"))
96 .withHeader("X-FromAppId", equalTo("ModelLoader"))
99 .withStatus(HttpStatus.CREATED.value())));
101 ModelArtifact modelArtifact = new ModelArtifact();
102 modelArtifact.setModelInvariantId("modelInvariantId");
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("Content-Type", equalTo("application/xml"))
140 .withHeader("X-TransactionId", equalTo("distributionId"))
141 .withHeader("X-FromAppId", equalTo("ModelLoader"))
144 .withStatus(HttpStatus.CREATED.value())));
146 ModelArtifact modelArtifact = new ModelArtifact();
147 modelArtifact.setModelInvariantId("modelInvariantId");
148 modelArtifact.setModelVerId("modelVersionId");
149 Node node = Mockito.mock(Node.class);
150 modelArtifact.setModelVer(node);
151 List<Artifact> artifacts = List.of(modelArtifact);
152 List<Artifact> completedArtifacts = new ArrayList<>();
154 boolean result = modelArtifactHandler.pushArtifacts(artifacts,
155 "distributionId", completedArtifacts, restClient);
156 verify(WireMock.putRequestedFor(urlEqualTo(
157 "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId")));
162 public void thatModelCanBeRolledBack() {
163 stubFor(WireMock.get(urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3"))
164 .withHeader("X-FromAppId", equalTo("ModelLoader"))
165 .withHeader("X-TransactionId", equalTo("distributionId"))
166 .willReturn(aResponse()
167 .withBodyFile("modelResponse.xml")));
169 stubFor(WireMock.delete(urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974"))
170 .withHeader("X-FromAppId", equalTo("ModelLoader"))
171 .withHeader("X-TransactionId", equalTo("distributionId"))
172 .willReturn(aResponse().withStatus(HttpStatus.OK.value())));
174 ModelArtifact modelArtifact = new ModelArtifact();
175 modelArtifact.setModelInvariantId("3a40ab73-6694-4e75-bb6d-9a4a86ce35b3");
176 modelArtifact.setModelVerId("modelVersionId");
177 Node node = Mockito.mock(Node.class);
178 modelArtifact.setModelVer(node);
179 List<Artifact> completedArtifacts = new ArrayList<>();
180 completedArtifacts.add(modelArtifact);
182 mockModelCreation(modelArtifact);
184 modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
188 urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974")));
192 * To test the rollback of the full model, the ModelArtifact must have the
193 * firstVersionOfModel = true state.
194 * This flag is set during the model creation and thus needs to run before
195 * testing this particular aspect.
197 * @param modelArtifact
199 private void mockModelCreation(ModelArtifact modelArtifact) {
200 OperationResult createdResult = Mockito.mock(OperationResult.class);
201 when(createdResult.getResultCode()).thenReturn(HttpStatus.CREATED.value());
202 OperationResult notFoundResult = Mockito.mock(OperationResult.class);
203 when(notFoundResult.getResultCode()).thenReturn(HttpStatus.NOT_FOUND.value());
204 AaiRestClient aaiClient = Mockito.mock(AaiRestClient.class);
205 when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(createdResult);
206 when(aaiClient.getResource(any(), any(), any())).thenReturn(notFoundResult);
207 modelArtifact.push(aaiClient, config, null, new ArrayList<>());
211 public void thatModelVersionCanBeRolledBack() {
213 // "http://localhost:10594/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"
215 ModelArtifact modelArtifact = new ModelArtifact();
216 modelArtifact.setModelInvariantId("modelInvariantId");
217 modelArtifact.setModelVerId("modelVersionId");
218 Node node = Mockito.mock(Node.class);
219 modelArtifact.setModelVer(node);
220 List<Artifact> completedArtifacts = new ArrayList<>();
221 completedArtifacts.add(modelArtifact);
223 modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);