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 javax.servlet.http.HttpServletResponse.SC_OK;
24 import static org.apache.commons.io.IOUtils.write;
25 import static org.hamcrest.CoreMatchers.equalTo;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.MatcherAssert.assertThat;
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.nio.charset.Charset;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Properties;
38 import javax.servlet.ServletException;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
42 import org.eclipse.jetty.server.Handler;
43 import org.eclipse.jetty.server.Request;
44 import org.eclipse.jetty.server.Server;
45 import org.eclipse.jetty.server.handler.AbstractHandler;
46 import org.junit.jupiter.api.AfterEach;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.onap.aai.babel.service.data.BabelArtifact;
50 import org.onap.aai.modelloader.config.ModelLoaderConfig;
51 import org.onap.aai.modelloader.service.HttpsBabelServiceClientFactory;
53 import com.google.gson.Gson;
56 * Local testing of the Babel service client.
59 public class TestBabelServiceClient {
61 private Server server;
62 private String responseBody;
65 public void startJetty() throws Exception {
66 List<BabelArtifact> response = new ArrayList<>();
67 response.add(new BabelArtifact("", null, ""));
68 response.add(new BabelArtifact("", null, ""));
69 response.add(new BabelArtifact("", null, ""));
70 responseBody = new Gson().toJson(response);
72 server = new Server(8080);
73 server.setHandler(getMockHandler());
78 public void stopJetty() throws Exception {
83 public void testRestClient() throws BabelServiceClientException, IOException, URISyntaxException {
84 Properties configProperties = new Properties();
85 configProperties.put("ml.babel.KEYSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
86 configProperties.put("ml.babel.KEYSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
87 configProperties.put("ml.babel.TRUSTSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
88 // In a real deployment this would be a different file (to the client keystore)
89 configProperties.put("ml.babel.TRUSTSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
90 configProperties.put("ml.babel.BASE_URL", "http://localhost:8080/");
91 configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
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)));
101 public void testRestClientHttp() throws BabelServiceClientException, IOException, URISyntaxException {
102 Properties configProperties = new Properties();
103 configProperties.put("ml.babel.USE_HTTPS", "false");
104 configProperties.put("ml.babel.BASE_URL", "http://localhost:8080/");
105 configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
106 BabelServiceClient client =
107 new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
108 List<BabelArtifact> result =
109 client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
110 "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
111 assertThat(result.size(), is(equalTo(3)));
115 private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
116 return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
120 * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
122 * @return never <code>null</code>.
124 private Handler getMockHandler() {
125 Handler handler = new AbstractHandler() {
127 public void handle(String target, Request request, HttpServletRequest servletRequest,
128 HttpServletResponse response) throws IOException, ServletException {
129 response.setStatus(SC_OK);
130 response.setContentType("text/xml;charset=utf-8");
131 write(responseBody, response.getOutputStream(), Charset.defaultCharset());
132 request.setHandled(true);