diff options
author | Michael Dürre <michael.duerre@highstreet-technologies.com> | 2020-02-06 12:57:23 +0100 |
---|---|---|
committer | KAPIL SINGAL <ks220y@att.com> | 2020-02-07 16:41:48 +0000 |
commit | f6f3b57be8e57ffc6790d139b0b7f168b8cc69e6 (patch) | |
tree | d510d50c0f651e5b7f853ec6247f57a1a7f2b432 /sdnr/wt/common/src/main/java | |
parent | 561aa2a8ef7bd222d274aea065929920aedd0723 (diff) |
improve code coverage
more unit tests for common and data-provider
Issue-ID: SDNC-1063
Signed-off-by: Michael Dürre <michael.duerre@highstreet-technologies.com>
Change-Id: Id99cb3e300b9649b77e0900347fbefae8d55cd87
Signed-off-by: Michael Dürre <michael.duerre@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/common/src/main/java')
7 files changed, 83 insertions, 207 deletions
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java index 7c48bf8a1..63e265bd1 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java @@ -146,7 +146,7 @@ public interface DatabaseClient { * @param query query to select item to update * @return esId which was updated or null if failed */ - public String doUpdate(String dataTypeName, String json, QueryBuilder query); + public boolean doUpdate(String dataTypeName, String json, QueryBuilder query); /** * diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java index 50a8244cb..6fb26f541 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java @@ -17,23 +17,13 @@ ******************************************************************************/ package org.onap.ccsdk.features.sdnr.wt.common.database; -import java.io.FileInputStream; import java.io.IOException; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; - -import javax.net.ssl.SSLContext; - import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; -import org.apache.http.ssl.SSLContexts; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; @@ -98,57 +88,57 @@ public class ExtRestClient { } } - private class SSLCercAuthHttpClientConfigCallback implements HttpClientConfigCallback { - - private final String certFilename; - - SSLCercAuthHttpClientConfigCallback(String certfile) { - this.certFilename = certfile; - } - - @Override - public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - if (this.certFilename == null) { - return httpClientBuilder; - } - - char[] keystorePass = "MY PASSWORD".toCharArray(); - - FileInputStream fis = null; - - // Loading KEYSTORE in JKS format - KeyStore keyStorePci = null; - try { - keyStorePci = KeyStore.getInstance(KeyStore.getDefaultType()); - } catch (KeyStoreException e1) { - LOG.warn("unable to load keystore: {}",e1); - } - if (keyStorePci != null) { - try { - fis = new FileInputStream(this.certFilename); - keyStorePci.load(fis, keystorePass); - } catch (Exception e) { - LOG.error("Error loading keystore: " + this.certFilename); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - - } - } - } - } - SSLContext sslcontext=null; - try { - sslcontext = SSLContexts.custom().loadKeyMaterial(keyStorePci, keystorePass).build(); - } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException - | KeyStoreException e) { - LOG.warn("unable to load sslcontext: {}",e); - } - return httpClientBuilder.setSSLContext(sslcontext); - } - } +// private class SSLCercAuthHttpClientConfigCallback implements HttpClientConfigCallback { +// +// private final String certFilename; +// +// SSLCercAuthHttpClientConfigCallback(String certfile) { +// this.certFilename = certfile; +// } +// +// @Override +// public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { +// if (this.certFilename == null) { +// return httpClientBuilder; +// } +// +// char[] keystorePass = "MY PASSWORD".toCharArray(); +// +// FileInputStream fis = null; +// +// // Loading KEYSTORE in JKS format +// KeyStore keyStorePci = null; +// try { +// keyStorePci = KeyStore.getInstance(KeyStore.getDefaultType()); +// } catch (KeyStoreException e1) { +// LOG.warn("unable to load keystore: {}",e1); +// } +// if (keyStorePci != null) { +// try { +// fis = new FileInputStream(this.certFilename); +// keyStorePci.load(fis, keystorePass); +// } catch (Exception e) { +// LOG.error("Error loading keystore: " + this.certFilename); +// } finally { +// if (fis != null) { +// try { +// fis.close(); +// } catch (IOException e) { +// +// } +// } +// } +// } +// SSLContext sslcontext=null; +// try { +// sslcontext = SSLContexts.custom().loadKeyMaterial(keyStorePci, keystorePass).build(); +// } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException +// | KeyStoreException e) { +// LOG.warn("unable to load sslcontext: {}",e); +// } +// return httpClientBuilder.setSSLContext(sslcontext); +// } +// } private RestClient client; diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java index 56402bdfc..9a04f0dfd 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java @@ -64,19 +64,16 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A this(hosts,REFRESH_AFTER_REWRITE_DEFAULT); } public HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite) { - super(hosts); - this.doRefreshAfterWrite = refreshAfterWrite; + this(hosts,refreshAfterWrite,null,null); + } + public HtDatabaseClient(HostInfo[] hosts,String username,String password) { + this(hosts,REFRESH_AFTER_REWRITE_DEFAULT,username,password); } - public HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite,String username,String password) { super(hosts,username,password); this.doRefreshAfterWrite = refreshAfterWrite; } - public HtDatabaseClient(HostInfo[] hosts,String username,String password) { - this(hosts,REFRESH_AFTER_REWRITE_DEFAULT,username,password); - } - - + /*---------------------------------- * Functions @@ -236,7 +233,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A @Override public String doUpdateOrCreate(String dataTypeName, String esId, String json) { - return this.doUpdateOrCreate(dataTypeName, esId, json,null); + return this.doUpdateOrCreate(dataTypeName, esId, json,null); } @@ -262,7 +259,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A return success ? esId : null; } @Override - public String doUpdate(String dataTypeName, String json, QueryBuilder query) { + public boolean doUpdate(String dataTypeName, String json, QueryBuilder query) { boolean success = false; UpdateByQueryRequest request = new UpdateByQueryRequest(dataTypeName, dataTypeName ); request.source(new JSONObject(json),query); @@ -275,7 +272,7 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A if(this.doRefreshAfterWrite) { this.doRefresh(dataTypeName); } - return success?"":null; + return success; } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchHit.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchHit.java index e86c5c36b..b6df2620b 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchHit.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchHit.java @@ -21,12 +21,14 @@ import org.json.JSONObject; public class SearchHit { - private String index; - private String type; - private String id; - private JSONObject source; + private final String index; + private final String type; + private final String id; + private final JSONObject source; + private final JSONObject raw; public SearchHit(JSONObject o) { + this.raw = o; this.index=o.getString("_index"); this.type = o.getString("_type"); this.id = o.getString("_id"); @@ -43,8 +45,18 @@ public class SearchHit { return this.id; } + public JSONObject getSource() { + return this.source; + } public String getSourceAsString() { return this.source.toString(); } + /** + * @return + */ + public JSONObject getRaw() { + return this.raw; + } + } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/FileWatchdog.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/FileWatchdog.java index ff513ea97..63c1f15ba 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/FileWatchdog.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/FileWatchdog.java @@ -21,7 +21,6 @@ * ============LICENSE_END======================================================= * */ - package org.onap.ccsdk.features.sdnr.wt.common.file; import java.io.File; diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java index 8242d35d4..b65984a58 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java @@ -100,10 +100,9 @@ public abstract class BaseServlet extends HttpServlet { sc.init(null, trustCerts, new java.security.SecureRandom()); } - private boolean trustInsecure; - private boolean isCorsEnabled; - public BaseServlet(boolean trustInsecure) { - this.trustInsecure = trustInsecure; + protected abstract boolean trustInsecure(); + protected abstract boolean isCorsEnabled(); + public BaseServlet() { this.trysslSetup(true); } @@ -118,11 +117,11 @@ public abstract class BaseServlet extends HttpServlet { */ private void trysslSetup(boolean force) { // if trustall config has changed - if (force || this.doTrustAll() != this.trustInsecure) { - this.trustAll(this.trustInsecure); + if (force || this.doTrustAll() != this.trustInsecure()) { + this.trustAll(this.trustInsecure()); // resetup ssl config try { - setupSslTrustAll(this.trustInsecure); + setupSslTrustAll(this.trustInsecure()); } catch (Exception e) { LOG.error("problem setting up SSL: {}", e.getMessage()); } @@ -347,7 +346,7 @@ public abstract class BaseServlet extends HttpServlet { resp.setHeader(entry.getKey(), v); s += String.format("%s:%s;", entry.getKey(), v); } - if (this.isCorsEnabled) { + if (this.isCorsEnabled()) { resp.setHeader("Access-Control-Allow-Origin", "*"); // resp.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE"); resp.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/FileWatchdog.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/FileWatchdog.java deleted file mode 100644 index 2830d688e..000000000 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/FileWatchdog.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP : ccsdk features - * ================================================================================ - * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. - * All rights reserved. - * ================================================================================ - * Update Copyright (C) 2020 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.common.util; - -import java.io.File; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Check every now and then that a certain file has not changed. If it has, then call the - * {@link #doOnChange} method. - * - * @author JunHo Yoon - * @since 3.1.1 - */ -public abstract class FileWatchdog extends Thread { - private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class); - /** - * The default delay between every file modification check, set to 60 seconds. - */ - public static final long DEFAULT_DELAY = 60000; - /** - * The name of the file to observe for changes. - */ - private final String filename; - - /** - * The delay to observe between every check. By default set {@link #DEFAULT_DELAY}. - */ - private long delay = DEFAULT_DELAY; - - private final File file; - private long lastModified = 0; - private boolean warnedAlready = false; - - protected FileWatchdog(String filename) { - this.filename = filename; - file = new File(filename); - setDaemon(true); - checkAndConfigure(); - } - - /** - * Set the delay to observe between each check of the file changes. - * - * @param delay the frequency of file watch. - */ - public void setDelay(long delay) { - this.delay = delay; - } - - /** - * abstract method to be run when the file is changed. - */ - protected abstract void doOnChange(); - - protected void checkAndConfigure() { - boolean fileExists; - try { - fileExists = file.exists(); - } catch (SecurityException e) { - LOGGER.warn("Was not allowed to read check file existence, file:[{}].",filename); - this.interrupt(); // there is no point in continuing - return; - } - - if (fileExists) { - long l = file.lastModified(); // this can also throw a - if (lastModified == 0) { - lastModified = l; // is very unlikely. - } - if (l > lastModified) { // however, if we reached this point this - lastModified = l; // is very unlikely. - doOnChange(); - warnedAlready = false; - } - } else { - if (!warnedAlready) { - LOGGER.debug("[{}] does not exist.", filename); - warnedAlready = true; - } - } - } - - @Override - public void run() { - while (!isInterrupted()) { - checkAndConfigure(); - try { - Thread.sleep(delay); - } catch (InterruptedException e) { - LOGGER.debug("Interrupted sleep. {}", e.getMessage()); - Thread.currentThread().interrupt(); - } - } - LOGGER.debug("Stoppen file watchdog for file {}", filename); - } -} |