Update babel dependency in model-loader
[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.hamcrest.MatcherAssert.assertThat;
28
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;
37
38 import javax.servlet.ServletException;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
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.After;
47 import org.junit.Before;
48 import org.junit.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;
52
53 import com.google.gson.Gson;
54
55 /**
56  * Local testing of the Babel service client.
57  *
58  */
59 public class TestBabelServiceClient {
60
61     private Server server;
62     private String responseBody;
63
64     @Before
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);
71
72         server = new Server(8080);
73         server.setHandler(getMockHandler());
74         server.start();
75     }
76
77     @After
78     public void stopJetty() throws Exception {
79         server.stop();
80     }
81
82     @Test
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)));
98     }
99
100     @Test
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)));
112     }
113
114
115     private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
116         return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
117     }
118
119     /**
120      * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
121      * 
122      * @return never <code>null</code>.
123      */
124     private Handler getMockHandler() {
125         Handler handler = new AbstractHandler() {
126             @Override
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);
133             }
134         };
135         return handler;
136     }
137 }