Add spring-boot actuator to model-loader
[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
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Set;
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.entity.model.ModelArtifact;
41 import org.onap.aai.modelloader.service.ArtifactInfoImpl;
42 import org.onap.sdc.api.notification.IArtifactInfo;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
46 import org.springframework.context.annotation.Import;
47 import org.springframework.http.MediaType;
48 import org.springframework.kafka.test.context.EmbeddedKafka;
49 import org.springframework.test.annotation.DirtiesContext;
50
51 @DirtiesContext
52 @AutoConfigureWireMock(port = 0)
53 @EmbeddedKafka(partitions = 1, ports = 9092, topics = {"${topics.distribution.notification}"})
54 @SpringBootTest(properties = { "ml.distribution.connection.enabled=true" })
55 @Import(DistributionClientTestConfiguration.class)
56 public class ArtifactDownloadManagerTest {
57
58   @Autowired ArtifactDownloadManager artifactDownloadManager;
59
60   @Test
61   public void downloadArtifacts() throws Exception {
62     NotificationDataImpl notificationData = new NotificationDataImpl();
63     notificationData.setDistributionID("distributionID");
64     notificationData.setServiceVersion("2.0");
65
66     stubFor(get(urlEqualTo("/sdc/v1/catalog/services/DemovlbCds/1.0/artifacts/service-TestSvc-csar.csar"))
67         .withHeader("Accept", equalTo(MediaType.APPLICATION_OCTET_STREAM_VALUE))
68         .withHeader("X-ECOMP-RequestID", matching(".+"))
69         .withHeader("X-ECOMP-InstanceID", equalTo("aai-ml-id-test"))
70         .willReturn(aResponse()
71           .withHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE)
72           .withBodyFile("service-TestSvc-csar.csar")));
73     
74     stubFor(
75         post(urlEqualTo("/services/babel-service/v1/app/generateArtifacts"))
76             .withHeader("X-TransactionId", equalTo("distributionID"))
77             .withHeader("X-FromAppId", equalTo("ModelLoader"))
78             .withHeader("Content-Type", equalTo(MediaType.APPLICATION_JSON_VALUE))
79             .withRequestBody(matchingJsonPath("$.artifactName", equalTo("service-TestSvc-csar.csar")))
80             .withRequestBody(matchingJsonPath("$.artifactVersion", equalTo("2.0")))
81             .withRequestBody(matchingJsonPath("$.csar", matching(".*")))
82             .willReturn(
83                 aResponse()
84                     .withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
85                     .withBodyFile("service-TestSvc-csar-babel-response.json")));
86
87     ArtifactInfoImpl artifactInfo = new ArtifactInfoImpl();
88     artifactInfo.setArtifactName("service-TestSvc-csar.csar");
89     artifactInfo.setArtifactVersion("1.0");
90     artifactInfo.setArtifactURL("/sdc/v1/catalog/services/DemovlbCds/1.0/artifacts/service-TestSvc-csar.csar");
91     artifactInfo.setArtifactType("TOSCA_CSAR");
92     artifactInfo.setArtifactChecksum("ZmI5NzQ1MWViZGFkMjRjZWEwNTQzY2U0OWQwYjlmYjQ=");
93     artifactInfo.setArtifactUUID("f6f907f1-3f45-4fb4-8cbe-15a4c6ee16db");
94     List<IArtifactInfo> artifacts = new ArrayList<>();
95     artifacts.add(artifactInfo);
96     List<Artifact> result = artifactDownloadManager.downloadArtifacts(notificationData, artifacts);
97     
98     assertEquals(1, result.size());
99     ModelArtifact modelArtifact = (ModelArtifact) result.get(0);
100     assertEquals(ArtifactType.MODEL, modelArtifact.getType());
101     assertEquals("3c8bc8e7-e387-46ed-8616-70e99e2206dc", modelArtifact.getModelInvariantId());
102     assertEquals("http://org.onap.aai.inventory/v28", modelArtifact.getModelNamespace());
103     assertEquals("v28", modelArtifact.getModelNamespaceVersion());
104     assertEquals("71f47717-b100-4eac-940b-7d4e86a4cbb7", modelArtifact.getModelVerId());
105     assertEquals(Set.of("82194af1-3c2c-485a-8f44-420e22a9eaa4|46b92144-923a-4d20-b85a-3cbd847668a9"), modelArtifact.getDependentModelIds());
106   }
107
108 }