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.onap.aai.restclient.client.OperationResult;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
49 import org.springframework.http.HttpStatus;
50 import org.w3c.dom.Node;
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("Content-Type", equalTo("application/xml"))
93 .withHeader("X-TransactionId", equalTo("someId"))
94 .withHeader("X-FromAppId", equalTo("ModelLoader"))
97 .withStatus(HttpStatus.CREATED.value())));
99 ModelArtifact modelArtifact = new ModelArtifact();
100 modelArtifact.setModelInvariantId("modelInvariantId");
101 List<Artifact> artifacts = List.of(modelArtifact);
102 List<Artifact> completedArtifacts = new ArrayList<>();
104 boolean result = modelArtifactHandler.pushArtifacts(artifacts,
105 "someId", completedArtifacts, restClient);
108 WireMock.putRequestedFor(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId")));
112 public void thatArtifactsCanBeUpdated() {
113 // Checks if model exists in resources service
115 WireMock.get(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId"))
116 .withHeader("Accept", equalTo("application/xml"))
117 .withHeader("X-TransactionId", equalTo("distributionId"))
118 .withHeader("X-FromAppId", equalTo("ModelLoader"))
121 .withStatus(HttpStatus.OK.value())));
123 // Checks if specific version of model exists in aai-resources
125 WireMock.get(urlEqualTo(
126 "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"))
127 .withHeader("Accept", equalTo("application/xml"))
128 .withHeader("X-TransactionId", equalTo("distributionId"))
129 .withHeader("X-FromAppId", equalTo("ModelLoader"))
132 .withStatus(HttpStatus.NOT_FOUND.value())));
135 WireMock.put(urlEqualTo(
136 "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"))
137 .withHeader("Content-Type", equalTo("application/xml"))
138 .withHeader("X-TransactionId", equalTo("distributionId"))
139 .withHeader("X-FromAppId", equalTo("ModelLoader"))
142 .withStatus(HttpStatus.CREATED.value())));
144 ModelArtifact modelArtifact = new ModelArtifact();
145 modelArtifact.setModelInvariantId("modelInvariantId");
146 modelArtifact.setModelVerId("modelVersionId");
147 Node node = Mockito.mock(Node.class);
148 modelArtifact.setModelVer(node);
149 List<Artifact> artifacts = List.of(modelArtifact);
150 List<Artifact> completedArtifacts = new ArrayList<>();
152 boolean result = modelArtifactHandler.pushArtifacts(artifacts,
153 "distributionId", completedArtifacts, restClient);
154 verify(WireMock.putRequestedFor(urlEqualTo(
155 "/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId")));
160 public void thatModelCanBeRolledBack() {
161 stubFor(WireMock.get(urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3"))
162 .withHeader("X-FromAppId", equalTo("ModelLoader"))
163 .withHeader("X-TransactionId", equalTo("distributionId"))
164 .willReturn(aResponse()
165 .withBodyFile("modelResponse.xml")));
167 stubFor(WireMock.delete(urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974"))
168 .withHeader("X-FromAppId", equalTo("ModelLoader"))
169 .withHeader("X-TransactionId", equalTo("distributionId"))
170 .willReturn(aResponse().withStatus(HttpStatus.OK.value())));
172 ModelArtifact modelArtifact = new ModelArtifact();
173 modelArtifact.setModelInvariantId("3a40ab73-6694-4e75-bb6d-9a4a86ce35b3");
174 modelArtifact.setModelVerId("modelVersionId");
175 Node node = Mockito.mock(Node.class);
176 modelArtifact.setModelVer(node);
177 List<Artifact> completedArtifacts = new ArrayList<>();
178 completedArtifacts.add(modelArtifact);
180 mockModelCreation(modelArtifact);
182 modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
186 urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974")));
190 * To test the rollback of the full model, the ModelArtifact must have the
191 * firstVersionOfModel = true state.
192 * This flag is set during the model creation and thus needs to run before
193 * testing this particular aspect.
195 * @param modelArtifact
197 private void mockModelCreation(ModelArtifact modelArtifact) {
198 OperationResult createdResult = Mockito.mock(OperationResult.class);
199 when(createdResult.getResultCode()).thenReturn(HttpStatus.CREATED.value());
200 OperationResult notFoundResult = Mockito.mock(OperationResult.class);
201 when(notFoundResult.getResultCode()).thenReturn(HttpStatus.NOT_FOUND.value());
202 AaiRestClient aaiClient = Mockito.mock(AaiRestClient.class);
203 when(aaiClient.putResource(any(), any(), any(), any())).thenReturn(createdResult);
204 when(aaiClient.getResource(any(), any(), any())).thenReturn(notFoundResult);
205 modelArtifact.push(aaiClient, config, null, new ArrayList<>());
209 public void thatModelVersionCanBeRolledBack() {
211 // "http://localhost:10594/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"
213 ModelArtifact modelArtifact = new ModelArtifact();
214 modelArtifact.setModelInvariantId("modelInvariantId");
215 modelArtifact.setModelVerId("modelVersionId");
216 Node node = Mockito.mock(Node.class);
217 modelArtifact.setModelVer(node);
218 List<Artifact> completedArtifacts = new ArrayList<>();
219 completedArtifacts.add(modelArtifact);
221 modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);