From 4a2409623b1744028abf6d5d8d1b6546919aec61 Mon Sep 17 00:00:00 2001 From: highstreetherbert Date: Fri, 10 Jul 2020 12:45:37 +0200 Subject: Reformat sdnr apigateway to ONAP code style Reformat to ONAP code style Issue-ID: SDNC-1271 Signed-off-by: highstreetherbert Change-Id: I18f7e93e461b94949cf5a7d9262eaf33b17c6ac9 Signed-off-by: highstreetherbert --- .../features/sdnr/wt/apigateway/AaiServlet.java | 93 +++--- .../features/sdnr/wt/apigateway/BaseServlet.java | 40 ++- .../features/sdnr/wt/apigateway/EsServlet.java | 136 +++++---- .../features/sdnr/wt/apigateway/MyProperties.java | 264 ++++++++-------- .../sdnr/wt/apigateway/test/TestAaiServlet.java | 99 +++--- .../wt/apigateway/test/TestDatabaseServlet.java | 120 ++++---- .../sdnr/wt/apigateway/test/TestProperties.java | 155 +++++----- .../wt/apigateway/test/helper/HelpAaiServlet.java | 60 ++-- .../wt/apigateway/test/helper/HelpEsServlet.java | 56 ++-- .../wt/apigateway/test/helper/HelpServlet.java | 44 +-- .../wt/apigateway/test/helper/HelpServletBase.java | 337 +++++++++++---------- .../wt/apigateway/test/helper/IPublicServlet.java | 14 +- 12 files changed, 723 insertions(+), 695 deletions(-) (limited to 'sdnr') diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java index 44cad5f12..0cdf79d43 100644 --- a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java +++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/AaiServlet.java @@ -27,58 +27,59 @@ import javax.servlet.http.HttpServletResponse; public class AaiServlet extends BaseServlet { - /** - * - */ - private static final long serialVersionUID = 5946205120796162644L; - private static final String OFFLINE_RESPONSE_MESSAGE = "AAI interface is offline"; - private static boolean trustAll = false; - public AaiServlet() { - super(); - } + /** + * + */ + private static final long serialVersionUID = 5946205120796162644L; + private static final String OFFLINE_RESPONSE_MESSAGE = "AAI interface is offline"; + private static boolean trustAll = false; - @Override - protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - resp.setStatus(200); - } + public AaiServlet() { + super(); + } - @Override - protected String getOfflineResponse() { - return OFFLINE_RESPONSE_MESSAGE; - } + @Override + protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setStatus(200); + } - @Override - protected boolean isOff() { - return MyProperties.getInstance().isAAIOff(); - } + @Override + protected String getOfflineResponse() { + return OFFLINE_RESPONSE_MESSAGE; + } - @Override - protected String getRemoteUrl(String uri) { + @Override + protected boolean isOff() { + return MyProperties.getInstance().isAAIOff(); + } - if (uri.startsWith("/")) { - uri = uri.substring(1); - } - if (uri.startsWith("aai")) { - uri = uri.substring("aai".length()); - } - if (uri.startsWith("/")) { - uri = uri.substring(1); - } - String base = MyProperties.getInstance().getAAIBaseUrl(); - if (!base.endsWith("/")) { - base += "/"; - } + @Override + protected String getRemoteUrl(String uri) { - return base + uri; - } + if (uri.startsWith("/")) { + uri = uri.substring(1); + } + if (uri.startsWith("aai")) { + uri = uri.substring("aai".length()); + } + if (uri.startsWith("/")) { + uri = uri.substring(1); + } + String base = MyProperties.getInstance().getAAIBaseUrl(); + if (!base.endsWith("/")) { + base += "/"; + } - @Override - protected boolean doTrustAll() { - return trustAll; - } + return base + uri; + } - @Override - protected void trustAll(boolean trust) { - trustAll = trust; - } + @Override + protected boolean doTrustAll() { + return trustAll; + } + + @Override + protected void trustAll(boolean trust) { + trustAll = trust; + } } diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java index a7173df28..dab7ad776 100644 --- a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java +++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/BaseServlet.java @@ -61,7 +61,9 @@ public abstract class BaseServlet extends HttpServlet { protected abstract boolean isOff(); protected abstract boolean doTrustAll(); + protected abstract void trustAll(boolean trust); + protected abstract String getRemoteUrl(String uri); /** @@ -74,7 +76,7 @@ public abstract class BaseServlet extends HttpServlet { sc = SSLContext.getInstance("TLSv1.2"); if (trustall) { if (trustCerts == null) { - trustCerts = new TrustManager[] { new javax.net.ssl.X509TrustManager() { + trustCerts = new TrustManager[] {new javax.net.ssl.X509TrustManager() { @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[] {}; @@ -89,7 +91,7 @@ public abstract class BaseServlet extends HttpServlet { public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { // do not check anything when trust all } - } }; + }}; } } else { if (trustCerts != null) { @@ -162,8 +164,7 @@ public abstract class BaseServlet extends HttpServlet { LOG.warn(e.getMessage()); } http.disconnect(); - } - else { + } else { this.set404Response(resp); } } @@ -188,8 +189,7 @@ public abstract class BaseServlet extends HttpServlet { LOG.warn(e.getMessage()); } http.disconnect(); - } - else { + } else { this.set404Response(resp); } } @@ -214,8 +214,7 @@ public abstract class BaseServlet extends HttpServlet { LOG.warn(e.getMessage()); } http.disconnect(); - } - else { + } else { this.set404Response(resp); } } @@ -240,8 +239,7 @@ public abstract class BaseServlet extends HttpServlet { LOG.warn(e.getMessage()); } http.disconnect(); - } - else { + } else { this.set404Response(resp); } } @@ -253,24 +251,24 @@ public abstract class BaseServlet extends HttpServlet { private URLConnection getConnection(HttpServletRequest req, final String method) throws IOException { - LOG.debug("{} Request to {}", method,req.getRequestURL()); + LOG.debug("{} Request to {}", method, req.getRequestURL()); String surl = this.getRemoteUrl(req.getRequestURI()); - if("GET".equals(method)) { + if ("GET".equals(method)) { Enumeration params = req.getParameterNames(); - if(params!=null) { + if (params != null) { String param; - if(params.hasMoreElements()) { - param=(String)params.nextElement(); - surl+="?"+param+"="+req.getParameter(param); + if (params.hasMoreElements()) { + param = (String) params.nextElement(); + surl += "?" + param + "=" + req.getParameter(param); } - while(params.hasMoreElements()) { - param=(String)params.nextElement(); - surl+="&"+param+"="+req.getParameter(param); + while (params.hasMoreElements()) { + param = (String) params.nextElement(); + surl += "&" + param + "=" + req.getParameter(param); } } } LOG.debug("RemoteURL: {}", surl); - if(surl==null) { + if (surl == null) { return null; } URL url = new URL(surl); @@ -288,7 +286,7 @@ public abstract class BaseServlet extends HttpServlet { String s = ""; Enumeration headers = req.getHeaderNames(); while (headers.hasMoreElements()) { - String h = (String)headers.nextElement(); + String h = (String) headers.nextElement(); String v = req.getHeader(h); if (h != null && h.equals("Host")) { v = url.getAuthority(); diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java index f08a7fffa..5db305ecb 100644 --- a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java +++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/EsServlet.java @@ -30,68 +30,74 @@ import org.slf4j.LoggerFactory; public class EsServlet extends BaseServlet { - /** - * - */ - private static final long serialVersionUID = -3996363343749995011L; - private static final String OFFLINE_RESPONSE_MESSAGE = "Database interface is offline"; - private static Logger LOG = LoggerFactory.getLogger(EsServlet.class); - - private static boolean trustAll = false; - - public EsServlet() { - super(); - } - - @Override - protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - if (MyProperties.getInstance().corsEnabled()) { - resp.addHeader("Access-Control-Allow-Origin", "*"); - resp.addHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE"); - resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); - } - resp.setStatus(200); - } - - @Override - protected String getOfflineResponse() { - return OFFLINE_RESPONSE_MESSAGE; - } - - @Override - protected boolean isOff() { - return MyProperties.getInstance().isEsOff(); - } - - @Override - protected String getRemoteUrl(String uri) { - if (uri != null && uri.length() > 0) { - uri = uri.substring("/database".length()); - } - return MyProperties.getInstance().getEsBaseUrl() + uri; - } - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.doGet(req, resp); - } - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.doPost(req, resp); - } - @Override - protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.doPut(req, resp); - } - @Override - protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.doDelete(req, resp); - } - @Override - protected boolean doTrustAll() { - return trustAll; - } - @Override - protected void trustAll(boolean trust) { - trustAll = trust; - } -} + /** + * + */ + private static final long serialVersionUID = -3996363343749995011L; + private static final String OFFLINE_RESPONSE_MESSAGE = "Database interface is offline"; + private static Logger LOG = LoggerFactory.getLogger(EsServlet.class); + + private static boolean trustAll = false; + + public EsServlet() { + super(); + } + + @Override + protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + if (MyProperties.getInstance().corsEnabled()) { + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE"); + resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + } + resp.setStatus(200); + } + + @Override + protected String getOfflineResponse() { + return OFFLINE_RESPONSE_MESSAGE; + } + + @Override + protected boolean isOff() { + return MyProperties.getInstance().isEsOff(); + } + + @Override + protected String getRemoteUrl(String uri) { + if (uri != null && uri.length() > 0) { + uri = uri.substring("/database".length()); + } + return MyProperties.getInstance().getEsBaseUrl() + uri; + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + + @Override + protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPut(req, resp); + } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doDelete(req, resp); + } + + @Override + protected boolean doTrustAll() { + return trustAll; + } + + @Override + protected void trustAll(boolean trust) { + trustAll = trust; + } +} diff --git a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java index 36fce2081..d49b4dce2 100644 --- a/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java +++ b/sdnr/wt/apigateway/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/apigateway/MyProperties.java @@ -35,137 +35,137 @@ import org.slf4j.LoggerFactory; public class MyProperties { - private static Logger LOG = LoggerFactory.getLogger(MyProperties.class); - public static final String PROPFILE = "etc/apigateway.properties"; - private static final String DEFAULT_AAI_HEADERS = "[\"X-FromAppId:SDNR\",\"Authorization:Basic QUFJOkFBSQ==\"]"; - private static final String DEFAULT_CORSENABLED = "0"; - private static final String DEFAULT_TRUSTINSECURE = "0"; - private static final String DEFAULT_ESDATABASE = "http://sdnrdb:9200"; - private static final String DEFAULT_AAI = "off"; - - @Override - public String toString() { - return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase - + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]"; - } - - private static MyProperties mObj; - - private String aaiBase; - private Map aaiHeaders; - private String esBase; - - private boolean trustInsecure; - - private boolean corsEnabled; - - public boolean isAAIOff() { - return this.aaiBase == null ? true : this.aaiBase.equals("off"); - } - - public boolean isEsOff() { - return this.esBase == null ? true : this.esBase.equals("off"); - } - - public String getAAIBaseUrl() { - return this.aaiBase; - } - - public String getEsBaseUrl() { - return this.esBase; - } - - public Map getAAIHeaders() { - return this.aaiHeaders; - } - - public boolean trustInsecure() { - return this.trustInsecure; - } - - public boolean corsEnabled() { - return this.corsEnabled; - } - - public static MyProperties Instantiate() throws IOException, NumberFormatException { - return Instantiate(new File(PROPFILE)); - } - - public static MyProperties Instantiate(File file) throws IOException, NumberFormatException { - - return Instantiate(file, false); - } - - public static MyProperties Instantiate(File file, boolean force) throws IOException, NumberFormatException { - if (mObj == null || force) { - mObj = new MyProperties(file); - LOG.debug("instantiated: {}", mObj.toString()); - } - return mObj; - } - - private MyProperties(File file) throws IOException, NumberFormatException { - this.aaiBase = "off"; - this.trustInsecure = false; - if (!file.exists()) { - this.writeDefaults(file); - } - this.load(new FileInputStream(file)); - } - - public void load(InputStream in) throws IOException, NumberFormatException { - - Properties defaultProps = new Properties(); - defaultProps.load(in); - in.close(); - - this.aaiBase = defaultProps.getProperty("aai", DEFAULT_AAI); - this.aaiHeaders = _parseHeadersMap(defaultProps.getProperty("aaiHeaders", DEFAULT_AAI_HEADERS)); - this.esBase = defaultProps.getProperty("database", DEFAULT_ESDATABASE); - this.trustInsecure = Integer.parseInt(defaultProps.getProperty("insecure", DEFAULT_TRUSTINSECURE)) == 1; - this.corsEnabled = Integer.parseInt(defaultProps.getProperty("cors", DEFAULT_CORSENABLED)) == 1; - } - - private static Map _parseHeadersMap(String s) { - Map r = new HashMap<>(); - try { - JSONArray a = new JSONArray(s); - if ( a.length() > 0) { - for (int i = 0; i < a.length(); i++) { - String item = a.getString(i); - String[] hlp = item.split(":"); - if (hlp.length > 1) { - r.put(hlp[0], hlp[1]); - } - } - } - } catch (Exception e) { - LOG.warn("problem loading headers map: {}",e.getMessage()); - } - return r; - } - - private String writeDefaults(File f) throws IOException { - StringBuilder sb = new StringBuilder(); - final String LR = "\n"; - FileWriter fw = new FileWriter(f); - sb.append("aai=" + DEFAULT_AAI + LR); - sb.append("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR); - sb.append("database=" + DEFAULT_ESDATABASE + LR); - sb.append("insecure=" + DEFAULT_TRUSTINSECURE + LR); - sb.append("cors=" + DEFAULT_CORSENABLED); - try { - fw.write(sb.toString()); - } catch (Exception e) { - LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath() , e.getMessage()); - } finally { - fw.close(); - } - return sb.toString(); - } - - public static MyProperties getInstance() { - return mObj; - } + private static Logger LOG = LoggerFactory.getLogger(MyProperties.class); + public static final String PROPFILE = "etc/apigateway.properties"; + private static final String DEFAULT_AAI_HEADERS = "[\"X-FromAppId:SDNR\",\"Authorization:Basic QUFJOkFBSQ==\"]"; + private static final String DEFAULT_CORSENABLED = "0"; + private static final String DEFAULT_TRUSTINSECURE = "0"; + private static final String DEFAULT_ESDATABASE = "http://sdnrdb:9200"; + private static final String DEFAULT_AAI = "off"; + + @Override + public String toString() { + return "MyProperties [aaiBase=" + aaiBase + ", aaiHeaders=" + aaiHeaders + ", esBase=" + esBase + + ", trustInsecure=" + trustInsecure + ", corsEnabled=" + corsEnabled + "]"; + } + + private static MyProperties mObj; + + private String aaiBase; + private Map aaiHeaders; + private String esBase; + + private boolean trustInsecure; + + private boolean corsEnabled; + + public boolean isAAIOff() { + return this.aaiBase == null ? true : this.aaiBase.equals("off"); + } + + public boolean isEsOff() { + return this.esBase == null ? true : this.esBase.equals("off"); + } + + public String getAAIBaseUrl() { + return this.aaiBase; + } + + public String getEsBaseUrl() { + return this.esBase; + } + + public Map getAAIHeaders() { + return this.aaiHeaders; + } + + public boolean trustInsecure() { + return this.trustInsecure; + } + + public boolean corsEnabled() { + return this.corsEnabled; + } + + public static MyProperties Instantiate() throws IOException, NumberFormatException { + return Instantiate(new File(PROPFILE)); + } + + public static MyProperties Instantiate(File file) throws IOException, NumberFormatException { + + return Instantiate(file, false); + } + + public static MyProperties Instantiate(File file, boolean force) throws IOException, NumberFormatException { + if (mObj == null || force) { + mObj = new MyProperties(file); + LOG.debug("instantiated: {}", mObj.toString()); + } + return mObj; + } + + private MyProperties(File file) throws IOException, NumberFormatException { + this.aaiBase = "off"; + this.trustInsecure = false; + if (!file.exists()) { + this.writeDefaults(file); + } + this.load(new FileInputStream(file)); + } + + public void load(InputStream in) throws IOException, NumberFormatException { + + Properties defaultProps = new Properties(); + defaultProps.load(in); + in.close(); + + this.aaiBase = defaultProps.getProperty("aai", DEFAULT_AAI); + this.aaiHeaders = _parseHeadersMap(defaultProps.getProperty("aaiHeaders", DEFAULT_AAI_HEADERS)); + this.esBase = defaultProps.getProperty("database", DEFAULT_ESDATABASE); + this.trustInsecure = Integer.parseInt(defaultProps.getProperty("insecure", DEFAULT_TRUSTINSECURE)) == 1; + this.corsEnabled = Integer.parseInt(defaultProps.getProperty("cors", DEFAULT_CORSENABLED)) == 1; + } + + private static Map _parseHeadersMap(String s) { + Map r = new HashMap<>(); + try { + JSONArray a = new JSONArray(s); + if (a.length() > 0) { + for (int i = 0; i < a.length(); i++) { + String item = a.getString(i); + String[] hlp = item.split(":"); + if (hlp.length > 1) { + r.put(hlp[0], hlp[1]); + } + } + } + } catch (Exception e) { + LOG.warn("problem loading headers map: {}", e.getMessage()); + } + return r; + } + + private String writeDefaults(File f) throws IOException { + StringBuilder sb = new StringBuilder(); + final String LR = "\n"; + FileWriter fw = new FileWriter(f); + sb.append("aai=" + DEFAULT_AAI + LR); + sb.append("aaiHeaders=" + DEFAULT_AAI_HEADERS + LR); + sb.append("database=" + DEFAULT_ESDATABASE + LR); + sb.append("insecure=" + DEFAULT_TRUSTINSECURE + LR); + sb.append("cors=" + DEFAULT_CORSENABLED); + try { + fw.write(sb.toString()); + } catch (Exception e) { + LOG.warn("problem writing default values to propertyfile {} : {}", f.getAbsolutePath(), e.getMessage()); + } finally { + fw.close(); + } + return sb.toString(); + } + + public static MyProperties getInstance() { + return mObj; + } } 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 0af4d69df..06bf2c17e 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 @@ -34,53 +34,56 @@ 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 class TestAaiServlet extends HelpServletBase { - private static final int PORT = 40001; - public TestAaiServlet() { - super("/aai",PORT); - } - - @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 + ":" + PORT + LR + "aaiHeaders=[]" + LR + "database=off"+ LR - + "insecure=1" + LR + "cors=1"; - this.setServlet(new HelpAaiServlet()); - // test disabled 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(); - - } - @Before - public void init() throws IOException{ - HelpServletBase.initEsTestWebserver(PORT); - } - @After - public void deinit() { - HelpServletBase.stopTestWebserver(); - } + private static final int PORT = 40001; + + public TestAaiServlet() { + super("/aai", PORT); + } + + @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 + ":" + PORT + LR + "aaiHeaders=[]" + LR + "database=off" + LR + + "insecure=1" + LR + "cors=1"; + this.setServlet(new HelpAaiServlet()); + // test disabled 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(); + + } + + @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/TestDatabaseServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestDatabaseServlet.java index d7b4c1511..bc6859ac1 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 @@ -30,63 +30,65 @@ import org.onap.ccsdk.features.sdnr.wt.apigateway.test.helper.HelpServletBase; import java.io.*; import javax.servlet.ServletException; -public class TestDatabaseServlet extends HelpServletBase{ - - private static final int PORT = 40002; - - public TestDatabaseServlet() { - super("/database",PORT); - } - - - - 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 + ":" + 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(); - - - } - @Before - public void init() throws IOException{ - HelpServletBase.initEsTestWebserver(PORT); - } - @After - public void deinit() { - HelpServletBase.stopTestWebserver(); - } +public class TestDatabaseServlet extends HelpServletBase { + + private static final int PORT = 40002; + + public TestDatabaseServlet() { + super("/database", PORT); + } + + + + 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 + ":" + 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(); + + + } + + @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/TestProperties.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/TestProperties.java index 6efd06feb..6fad884ff 100644 --- 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 @@ -34,82 +34,83 @@ 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 DEFAULT_AAIHEADERS = new HashMap(); - private static final String DEFAULT_ESBASEURL = "http://sdnrdb: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 CUSTOM_AAIHEADERS = new HashMap(); - private static final String CUSTOM_ESBASEURL = "http://localhost:9200"; - - private static final String LR = "\n"; - final String tmpFilename = "tmp2.cfg"; - - @Before - @After - public void init() { - File f = new File(tmpFilename); - if(f.exists()) { - f.delete(); - } - } - @Test - public void test() { - 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); - 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(); - - } + 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 DEFAULT_AAIHEADERS = new HashMap(); + private static final String DEFAULT_ESBASEURL = "http://sdnrdb: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 CUSTOM_AAIHEADERS = new HashMap(); + private static final String CUSTOM_ESBASEURL = "http://localhost:9200"; + + private static final String LR = "\n"; + final String tmpFilename = "tmp2.cfg"; + + @Before + @After + public void init() { + File f = new File(tmpFilename); + if (f.exists()) { + f.delete(); + } + } + + @Test + public void test() { + 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); + 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 index 5c28c8544..f6987b2fa 100644 --- 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 @@ -28,32 +28,36 @@ 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); - } +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 index a9cb06b10..901dd5810 100644 --- 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 @@ -30,30 +30,34 @@ 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); - } + 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/HelpServlet.java b/sdnr/wt/apigateway/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/apigateway/test/helper/HelpServlet.java index ea7690c75..ddab06add 100644 --- 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 @@ -30,24 +30,28 @@ 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); - } + @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 index f89ca4dcf..0bc42c342 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 @@ -51,172 +51,173 @@ import com.sun.net.httpserver.HttpServer; public class HelpServletBase { - 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 static HttpServer server; - private static ExecutorService httpThreadPool; - - public final String HOST = "localhost"; - protected static int testPort; - private final String baseUri; - protected static final String LR = "\n"; - - public HelpServletBase(String baseuri, int port) { - this.baseUri=baseuri; - testPort = port; - } - - public void setServlet(IPublicServlet s) - { - this.servlet=s; - } - - protected void testrequest(String method, String data, String expectedResponse, boolean exact) { - 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 uri,String method, String data, String expectedResponse, boolean exact, - Map 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 headers=new Vector(); - headers.addElement("Accept"); - headers.add("User-Agent"); - Enumeration headerNames = headers.elements(); - try { - 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"); - 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 - 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()); - server.setExecutor(null); // creates a default executor - server.start(); - System.out.println("http server started"); - } - - public static void stopTestWebserver() { - if (server != null) { - server.stop(0); - httpThreadPool.shutdownNow(); - System.out.println("http server stopped" ); - } - } - - - - public static class MyHandler implements HttpHandler { - @Override - public void handle(HttpExchange t) throws IOException { - String method = t.getRequestMethod(); - System.out.println(String.format("req received: %s %s" ,method,t.getRequestURI())); - 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(); - } - } - } - } + 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 static HttpServer server; + private static ExecutorService httpThreadPool; + + public final String HOST = "localhost"; + protected static int testPort; + private final String baseUri; + protected static final String LR = "\n"; + + public HelpServletBase(String baseuri, int port) { + this.baseUri = baseuri; + testPort = port; + } + + public void setServlet(IPublicServlet s) { + this.servlet = s; + } + + protected void testrequest(String method, String data, String expectedResponse, boolean exact) { + 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 uri, String method, String data, String expectedResponse, boolean exact, + Map 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 headers = new Vector(); + headers.addElement("Accept"); + headers.add("User-Agent"); + Enumeration headerNames = headers.elements(); + try { + 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"); + 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 + 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()); + server.setExecutor(null); // creates a default executor + server.start(); + System.out.println("http server started"); + } + + public static void stopTestWebserver() { + if (server != null) { + server.stop(0); + httpThreadPool.shutdownNow(); + System.out.println("http server stopped"); + } + } + + + + public static class MyHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + String method = t.getRequestMethod(); + System.out.println(String.format("req received: %s %s", method, t.getRequestURI())); + 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 index 77267e793..86a9467ec 100644 --- 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 @@ -30,9 +30,13 @@ 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 ; + 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; } -- cgit 1.2.3-korg