aboutsummaryrefslogtreecommitdiffstats
path: root/BRMSGateway/src/main
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@ericsson.com>2018-04-24 23:47:45 +0100
committerliamfallon <liam.fallon@ericsson.com>2018-04-24 23:54:17 +0100
commit303742b965387dacfeedda939323424716cf9b7c (patch)
tree116bcf136d0c99e57c3242c18a770990776fcfee /BRMSGateway/src/main
parent30b44d3e752d44f8f870594a05464c9e1c5158b6 (diff)
Add JUnit for direct Sonar Nexus REST interface
JUnit tests and changes to the source code to fix issues that were identified in JUnit testing. JUnit testing runs tests towards the Nexus server at https://nexus.onap.org If that server does not exist then the unit tests will fail but as the nexus serveir is required for the build to work then it should be OK to use that server for unit testing. This removes the need to write complex (and less effective) Mockito type unit tests. Issue-ID: POLICY-700 Change-Id: I4aa33a1022033b42bb1fd1fa36785a8e0216b887 Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'BRMSGateway/src/main')
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java103
-rw-r--r--BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java83
2 files changed, 112 insertions, 74 deletions
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java
index eec37392e..1288b5982 100644
--- a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestSearchParameters.java
@@ -45,11 +45,12 @@ public class NexusRestSearchParameters {
private static final String COUNT_QUERY_PARAM = "count";
private static final String REPOSITORY_ID_QUERY_PARAM = "repositoryId";
- private enum SearchType {
- KEYWORD, // Search using a keyword
- FILTER, // Search using a group ID, artifact ID, version, packaging type, and/or classifier filter
- CLASS_NAME, // Search for a class name
- CHECKSUM // Search for artifacts matching a certain checksum
+ /** The type of searches that can be performed. */
+ public enum SearchType {
+ KEYWORD, /** Search using a keyword. */
+ FILTER, /** Search using a group ID, artifact ID, version, packaging type, and/or classifier filter. */
+ CLASS_NAME, /** Search for a class name. */
+ CHECKSUM /** Search for artifacts matching a certain checksum. */
}
// The type of search to perform
@@ -76,15 +77,19 @@ public class NexusRestSearchParameters {
* Specify searching using a keyword.
*
* @param keyword The keyword to search for
+ * @return this object to allow chaining of methods
* @throws NexusRestWrapperException on invalid keywords
*/
- public void useKeywordSearch(final String keyword) throws NexusRestWrapperException {
+ public NexusRestSearchParameters useKeywordSearch(final String keyword) throws NexusRestWrapperException {
+ clearSearchParameters();
+
if (isNullOrBlank(keyword)) {
throw new NexusRestWrapperException("keyword must be specified for Nexus keyword searches");
}
searchType = SearchType.KEYWORD;
this.keyword = keyword;
+ return this;
}
/**
@@ -95,14 +100,18 @@ public class NexusRestSearchParameters {
* @param version The version to filter on
* @param packagingType The packaging type to filter on
* @param classifier The classifier to filter on
+ * @return this object to allow chaining of methods
* @throws NexusRestWrapperException on invalid filters
*/
- public void useFilterSearch(final String groupId, final String artifactId, final String version,
- final String packagingType, final String classifier) throws NexusRestWrapperException {
+ public NexusRestSearchParameters useFilterSearch(final String groupId, final String artifactId,
+ final String version, final String packagingType, final String classifier)
+ throws NexusRestWrapperException {
+ clearSearchParameters();
+
if (isNullOrBlank(groupId) && isNullOrBlank(artifactId) && isNullOrBlank(version)
- && isNullOrBlank(packagingType) && isNullOrBlank(classifier)) {
+ && isNullOrBlank(packagingType) && isNullOrBlank(classifier)) {
throw new NexusRestWrapperException(
- "at least one filter parameter must be specified for Nexus keyword searches");
+ "at least one filter parameter must be specified for Nexus filter searches");
}
searchType = SearchType.FILTER;
@@ -111,36 +120,45 @@ public class NexusRestSearchParameters {
this.version = version;
this.packagingType = packagingType;
this.classifier = classifier;
+ return this;
}
/**
* Specify searching using a class name.
*
* @param className The class name to search for
+ * @return this object to allow chaining of methods
* @throws NexusRestWrapperException on invalid className
*/
- public void useClassNameSearch(final String className) throws NexusRestWrapperException {
+ public NexusRestSearchParameters useClassNameSearch(final String className) throws NexusRestWrapperException {
+ clearSearchParameters();
+
if (isNullOrBlank(className)) {
- throw new NexusRestWrapperException("className must be specified for Nexus keyword searches");
+ throw new NexusRestWrapperException("className must be specified for Nexus class name searches");
}
searchType = SearchType.CLASS_NAME;
this.className = className;
+ return this;
}
/**
* Specify searching using a checksum.
*
* @param checksum The checksum to search for
+ * @return this object to allow chaining of methods
* @throws NexusRestWrapperException on invalid checksum
*/
- public void useChecksumSearch(final String checksum) throws NexusRestWrapperException {
+ public NexusRestSearchParameters useChecksumSearch(final String checksum) throws NexusRestWrapperException {
+ clearSearchParameters();
+
if (isNullOrBlank(checksum)) {
- throw new NexusRestWrapperException("checksum must be specified for Nexus keyword searches");
+ throw new NexusRestWrapperException("checksum must be specified for Nexus checksum searches");
}
searchType = SearchType.CHECKSUM;
this.checksum = checksum;
+ return this;
}
/**
@@ -152,7 +170,7 @@ public class NexusRestSearchParameters {
*/
public NexusRestSearchParameters setRepositoryId(String repositoryId) throws NexusRestWrapperException {
if (isNullOrBlank(repositoryId)) {
- throw new NexusRestWrapperException("repositoryId must be specified for Nexus keyword searches");
+ throw new NexusRestWrapperException("a repositoryId must be specified");
}
this.repositoryId = repositoryId;
@@ -168,7 +186,7 @@ public class NexusRestSearchParameters {
*/
public NexusRestSearchParameters setFrom(int from) throws NexusRestWrapperException {
if (from < 0) {
- throw new NexusRestWrapperException("from cannot be less than 0 for Nexus keyword searches");
+ throw new NexusRestWrapperException("from cannot be less than 0 when from is specified");
}
this.from = from;
@@ -184,7 +202,7 @@ public class NexusRestSearchParameters {
*/
public NexusRestSearchParameters setCount(int count) throws NexusRestWrapperException {
if (count < 1) {
- throw new NexusRestWrapperException("count cannot be less than 1 for Nexus keyword searches");
+ throw new NexusRestWrapperException("count cannot be less than 1 when count is specified");
}
this.count = count;
@@ -200,13 +218,17 @@ public class NexusRestSearchParameters {
*/
public URI getSearchUri(final String nexusServerUrl) throws NexusRestWrapperException {
if (isNullOrBlank(nexusServerUrl)) {
- throw new NexusRestWrapperException("nexusServerUrl must be specified for Nexus keyword searches");
+ throw new NexusRestWrapperException("nexusServerUrl must be specified for the search URI");
+ }
+
+ if (searchType == null) {
+ throw new NexusRestWrapperException("search parameters have not been set");
}
// Use a URI builder to build up the search URI
UriBuilder uriBuilder = UriBuilder
- .fromPath(nexusServerUrl)
- .path(NEXUS_LUCENE_SEARCH_PATH);
+ .fromPath(nexusServerUrl)
+ .path(NEXUS_LUCENE_SEARCH_PATH);
switch (searchType) {
case KEYWORD:
@@ -226,7 +248,7 @@ public class NexusRestSearchParameters {
break;
default:
- throw new NexusRestWrapperException("search parameters have not been specified for the NExus search");
+ throw new NexusRestWrapperException("search parameters have not been specified for the Nexus search");
}
// Add the repository ID query parameter is required
@@ -258,19 +280,19 @@ public class NexusRestSearchParameters {
* @param uriBuilder The builder to add query parameters to
*/
private void getFitlerSearchUri(UriBuilder uriBuilder) {
- if (null != groupId) {
+ if (!isNullOrBlank(groupId)) {
uriBuilder.queryParam(GROUP_ID_QUERY_PARAM, groupId);
}
- if (null != artifactId) {
+ if (!isNullOrBlank(artifactId)) {
uriBuilder.queryParam(ARTIFACT_ID_QUERY_PARAM, artifactId);
}
- if (null != version) {
+ if (!isNullOrBlank(version)) {
uriBuilder.queryParam(VERSION_QUERY_PARAM, version);
}
- if (null != packagingType) {
+ if (!isNullOrBlank(packagingType)) {
uriBuilder.queryParam(PACKAGING_TYPE_QUERY_PARAM, packagingType);
}
- if (null != classifier) {
+ if (!isNullOrBlank(classifier)) {
uriBuilder.queryParam(CLASSIFIER_QUERY_PARAM, classifier);
}
}
@@ -346,11 +368,34 @@ public class NexusRestSearchParameters {
return null == parameter || parameter.trim().isEmpty();
}
+ /**
+ * Clear all search parameters.
+ *
+ */
+ private void clearSearchParameters() {
+ searchType = null;
+
+ keyword = null;
+ groupId = null;
+ artifactId = null;
+ version = null;
+ packagingType = null;
+ classifier = null;
+ className = null;
+ checksum = null;
+
+ repositoryId = null;
+
+ // Scope filters
+ from = -1;
+ count = -1;
+ }
+
@Override
public String toString() {
return "NexusRestSearchParameters [searchType=" + searchType + ", keyword=" + keyword + ", groupId=" + groupId
- + ", artifactId=" + artifactId + ", version=" + version + ", packagingType=" + packagingType
- + ", classifier=" + classifier + ", className=" + className + ", checksum=" + checksum
- + ", repositoryId=" + repositoryId + ", from=" + from + ", count=" + count + "]";
+ + ", artifactId=" + artifactId + ", version=" + version + ", packagingType=" + packagingType
+ + ", classifier=" + classifier + ", className=" + className + ", checksum=" + checksum
+ + ", repositoryId=" + repositoryId + ", from=" + from + ", count=" + count + "]";
}
}
diff --git a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java
index afa3e3794..9ee7598fd 100644
--- a/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java
+++ b/BRMSGateway/src/main/java/org/onap/policy/brms/api/nexus/NexusRestWrapper.java
@@ -54,25 +54,6 @@ public class NexusRestWrapper {
private String nexusPassword;
/**
- * Instantiates a new Nexus REST agent.
- *
- * @param nexusServerUrl the URL of the Nexus server as a string
- * @throws NexusRestWrapperException on errors on the Nexus server URL
- */
- public NexusRestWrapper(final String nexusServerUrl) throws NexusRestWrapperException {
- LOGGER.trace("new NexusRestWrapper: nexusServerUrl=" + nexusServerUrl);
-
- if (isNullOrBlank(nexusServerUrl)) {
- throw new NexusRestWrapperException("nexusServerUrl must be specified for the Nexus server");
- }
-
- this.nexusServerUrl = nexusServerUrl;
-
- // Create a client for RST calls towards the Nexus server
- client = ClientBuilder.newClient();
- }
-
- /**
* Instantiates a new Nexus REST agent with credentials.
*
* @param nexusServerUrl the URL of the Nexus server as a string
@@ -81,15 +62,17 @@ public class NexusRestWrapper {
* @throws NexusRestWrapperException on parameter exceptions
*/
public NexusRestWrapper(final String nexusServerUrl, final String nexusUser, final String nexusPassword)
- throws NexusRestWrapperException {
+ throws NexusRestWrapperException {
LOGGER.trace("new NexusRestWrapper: nexusServerUrl=" + nexusServerUrl);
if (isNullOrBlank(nexusServerUrl)) {
throw new NexusRestWrapperException("nexusServerUrl must be specified for the Nexus server");
}
- if (isNullOrBlank(nexusUser) || isNullOrBlank(nexusPassword)) {
- throw new NexusRestWrapperException("nexuusUser and nexusPassword must both be specified");
+ if ((isNullOrBlank(nexusUser) && !isNullOrBlank(nexusPassword))
+ || (!isNullOrBlank(nexusUser) && isNullOrBlank(nexusPassword))) {
+ throw new NexusRestWrapperException(
+ "if either nexusUser or nexusPassword are specified, both must be specified");
}
this.nexusServerUrl = nexusServerUrl;
@@ -124,10 +107,13 @@ public class NexusRestWrapper {
* Exceptions accessing the Nexus server
*/
public NexusSearchResult findArtifact(final NexusRestSearchParameters searchParameters)
- throws NexusRestWrapperException {
-
+ throws NexusRestWrapperException {
LOGGER.trace("new search with search parameters: " + searchParameters);
+ if (null == searchParameters) {
+ throw new NexusRestWrapperException("searchParameters may not be null");
+ }
+
// Issue the REST request to perform the search
URI searchUri = searchParameters.getSearchUri(nexusServerUrl);
@@ -138,14 +124,22 @@ public class NexusRestWrapper {
getAuthorizationHeader(requestBuilder);
// Issue the REST request
- Response response = requestBuilder.get();
+ Response response = null;
+ try {
+ response = requestBuilder.get();
+ } catch (Exception e) {
+ String message = "search to URI " + searchUri.toString() + " failed with message: " + e.getMessage();
+ LOGGER.warn(message, e);
+ throw new NexusRestWrapperException(message, e);
+ }
LOGGER.debug("search response is: " + response.toString());
// Check the HTTP response code for the search
if (Response.Status.OK.getStatusCode() != response.getStatus()) {
- LOGGER.warn("search to URI " + searchUri.toString() + "failed, response was: " + response.toString());
- throw new NexusRestWrapperException("query to Nexus failed with message: " + response.toString());
+ String message = "search to URI " + searchUri.toString() + " failed, response was: " + response.toString();
+ LOGGER.warn(message);
+ throw new NexusRestWrapperException(message);
}
try {
@@ -160,23 +154,22 @@ public class NexusRestWrapper {
return searchResult;
} catch (Exception e) {
- LOGGER.warn("processing of result from search to URI " + searchUri
- + " failed with message " + e.getMessage());
- throw new NexusRestWrapperException(
- "processing of result from query to Nexus failed with message: " + e.getMessage(), e);
+ String message = "processing of result from query to Nexus failed with message: " + e.getMessage();
+ LOGGER.warn(message, e);
+ throw new NexusRestWrapperException(message, e);
}
}
/**
* Get the authorisation header for the user name and password.
- * @param requestBuilder the request builder to add authorization to
+ * @param requestBuilder the request builder to add authorisation to
* @return the authorisation header
*/
private Builder getAuthorizationHeader(Builder requestBuilder) {
if (null != nexusUser && null != nexusPassword) {
String userPassString = nexusUser + ":" + nexusPassword;
requestBuilder.header("Authorization", "Basic "
- + java.util.Base64.getEncoder().encodeToString(userPassString.getBytes()));
+ + java.util.Base64.getEncoder().encodeToString(userPassString.getBytes()));
}
return requestBuilder;
@@ -210,18 +203,18 @@ public class NexusRestWrapper {
NexusRepository repository = repositoryMap.get(artifact.getArtifactHits().get(0).getRepositoryId());
return new StringBuilder()
- .append(repository.getRepositoryUrl())
- .append("/content/")
- .append(artifact.getGroupId().replace('.', '/'))
- .append('/')
- .append(artifact.getArtifactId())
- .append('/')
- .append(artifact.getVersion())
- .append('/')
- .append(artifact.getArtifactId())
- .append('-')
- .append(artifact.getVersion())
- .toString();
+ .append(repository.getRepositoryUrl())
+ .append("/content/")
+ .append(artifact.getGroupId().replace('.', '/'))
+ .append('/')
+ .append(artifact.getArtifactId())
+ .append('/')
+ .append(artifact.getVersion())
+ .append('/')
+ .append(artifact.getArtifactId())
+ .append('-')
+ .append(artifact.getVersion())
+ .toString();
}
/**