Refactor babel-related code to not update parameter values
[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.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;
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("Content-Type", equalTo("application/xml"))
93             .withHeader("X-TransactionId", equalTo("someId"))
94             .withHeader("X-FromAppId", equalTo("ModelLoader"))
95             .willReturn(
96                 WireMock.aResponse()
97                     .withStatus(HttpStatus.CREATED.value())));
98
99     ModelArtifact modelArtifact = new ModelArtifact();
100     modelArtifact.setModelInvariantId("modelInvariantId");
101     List<Artifact> artifacts = List.of(modelArtifact);
102     List<Artifact> completedArtifacts = new ArrayList<>();
103
104     boolean result = modelArtifactHandler.pushArtifacts(artifacts,
105         "someId", completedArtifacts, restClient);
106     assertTrue(result);
107     WireMock.verify(
108         WireMock.putRequestedFor(urlEqualTo("/aai/v28/service-design-and-creation/models/model/modelInvariantId")));
109   }
110
111   @Test
112   public void thatArtifactsCanBeUpdated() {
113     // Checks if model exists in resources service
114     WireMock.stubFor(
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"))
119             .willReturn(
120                 WireMock.aResponse()
121                     .withStatus(HttpStatus.OK.value())));
122
123     // Checks if specific version of model exists in aai-resources
124     WireMock.stubFor(
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"))
130             .willReturn(
131                 WireMock.aResponse()
132                     .withStatus(HttpStatus.NOT_FOUND.value())));
133
134     WireMock.stubFor(
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"))
140             .willReturn(
141                 WireMock.aResponse()
142                     .withStatus(HttpStatus.CREATED.value())));
143
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<>();
151
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")));
156     assertTrue(result);
157   }
158
159   @Test
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")));
166
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())));
171
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);
179
180     mockModelCreation(modelArtifact);
181
182     modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
183
184     verify(
185         deleteRequestedFor(
186             urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974")));
187   }
188
189   /**
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.
194    *
195    * @param modelArtifact
196    */
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<>());
206   }
207
208   @Test
209   public void thatModelVersionCanBeRolledBack() {
210
211     // "http://localhost:10594/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"
212
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);
220
221     modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
222
223   }
224 }