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