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.junit.Assert.assertThat;
29 import com.google.gson.Gson;
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.security.KeyManagementException;
35 import java.security.KeyStoreException;
36 import java.security.NoSuchAlgorithmException;
37 import java.security.UnrecoverableKeyException;
38 import java.security.cert.CertificateException;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Properties;
42 import javax.servlet.ServletException;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import org.eclipse.jetty.server.Handler;
46 import org.eclipse.jetty.server.Request;
47 import org.eclipse.jetty.server.Server;
48 import org.eclipse.jetty.server.handler.AbstractHandler;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.onap.aai.babel.service.data.BabelArtifact;
53 import org.onap.aai.modelloader.config.ModelLoaderConfig;
54 import org.onap.aai.modelloader.restclient.BabelServiceClient.BabelServiceException;
57 * Local testing of the Babel service
60 public class TestBabelServiceClient {
62 private Server server;
63 private String responseBody;
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);
74 public void startJetty() throws Exception {
75 server = new Server(8080);
76 server.setHandler(getMockHandler());
81 public void stopJetty() throws Exception {
86 public void testRestClient() throws UnrecoverableKeyException, KeyManagementException, NoSuchAlgorithmException,
87 KeyStoreException, CertificateException, IOException, BabelServiceException, URISyntaxException {
88 Properties configProperties = new Properties();
89 configProperties.put("ml.babel.KEYSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
90 configProperties.put("ml.babel.BASE_URL", "http://localhost:8080/");
91 configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
92 BabelServiceClient client =
93 new BabelServiceClientFactory().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)));
100 private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
101 return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
105 * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
107 * @return never <code>null</code>.
109 private Handler getMockHandler() {
110 Handler handler = new AbstractHandler() {
112 public void handle(String target, Request request, HttpServletRequest servletRequest,
113 HttpServletResponse response) throws IOException, ServletException {
114 response.setStatus(SC_OK);
115 response.setContentType("text/xml;charset=utf-8");
116 write(responseBody, response.getOutputStream());
117 request.setHandled(true);