Convert project from AJSC to Spring Boot
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / restclient / TestAaiServiceClient.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
26 import java.io.IOException;
27 import java.util.Properties;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import javax.ws.rs.core.MediaType;
32 import org.eclipse.jetty.server.Handler;
33 import org.eclipse.jetty.server.Request;
34 import org.eclipse.jetty.server.Server;
35 import org.eclipse.jetty.server.handler.AbstractHandler;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.aai.modelloader.config.ModelLoaderConfig;
40
41 /**
42  * Local testing of the Babel service
43  *
44  */
45 public class TestAaiServiceClient {
46
47     private Server server;
48     private AaiRestClient aaiClient;
49
50     @Before
51     public void startJetty() throws Exception {
52         server = new Server(8080);
53         server.setHandler(getMockHandler());
54         server.start();
55
56         Properties props = new Properties();
57         props.put("ml.aai.KEYSTORE_PASSWORD", "2244");
58         ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
59         aaiClient = new AaiRestClient(config);
60     }
61
62     @After
63     public void stopJetty() throws Exception {
64         server.stop();
65     }
66
67     @Test
68     public void testBuildAaiRestClient() {
69         Properties props = new Properties();
70         ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
71         new AaiRestClient(config);
72     }
73
74     @Test
75     public void testOperations() {
76         String url = "http://localhost";
77         String transId = "";
78         MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
79         aaiClient.getResource(url, "", mediaType);
80         aaiClient.deleteResource("http://localhost", transId, "");
81         aaiClient.getAndDeleteResource(url, transId);
82         aaiClient.postResource(url, "", transId, mediaType);
83         aaiClient.putResource(url, "", transId, mediaType);
84     }
85
86
87     /**
88      * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
89      * 
90      * @return never <code>null</code>.
91      */
92     private Handler getMockHandler() {
93         Handler handler = new AbstractHandler() {
94             @Override
95             public void handle(String target, Request request, HttpServletRequest servletRequest,
96                     HttpServletResponse response) throws IOException, ServletException {
97                 response.setStatus(SC_OK);
98                 response.setContentType("text/json;charset=utf-8");
99                 write("", response.getOutputStream());
100                 request.setHandled(true);
101             }
102         };
103         return handler;
104     }
105 }
106