From d4eb053812ed5fd74f49face32f094b03a707166 Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Mon, 24 Apr 2017 19:31:54 +0200 Subject: SONAR-9099 add visibility flag to response of api/components/show --- .../org/sonar/server/component/ws/ShowAction.java | 4 +- .../sonar/server/component/ws/show-example.json | 3 +- .../sonar/server/component/ws/ShowActionTest.java | 52 ++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) (limited to 'server/sonar-server/src') diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ShowAction.java index 4aef9e3f4d0..ef737d714c9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/ShowAction.java @@ -70,7 +70,9 @@ public class ShowAction implements ComponentsWsAction { .setSince("5.4") .setChangelog( new Change("6.4", "Analysis date has been added to the response"), - new Change("6.4", "The field 'id' is deprecated in the response")) + new Change("6.4", "The field 'id' is deprecated in the response"), + new Change("6.4", "The 'visibility' field is added") + ) .setHandler(this); action.createParam(PARAM_COMPONENT_ID) diff --git a/server/sonar-server/src/main/resources/org/sonar/server/component/ws/show-example.json b/server/sonar-server/src/main/resources/org/sonar/server/component/ws/show-example.json index 5d21fab0c1a..18030a1004d 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/component/ws/show-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/component/ws/show-example.json @@ -30,7 +30,8 @@ "tags": [ "language", "plugin" - ] + ], + "visibility": "private" } ] } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java index 41294cd3570..4fc68854faf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ShowActionTest.java @@ -26,11 +26,14 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.resources.Qualifiers; +import org.sonar.api.server.ws.Change; +import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentTesting; import org.sonar.db.organization.OrganizationDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.exceptions.ForbiddenException; @@ -42,6 +45,7 @@ import org.sonarqube.ws.WsComponents; import org.sonarqube.ws.WsComponents.ShowWsResponse; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.api.utils.DateUtils.parseDateTime; import static org.sonar.db.component.ComponentTesting.newDirectory; @@ -65,6 +69,35 @@ public class ShowActionTest { private WsActionTester ws = new WsActionTester(new ShowAction(userSession, db.getDbClient(), new ComponentFinder(db.getDbClient()))); + @Test + public void verify_definition() throws Exception { + WebService.Action action = ws.getDef(); + + assertThat(action.since()).isEqualTo("5.4"); + assertThat(action.description()).isNotNull(); + assertThat(action.responseExample()).isNotNull(); + assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactlyInAnyOrder( + tuple("6.4", "Analysis date has been added to the response"), + tuple("6.4", "The field 'id' is deprecated in the response"), + tuple("6.4", "The 'visibility' field is added") + ); + + WebService.Param componentId = action.param(PARAM_COMPONENT_ID); + assertThat(componentId.isRequired()).isFalse(); + assertThat(componentId.description()).isNotNull(); + assertThat(componentId.exampleValue()).isNotNull(); + assertThat(componentId.deprecatedSince()).isEqualTo("6.4"); + assertThat(componentId.deprecatedKey()).isEqualTo("id"); + assertThat(componentId.deprecatedKeySince()).isEqualTo("6.4"); + + WebService.Param component = action.param(PARAM_COMPONENT); + assertThat(component.isRequired()).isFalse(); + assertThat(component.description()).isNotNull(); + assertThat(component.exampleValue()).isNotNull(); + assertThat(component.deprecatedKey()).isEqualTo("key"); + assertThat(component.deprecatedKeySince()).isEqualTo("6.4"); + } + @Test public void json_example() throws IOException { userSession.logIn().setRoot(); @@ -168,6 +201,25 @@ public class ShowActionTest { .containsOnly(expectedDate, expectedDate, expectedDate); } + @Test + public void return_visibility_field() throws Exception { + userSession.logIn().setRoot(); + ComponentDto privateProject = db.components().insertPrivateProject(); + ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(privateProject)); + ComponentDto publicProject = db.components().insertPublicProject(); + + ShowWsResponse result = newRequest(null, privateProject.key()); + assertThat(result.getComponent().hasVisibility()).isTrue(); + assertThat(result.getComponent().getVisibility()).isEqualTo(privateProject.isPrivate() ? "private" : "public"); + + ShowWsResponse result2 = newRequest(null, module.key()); + assertThat(result2.getComponent().hasVisibility()).isFalse(); + + ShowWsResponse result3 = newRequest(null, publicProject.key()); + assertThat(result3.getComponent().hasVisibility()).isTrue(); + assertThat(result3.getComponent().getVisibility()).isEqualTo(publicProject.isPrivate() ? "private" : "public"); + } + @Test public void throw_ForbiddenException_if_user_doesnt_have_browse_permission_on_project() { userSession.logIn(); -- cgit v1.2.3