2a6f196bde5c3106866fe29e4567b31df4d57a3c
[ccsdk/features.git] / sdnr / wt / apigateway / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / apigateway / test / TestMediatorServerServlet.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps.sdnr.wt.apigateway
4  * ================================================================================
5  * Copyright (C) 2018 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.File;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.servlet.ServletException;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties;
34 import org.onap.ccsdk.features.sdnr.wt.apigateway.database.MediatorServerInfo;
35 import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpMsServlet;
36 import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase;
37
38 public class TestMediatorServerServlet extends HelpServletBase {
39
40     private static final int PORT = 40004;
41
42     public TestMediatorServerServlet() {
43         super("/ms", PORT);
44     }
45
46     @Test
47     public void test() throws ServletException, IOException {
48
49         String tmpFilename = "tmp3.cfg";
50         File tmpFile = new File(tmpFilename);
51         if (tmpFile.exists()) {
52             tmpFile.delete();
53         }
54         MyProperties.Instantiate(tmpFile, true);
55         String query = "{\"query\":{\"match_all\":{}}}";
56         HelpMsServlet servlet = new HelpMsServlet();
57         this.setServlet(servlet);
58         // test diabled message
59         servlet.setOfflineStatus(true);
60         servlet.setBaseUrl("http://localhost:" + PORT);
61         String expectedResponse = "offline";
62         testrequest(HTTPMETHOD_GET, query, expectedResponse, false);
63         testrequest(HTTPMETHOD_POST, query, expectedResponse, false);
64         testrequest(HTTPMETHOD_PUT, query, expectedResponse, false);
65         testrequest(HTTPMETHOD_DELETE, query, expectedResponse, false);
66
67         servlet.setOfflineStatus(false);
68         Map<String, MediatorServerInfo> entries = new HashMap<String,MediatorServerInfo>();
69         entries.put("123", new MediatorServerInfo("1", "http://localhost:" + PORT));
70         servlet.setMediatorEntries(entries);
71         testrequest("/123/test/abc",HTTPMETHOD_GET, query, HelpMsServlet.RESPONSE_GET, true);
72         testrequest("/123/test/abc",HTTPMETHOD_POST, query, HelpMsServlet.RESPONSE_POST, true);
73         testrequest("/123/test/abc",HTTPMETHOD_PUT, query, HelpMsServlet.RESPONSE_PUT, true);
74         testrequest("/123/test/abc",HTTPMETHOD_DELETE, query, HelpMsServlet.RESPONSE_DELETE, true);
75         testrequest("/123/test/abc",HTTPMETHOD_OPTIONS, query, "", false);
76
77         if (tmpFile.exists()) {
78             tmpFile.delete();
79         }
80
81     }
82         @Before
83         public void init() throws IOException{  
84                 HelpServletBase.initEsTestWebserver(PORT,"/test");
85         }
86         @After
87         public void deinit() {
88                 HelpServletBase.stopTestWebserver();
89         }
90
91 }