return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto, uuid), true, defaults(), dtoPopulator);
}
-
public final ComponentDto insertPrivateProjectWithCustomBranch(String branchKey) {
- return insertPrivateProjectWithCustomBranch(db.getDefaultOrganization(), b -> b.setBranchType(BRANCH).setKey(branchKey), defaults());
+ return insertPrivateProjectWithCustomBranch(db.getDefaultOrganization(), b -> b.setBranchType(BRANCH).setKey(branchKey), defaults());
}
public final ComponentDto insertPrivateProjectWithCustomBranch(OrganizationDto organizationDto, Consumer<BranchDto> branchPopulator) {
}
public void addApplicationProject(ComponentDto application, ComponentDto... projects) {
- for(ComponentDto project : projects){
+ for (ComponentDto project : projects) {
dbClient.applicationProjectsDao().addProject(dbSession, application.uuid(), project.uuid());
}
db.commit();
}
public void addApplicationProject(ProjectDto application, ProjectDto... projects) {
- for(ProjectDto project : projects){
+ for (ProjectDto project : projects) {
dbClient.applicationProjectsDao().addProject(dbSession, application.getUuid(), project.getUuid());
}
db.commit();
}
public void addProjectBranchToApplicationBranch(BranchDto applicationBranch, BranchDto... projectBranches) {
- for(BranchDto projectBranch : projectBranches){
+ for (BranchDto projectBranch : projectBranches) {
dbClient.applicationProjectsDao().addProjectBranchToAppBranch(dbSession, applicationBranch, projectBranch);
}
db.commit();
private ComponentDto insertComponentImpl(ComponentDto component, @Nullable Boolean isPrivate, Consumer<ComponentDto> dtoPopulator) {
dtoPopulator.accept(component);
checkState(isPrivate == null || component.isPrivate() == isPrivate, "Illegal modification of private flag");
+ if (component.getOrganizationUuid() == null) {
+ component.setOrganizationUuid(db.getDefaultOrganization().getUuid());
+ }
dbClient.componentDao().insert(dbSession, component);
db.commit();
}
public void insertComponents(ComponentDto... components) {
+ String defaultOrgUuid = db.getDefaultOrganization().getUuid();
+ Arrays.stream(components).forEach(c -> {
+ if (c.getOrganizationUuid() == null) {
+ c.setOrganizationUuid(defaultOrgUuid);
+ }
+ });
+
dbClient.componentDao().insert(dbSession, asList(components));
db.commit();
}
return newModuleDto(Uuids.createFast(), subProjectOrProject);
}
+ public static ComponentDto newPrivateProjectDto() {
+ return newProjectDto(Uuids.createFast(), true);
+ }
+
+ // TODO remove
+ @Deprecated
public static ComponentDto newPrivateProjectDto(OrganizationDto organizationDto) {
return newProjectDto(organizationDto.getUuid(), Uuids.createFast(), true);
}
+ // TODO remove
+ @Deprecated
public static ProjectDto createPrivateProjectDto(OrganizationDto organizationDto) {
return createProjectDto(organizationDto.getUuid(), Uuids.createFast(), true);
}
+ public static ComponentDto newPrivateProjectDto(String uuid) {
+ return newProjectDto(uuid, true);
+ }
+
+ // TODO remove
+ @Deprecated
public static ComponentDto newPrivateProjectDto(OrganizationDto organizationDto, String uuid) {
return newProjectDto(organizationDto.getUuid(), uuid, true);
}
+ // TODO remove
+ @Deprecated
public static ComponentDto newPublicProjectDto(OrganizationDto organizationDto) {
return newProjectDto(organizationDto.getUuid(), Uuids.createFast(), false);
}
+ // TODO remove
+ @Deprecated
public static ComponentDto newPublicProjectDto(OrganizationDto organizationDto, String uuid) {
return newProjectDto(organizationDto.getUuid(), uuid, false);
}
+ // TODO remove
+ @Deprecated
private static ProjectDto createProjectDto(String organizationUuid, String uuid, boolean isPrivate) {
return new ProjectDto()
.setOrganizationUuid(organizationUuid)
.setPrivate(isPrivate);
}
+ private static ComponentDto newProjectDto(String uuid, boolean isPrivate) {
+ return new ComponentDto()
+ .setUuid(uuid)
+ .setUuidPath(UUID_PATH_OF_ROOT)
+ .setProjectUuid(uuid)
+ .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR)
+ .setRootUuid(uuid)
+ .setDbKey("KEY_" + uuid)
+ .setName("NAME_" + uuid)
+ .setLongName("LONG_NAME_" + uuid)
+ .setDescription("DESCRIPTION_" + uuid)
+ .setScope(Scopes.PROJECT)
+ .setQualifier(Qualifiers.PROJECT)
+ .setPath(null)
+ .setLanguage(null)
+ .setEnabled(true)
+ .setPrivate(isPrivate);
+ }
+
private static ComponentDto newProjectDto(String organizationUuid, String uuid, boolean isPrivate) {
return new ComponentDto()
.setOrganizationUuid(organizationUuid)
return newApplication(organizationDto.getUuid(), uuid);
}
-
public static ComponentDto newApplication(String organizationUuid) {
return newApplication(organizationUuid, Uuids.createFast());
}
.setPrivate(project.isPrivate());
}
-
public static ComponentDto newBranchComponent(ComponentDto project, BranchDto branchDto) {
checkArgument(project.qualifier().equals(Qualifiers.PROJECT) || project.qualifier().equals(Qualifiers.APP));
checkArgument(project.getMainBranchProjectUuid() == null);
package org.sonar.server.favorite.ws;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.Paging;
import org.sonar.api.web.UserRole;
import org.sonar.core.util.stream.MoreCollectors;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.favorite.FavoriteFinder;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Common;
import org.sonarqube.ws.Favorites.Favorite;
import org.sonarqube.ws.Favorites.SearchResponse;
-import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Optional.ofNullable;
-import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonar.server.favorite.ws.FavoritesWsParameters.ACTION_SEARCH;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class SearchAction implements FavoritesWsAction {
private static final int MAX_PAGE_SIZE = 500;
private final FavoriteFinder favoriteFinder;
- private final DbClient dbClient;
private final UserSession userSession;
- public SearchAction(FavoriteFinder favoriteFinder, DbClient dbClient, UserSession userSession) {
+ public SearchAction(FavoriteFinder favoriteFinder, UserSession userSession) {
this.favoriteFinder = favoriteFinder;
- this.dbClient = dbClient;
this.userSession = userSession;
}
private SearchResults toSearchResults(SearchRequest request) {
userSession.checkLoggedIn();
- try (DbSession dbSession = dbClient.openSession(false)) {
- List<ComponentDto> authorizedFavorites = getAuthorizedFavorites();
- Paging paging = Paging.forPageIndex(Integer.parseInt(request.getP())).withPageSize(Integer.parseInt(request.getPs())).andTotal(authorizedFavorites.size());
- List<ComponentDto> displayedFavorites = authorizedFavorites.stream()
- .skip(paging.offset())
- .limit(paging.pageSize())
- .collect(MoreCollectors.toList());
- Map<String, OrganizationDto> organizationsByUuid = getOrganizationsOfComponents(dbSession, displayedFavorites);
- return new SearchResults(paging, displayedFavorites, organizationsByUuid);
- }
+
+ List<ComponentDto> authorizedFavorites = getAuthorizedFavorites();
+ Paging paging = Paging.forPageIndex(Integer.parseInt(request.getP())).withPageSize(Integer.parseInt(request.getPs())).andTotal(authorizedFavorites.size());
+ List<ComponentDto> displayedFavorites = authorizedFavorites.stream()
+ .skip(paging.offset())
+ .limit(paging.pageSize())
+ .collect(MoreCollectors.toList());
+ return new SearchResults(paging, displayedFavorites);
}
private List<ComponentDto> getAuthorizedFavorites() {
return userSession.keepAuthorizedComponents(UserRole.USER, componentDtos);
}
- private Map<String, OrganizationDto> getOrganizationsOfComponents(DbSession dbSession, List<ComponentDto> displayedFavorites) {
- Set<String> organizationUuids = displayedFavorites.stream()
- .map(ComponentDto::getOrganizationUuid)
- .collect(MoreCollectors.toSet());
- return dbClient.organizationDao().selectByUuids(dbSession, organizationUuids)
- .stream()
- .collect(MoreCollectors.uniqueIndex(OrganizationDto::getUuid));
- }
-
private static class SearchResults {
private final List<ComponentDto> favorites;
private final Paging paging;
- private final Map<String, OrganizationDto> organizationsByUuid;
- private SearchResults(Paging paging, List<ComponentDto> favorites, Map<String, OrganizationDto> organizationsByUuid) {
+ private SearchResults(Paging paging, List<ComponentDto> favorites) {
this.paging = paging;
this.favorites = favorites;
- this.organizationsByUuid = organizationsByUuid;
}
}
private static void addFavorites(SearchResponse.Builder builder, SearchResults results) {
Favorite.Builder favoriteBuilder = Favorite.newBuilder();
results.favorites.stream()
- .map(componentDto -> toWsFavorite(favoriteBuilder, results, componentDto))
+ .map(componentDto -> toWsFavorite(favoriteBuilder, componentDto))
.forEach(builder::addFavorites);
}
- private static Favorite toWsFavorite(Favorite.Builder builder, SearchResults results, ComponentDto componentDto) {
- OrganizationDto organization = results.organizationsByUuid.get(componentDto.getOrganizationUuid());
- checkArgument(organization != null,
- "Organization with uuid '%s' not found for favorite with uuid '%s'",
- componentDto.getOrganizationUuid(), componentDto.uuid());
+ private static Favorite toWsFavorite(Favorite.Builder builder, ComponentDto componentDto) {
builder
.clear()
- .setOrganization(organization.getKey())
.setKey(componentDto.getDbKey());
ofNullable(componentDto.name()).ifPresent(builder::setName);
ofNullable(componentDto.qualifier()).ifPresent(builder::setQualifier);
},
"favorites": [
{
- "organization": "my-org",
"key": "K2",
"name": "Apache HBase",
"qualifier": "TRK"
},
{
- "organization": "openjdk",
"key": "K3",
"name": "JDK9",
"qualifier": "TRK"
},
{
- "organization": "my-org",
"key": "K1",
"name": "Samba",
"qualifier": "TRK"
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.db.user.UserDto;
@Rule
public DbTester db = DbTester.create();
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
-
- private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
- private WsActionTester ws = new WsActionTester(new AddAction(userSession, dbClient, favoriteUpdater, TestComponentFinder.from(db)));
+ private final DbClient dbClient = db.getDbClient();
+ private final DbSession dbSession = db.getSession();
+ private final FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
+ private final WsActionTester ws = new WsActionTester(new AddAction(userSession, dbClient, favoriteUpdater, TestComponentFinder.from(db)));
@Test
public void add_a_project() {
@Test
public void fail_when_using_branch_db_key() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
ComponentDto branch = db.components().insertProjectBranch(project);
UserDto user = db.users().insertUser();
userSession.logIn(user).addProjectPermission(USER, project);
assertThat(definition.key()).isEqualTo("add");
assertThat(definition.isPost()).isTrue();
- assertThat(definition.param("component").isRequired()).isTrue();
+ WebService.Param param = definition.param("component");
+ assertThat(param).isNotNull();
+ assertThat(param.isRequired()).isTrue();
}
private TestResponse call(@Nullable String componentKey) {
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.util.Optional.ofNullable;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.server.favorite.ws.FavoritesWsParameters.PARAM_COMPONENT;
private static final String PROJECT_UUID = "project-uuid";
private UserDto user;
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create();
- private DbClient dbClient = db.getDbClient();
-
- private FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
- private WsActionTester ws = new WsActionTester(new RemoveAction(userSession, dbClient, favoriteUpdater, TestComponentFinder.from(db)));
+ private final DbClient dbClient = db.getDbClient();
+ private final FavoriteUpdater favoriteUpdater = new FavoriteUpdater(dbClient);
+ private final WsActionTester ws = new WsActionTester(new RemoveAction(userSession, dbClient, favoriteUpdater, TestComponentFinder.from(db)));
@Before
public void before() {
public void fail_if_not_already_a_favorite() {
insertProjectAndPermissions();
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Component '" + PROJECT_KEY + "' is not a favorite");
-
- call(PROJECT_KEY);
+ assertThatThrownBy(() -> call(PROJECT_KEY))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Component '" + PROJECT_KEY + "' is not a favorite");
}
@Test
public void fail_when_component_is_not_found() {
userSession.logIn();
- expectedException.expect(NotFoundException.class);
-
- call("P42");
+ assertThatThrownBy(() -> call("P42"))
+ .isInstanceOf(NotFoundException.class);
}
@Test
public void fail_when_user_is_not_authenticated() {
insertProject();
- expectedException.expect(UnauthorizedException.class);
-
- call(PROJECT_KEY);
+ assertThatThrownBy(() -> call(PROJECT_KEY))
+ .isInstanceOf(UnauthorizedException.class);
}
@Test
public void fail_when_using_branch_db_key() {
- OrganizationDto organization = db.organizations().insert();
- ComponentDto project = db.components().insertPrivateProject(organization);
+ ComponentDto project = db.components().insertPrivateProject();
userSession.logIn().addProjectPermission(UserRole.USER, project);
ComponentDto branch = db.components().insertProjectBranch(project);
+ String branchKey = branch.getDbKey();
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
-
- call(branch.getDbKey());
+ assertThatThrownBy(() -> call(branchKey))
+ .isInstanceOf(NotFoundException.class)
+ .hasMessage(format("Component key '%s' not found", branchKey));
}
@Test
assertThat(definition.key()).isEqualTo("remove");
assertThat(definition.isPost()).isTrue();
- assertThat(definition.param("component").isRequired()).isTrue();
+ WebService.Param param = definition.param("component");
+ assertThat(param).isNotNull();
+ assertThat(param.isRequired()).isTrue();
}
private ComponentDto insertProject() {
- return db.components().insertComponent(newPrivateProjectDto(db.organizations().insert(), PROJECT_UUID).setDbKey(PROJECT_KEY));
+ return db.components().insertComponent(newPrivateProjectDto(PROJECT_UUID).setDbKey(PROJECT_KEY));
}
private ComponentDto insertProjectAndPermissions() {
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.favorite.FavoriteFinder;
import static java.util.Optional.ofNullable;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.resources.Qualifiers.FILE;
import static org.sonar.api.resources.Qualifiers.PROJECT;
public class SearchActionTest {
private String userUuid;
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
+
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create();
- private DbClient dbClient = db.getDbClient();
+ private final DbClient dbClient = db.getDbClient();
- private FavoriteFinder favoriteFinder = new FavoriteFinder(dbClient, userSession);
+ private final FavoriteFinder favoriteFinder = new FavoriteFinder(dbClient, userSession);
- private WsActionTester ws = new WsActionTester(new SearchAction(favoriteFinder, dbClient, userSession));
+ private final WsActionTester ws = new WsActionTester(new SearchAction(favoriteFinder, userSession));
@Before
public void before() {
@Test
public void return_favorites() {
- ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "P1").setDbKey("K1").setName("N1");
+ ComponentDto project = newPrivateProjectDto("P1").setDbKey("K1").setName("N1");
addComponent(project);
addComponent(newFileDto(project).setDbKey("K11").setName("N11"));
- addComponent(newPrivateProjectDto(db.getDefaultOrganization(), "P2").setDbKey("K2").setName("N2"));
+ addComponent(newPrivateProjectDto("P2").setDbKey("K2").setName("N2"));
SearchResponse result = call();
@Test
public void filter_authorized_components() {
- OrganizationDto organizationDto = db.organizations().insert();
- addComponent(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("K1"));
- ComponentDto unauthorizedProject = db.components().insertComponent(ComponentTesting.newPrivateProjectDto(organizationDto));
+ addComponent(ComponentTesting.newPrivateProjectDto().setDbKey("K1"));
+ ComponentDto unauthorizedProject = db.components().insertComponent(ComponentTesting.newPrivateProjectDto());
db.favorites().add(unauthorizedProject, userUuid);
SearchResponse result = call();
@Test
public void paginate_results() {
IntStream.rangeClosed(1, 9)
- .forEach(i -> addComponent(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("K" + i).setName("N" + i)));
- ComponentDto unauthorizedProject = db.components().insertComponent(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()));
+ .forEach(i -> addComponent(ComponentTesting.newPrivateProjectDto().setDbKey("K" + i).setName("N" + i)));
+ ComponentDto unauthorizedProject = db.components().insertComponent(ComponentTesting.newPrivateProjectDto());
db.favorites().add(unauthorizedProject, userUuid);
SearchResponse result = call(2, 3);
@Test
public void return_only_users_favorite() {
- OrganizationDto organizationDto = db.organizations().insert();
- addComponent(ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("K1"));
- ComponentDto otherUserFavorite = ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("K42");
+ addComponent(ComponentTesting.newPrivateProjectDto().setDbKey("K1"));
+ ComponentDto otherUserFavorite = ComponentTesting.newPrivateProjectDto().setDbKey("K42");
db.components().insertComponent(otherUserFavorite);
db.favorites().add(otherUserFavorite, "42");
db.commit();
@Test
public void favorites_ordered_by_name() {
- OrganizationDto organizationDto = db.organizations().insert();
- addComponent(ComponentTesting.newPrivateProjectDto(organizationDto).setName("N2"));
- addComponent(ComponentTesting.newPrivateProjectDto(organizationDto).setName("N3"));
- addComponent(ComponentTesting.newPrivateProjectDto(organizationDto).setName("N1"));
+ addComponent(ComponentTesting.newPrivateProjectDto().setName("N2"));
+ addComponent(ComponentTesting.newPrivateProjectDto().setName("N3"));
+ addComponent(ComponentTesting.newPrivateProjectDto().setName("N1"));
SearchResponse result = call();
@Test
public void json_example() {
- OrganizationDto organization1 = db.organizations().insertForKey("my-org");
- OrganizationDto organization2 = db.organizations().insertForKey("openjdk");
- addComponent(ComponentTesting.newPrivateProjectDto(organization1).setDbKey("K1").setName("Samba"));
- addComponent(ComponentTesting.newPrivateProjectDto(organization1).setDbKey("K2").setName("Apache HBase"));
- addComponent(ComponentTesting.newPrivateProjectDto(organization2).setDbKey("K3").setName("JDK9"));
+ addComponent(ComponentTesting.newPrivateProjectDto().setDbKey("K1").setName("Samba"));
+ addComponent(ComponentTesting.newPrivateProjectDto().setDbKey("K2").setName("Apache HBase"));
+ addComponent(ComponentTesting.newPrivateProjectDto().setDbKey("K3").setName("JDK9"));
String result = ws.newRequest().execute().getInput();
+ assertThat(ws.getDef().responseExampleAsString()).isNotNull();
assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
}
public void fail_if_not_authenticated() {
userSession.anonymous();
- expectedException.expect(UnauthorizedException.class);
-
- call();
+ assertThatThrownBy(this::call)
+ .isInstanceOf(UnauthorizedException.class);
}
private void addComponent(ComponentDto component) {
}
message Favorite {
- optional string organization = 4;
optional string key = 1;
optional string name = 2;
optional string qualifier = 3;