aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/modelloader/restclient/TestAaiServiceClient.java
blob: fd65383b6abe1cc7863f8088766b34d8b0aa1d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.modelloader.restclient;

import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.apache.commons.io.IOUtils.write;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.onap.aai.modelloader.config.ModelLoaderConfig;

/**
 * Local testing of the A&AI Service client.
 *
 */
public class TestAaiServiceClient {

    private Server server;
    private AaiRestClient aaiClient;

    @BeforeEach
    public void startJetty() throws Exception {
        server = new Server(8080);
        server.setHandler(getMockHandler());
        server.start();

        Properties props = new Properties();
        props.put("ml.aai.KEYSTORE_PASSWORD", "2244");
        ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
        aaiClient = new AaiRestClient(config);
    }

    @AfterEach
    public void stopJetty() throws Exception {
        server.stop();
    }

    @Test
    public void testBuildAaiRestClient() {
        Properties props = new Properties();
        ModelLoaderConfig config = new ModelLoaderConfig(props, ".");
        new AaiRestClient(config);
        assertTrue(true);
    }

    @Test
    public void testOperations() {
        String url = "http://localhost:8080";
        String transId = "";
        MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
        aaiClient.getResource(url, "", mediaType);
        aaiClient.deleteResource("http://localhost", transId, "");
        aaiClient.getAndDeleteResource(url, transId);
        aaiClient.postResource(url, "", transId, mediaType);
        aaiClient.putResource(url, "", transId, mediaType);
        assertTrue(true);
    }


    /**
     * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response.
     * 
     * @return never <code>null</code>.
     */
    private Handler getMockHandler() {
        Handler handler = new AbstractHandler() {
            @Override
            public void handle(String target, Request request, HttpServletRequest servletRequest,
                    HttpServletResponse response) throws IOException, ServletException {
                response.setStatus(SC_OK);
                response.setContentType("text/json;charset=utf-8");
                write("", response.getOutputStream(), Charset.defaultCharset());
                request.setHandled(true);
            }
        };
        return handler;
    }
}