2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.modelloader.restclient;
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.MatcherAssert.assertThat;
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;
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;
46 import com.fasterxml.jackson.core.JsonProcessingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.github.tomakehurst.wiremock.client.WireMock;
51 * Local testing of the Babel service client.
56 @AutoConfigureWireMock(port = 0)
57 @Import(BabelClientTestConfiguration.class)
58 public class TestBabelServiceClient {
60 @Value("${wiremock.server.port}")
61 private int wiremockPort;
63 @Autowired BabelServiceClient client;
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, ""));
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(".*")))
81 .withHeader("Content-Type", "application/json")
82 .withBody(objectMapper.writeValueAsString(artifacts))));
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");
92 List<BabelArtifact> result =
93 client.postArtifact(babelRequest, "Test-Transaction-ID-BabelClient");
94 assertThat(result.size(), is(equalTo(3)));
98 private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
99 return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));