8da5f89573db840e13d0c24788aa28566ccfeb3d
[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 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;
28
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.util.ArrayList;
35 import java.util.List;
36 import java.util.Properties;
37 import javax.servlet.ServletException;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import org.eclipse.jetty.server.Handler;
41 import org.eclipse.jetty.server.Request;
42 import org.eclipse.jetty.server.Server;
43 import org.eclipse.jetty.server.handler.AbstractHandler;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.onap.aai.babel.service.data.BabelArtifact;
48 import org.onap.aai.modelloader.config.ModelLoaderConfig;
49 import org.onap.aai.modelloader.service.HttpsBabelServiceClientFactory;
50
51 /**
52  * Local testing of the Babel service client.
53  *
54  */
55 public class TestBabelServiceClient {
56
57     private Server server;
58     private String responseBody;
59
60     @Before
61     public void startJetty() throws Exception {
62         List<BabelArtifact> response = new ArrayList<>();
63         response.add(new BabelArtifact("", null, ""));
64         response.add(new BabelArtifact("", null, ""));
65         response.add(new BabelArtifact("", null, ""));
66         responseBody = new Gson().toJson(response);
67
68         server = new Server(8080);
69         server.setHandler(getMockHandler());
70         server.start();
71     }
72
73     @After
74     public void stopJetty() throws Exception {
75         server.stop();
76     }
77
78     @Test
79     public void testRestClient() throws BabelServiceClientException, IOException, URISyntaxException {
80         Properties configProperties = new Properties();
81         configProperties.put("ml.babel.KEYSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
82         configProperties.put("ml.babel.KEYSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
83         configProperties.put("ml.babel.TRUSTSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
84         // In a real deployment this would be a different file (to the client keystore)
85         configProperties.put("ml.babel.TRUSTSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
86         configProperties.put("ml.babel.BASE_URL", "http://localhost:8080/");
87         configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
88         BabelServiceClient client =
89                 new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
90         List<BabelArtifact> result =
91                 client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
92                         "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
93         assertThat(result.size(), is(equalTo(3)));
94     }
95
96     @Test
97     public void testRestClientHttp() throws BabelServiceClientException, IOException, URISyntaxException {
98         Properties configProperties = new Properties();
99         configProperties.put("ml.babel.USE_HTTPS", "false");
100         configProperties.put("ml.babel.BASE_URL", "http://localhost:8080/");
101         configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "generate");
102         BabelServiceClient client =
103                 new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
104         List<BabelArtifact> result =
105                 client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
106                         "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
107         assertThat(result.size(), is(equalTo(3)));
108     }
109
110
111     private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
112         return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
113     }
114
115     /**
116      * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
117      * 
118      * @return never <code>null</code>.
119      */
120     private Handler getMockHandler() {
121         Handler handler = new AbstractHandler() {
122             @Override
123             public void handle(String target, Request request, HttpServletRequest servletRequest,
124                     HttpServletResponse response) throws IOException, ServletException {
125                 response.setStatus(SC_OK);
126                 response.setContentType("text/xml;charset=utf-8");
127                 write(responseBody, response.getOutputStream());
128                 request.setHandled(true);
129             }
130         };
131         return handler;
132     }
133 }