diff options
Diffstat (limited to 'sdnr/wt/apigateway/provider/src/test/java')
11 files changed, 867 insertions, 0 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 new file mode 100644 index 000000000..a34d3cac1 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/AllTests.java @@ -0,0 +1,31 @@ +/* + * ============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 new file mode 100644 index 000000000..fd76be7af --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestAaiServlet.java @@ -0,0 +1,76 @@ +/* + * ============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 java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpAaiServlet; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpEsServlet; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase; + +public class TestAaiServlet extends HelpServletBase{ + + public TestAaiServlet() { + super("/aai",40001); + } + + @Test + public void test() throws ServletException, IOException { + + String tmpFilename = "tmp.cfg"; + File tmpFile = new File(tmpFilename); + if (tmpFile.exists()) + tmpFile.delete(); + MyProperties properties = MyProperties.Instantiate(tmpFile,true); + 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 + + "insecure=1" + LR + "cors=1"; + this.setServlet(new HelpAaiServlet()); + // test diabled message + properties.load(new ByteArrayInputStream(tmpconfigcontent.getBytes())); + String expectedResponse = "offline"; + testrequest(HTTPMETHOD_GET, query, expectedResponse, false); + testrequest(HTTPMETHOD_POST, query, expectedResponse, false); + testrequest(HTTPMETHOD_PUT, query, expectedResponse, false); + testrequest(HTTPMETHOD_DELETE, query, expectedResponse, false); + + // initEsTestWebserver(port); + properties.load(new ByteArrayInputStream(tmpconfigcontent2.getBytes())); + testrequest(HTTPMETHOD_GET, query, HelpEsServlet.RESPONSE_GET, true); + testrequest(HTTPMETHOD_POST, query, HelpEsServlet.RESPONSE_POST, true); + testrequest(HTTPMETHOD_PUT, query, HelpEsServlet.RESPONSE_PUT, true); + testrequest(HTTPMETHOD_DELETE, query, HelpEsServlet.RESPONSE_DELETE, true); + testrequest(HTTPMETHOD_OPTIONS, query, "", false); + // stopTestWebserver(); + if (tmpFile.exists()) + tmpFile.delete(); + + } + +} 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 new file mode 100644 index 000000000..143766fdb --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseServlet.java @@ -0,0 +1,81 @@ +/* + * ============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.Test; +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; + +import java.io.*; +import javax.servlet.ServletException; + +public class TestDatabaseServlet extends HelpServletBase{ + + public TestDatabaseServlet() { + super("/database",40002); + } + + + + final String LR = "\n"; + + + + @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 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 + + "insecure=1" + LR + "cors=1"; + this.setServlet(new HelpEsServlet()); + // test diabled message + properties.load(new ByteArrayInputStream(tmpconfigcontent.getBytes())); + String expectedResponse = "offline"; + testrequest(HTTPMETHOD_GET, query, expectedResponse, false); + testrequest(HTTPMETHOD_POST, query, expectedResponse, false); + testrequest(HTTPMETHOD_PUT, query, expectedResponse, false); + testrequest(HTTPMETHOD_DELETE, query, expectedResponse, false); + + // initEsTestWebserver(port); + properties.load(new ByteArrayInputStream(tmpconfigcontent2.getBytes())); + testrequest(HTTPMETHOD_GET, query, HelpEsServlet.RESPONSE_GET, true); + testrequest(HTTPMETHOD_POST, query, HelpEsServlet.RESPONSE_POST, true); + testrequest(HTTPMETHOD_PUT, query, HelpEsServlet.RESPONSE_PUT, true); + testrequest(HTTPMETHOD_DELETE, query, HelpEsServlet.RESPONSE_DELETE, true); + testrequest(HTTPMETHOD_OPTIONS, query, "", false); + // stopTestWebserver(); + + + if (tmpFile.exists()) + tmpFile.delete(); + + + } + +} 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 new file mode 100644 index 000000000..d4197c6db --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestMediatorServerServlet.java @@ -0,0 +1,71 @@ +/* + * ============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 java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpMsServlet; +import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase; + +public class TestMediatorServerServlet extends HelpServletBase{ + + private static final int PORT = 40004; + + public TestMediatorServerServlet() { + super("/ms",PORT); + } + + @Test + public void test() throws ServletException, IOException { + + String tmpFilename = "tmp3.cfg"; + File tmpFile = new File(tmpFilename); + if (tmpFile.exists()) + tmpFile.delete(); + MyProperties.Instantiate(tmpFile,true); + String query = "{\"query\":{\"match_all\":{}}}"; + HelpMsServlet servlet = new HelpMsServlet(); + this.setServlet(servlet); + // test diabled message + servlet.setOfflineStatus(true); + servlet.setBaseUrl("http://localhost:"+PORT); + String expectedResponse = "offline"; + testrequest(HTTPMETHOD_GET, query, expectedResponse, false); + testrequest(HTTPMETHOD_POST, query, expectedResponse, false); + testrequest(HTTPMETHOD_PUT, query, expectedResponse, false); + 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); + + } + +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java new file mode 100644 index 000000000..1a63240d8 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java @@ -0,0 +1,108 @@ +/* + * ============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 static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.onap.ccsdk.features.sdnr.wt.apigateway.MyProperties; + +public class TestProperties { + + private static final boolean DEFAULT_CORSENABLED = false; + private static final boolean DEFAULT_AAIOFF = true; + private static final boolean DEFAULT_ESOFF = false; + private static final boolean DEFAULT_TRUSTINSECURE = false; + private static final String DEFAULT_AAIBASEURL = "off"; + private static Map<String, String> DEFAULT_AAIHEADERS = new HashMap<String, String>(); + private static final String DEFAULT_ESBASEURL = "http://localhost:9200"; + + private static final boolean CUSTOM_CORSENABLED = true; + private static final boolean CUSTOM_AAIOFF = false; + private static final boolean CUSTOM_ESOFF = false; + private static final boolean CUSTOM_TRUSTINSECURE = true; + private static final String CUSTOM_AAIBASEURL = "https://aai.tld:2214"; + private static Map<String, String> CUSTOM_AAIHEADERS = new HashMap<String, String>(); + private static final String CUSTOM_ESBASEURL = "http://localhost:9200"; + + private static final String LR = "\n"; + + @Test + public void test() { + final String tmpFilename = "tmp2.cfg"; + DEFAULT_AAIHEADERS.put("X-FromAppId", "SDNR"); + DEFAULT_AAIHEADERS.put("Authorization", "Basic QUFJOkFBSQ=="); + CUSTOM_AAIHEADERS.put("X-FromAppId", "SDNC"); + CUSTOM_AAIHEADERS.put("Authorization", "Basic 1234"); + final String TESTPROPERTYFILECONTENT = "aai=" + CUSTOM_AAIBASEURL + LR + "aaiHeaders=[\"X-FromAppId:" + + CUSTOM_AAIHEADERS.get("X-FromAppId") + "\",\"Authorization:" + CUSTOM_AAIHEADERS.get("Authorization") + + "\"]" + LR + "database=" + CUSTOM_ESBASEURL + LR + "insecure=" + (CUSTOM_TRUSTINSECURE ? "1" : "0") + + LR + "cors=" + (CUSTOM_CORSENABLED ? "1" : "0"); + File ftest = new File(tmpFilename); + // delete autogenerated testfile + if (ftest.exists()) + ftest.delete(); + MyProperties prop = null; + ftest = new File(tmpFilename); + try { + prop = MyProperties.Instantiate(ftest, true); + } catch (Exception e) { + fail("error instantiating properties"); + } + assertNotNull("problem without exception instantiating properties", prop); + + assertEquals("default config file was not created", true, ftest.exists()); + + // test default values + assertEquals("default value is not correct", DEFAULT_CORSENABLED, prop.corsEnabled()); + assertEquals("default value is not correct", DEFAULT_AAIOFF, prop.isAAIOff()); + assertEquals("default value is not correct", DEFAULT_ESOFF, prop.isEsOff()); + assertEquals("default value is not correct", DEFAULT_TRUSTINSECURE, prop.trustInsecure()); + assertEquals("default value is not correct", DEFAULT_AAIBASEURL, prop.getAAIBaseUrl()); + assertEquals("default value is not correct", DEFAULT_AAIHEADERS, prop.getAAIHeaders()); + assertEquals("default value is not correct", DEFAULT_ESBASEURL, prop.getEsBaseUrl()); + + try { + prop.load(new ByteArrayInputStream(TESTPROPERTYFILECONTENT.getBytes())); + } catch (Exception e) { + fail("error loading custom values into properties"); + } + + // test custom values + assertEquals("custom value is not correct", CUSTOM_CORSENABLED, prop.corsEnabled()); + assertEquals("custom value is not correct", CUSTOM_AAIOFF, prop.isAAIOff()); + assertEquals("custom value is not correct", CUSTOM_ESOFF, prop.isEsOff()); + assertEquals("custom value is not correct", CUSTOM_TRUSTINSECURE, prop.trustInsecure()); + assertEquals("custom value is not correct", CUSTOM_AAIBASEURL, prop.getAAIBaseUrl()); + assertEquals("custom value is not correct", CUSTOM_AAIHEADERS, prop.getAAIHeaders()); + assertEquals("custom value is not correct", CUSTOM_ESBASEURL, prop.getEsBaseUrl()); + + // delete autogenerated testfile + ftest.delete(); + + } + +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpAaiServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpAaiServlet.java new file mode 100644 index 000000000..5c28c8544 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpAaiServlet.java @@ -0,0 +1,59 @@ +/* + * ============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 + * + * 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.helper; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.ccsdk.features.sdnr.wt.apigateway.AaiServlet; + +public class HelpAaiServlet extends AaiServlet implements IPublicServlet{ + + public static final String RESPONSE_GET = "This is the response get"; + public static final String RESPONSE_POST = "This is the response post"; + public static final String RESPONSE_PUT = "This is the response put"; + public static final String RESPONSE_DELETE = "This is the response delete"; + public static final String RESPONSE_OPTIONS = "This is the response options"; + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + @Override + public void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPut(req, resp); + } + @Override + public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doOptions(req, resp); + } + @Override + public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doDelete(req, resp); + } +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpEsServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpEsServlet.java new file mode 100644 index 000000000..a9cb06b10 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpEsServlet.java @@ -0,0 +1,59 @@ +/* + * ============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 + * + * 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.helper; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.ccsdk.features.sdnr.wt.apigateway.EsServlet; + +public class HelpEsServlet extends EsServlet implements IPublicServlet { + + public static final String RESPONSE_GET = "This is the response get"; + public static final String RESPONSE_POST = "This is the response post"; + public static final String RESPONSE_PUT = "This is the response put"; + public static final String RESPONSE_DELETE = "This is the response delete"; + public static final String RESPONSE_OPTIONS = "This is the response options"; + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + @Override + public void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPut(req, resp); + } + @Override + public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doOptions(req, resp); + } + @Override + public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doDelete(req, resp); + } +} 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 new file mode 100644 index 000000000..b030f6487 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpMsServlet.java @@ -0,0 +1,83 @@ +/* + * ============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 + * + * 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.helper; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.ccsdk.features.sdnr.wt.apigateway.MsServlet; + +public class HelpMsServlet extends MsServlet implements IPublicServlet { + + public static final String RESPONSE_GET = "This is the response get"; + public static final String RESPONSE_POST = "This is the response post"; + public static final String RESPONSE_PUT = "This is the response put"; + public static final String RESPONSE_DELETE = "This is the response delete"; + public static final String RESPONSE_OPTIONS = "This is the response options"; + private boolean offline = true; + private String baseurl; + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + + @Override + public void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPut(req, resp); + } + + @Override + public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doOptions(req, resp); + } + + @Override + public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doDelete(req, resp); + } + + public void setOfflineStatus(boolean offline) { + this.offline = offline; + } + + public void setBaseUrl(String url) { + this.baseurl = url; + } + + @Override + protected boolean isOff() { + return this.offline; + } + + @Override + protected String getBaseUrl(String dbServerId) { + return this.baseurl; + } +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServlet.java new file mode 100644 index 000000000..ea7690c75 --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServlet.java @@ -0,0 +1,53 @@ +/* + * ============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 + * + * 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.helper; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.ccsdk.features.sdnr.wt.apigateway.EsServlet; + +public class HelpServlet extends EsServlet { + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + @Override + public void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPut(req, resp); + } + @Override + public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doOptions(req, resp); + } + @Override + public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doDelete(req, resp); + } +} 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 new file mode 100644 index 000000000..30732073b --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServletBase.java @@ -0,0 +1,210 @@ +/* + * ============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 + * + * 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.helper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.StringWriter; +import java.net.InetSocketAddress; +import java.util.Enumeration; +import java.util.Map; +import java.util.Vector; +import java.util.concurrent.ExecutorService; +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"; + private IPublicServlet servlet; + private HttpServer server; + public final String HOST = "localhost"; + protected final int port; + private final String baseUri; + protected static final String LR = "\n"; + + public HelpServletBase(String baseuri, int port) { + this.baseUri=baseuri; + this.port = port; + } + private ExecutorService httpThreadPool; + + public void setServlet(IPublicServlet s) + { + this.servlet=s; + } + + protected void testrequest(String method, String data, String expectedResponse, boolean exact) { + this.testrequest(method, data, expectedResponse, exact, null); + } + + protected void testrequest(String method, String data, String expectedResponse, boolean exact, + Map<String, String> headersToCheck) { + + HttpServletRequest mockRequest = mock(HttpServletRequest.class); + HttpServletResponse mockResponse = mock(HttpServletResponse.class); + + StringWriter out = new StringWriter(); + ServletOutputStream printOut = new ServletOutputStream() { + + @Override + public void write(int arg0) throws IOException { + out.write(arg0); + } + }; + ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes()); + ServletInputStream inputStream= new ServletInputStream() { + @Override + public int read() throws IOException { + return bis.read(); + } + }; + Vector<String> headers=new Vector<String>(); + headers.addElement("Accept"); + headers.add("User-Agent"); + Enumeration<String> headerNames = headers.elements(); + try { + when(mockRequest.getRequestURI()).thenReturn(this.baseUri+"/mwtn/test"); + when(mockRequest.getHeaderNames()).thenReturn(headerNames); + when(mockRequest.getHeader("Accept")).thenReturn("application/json"); + when(mockRequest.getHeader("User-Agent")).thenReturn("Gecko abc"); + when(mockRequest.getInputStream()).thenReturn(inputStream); + when(mockResponse.getOutputStream()).thenReturn(printOut); + System.out.println("do a " + method + " request"); + if (method == HTTPMETHOD_GET) + this.servlet.doGet(mockRequest, mockResponse); + else if (method == HTTPMETHOD_POST) + this.servlet.doPost(mockRequest, mockResponse); + else if (method == HTTPMETHOD_PUT) + this.servlet.doPut(mockRequest, mockResponse); + else if (method == HTTPMETHOD_DELETE) + this.servlet.doDelete(mockRequest, mockResponse); + else if (method == HTTPMETHOD_OPTIONS) + this.servlet.doOptions(mockRequest, mockResponse); + else + fail("http request method " + method + " test not implemented"); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + + verify(mockResponse).setStatus(200); + if (exact) + assertEquals(expectedResponse, out.toString()); + else + assertTrue("response not for method " + method + "correct", out.toString().contains(expectedResponse)); + // currently unable to check extra headers + if (headersToCheck != null) { + + } + } + + @Before + public void initEsTestWebserver() throws IOException { + this.server = HttpServer.create(new InetSocketAddress(this.port), 0); + this.httpThreadPool = Executors.newFixedThreadPool(5); + this.server.setExecutor(this.httpThreadPool); + this.server.createContext("/mwtn/test", new MyHandler()); + //server.createContext("/", new MyRootHandler()); + this.server.setExecutor(null); // creates a default executor + this.server.start(); + System.out.println("http server started"); + } + + @After + public void stopTestWebserver() { + if (this.server != null) { + this.server.stop(0); + this.httpThreadPool.shutdownNow(); + System.out.println("http server stopped" ); + } + } + + + + static class MyHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + String method = t.getRequestMethod(); + System.out.println("req method: " + method); + OutputStream os = null; + try { + if (method.equals(HTTPMETHOD_GET)) { + t.sendResponseHeaders(200, HelpEsServlet.RESPONSE_GET.length()); + os = t.getResponseBody(); + os.write(HelpEsServlet.RESPONSE_GET.getBytes()); + } else if (method.equals(HTTPMETHOD_POST)) { + t.sendResponseHeaders(200, HelpEsServlet.RESPONSE_POST.length()); + os = t.getResponseBody(); + os.write(HelpEsServlet.RESPONSE_POST.getBytes()); + } else if (method.equals(HTTPMETHOD_PUT)) { + t.sendResponseHeaders(200, HelpEsServlet.RESPONSE_PUT.length()); + os = t.getResponseBody(); + os.write(HelpEsServlet.RESPONSE_PUT.getBytes()); + } else if (method.equals(HTTPMETHOD_DELETE)) { + t.sendResponseHeaders(200, HelpEsServlet.RESPONSE_DELETE.length()); + os = t.getResponseBody(); + os.write(HelpEsServlet.RESPONSE_DELETE.getBytes()); + } else if (method.equals(HTTPMETHOD_OPTIONS)) { + t.sendResponseHeaders(200, HelpEsServlet.RESPONSE_OPTIONS.length()); + //os = t.getResponseBody(); + //os.write(RESPONSE_OPTIONS.getBytes()); + } else { + t.sendResponseHeaders(404, 0); + } + System.out.println("req handled successful"); + + } catch (Exception e) { + System.out.println(e.getMessage()); + } + finally { + if (os != null) + { + os.close(); + } + } + } + } +} diff --git a/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/IPublicServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/IPublicServlet.java new file mode 100644 index 000000000..4a640a35f --- /dev/null +++ b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/IPublicServlet.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK + * ================================================================================ + * Copyright (C) 2017 AT&T 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.helper; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public interface IPublicServlet { + + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException; + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException; + public void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException; + public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ; + public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ; +} |