45eac88cdd0b283d0f27bab43e3bb826f54a9198
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps.sdnr.wt.apigateway
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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.ccsdk.features.sdnr.wt.apigateway.test;
22
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties;
27 import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpEsServlet;
28 import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase;
29 import java.io.*;
30 import javax.servlet.ServletException;
31
32 public class TestDatabaseServlet extends HelpServletBase {
33
34     private static final int PORT = 40002;
35
36     public TestDatabaseServlet() {
37         super("/database", PORT);
38     }
39
40
41
42     final String LR = "\n";
43
44
45
46     @Test
47     public void test() throws ServletException, IOException {
48
49         String tmpFilename = "tmp1.cfg";
50         File tmpFile = new File(tmpFilename);
51         if (tmpFile.exists())
52             tmpFile.delete();
53         MyProperties properties = MyProperties.Instantiate(tmpFile, true);
54         String query = "{\"query\":{\"match_all\":{}}}";
55         String tmpconfigcontent =
56                 "aai=off" + LR + "aaiHeaders=[]" + LR + "database=off" + LR + "insecure=0" + LR + "cors=0";
57         String tmpconfigcontent2 = "aai=off" + LR + "aaiHeaders=[]" + LR + "database=http://" + HOST + ":" + PORT + LR
58                 + "insecure=1" + LR + "cors=1";
59         this.setServlet(new HelpEsServlet());
60         // test diabled message
61         properties.load(new ByteArrayInputStream(tmpconfigcontent.getBytes()));
62         String expectedResponse = "offline";
63         testrequest(HTTPMETHOD_GET, query, expectedResponse, false);
64         testrequest(HTTPMETHOD_POST, query, expectedResponse, false);
65         testrequest(HTTPMETHOD_PUT, query, expectedResponse, false);
66         testrequest(HTTPMETHOD_DELETE, query, expectedResponse, false);
67
68         // initEsTestWebserver(port);
69         properties.load(new ByteArrayInputStream(tmpconfigcontent2.getBytes()));
70         testrequest(HTTPMETHOD_GET, query, HelpEsServlet.RESPONSE_GET, true);
71         testrequest(HTTPMETHOD_POST, query, HelpEsServlet.RESPONSE_POST, true);
72         testrequest(HTTPMETHOD_PUT, query, HelpEsServlet.RESPONSE_PUT, true);
73         testrequest(HTTPMETHOD_DELETE, query, HelpEsServlet.RESPONSE_DELETE, true);
74         testrequest(HTTPMETHOD_OPTIONS, query, "", false);
75         // stopTestWebserver();
76
77
78         if (tmpFile.exists())
79             tmpFile.delete();
80
81
82     }
83
84     @Before
85     public void init() throws IOException {
86         HelpServletBase.initEsTestWebserver(PORT);
87     }
88
89     @After
90     public void deinit() {
91         HelpServletBase.stopTestWebserver();
92     }
93 }