diff options
author | Jack <michael.duerre@highstreet-technologies.com> | 2019-08-01 06:36:08 +0200 |
---|---|---|
committer | Michael Dürre <michael.duerre@highstreet-technologies.com> | 2019-08-01 05:36:28 +0000 |
commit | e57b5a77c5679e512651c29a5636f00d7b9ee931 (patch) | |
tree | 49465396bae91c62e684471ff3d82c6c02a8af64 /sdnr/wt/apigateway/provider/src/test/java | |
parent | 6e4edaf8f5ceba8f8334e614003d271daf70fa21 (diff) |
fix apigateway for mediatorserver routes
fix apigateway for routing requests to mediatorserver
Issue-ID: SDNC-823
Signed-off-by: Michael Dürre <michael.duerre@highstreet-technologies.com>
Change-Id: I4f04b6ac089455246931d563fc08353509ad0bf8
Signed-off-by: Michael Dürre <michael.duerre@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/apigateway/provider/src/test/java')
9 files changed, 386 insertions, 85 deletions
diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/AllTests.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/AllTests.java deleted file mode 100644 index a34d3cac1..000000000 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/AllTests.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : CCSDK.apps.sdnr.wt.apigateway - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * 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.ccsdk.features.sdnr.wt.apigateway.test; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.runners.Suite.SuiteClasses; - -@RunWith(Suite.class) -@SuiteClasses({ TestAaiServlet.class, TestProperties.class,TestDatabaseServlet.class, TestMediatorServerServlet.class }) -public class AllTests { - -} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestAaiServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestAaiServlet.java index fd76be7af..0af4d69df 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestAaiServlet.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestAaiServlet.java @@ -26,6 +26,8 @@ import java.io.IOException; import javax.servlet.ServletException; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpAaiServlet; @@ -34,8 +36,9 @@ import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase; public class TestAaiServlet extends HelpServletBase{ + private static final int PORT = 40001; public TestAaiServlet() { - super("/aai",40001); + super("/aai",PORT); } @Test @@ -49,10 +52,10 @@ public class TestAaiServlet extends HelpServletBase{ String query = "{\"query\":{\"match_all\":{}}}"; String tmpconfigcontent = "aai=off" + LR + "aaiHeaders=[]" + LR + "database=off" + LR + "insecure=0" + LR + "cors=0"; - String tmpconfigcontent2 = "aai=http://" + HOST + ":" + this.port + LR + "aaiHeaders=[]" + LR + "database=off"+ LR + String tmpconfigcontent2 = "aai=http://" + HOST + ":" + PORT + LR + "aaiHeaders=[]" + LR + "database=off"+ LR + "insecure=1" + LR + "cors=1"; this.setServlet(new HelpAaiServlet()); - // test diabled message + // test disabled message properties.load(new ByteArrayInputStream(tmpconfigcontent.getBytes())); String expectedResponse = "offline"; testrequest(HTTPMETHOD_GET, query, expectedResponse, false); @@ -72,5 +75,12 @@ public class TestAaiServlet extends HelpServletBase{ tmpFile.delete(); } - + @Before + public void init() throws IOException{ + HelpServletBase.initEsTestWebserver(PORT); + } + @After + public void deinit() { + HelpServletBase.stopTestWebserver(); + } } diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseHttpClient.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseHttpClient.java new file mode 100644 index 000000000..795228834 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseHttpClient.java @@ -0,0 +1,120 @@ +package org.onap.ccsdk.features.sdnr.wt.apigateway.test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.apigateway.database.DatabaseEntryProvider; +import org.onap.ccsdk.features.sdnr.wt.apigateway.database.DatabaseHttpClient; +import org.onap.ccsdk.features.sdnr.wt.apigateway.database.MediatorServerInfo; +import org.onap.ccsdk.features.sdnr.wt.apigateway.database.http.BaseHTTPResponse; + +public class TestDatabaseHttpClient { + + private static final String ID_1="AWwscPepjf5-TrAFEdUD"; + private static final String HOSTNAME_1="http://192.168.178.89:7070"; + private static final String NAME_1="internal mediatorserver"; + private static final String ID_2="AWwscSCWjf5-TrAFEdUE"; + private static final String HOSTNAME_2="http://192.168.178.89:7071"; + private static final String NAME_2="internal mediatorserver2"; + private static final String ID_3="AWwscSCWjf5-TrAFEdsE"; + private static final String HOSTNAME_3="http://192.168.178.88:7371"; + private static final String NAME_3="test mediatorserver2"; + + private static final String RESPONSE_VALID="{\n" + + "\"took\": 3,\n" + + "\"timed_out\": false,\n" + + "\"_shards\": {\n" + + "\"total\": 5,\n" + + "\"successful\": 5,\n" + + "\"failed\": 0\n" + + "},\n" + + "\"hits\": {\n" + + "\"total\": 2,\n" + + "\"max_score\": 1,\n" + + "\"hits\": [\n" + + "{\n" + + "\"_index\": \"mwtn_v1\",\n" + + "\"_type\": \"mediator-server\",\n" + + "\"_id\": \"AWwscPepjf5-TrAFEdUD\",\n" + + "\"_score\": 1,\n" + + "\"_source\": {\n" + + "\"name\": \"internal mediatorserver\",\n" + + "\"url\": \"http://192.168.178.89:7070\"\n" + + "}\n" + + "}\n" + + ",\n" + + "{\n" + + "\"_index\": \"mwtn_v1\",\n" + + "\"_type\": \"mediator-server\",\n" + + "\"_id\": \"AWwscSCWjf5-TrAFEdUE\",\n" + + "\"_score\": 1,\n" + + "\"_source\": {\n" + + "\"name\": \"internal mediatorserver2\",\n" + + "\"url\": \"http://192.168.178.89:7071\"\n" + + "}\n" + + "}\n" + + "]\n" + + "}\n" + + "}"; + + private class PublicDatabaseHttpClient extends DatabaseHttpClient{ + + public PublicDatabaseHttpClient(String base, boolean trustAllCerts) { + super(base, trustAllCerts); + } + @Override + public BaseHTTPResponse sendRequest(String uri, String method, String body, Map<String, String> headers) { + return new BaseHTTPResponse(200, RESPONSE_VALID); + } + } + public class PublicDatabaseEntryProvider extends DatabaseEntryProvider { + + protected PublicDatabaseEntryProvider() { + super(new PublicDatabaseHttpClient("http://localhost/",false), 60); + } + + } + @Test + public void test() { + + final PublicDatabaseHttpClient client = new PublicDatabaseHttpClient("http://localhost/",false); + Map<String, MediatorServerInfo> entries = client.requestEntries(); + assertEquals("result size is not correct",2,entries.size()); + assertEquals("hostname 1 is not correct",HOSTNAME_1,entries.get(ID_1).getHost()); + assertEquals("name 1 is not correct",NAME_1,entries.get(ID_1).getName()); + assertEquals("hostname 2 is not correct",HOSTNAME_2,entries.get(ID_2).getHost()); + assertEquals("name 2 is not correct",NAME_2,entries.get(ID_2).getName()); + } + @Test + public void test2() { + final PublicDatabaseEntryProvider provider = new PublicDatabaseEntryProvider(); + boolean reloaded = provider.triggerReloadSync(); + assertTrue("data were not reloaded",reloaded); + try { + Thread.sleep(1500); + } catch (InterruptedException e) { + Thread.interrupted(); + } + + System.out.println(provider.printEntries()); + assertEquals("provider has not loaded data",HOSTNAME_1,provider.getHostUrl(ID_1)); + assertEquals("provider has not loaded data",HOSTNAME_2,provider.getHostUrl(ID_2)); + Map<String, MediatorServerInfo> entries2 = new HashMap<String,MediatorServerInfo>(); + entries2.put(ID_3, new MediatorServerInfo(NAME_3, HOSTNAME_3)); + provider.setEntries(entries2); + assertEquals("provider has not loaded data",HOSTNAME_3,provider.getHostUrl(ID_3)); + try { + provider.close(); + } catch (Exception e) { + + } + + + } +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseServlet.java index 143766fdb..d7b4c1511 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseServlet.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseServlet.java @@ -20,6 +20,8 @@ */ package org.onap.ccsdk.features.sdnr.wt.apigateway.test; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpEsServlet; @@ -30,8 +32,10 @@ import javax.servlet.ServletException; public class TestDatabaseServlet extends HelpServletBase{ + private static final int PORT = 40002; + public TestDatabaseServlet() { - super("/database",40002); + super("/database",PORT); } @@ -51,7 +55,7 @@ public class TestDatabaseServlet extends HelpServletBase{ String query = "{\"query\":{\"match_all\":{}}}"; String tmpconfigcontent = "aai=off" + LR + "aaiHeaders=[]" + LR + "database=off" + LR + "insecure=0" + LR + "cors=0"; - String tmpconfigcontent2 = "aai=off" + LR + "aaiHeaders=[]" + LR + "database=http://" + HOST + ":" + this.port + LR + String tmpconfigcontent2 = "aai=off" + LR + "aaiHeaders=[]" + LR + "database=http://" + HOST + ":" + PORT + LR + "insecure=1" + LR + "cors=1"; this.setServlet(new HelpEsServlet()); // test diabled message @@ -77,5 +81,12 @@ public class TestDatabaseServlet extends HelpServletBase{ } - + @Before + public void init() throws IOException{ + HelpServletBase.initEsTestWebserver(PORT); + } + @After + public void deinit() { + HelpServletBase.stopTestWebserver(); + } } diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestHttpClient.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestHttpClient.java new file mode 100644 index 000000000..6e21bc133 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestHttpClient.java @@ -0,0 +1,54 @@ +package org.onap.ccsdk.features.sdnr.wt.apigateway.test; + +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.apigateway.database.http.BaseHTTPClient; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase; + +public class TestHttpClient { + + private static final int PORT =40010; + private static final String BASEURI = "/test"; + + private class TestBaseHTTPClient extends BaseHTTPClient { + public TestBaseHTTPClient() { + super(String.format("http://localhost:%s%s",PORT,BASEURI)); + } + public void doTest() { + String[] methods=new String[] {"GET","POST","PUT","DELETE"}; + Map<String, String> headers=new HashMap<String,String>(); + headers.put("Content-Type","application/json"); + headers.put("Authorization",BaseHTTPClient.getAuthorizationHeaderValue("admin","admin")); + for(String method:methods) { + try { + this.sendRequest(String.format("%s%s", BASEURI,"/abc"), method, "abddef", headers); + } catch (IOException e) { + e.printStackTrace(); + fail(String.format("problem with method %s: %s",method,e.getMessage())); + } + } + } + } + @Test + public void test() { + TestBaseHTTPClient client = new TestBaseHTTPClient(); + client.doTest(); + } + + + @Before + public void init() throws IOException{ + HelpServletBase.initEsTestWebserver(PORT); + } + @After + public void deinit() { + HelpServletBase.stopTestWebserver(); + } +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestMediatorServerServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestMediatorServerServlet.java index 7fa20f788..2a6f196bd 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestMediatorServerServlet.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestMediatorServerServlet.java @@ -1,26 +1,37 @@ /* - * ============LICENSE_START======================================================= ONAP : - * CCSDK.apps.sdnr.wt.apigateway - * ================================================================================ Copyright (C) - * 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. - * ================================================================================ 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 + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps.sdnr.wt.apigateway + * ================================================================================ + * Copyright (C) 2018 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * 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 + * 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========================================================= + * 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.ccsdk.features.sdnr.wt.apigateway.test; import java.io.File; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + import javax.servlet.ServletException; + +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; +import org.onap.ccsdk.features.sdnr.wt.apigateway.database.MediatorServerInfo; import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpMsServlet; import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase; @@ -54,16 +65,27 @@ public class TestMediatorServerServlet extends HelpServletBase { testrequest(HTTPMETHOD_DELETE, query, expectedResponse, false); servlet.setOfflineStatus(false); - testrequest(HTTPMETHOD_GET, query, HelpMsServlet.RESPONSE_GET, true); - testrequest(HTTPMETHOD_POST, query, HelpMsServlet.RESPONSE_POST, true); - testrequest(HTTPMETHOD_PUT, query, HelpMsServlet.RESPONSE_PUT, true); - testrequest(HTTPMETHOD_DELETE, query, HelpMsServlet.RESPONSE_DELETE, true); - testrequest(HTTPMETHOD_OPTIONS, query, "", false); + Map<String, MediatorServerInfo> entries = new HashMap<String,MediatorServerInfo>(); + entries.put("123", new MediatorServerInfo("1", "http://localhost:" + PORT)); + servlet.setMediatorEntries(entries); + testrequest("/123/test/abc",HTTPMETHOD_GET, query, HelpMsServlet.RESPONSE_GET, true); + testrequest("/123/test/abc",HTTPMETHOD_POST, query, HelpMsServlet.RESPONSE_POST, true); + testrequest("/123/test/abc",HTTPMETHOD_PUT, query, HelpMsServlet.RESPONSE_PUT, true); + testrequest("/123/test/abc",HTTPMETHOD_DELETE, query, HelpMsServlet.RESPONSE_DELETE, true); + testrequest("/123/test/abc",HTTPMETHOD_OPTIONS, query, "", false); if (tmpFile.exists()) { tmpFile.delete(); } } + @Before + public void init() throws IOException{ + HelpServletBase.initEsTestWebserver(PORT,"/test"); + } + @After + public void deinit() { + HelpServletBase.stopTestWebserver(); + } } diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestQueryCallback.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestQueryCallback.java new file mode 100644 index 000000000..78341dfad --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestQueryCallback.java @@ -0,0 +1,98 @@ +package org.onap.ccsdk.features.sdnr.wt.apigateway.test; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.apigateway.EsServlet.IRequestCallback; +import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpEsServlet; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase; + +public class TestQueryCallback extends HelpServletBase{ + + private static final int PORT = 40011; + + public TestQueryCallback() { + super("/database",PORT); + } + + final String LR = "\n"; + + + private boolean hasCallback=false; + + @Test + public void test() throws ServletException, IOException { + + String tmpFilename = "tmp1.cfg"; + File tmpFile = new File(tmpFilename); + if (tmpFile.exists()) + tmpFile.delete(); + MyProperties properties = MyProperties.Instantiate(tmpFile,true); + String query = "{\"query\":{\"match_all\":{}}}"; + String tmpconfigcontent2 = "aai=off" + LR + "aaiHeaders=[]" + LR + "database=http://" + HOST + ":" + PORT + LR + + "insecure=1" + LR + "cors=1"; + HelpEsServlet servlet = new HelpEsServlet(); + this.setServlet(servlet); + HelpEsServlet.registerRequestCallback("/mwtn/mediator-server", new IRequestCallback() { + + @Override + public void onRequest(String uri, String method) { + hasCallback=true; + + } + }); + properties.load(new ByteArrayInputStream(tmpconfigcontent2.getBytes())); + testrequest("/database/mwtn/mediator-server/_search",HTTPMETHOD_POST, query, HelpEsServlet.RESPONSE_POST, true); + int wait=10; + while(wait-->0) { + if(hasCallback) { + break; + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.interrupted(); + } + } + assertTrue("no request callback received",hasCallback); + hasCallback=false; + testrequest("/database/mwtn/mediatr-server/_search",HTTPMETHOD_POST, query, HelpEsServlet.RESPONSE_POST, true); + wait=5; + while(wait-->0) { + if(hasCallback) { + break; + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.interrupted(); + } + } + assertFalse("received request callback",hasCallback); + + + + if (tmpFile.exists()) + tmpFile.delete(); + + + } + @Before + public void init() throws IOException{ + HelpServletBase.initEsTestWebserver(PORT,"/database"); + } + @After + public void deinit() { + HelpServletBase.stopTestWebserver(); + } + +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpMsServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpMsServlet.java index b030f6487..344dff671 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpMsServlet.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpMsServlet.java @@ -21,12 +21,14 @@ package org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper; import java.io.IOException; +import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.onap.ccsdk.features.sdnr.wt.apigateway.MsServlet; +import org.onap.ccsdk.features.sdnr.wt.apigateway.database.MediatorServerInfo; public class HelpMsServlet extends MsServlet implements IPublicServlet { @@ -80,4 +82,7 @@ public class HelpMsServlet extends MsServlet implements IPublicServlet { protected String getBaseUrl(String dbServerId) { return this.baseurl; } + public void setMediatorEntries(Map<String, MediatorServerInfo> entries) { + this.getEntryProvider().setEntries(entries); + } } diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServletBase.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServletBase.java index 7b7cba239..f89ca4dcf 100644 --- a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServletBase.java +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServletBase.java @@ -40,37 +40,35 @@ import java.util.concurrent.Executors; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.After; import org.junit.Before; -import org.onap.ccsdk.features.sdnr.wt.apigateway.AaiServlet; - import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class HelpServletBase { - protected static final String HTTPMETHOD_GET = "GET"; - protected static final String HTTPMETHOD_POST = "POST"; - protected static final String HTTPMETHOD_PUT = "PUT"; - protected static final String HTTPMETHOD_DELETE = "DELETE"; - protected static final String HTTPMETHOD_OPTIONS = "OPTIONS"; + public static final String HTTPMETHOD_GET = "GET"; + public static final String HTTPMETHOD_POST = "POST"; + public static final String HTTPMETHOD_PUT = "PUT"; + public static final String HTTPMETHOD_DELETE = "DELETE"; + public static final String HTTPMETHOD_OPTIONS = "OPTIONS"; private IPublicServlet servlet; - private HttpServer server; + private static HttpServer server; + private static ExecutorService httpThreadPool; + public final String HOST = "localhost"; - protected final int port; + protected static int testPort; private final String baseUri; protected static final String LR = "\n"; public HelpServletBase(String baseuri, int port) { this.baseUri=baseuri; - this.port = port; + testPort = port; } - private ExecutorService httpThreadPool; public void setServlet(IPublicServlet s) { @@ -78,10 +76,13 @@ public class HelpServletBase { } protected void testrequest(String method, String data, String expectedResponse, boolean exact) { - this.testrequest(method, data, expectedResponse, exact, null); + this.testrequest("/mwtn/test",method, data, expectedResponse, exact, null); + } + protected void testrequest(String uri,String method, String data, String expectedResponse, boolean exact) { + this.testrequest(uri,method, data, expectedResponse, exact, null); } - protected void testrequest(String method, String data, String expectedResponse, boolean exact, + protected void testrequest(String uri,String method, String data, String expectedResponse, boolean exact, Map<String, String> headersToCheck) { HttpServletRequest mockRequest = mock(HttpServletRequest.class); @@ -107,7 +108,7 @@ public class HelpServletBase { headers.add("User-Agent"); Enumeration<String> headerNames = headers.elements(); try { - when(mockRequest.getRequestURI()).thenReturn(this.baseUri+"/mwtn/test"); + when(mockRequest.getRequestURI()).thenReturn(this.baseUri+uri); when(mockRequest.getHeaderNames()).thenReturn(headerNames); when(mockRequest.getHeader("Accept")).thenReturn("application/json"); when(mockRequest.getHeader("User-Agent")).thenReturn("Gecko abc"); @@ -140,35 +141,46 @@ public class HelpServletBase { } } - @Before - public void initEsTestWebserver() throws IOException { - this.server = HttpServer.create(new InetSocketAddress("127.0.0.1",this.port), 0); - this.httpThreadPool = Executors.newFixedThreadPool(5); - this.server.setExecutor(this.httpThreadPool); - this.server.createContext("/mwtn/test", new MyHandler()); + private void init() throws IOException{ + + + initEsTestWebserver(testPort); + } + @After + private void deinit() { + stopTestWebserver(); + } + + public static void initEsTestWebserver(int port) throws IOException { + initEsTestWebserver(port, "/mwtn/test"); + } + public static void initEsTestWebserver(int port,String baseUri) throws IOException { + server = HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0); + httpThreadPool = Executors.newFixedThreadPool(5); + server.setExecutor(httpThreadPool); + server.createContext(baseUri, new MyHandler()); //server.createContext("/", new MyRootHandler()); - this.server.setExecutor(null); // creates a default executor - this.server.start(); + server.setExecutor(null); // creates a default executor + server.start(); System.out.println("http server started"); } - @After - public void stopTestWebserver() { - if (this.server != null) { - this.server.stop(0); - this.httpThreadPool.shutdownNow(); + public static void stopTestWebserver() { + if (server != null) { + server.stop(0); + httpThreadPool.shutdownNow(); System.out.println("http server stopped" ); } } - static class MyHandler implements HttpHandler { + public static class MyHandler implements HttpHandler { @Override public void handle(HttpExchange t) throws IOException { String method = t.getRequestMethod(); - System.out.println("req method: " + method); + System.out.println(String.format("req received: %s %s" ,method,t.getRequestURI())); OutputStream os = null; try { if (method.equals(HTTPMETHOD_GET)) { |