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