3340ec66cdc06dc0e01cea9aaab54db3dd218090
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.apigateway.test;
19
20 import static org.junit.Assert.*;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.IOException;
25
26 import javax.servlet.ServletException;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.ccsdk.features.sdnr.wt.apigateway.EsServlet.IRequestCallback;
32 import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties;
33 import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpEsServlet;
34 import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase;
35
36 public class TestQueryCallback extends HelpServletBase{
37
38         private static final int PORT = 40011;
39         
40         public TestQueryCallback() {
41                 super("/database",PORT);
42         }
43
44         final String LR = "\n";
45
46         
47         private boolean hasCallback=false;
48         
49         @Test
50         public void test() throws ServletException, IOException {
51
52                 String tmpFilename = "tmp1.cfg";
53                 File tmpFile = new File(tmpFilename);
54                 if (tmpFile.exists())
55                         tmpFile.delete();
56                 MyProperties properties = MyProperties.Instantiate(tmpFile,true);
57                 String query = "{\"query\":{\"match_all\":{}}}";
58                 String tmpconfigcontent2 = "aai=off" + LR + "aaiHeaders=[]" + LR + "database=http://" + HOST + ":" + PORT + LR
59                                 + "insecure=1" + LR + "cors=1";
60                 HelpEsServlet servlet = new HelpEsServlet();
61                 this.setServlet(servlet);
62                 HelpEsServlet.registerRequestCallback("/mwtn/mediator-server", new IRequestCallback() {
63                         
64                         @Override
65                         public void onRequest(String uri, String method) {
66                                 hasCallback=true;
67                                 
68                         }
69                 });
70                 properties.load(new ByteArrayInputStream(tmpconfigcontent2.getBytes()));
71                 testrequest("/database/mwtn/mediator-server/_search",HTTPMETHOD_POST, query, HelpEsServlet.RESPONSE_POST, true);
72                 int wait=10;
73                 while(wait-->0) {
74                         if(hasCallback) {
75                                 break;
76                         }
77                         try {
78                                 Thread.sleep(1000);
79                         } catch (InterruptedException e) {
80                                 Thread.interrupted();
81                         }                       
82                 }
83                 assertTrue("no request callback received",hasCallback);
84                 hasCallback=false;
85                 testrequest("/database/mwtn/mediatr-server/_search",HTTPMETHOD_POST, query, HelpEsServlet.RESPONSE_POST, true);
86                 wait=5;
87                 while(wait-->0) {
88                         if(hasCallback) {
89                                 break;
90                         }
91                         try {
92                                 Thread.sleep(1000);
93                         } catch (InterruptedException e) {
94                                 Thread.interrupted();
95                         }                       
96                 }
97                 assertFalse("received request callback",hasCallback);
98                 
99                  
100                 
101                 if (tmpFile.exists())
102                         tmpFile.delete();
103                 
104                 
105         }
106         @Before
107         public void init() throws IOException{  
108                 HelpServletBase.initEsTestWebserver(PORT,"/database");
109         }
110         @After
111         public void deinit() {
112                 HelpServletBase.stopTestWebserver();
113         }
114
115 }