Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / ArtifactDownloadManagerTest.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 com.github.tomakehurst.wiremock.client.WireMock.aResponse;
23 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.matching;
26 import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
27 import static com.github.tomakehurst.wiremock.client.WireMock.post;
28 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
30 import static org.junit.jupiter.api.Assertions.assertEquals;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import org.junit.jupiter.api.Test;
37 import org.onap.aai.modelloader.DistributionClientTestConfiguration;
38 import org.onap.aai.modelloader.entity.Artifact;
39 import org.onap.aai.modelloader.entity.ArtifactType;
40 import org.onap.aai.modelloader.service.ArtifactInfoImpl;
41 import org.onap.sdc.api.notification.IArtifactInfo;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
45 import org.springframework.context.annotation.Import;
46 import org.springframework.http.MediaType;
47 import org.springframework.kafka.test.context.EmbeddedKafka;
48 import org.springframework.test.annotation.DirtiesContext;
49
50 import com.fasterxml.jackson.core.JsonProcessingException;
51
52 @DirtiesContext
53 @AutoConfigureWireMock(port = 0)
54 @EmbeddedKafka(partitions = 1, ports = 9092, topics = {"${topics.distribution.notification}"})
55 @SpringBootTest(properties = { "ml.distribution.connection.enabled=true" })
56 @Import(DistributionClientTestConfiguration.class)
57 public class ArtifactDownloadManagerTest {
58
59   @Autowired ArtifactDownloadManager artifactDownloadManager;
60
61   @Test
62   public void downloadArtifacts() throws JsonProcessingException {
63     NotificationDataImpl notificationData = new NotificationDataImpl();
64     notificationData.setDistributionID("distributionID");
65     notificationData.setServiceVersion("2.0");
66
67     stubFor(get(urlEqualTo("/sdc/v1/catalog/services/DemovlbCds/1.0/artifacts/service-TestSvc-csar.csar"))
68         .withHeader("Accept", equalTo(MediaType.APPLICATION_OCTET_STREAM_VALUE))
69         .withHeader("X-ECOMP-RequestID", matching(".+"))
70         .withHeader("X-ECOMP-InstanceID", equalTo("aai-ml-id-test"))
71         .willReturn(aResponse()
72           .withHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE)
73           .withBodyFile("service-TestSvc-csar.csar")));
74     
75     stubFor(
76         post(urlEqualTo("/services/babel-service/v1/app/generateArtifacts"))
77             .withHeader("X-TransactionId", equalTo("distributionID"))
78             .withHeader("X-FromAppId", equalTo("ModelLoader"))
79             .withHeader("Content-Type", equalTo(MediaType.APPLICATION_JSON_VALUE))
80             .withRequestBody(matchingJsonPath("$.artifactName", equalTo("service-TestSvc-csar.csar")))
81             .withRequestBody(matchingJsonPath("$.artifactVersion", equalTo("2.0")))
82             .withRequestBody(matchingJsonPath("$.csar", matching(".*")))
83             .willReturn(
84                 aResponse()
85                     .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
86                     .withBodyFile("service-TestSvc-csar-babel-response.json")));
87
88     ArtifactInfoImpl artifactInfo = new ArtifactInfoImpl();
89     artifactInfo.setArtifactName("service-TestSvc-csar.csar");
90     artifactInfo.setArtifactVersion("1.0");
91     artifactInfo.setArtifactURL("/sdc/v1/catalog/services/DemovlbCds/1.0/artifacts/service-TestSvc-csar.csar");
92     artifactInfo.setArtifactType("TOSCA_CSAR");
93     artifactInfo.setArtifactChecksum("ZmI5NzQ1MWViZGFkMjRjZWEwNTQzY2U0OWQwYjlmYjQ=");
94     artifactInfo.setArtifactUUID("f6f907f1-3f45-4fb4-8cbe-15a4c6ee16db");
95     List<IArtifactInfo> artifacts = new ArrayList<>();
96     artifacts.add(artifactInfo);
97     List<Artifact> modelArtifacts = new ArrayList<>(); // processed artifacts will be written to this list
98     List<Artifact> catalogArtifacts = new ArrayList<>(); // processed artifacts will be written to this list
99     boolean result = artifactDownloadManager.downloadArtifacts(notificationData, artifacts, modelArtifacts, catalogArtifacts);
100     
101     assertEquals(1, modelArtifacts.size());
102     assertEquals(ArtifactType.MODEL, modelArtifacts.get(0).getType());
103     assertTrue(result);
104   }
105
106 }