Add JUnit for direct Sonar Nexus REST interface
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brms / api / nexus / NexusRestSearchParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.brms.api.nexus;
22
23 import java.net.URI;
24
25 import javax.ws.rs.core.UriBuilder;
26
27 /**
28  * The Class NexusRestSearchParameters is used to specify the parameters of a search on Nexus.
29  * The parameters are used to return the search part of the URL for the Maven search.
30  */
31 public class NexusRestSearchParameters {
32     // The REST end point for Nexus Lucene searches
33     private static final String NEXUS_LUCENE_SEARCH_PATH = "service/local/lucene/search";
34
35     // REST search query parameter names
36     private static final String KEYWORD_QUERY_PARAM        = "q";
37     private static final String GROUP_ID_QUERY_PARAM       = "g";
38     private static final String ARTIFACT_ID_QUERY_PARAM    = "a";
39     private static final String VERSION_QUERY_PARAM        = "v";
40     private static final String PACKAGING_TYPE_QUERY_PARAM = "p";
41     private static final String CLASSIFIER_QUERY_PARAM     = "c";
42     private static final String CLASS_NAME_QUERY_PARAM     = "cn";
43     private static final String CHECKSUM_QUERY_PARAM       = "sha1";
44     private static final String FROM_QUERY_PARAM           = "from";
45     private static final String COUNT_QUERY_PARAM          = "count";
46     private static final String REPOSITORY_ID_QUERY_PARAM = "repositoryId";
47
48     /** The type of searches that can be performed. */
49     public enum SearchType {
50         KEYWORD,    /** Search using a keyword. */
51         FILTER,     /** Search using a group ID, artifact ID, version, packaging type, and/or classifier filter. */
52         CLASS_NAME, /** Search for a class name. */
53         CHECKSUM    /** Search for artifacts matching a certain checksum. */
54     }
55
56     // The type of search to perform
57     private SearchType searchType = null;
58
59     // Search filters
60     private String keyword;
61     private String groupId;
62     private String artifactId;
63     private String version;
64     private String packagingType;
65     private String classifier;
66     private String className;
67     private String checksum;
68
69     // Repository filter
70     private String repositoryId;
71
72     // Scope filters
73     private int from = -1;
74     private int count = -1;
75
76     /**
77      * Specify searching using a keyword.
78      * 
79      * @param keyword The keyword to search for
80      * @return this object to allow chaining of methods
81      * @throws NexusRestWrapperException on invalid keywords
82      */
83     public NexusRestSearchParameters useKeywordSearch(final String keyword) throws NexusRestWrapperException {
84         clearSearchParameters();
85
86         if (isNullOrBlank(keyword)) {
87             throw new NexusRestWrapperException("keyword must be specified for Nexus keyword searches");
88         }
89
90         searchType = SearchType.KEYWORD;
91         this.keyword = keyword;
92         return this;
93     }
94
95     /**
96      * Specify searching using a filter.
97      * 
98      * @param groupId The group ID to filter on
99      * @param artifactId The artifact ID to filter on
100      * @param version The version to filter on
101      * @param packagingType The packaging type to filter on
102      * @param classifier The classifier to filter on
103      * @return this object to allow chaining of methods
104      * @throws NexusRestWrapperException on invalid filters
105      */
106     public NexusRestSearchParameters useFilterSearch(final String groupId, final String artifactId,
107                     final String version, final String packagingType, final String classifier)
108                                     throws NexusRestWrapperException {
109         clearSearchParameters();
110
111         if (isNullOrBlank(groupId) && isNullOrBlank(artifactId) && isNullOrBlank(version)
112                         && isNullOrBlank(packagingType) && isNullOrBlank(classifier)) {
113             throw new NexusRestWrapperException(
114                             "at least one filter parameter must be specified for Nexus filter searches");
115         }
116
117         searchType = SearchType.FILTER;
118         this.groupId = groupId;
119         this.artifactId = artifactId;
120         this.version = version;
121         this.packagingType = packagingType;
122         this.classifier = classifier;
123         return this;
124     }
125
126     /**
127      * Specify searching using a class name.
128      * 
129      * @param className The class name to search for
130      * @return this object to allow chaining of methods
131      * @throws NexusRestWrapperException on invalid className
132      */
133     public NexusRestSearchParameters useClassNameSearch(final String className) throws NexusRestWrapperException {
134         clearSearchParameters();
135
136         if (isNullOrBlank(className)) {
137             throw new NexusRestWrapperException("className must be specified for Nexus class name searches");
138         }
139
140         searchType = SearchType.CLASS_NAME;
141         this.className = className;
142         return this;
143     }
144
145     /**
146      * Specify searching using a checksum.
147      * 
148      * @param checksum The checksum to search for
149      * @return this object to allow chaining of methods
150      * @throws NexusRestWrapperException on invalid checksum
151      */
152     public NexusRestSearchParameters useChecksumSearch(final String checksum) throws NexusRestWrapperException {
153         clearSearchParameters();
154
155         if (isNullOrBlank(checksum)) {
156             throw new NexusRestWrapperException("checksum must be specified for Nexus checksum searches");
157         }
158
159         searchType = SearchType.CHECKSUM;
160         this.checksum = checksum;
161         return this;
162     }
163
164     /**
165      * Search on a specific repository.
166      * 
167      * @param repositoryId The repository to search
168      * @return this object to allow chaining of methods
169      * @throws NexusRestWrapperException on invalid repositoryId
170      */
171     public NexusRestSearchParameters setRepositoryId(String repositoryId) throws NexusRestWrapperException {
172         if (isNullOrBlank(repositoryId)) {
173             throw new NexusRestWrapperException("a repositoryId must be specified");
174         }
175
176         this.repositoryId = repositoryId;
177         return this;
178     }
179
180     /**
181      * Return the search results from this result number.
182      * 
183      * @param from The number of the first result to return
184      * @return this object to allow chaining of methods
185      * @throws NexusRestWrapperException on invalid from value
186      */
187     public NexusRestSearchParameters setFrom(int from) throws NexusRestWrapperException {
188         if (from < 0) {
189             throw new NexusRestWrapperException("from cannot be less than 0 when from is specified");
190         }
191
192         this.from = from;
193         return this;
194     }
195
196     /**
197      * Return the specified number of search results.
198      * 
199      * @param count The number of results to return
200      * @return this object to allow chaining of methods
201      * @throws NexusRestWrapperException on invalid count value
202      */
203     public NexusRestSearchParameters setCount(int count) throws NexusRestWrapperException {
204         if (count < 1) {
205             throw new NexusRestWrapperException("count cannot be less than 1 when count is specified");
206         }
207
208         this.count = count;
209         return this;
210     }
211
212     /**
213      * Compose the URI for the search to the Nexus server.
214      * 
215      * @param nexusServerUrl the URL of the server on which to search
216      * @return the search URI
217      * @throws NexusRestWrapperException on search URL composition exceptions
218      */
219     public URI getSearchUri(final String nexusServerUrl) throws NexusRestWrapperException {
220         if (isNullOrBlank(nexusServerUrl)) {
221             throw new NexusRestWrapperException("nexusServerUrl must be specified for the search URI");
222         }
223
224         if (searchType == null) {
225             throw new NexusRestWrapperException("search parameters have not been set");
226         }
227
228         // Use a URI builder to build up the search URI
229         UriBuilder uriBuilder = UriBuilder
230                         .fromPath(nexusServerUrl)
231                         .path(NEXUS_LUCENE_SEARCH_PATH);
232
233         switch (searchType) {
234             case KEYWORD:
235                 getKeywordSearchUri(uriBuilder);
236                 break;
237
238             case FILTER:
239                 getFitlerSearchUri(uriBuilder);
240                 break;
241
242             case CLASS_NAME:
243                 getClassNameSearchUri(uriBuilder);
244                 break;
245
246             case CHECKSUM:
247                 getChecksumSearchUri(uriBuilder);
248                 break;
249
250             default:
251                 throw new NexusRestWrapperException("search parameters have not been specified for the Nexus search");
252         }
253
254         // Add the repository ID query parameter is required
255         if (null != repositoryId) {
256             uriBuilder.queryParam(REPOSITORY_ID_QUERY_PARAM, repositoryId);
257         }
258
259         // Add the from and count values if required
260         if (from >= 0) {
261             uriBuilder.queryParam(FROM_QUERY_PARAM, from);
262         }
263         if (count >= 0) {
264             uriBuilder.queryParam(COUNT_QUERY_PARAM, count);
265         }
266
267         return uriBuilder.build();
268     }
269
270     /**
271      * Compose the query parameters for a keyword search.
272      * @param uriBuilder The builder to add query parameters to
273      */
274     private void getKeywordSearchUri(UriBuilder uriBuilder) {
275         uriBuilder.queryParam(KEYWORD_QUERY_PARAM, keyword);
276     }
277
278     /**
279      * Compose the query parameters for a filter search.
280      * @param uriBuilder The builder to add query parameters to
281      */
282     private void getFitlerSearchUri(UriBuilder uriBuilder) {
283         if (!isNullOrBlank(groupId)) {
284             uriBuilder.queryParam(GROUP_ID_QUERY_PARAM, groupId);
285         }
286         if (!isNullOrBlank(artifactId)) {
287             uriBuilder.queryParam(ARTIFACT_ID_QUERY_PARAM, artifactId);
288         }
289         if (!isNullOrBlank(version)) {
290             uriBuilder.queryParam(VERSION_QUERY_PARAM, version);
291         }
292         if (!isNullOrBlank(packagingType)) {
293             uriBuilder.queryParam(PACKAGING_TYPE_QUERY_PARAM, packagingType);
294         }
295         if (!isNullOrBlank(classifier)) {
296             uriBuilder.queryParam(CLASSIFIER_QUERY_PARAM, classifier);
297         }
298     }
299
300     /**
301      * Compose the query parameters for a class name search.
302      * @param uriBuilder The builder to add query parameters to
303      */
304     private void getClassNameSearchUri(UriBuilder uriBuilder) {
305         uriBuilder.queryParam(CLASS_NAME_QUERY_PARAM, className);
306     }
307
308     /**
309      * Compose the query parameters for a checksum search.
310      * @param uriBuilder The builder to add query parameters to
311      */
312     private void getChecksumSearchUri(UriBuilder uriBuilder) {
313         uriBuilder.queryParam(CHECKSUM_QUERY_PARAM, checksum);
314     }
315
316     public SearchType getSearchType() {
317         return searchType;
318     }
319
320     public String getKeyword() {
321         return keyword;
322     }
323
324     public String getGroupId() {
325         return groupId;
326     }
327
328     public String getArtifactId() {
329         return artifactId;
330     }
331
332     public String getVersion() {
333         return version;
334     }
335
336     public String getPackagingType() {
337         return packagingType;
338     }
339
340     public String getClassifier() {
341         return classifier;
342     }
343
344     public String getClassName() {
345         return className;
346     }
347
348     public String getChecksum() {
349         return checksum;
350     }
351
352     public String getRepositoryId() {
353         return repositoryId;
354     }
355
356     public int getFrom() {
357         return from;
358     }
359
360     public int getCount() {
361         return count;
362     }
363
364     /**
365      * Check if a string is null or all white space.
366      */
367     private boolean isNullOrBlank(final String parameter) {
368         return null == parameter || parameter.trim().isEmpty();
369     }
370
371     /**
372      * Clear all search parameters.
373      * 
374      */
375     private void clearSearchParameters() {
376         searchType = null;
377
378         keyword = null;
379         groupId = null;
380         artifactId = null;
381         version = null;
382         packagingType = null;
383         classifier = null;
384         className = null;
385         checksum = null;
386
387         repositoryId = null;
388
389         // Scope filters
390         from = -1;
391         count = -1;
392     }
393
394     @Override
395     public String toString() {
396         return "NexusRestSearchParameters [searchType=" + searchType + ", keyword=" + keyword + ", groupId=" + groupId
397                         + ", artifactId=" + artifactId + ", version=" + version + ", packagingType=" + packagingType
398                         + ", classifier=" + classifier + ", className=" + className + ", checksum=" + checksum
399                         + ", repositoryId=" + repositoryId + ", from=" + from + ", count=" + count + "]";
400     }
401 }