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