Refactor babel-related code to not update parameter values
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / restclient / TestBabelServiceClient.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.restclient;
22
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.MatcherAssert.assertThat;
26
27 import java.io.IOException;
28 import java.net.URISyntaxException;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.Base64;
32 import java.util.List;
33
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.aai.babel.service.data.BabelArtifact;
37 import org.onap.aai.babel.service.data.BabelRequest;
38 import org.onap.aai.modelloader.BabelClientTestConfiguration;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
43 import org.springframework.context.annotation.Import;
44 import org.springframework.test.annotation.DirtiesContext;
45
46 import com.fasterxml.jackson.core.JsonProcessingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.github.tomakehurst.wiremock.client.WireMock;
49
50 /**
51  * Local testing of the Babel service client.
52  *
53  */
54 @SpringBootTest
55 @DirtiesContext
56 @AutoConfigureWireMock(port = 0)
57 @Import(BabelClientTestConfiguration.class)
58 public class TestBabelServiceClient {
59
60     @Value("${wiremock.server.port}")
61     private int wiremockPort;
62
63     @Autowired BabelServiceClient client;
64
65     @BeforeAll
66     public static void setup() throws JsonProcessingException {
67         ObjectMapper objectMapper = new ObjectMapper();
68         List<BabelArtifact> artifacts = List.of(
69             new BabelArtifact("art1", null, ""),
70             new BabelArtifact("art2", null, ""),
71             new BabelArtifact("art3", null, ""));
72         WireMock.stubFor(
73             WireMock.post(WireMock.urlEqualTo("/services/babel-service/v1/app/generateArtifacts"))
74                 .withHeader("X-TransactionId", WireMock.equalTo("Test-Transaction-ID-BabelClient"))
75                 .withHeader("X-FromAppId", WireMock.equalTo("ModelLoader"))
76                 .withRequestBody(WireMock.matchingJsonPath("$.artifactName", WireMock.equalTo("service-Vscpass-Test")))
77                 .withRequestBody(WireMock.matchingJsonPath("$.artifactVersion", WireMock.equalTo("1.0")))
78                 .withRequestBody(WireMock.matchingJsonPath("$.csar", WireMock.matching(".*")))
79                 .willReturn(
80                     WireMock.aResponse()
81                         .withHeader("Content-Type", "application/json")
82                         .withBody(objectMapper.writeValueAsString(artifacts))));
83     }
84
85     @Test
86     public void testRestClient() throws BabelServiceClientException, IOException, URISyntaxException {
87         BabelRequest babelRequest = new BabelRequest();
88         babelRequest.setArtifactName("service-Vscpass-Test");
89         babelRequest.setCsar(Base64.getEncoder().encodeToString(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar")));
90         babelRequest.setArtifactVersion("1.0");
91
92         List<BabelArtifact> result =
93                 client.postArtifact(babelRequest, "Test-Transaction-ID-BabelClient");
94         assertThat(result.size(), is(equalTo(3)));
95     }
96
97
98     private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
99         return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
100     }
101 }