import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.api.server.ws.WebService.NewController;
-import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.util.Protobuf;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.qualityprofile.QProfileLookup;
+import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse;
+import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse.QualityProfile;
+
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class InheritanceAction implements QProfileWsAction {
List<QualityProfileDto> children = dbClient.qualityProfileDao().selectChildren(dbSession, profile.getKey());
Statistics statistics = new Statistics(dbSession, organization);
- writeResponse(response.newJsonWriter(), profile, ancestors, children, statistics);
+ writeProtobuf(buildResponse(profile, ancestors, children, statistics), request, response);
}
}
- private static void writeResponse(JsonWriter json, QualityProfileDto profile, List<QualityProfileDto> ancestors, List<QualityProfileDto> children, Statistics statistics) {
- json.beginObject();
- writeProfile(json, profile, statistics);
- writeAncestors(json, ancestors, statistics);
- writeChildren(json, children, statistics);
- json.endObject().close();
+ private static InheritanceWsResponse buildResponse(QualityProfileDto profile, List<QualityProfileDto> ancestors, List<QualityProfileDto> children, Statistics statistics) {
+ return InheritanceWsResponse.newBuilder()
+ .setProfile(buildProfile(profile, statistics))
+ .addAllAncestors(buildAncestors(ancestors, statistics))
+ .addAllChildren(buildChildren(children, statistics))
+ .build();
}
- private static void writeProfile(JsonWriter json, QualityProfileDto profile, Statistics statistics) {
- String profileKey = profile.getKey();
- json.name("profile");
- writeProfileAttributes(json, profileKey, profile.getName(), profile.getParentKee(), statistics);
+ private static QualityProfile buildProfile(QualityProfileDto profile, Statistics statistics) {
+ return buildProfile(profile.getKey(), profile.getName(), profile.getParentKee(), statistics);
}
- private static void writeAncestors(JsonWriter json, List<QualityProfileDto> ancestors, Statistics statistics) {
- json.name("ancestors").beginArray();
- for (QualityProfileDto ancestor : ancestors) {
- String ancestorKey = ancestor.getKey();
- writeProfileAttributes(json, ancestorKey, ancestor.getName(), ancestor.getParentKee(), statistics);
- }
- json.endArray();
- }
-
- private static void writeChildren(JsonWriter json, List<QualityProfileDto> children, Statistics statistics) {
- json.name("children").beginArray();
- for (QualityProfileDto child : children) {
- String childKey = child.getKey();
- writeProfileAttributes(json, childKey, child.getName(), null, statistics);
- }
- json.endArray();
+ private static Iterable<QualityProfile> buildAncestors(List<QualityProfileDto> ancestors, Statistics statistics) {
+ return ancestors.stream()
+ .map(ancestor -> buildProfile(ancestor.getKey(), ancestor.getName(), ancestor.getParentKee(), statistics))
+ .collect(Collectors.toList());
}
- private static void writeProfileAttributes(JsonWriter json, String key, String name, @Nullable String parentKey, Statistics statistics) {
- json.beginObject();
- json.prop("key", key)
- .prop("name", name)
- .prop("parent", parentKey);
- writeStats(json, key, statistics);
- json.endObject();
+ private static Iterable<QualityProfile> buildChildren(List<QualityProfileDto> children, Statistics statistics) {
+ return children.stream()
+ .map(child -> buildProfile(child.getKey(), child.getName(), null, statistics))
+ .collect(Collectors.toList());
}
- private static void writeStats(JsonWriter json, String profileKey, Statistics statistics) {
- json.prop("activeRuleCount", statistics.countRulesByProfileKey.getOrDefault(profileKey, 0L));
- json.prop("overridingRuleCount", statistics.countOverridingRulesByProfileKey.getOrDefault(profileKey, 0L));
+ private static QualityProfile buildProfile(String key, String name, @Nullable String parentKey, Statistics statistics) {
+ QualityProfile.Builder builder = QualityProfile.newBuilder()
+ .setKey(key)
+ .setName(name)
+ .setActiveRuleCount(statistics.countRulesByProfileKey.getOrDefault(key, 0L))
+ .setOverridingRuleCount(statistics.countOverridingRulesByProfileKey.getOrDefault(key, 0L));
+ Protobuf.setNullable(parentKey, builder::setParent);
+ return builder.build();
}
private class Statistics {
*/
package org.sonar.server.qualityprofile.ws;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import org.junit.Before;
import org.sonar.server.util.TypeValidations;
import org.sonar.server.ws.WsActionTester;
import org.sonar.test.JsonAssert;
+import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonarqube.ws.MediaTypes.PROTOBUF;
public class InheritanceActionTest {
@Test
public void inheritance_parent_child() throws Exception {
RuleDefinitionDto rule1 = dbTester.rules().insert();
- ruleIndexer.index(organization, rule1.getKey());
+ ruleIndexer.indexRuleDefinition(rule1.getKey());
RuleDefinitionDto rule2 = dbTester.rules().insert();
- ruleIndexer.index(organization, rule1.getKey());
+ ruleIndexer.indexRuleDefinition(rule1.getKey());
RuleDefinitionDto rule3 = dbTester.rules().insert();
- ruleIndexer.index(organization, rule1.getKey());
+ ruleIndexer.indexRuleDefinition(rule1.getKey());
QualityProfileDto parent = dbTester.qualityProfiles().insert(organization);
dbTester.qualityProfiles().activateRule(parent, rule1);
dbTester.qualityProfiles().activateRule(parent, rule2);
- int parentRules = 2;
+ long parentRules = 2;
QualityProfileDto child = dbTester.qualityProfiles().insert(organization, q -> q.setParentKee(parent.getKee()));
dbTester.qualityProfiles().activateRule(child, rule3);
- int childRules = 1;
+ long childRules = 1;
activeRuleIndexer.index();
- String response = wsActionTester.newRequest()
+ InputStream response = wsActionTester.newRequest()
.setMethod("GET")
+ .setMediaType(PROTOBUF)
.setParam("profileKey", child.getKey())
.execute()
- .getInput();
+ .getInputStream();
- JsonAssert.assertJson(response).isSimilarTo("" +
- "{" +
- " \"profile\":" + generateJsonProfile(child, childRules) +
- "}");
+ InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
- JsonAssert.assertJson(response).isSimilarTo("" +
- "{" +
- " \"ancestors\":[" + generateJsonProfile(parent, parentRules) + "]" +
- "}");
+ assertThat(result.getProfile().getKey()).isEqualTo(child.getKey());
+ assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(childRules);
+
+ assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getKey).containsExactly(parent.getKey());
+ assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getActiveRuleCount).containsExactly(parentRules);
}
@Test
public void inheritance_ignores_removed_rules() throws Exception {
RuleDefinitionDto rule = dbTester.rules().insert(r -> r.setStatus(RuleStatus.REMOVED));
- ruleIndexer.index(organization, rule.getKey());
+ ruleIndexer.indexRuleDefinition(rule.getKey());
QualityProfileDto profile = dbTester.qualityProfiles().insert(organization);
dbTester.qualityProfiles().activateRule(profile, rule);
- int activeRules = 0;
+ long activeRules = 0;
activeRuleIndexer.index();
- String response = wsActionTester.newRequest()
+ InputStream response = wsActionTester.newRequest()
.setMethod("GET")
+ .setMediaType(PROTOBUF)
.setParam("profileKey", profile.getKey())
.execute()
- .getInput();
-
- JsonAssert.assertJson(response).isSimilarTo("" +
- "{" +
- " \"profile\":" + generateJsonProfile(profile, activeRules) +
- "}");
- }
+ .getInputStream();
- private String generateJsonProfile(QualityProfileDto child, int numberOfRules) {
- return "" +
- "{" +
- " \"key\":\"" + child.getKey() + "\"," +
- " \"activeRuleCount\":" + numberOfRules +
- "}";
+ InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
+ assertThat(result.getProfile().getKey()).isEqualTo(profile.getKey());
+ assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(activeRules);
}
@Test