Add model-loader integration tests
[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 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
32
33 import java.util.List;
34
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;
53
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.github.tomakehurst.wiremock.client.WireMock;
56
57 import java.util.ArrayList;
58
59 @SpringBootTest
60 @AutoConfigureWireMock(port = 0)
61 public class ModelArtifactHandlerTest {
62
63   @Value("${wiremock.server.port}")
64   private int wiremockPort;
65
66   final ObjectMapper objectMapper = new ObjectMapper();
67   @Mock
68   ModelLoaderConfig config;
69   @InjectMocks
70   ModelArtifactHandler modelArtifactHandler;
71
72   @Autowired
73   AaiRestClient restClient;
74
75   @BeforeEach
76   public void setUp() {
77     when(config.getAaiBaseUrl()).thenReturn("http://localhost:" + wiremockPort);
78     when(config.getAaiModelUrl(any())).thenReturn("/aai/v28/service-design-and-creation/models/model/");
79   }
80
81   @Test
82   public void thatArtifactsCanBeCreated() {
83     WireMock.stubFor(
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"))
88             .willReturn(
89                 WireMock.aResponse()
90                     .withStatus(HttpStatus.NOT_FOUND.value())));
91
92     WireMock.stubFor(
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"))
97             .willReturn(
98                 WireMock.aResponse()
99                     .withStatus(HttpStatus.CREATED.value())));
100
101     ModelArtifact modelArtifact = new ModelArtifact();
102     modelArtifact.setModelInvariantId("modelInvariantId");
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("Content-Type", equalTo("application/xml"))
140             .withHeader("X-TransactionId", equalTo("distributionId"))
141             .withHeader("X-FromAppId", equalTo("ModelLoader"))
142             .willReturn(
143                 WireMock.aResponse()
144                     .withStatus(HttpStatus.CREATED.value())));
145
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<>();
153
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")));
158     assertTrue(result);
159   }
160
161   @Test
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")));
168
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())));
173
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);
181
182     mockModelCreation(modelArtifact);
183
184     modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
185
186     verify(
187         deleteRequestedFor(
188             urlEqualTo("/aai/v28/service-design-and-creation/models/model/3a40ab73-6694-4e75-bb6d-9a4a86ce35b3?resource-version=1710523260974")));
189   }
190
191   /**
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.
196    *
197    * @param modelArtifact
198    */
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<>());
208   }
209
210   @Test
211   public void thatModelVersionCanBeRolledBack() {
212
213     // "http://localhost:10594/aai/v28/service-design-and-creation/models/model/modelInvariantId/model-vers/model-ver/modelVersionId"
214
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);
222
223     modelArtifactHandler.rollback(completedArtifacts, "distributionId", restClient);
224
225   }
226 }