From 441f76115d6ab49c85766bbebc7df38c529b07e8 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 6 May 2016 11:13:12 +0200 Subject: [PATCH] SONAR-7262 Deprecated qualifiers 'PAC' and 'CLA' in WS documentation --- .../server/component/ws/SearchAction.java | 55 +++---------------- .../sonar/server/component/ws/TreeAction.java | 3 +- .../sonar/server/ws/WsParameterBuilder.java | 20 ++++++- .../server/component/ws/SearchActionTest.java | 6 +- 4 files changed, 30 insertions(+), 54 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java index e0df2a87fd8..2f6d5e497f9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java @@ -21,11 +21,9 @@ package org.sonar.server.component.ws; import com.google.common.base.Function; import java.util.List; -import java.util.Set; import javax.annotation.Nonnull; import org.sonar.api.i18n.I18n; import org.sonar.api.resources.Languages; -import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -43,18 +41,13 @@ import org.sonarqube.ws.WsComponents.SearchWsResponse; import org.sonarqube.ws.client.component.SearchWsRequest; import static com.google.common.collect.FluentIterable.from; -import static com.google.common.collect.Ordering.natural; -import static java.lang.String.format; -import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER; -import static org.sonar.server.ws.WsUtils.checkRequest; +import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext; +import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_LANGUAGE; import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_QUALIFIERS; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER; public class SearchAction implements ComponentsWsAction { - private static final String QUALIFIER_PROPERTY_PREFIX = "qualifiers."; - private final DbClient dbClient; private final ResourceTypes resourceTypes; private final I18n i18n; @@ -80,10 +73,8 @@ public class SearchAction implements ComponentsWsAction { .setResponseExample(getClass().getResource("search-components-example.json")) .setHandler(this); - action.createParam(PARAM_QUALIFIERS) - .setRequired(true) - .setExampleValue(format("%s,%s", Qualifiers.PROJECT, Qualifiers.MODULE)) - .setDescription("Comma-separated list of component qualifiers. Possible values are " + buildQualifiersDescription()); + createQualifiersParameter(action, newQualifierParameterContext(userSession, i18n, resourceTypes)) + .setRequired(true); action .createParam(PARAM_LANGUAGE) @@ -102,12 +93,9 @@ public class SearchAction implements ComponentsWsAction { private SearchWsResponse doHandle(SearchWsRequest request) { userSession.checkLoggedIn().checkPermission(GlobalPermissions.SYSTEM_ADMIN); - List qualifiers = request.getQualifiers(); - validateQualifiers(qualifiers); - DbSession dbSession = dbClient.openSession(false); try { - ComponentQuery query = buildQuery(request, qualifiers); + ComponentQuery query = buildQuery(request); Paging paging = buildPaging(dbSession, request, query); List components = searchComponents(dbSession, query, paging); return buildResponse(components, paging); @@ -155,7 +143,8 @@ public class SearchAction implements ComponentsWsAction { .andTotal(total); } - private static ComponentQuery buildQuery(SearchWsRequest request, List qualifiers) { + private static ComponentQuery buildQuery(SearchWsRequest request) { + List qualifiers = request.getQualifiers(); return ComponentQuery.builder() .setNameOrKeyQuery(request.getQuery()) .setLanguage(request.getLanguage()) @@ -163,36 +152,6 @@ public class SearchAction implements ComponentsWsAction { .build(); } - private void validateQualifiers(List qualifiers) { - Set possibleQualifiers = allQualifiers(); - for (String qualifier : qualifiers) { - checkRequest(possibleQualifiers.contains(qualifier), - format("The '%s' parameter must be one of %s. '%s' was passed.", PARAM_QUALIFIER, possibleQualifiers, qualifier)); - } - } - - private Set allQualifiers() { - return from(resourceTypes.getAll()) - .transform(RESOURCE_TYPE_TO_QUALIFIER) - .toSortedSet(natural()); - } - - private String buildQualifiersDescription() { - StringBuilder description = new StringBuilder(); - description.append("
    "); - String qualifierPattern = "
  • %s - %s
  • "; - for (String qualifier : allQualifiers()) { - description.append(format(qualifierPattern, qualifier, i18n(qualifier))); - } - description.append("
"); - - return description.toString(); - } - - private String i18n(String qualifier) { - return i18n.message(userSession.locale(), QUALIFIER_PROPERTY_PREFIX + qualifier, ""); - } - private enum ComponentDToComponentResponseFunction implements Function { INSTANCE; diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java index 292b77400e7..407eda1e2d1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java @@ -80,7 +80,8 @@ public class TreeAction implements ComponentsWsAction { private static final Set STRATEGIES = ImmutableSortedSet.of(ALL_STRATEGY, CHILDREN_STRATEGY, LEAVES_STRATEGY); private static final String NAME_SORT = "name"; private static final String PATH_SORT = "path"; - private static final Set SORTS = ImmutableSortedSet.of(NAME_SORT, PATH_SORT, "qualifier"); + private static final String QUALIFIER_SORT = "qualifier"; + private static final Set SORTS = ImmutableSortedSet.of(NAME_SORT, PATH_SORT, QUALIFIER_SORT); private final DbClient dbClient; private final ComponentFinder componentFinder; diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/WsParameterBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/ws/WsParameterBuilder.java index c0df2e7962e..4fb89a3fda2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/WsParameterBuilder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/WsParameterBuilder.java @@ -19,12 +19,17 @@ */ package org.sonar.server.ws; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableSet; import java.util.Set; +import javax.annotation.Nullable; import org.sonar.api.i18n.I18n; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.ResourceTypes; import org.sonar.api.server.ws.WebService; import org.sonar.server.user.UserSession; +import static com.google.common.base.Predicates.not; import static com.google.common.collect.FluentIterable.from; import static com.google.common.collect.Ordering.natural; import static java.lang.String.format; @@ -33,6 +38,8 @@ import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_ public class WsParameterBuilder { private static final String PARAM_QUALIFIER = "qualifier"; private static final String PARAM_QUALIFIERS = "qualifiers"; + private static final Set DEPRECATED_QUALIFIERS = ImmutableSet.of( + Qualifiers.CLASS, Qualifiers.PACKAGE, Qualifiers.FIELD, Qualifiers.LIBRARY, Qualifiers.METHOD, Qualifiers.PARAGRAPH); private WsParameterBuilder() { // static methods only @@ -54,12 +61,14 @@ public class WsParameterBuilder { private static Set getRootQualifiers(ResourceTypes resourceTypes) { return from(resourceTypes.getRoots()) .transform(RESOURCE_TYPE_TO_QUALIFIER) + .filter(not(IsDeprecatedQualifier.INSTANCE)) .toSortedSet(natural()); } private static Set getAllQualifiers(ResourceTypes resourceTypes) { return from(resourceTypes.getAll()) .transform(RESOURCE_TYPE_TO_QUALIFIER) + .filter(not(IsDeprecatedQualifier.INSTANCE)) .toSortedSet(natural()); } @@ -85,7 +94,16 @@ public class WsParameterBuilder { private static String qualifierLabel(QualifierParameterContext context, String qualifier) { String qualifiersPropertyPrefix = "qualifiers."; - return context.getI18n().message(context.getUserSession().locale(), qualifiersPropertyPrefix + qualifier, ""); + return context.getI18n().message(context.getUserSession().locale(), qualifiersPropertyPrefix + qualifier, "no description available"); + } + + private enum IsDeprecatedQualifier implements Predicate { + INSTANCE; + + @Override + public boolean apply(@Nullable String input) { + return DEPRECATED_QUALIFIERS.contains(input); + } } public static class QualifierParameterContext { diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java index 18d4694a9e6..7c44116a5ab 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java @@ -36,7 +36,6 @@ import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ResourceTypesRule; -import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.i18n.I18nRule; @@ -80,7 +79,6 @@ public class SearchActionTest { when(languages.all()).thenReturn(javaLanguage()); ws = new WsActionTester(new SearchAction(db.getDbClient(), resourceTypes, i18n, userSession, languages)); - } @Test @@ -166,8 +164,8 @@ public class SearchActionTest { @Test public void fail_if_unknown_qualifier_provided() { - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("The 'qualifier' parameter must be one of [BRC, DIR, FIL, TRK]. 'Unknown-Qualifier' was passed."); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Value of parameter 'qualifiers' (Unknown-Qualifier) must be one of: [BRC, DIR, FIL, TRK]"); newRequest("Unknown-Qualifier").execute(); } -- 2.39.5