Add model-loader integration tests
[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.List;
32 import java.util.Properties;
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.modelloader.config.ModelLoaderConfig;
38 import org.onap.aai.modelloader.service.HttpsBabelServiceClientFactory;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
42
43 import com.fasterxml.jackson.core.JsonProcessingException;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45 import com.github.tomakehurst.wiremock.client.WireMock;
46 import com.github.tomakehurst.wiremock.matching.EqualToPattern;
47
48 /**
49  * Local testing of the Babel service client.
50  *
51  */
52 @SpringBootTest
53 @AutoConfigureWireMock(port = 0)
54 public class TestBabelServiceClient {
55
56     @Value("${wiremock.server.port}")
57     private int wiremockPort;
58
59     @BeforeAll
60     public static void setup() throws JsonProcessingException {
61         ObjectMapper objectMapper = new ObjectMapper();
62         List<BabelArtifact> artifacts = List.of(
63             new BabelArtifact("art1", null, ""),
64             new BabelArtifact("art2", null, ""),
65             new BabelArtifact("art3", null, ""));
66         WireMock.stubFor(
67             WireMock.post(WireMock.urlEqualTo("/generate"))
68                 .withHeader("X-TransactionId", WireMock.equalTo("Test-Transaction-ID-BabelClient"))
69                 .withHeader("X-FromAppId", WireMock.equalTo("ModelLoader"))
70                 .withRequestBody(WireMock.matchingJsonPath("$.artifactName", WireMock.equalTo("service-Vscpass-Test")))
71                 .withRequestBody(WireMock.matchingJsonPath("$.artifactVersion", WireMock.equalTo("1.0")))
72                 .withRequestBody(WireMock.matchingJsonPath("$.csar", WireMock.matching(".*")))
73                 .willReturn(
74                     WireMock.aResponse()
75                         .withHeader("Content-Type", "application/json")
76                         .withBody(objectMapper.writeValueAsString(artifacts))));
77     }
78
79     @Test
80     public void testRestClient() throws BabelServiceClientException, IOException, URISyntaxException {
81         String url = "http://localhost:" + wiremockPort;
82         Properties configProperties = new Properties();
83         configProperties.put("ml.babel.KEYSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
84         configProperties.put("ml.babel.KEYSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
85         configProperties.put("ml.babel.TRUSTSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
86         // In a real deployment this would be a different file (to the client keystore)
87         configProperties.put("ml.babel.TRUSTSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
88         configProperties.put("ml.babel.BASE_URL", url);
89         configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "/generate");
90         configProperties.put("ml.aai.RESTCLIENT_CONNECT_TIMEOUT", "12000");
91         configProperties.put("ml.aai.RESTCLIENT_READ_TIMEOUT", "12000");
92         BabelServiceClient client =
93                 new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
94         List<BabelArtifact> result =
95                 client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
96                         "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
97         assertThat(result.size(), is(equalTo(3)));
98     }
99
100     @Test
101     public void testRestClientHttp() throws BabelServiceClientException, IOException, URISyntaxException {
102         String url = "http://localhost:" + wiremockPort;
103         Properties configProperties = new Properties();
104         configProperties.put("ml.babel.USE_HTTPS", "false");
105         configProperties.put("ml.babel.BASE_URL", url);
106         configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "/generate");
107         configProperties.put("ml.aai.RESTCLIENT_CONNECT_TIMEOUT", "3000");
108         configProperties.put("ml.aai.RESTCLIENT_READ_TIMEOUT", "3000");
109         BabelServiceClient client =
110                 new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
111         List<BabelArtifact> result =
112                 client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
113                         "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
114         assertThat(result.size(), is(equalTo(3)));
115     }
116
117
118     private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
119         return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
120     }
121 }