}
public ComponentDto getById(Long id, DbSession session) {
- ComponentDto componentDto = getNullableById(id, session);
- if (componentDto == null) {
- throw new NotFoundException(String.format("Project with id '%s' not found", id));
- }
- return componentDto;
- }
-
- @CheckForNull
- public ComponentDto getNullableById(Long id, DbSession session) {
return mapper(session).selectById(id);
}
}
@Override
- @CheckForNull
protected ComponentDto doGetNullableByKey(DbSession session, String key) {
return mapper(session).selectByKey(key);
}
private ComponentDto getProject(@Nullable Long projectId, Map<Long, ComponentDto> projectsById, DbSession session) {
ComponentDto project = projectsById.get(projectId);
if (project == null && projectId != null) {
- project = componentDao.getNullableById(projectId, session);
+ project = componentDao.getById(projectId, session);
if (project != null) {
projectsById.put(project.getId(), project);
}
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metric.ValueType;
import org.sonar.api.measures.MetricFinder;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.component.ComponentDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
}
public void associateProject(Long qGateId, Long projectId) {
+ checkPermission(UserSession.get());
+
DbSession session = myBatis.openSession(false);
try {
getNonNullQgate(qGateId);
- ComponentDto project = componentDao.getById(projectId, session);
- checkPermission(UserSession.get(), project.key());
+ checkNonNullProject(projectId, session);
propertiesDao.setProperty(new PropertyDto().setKey(SONAR_QUALITYGATE_PROPERTY).setResourceId(projectId).setValue(qGateId.toString()));
} finally {
MyBatis.closeQuietly(session);
}
public void dissociateProject(Long qGateId, Long projectId) {
+ checkPermission(UserSession.get());
+
DbSession session = myBatis.openSession(false);
try {
getNonNullQgate(qGateId);
- ComponentDto project = componentDao.getById(projectId, session);
- checkPermission(UserSession.get(), project.key());
+ checkNonNullProject(projectId, session);
propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, projectId);
} finally {
MyBatis.closeQuietly(session);
return condition;
}
+ private void checkNonNullProject(long projectId, DbSession session) {
+ if (!componentDao.existsById(projectId, session)) {
+ throw new NotFoundException("There is no project with id=" + projectId);
+ }
+ }
+
private void validateQualityGate(@Nullable Long updatingQgateId, @Nullable String name) {
Errors errors = new Errors();
if (Strings.isNullOrEmpty(name)) {
private void checkPermission(UserSession userSession) {
userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
}
-
- private void checkPermission(UserSession userSession, String projectKey) {
- if (!userSession.hasGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN) && !userSession.hasProjectPermission(UserRole.ADMIN, projectKey)) {
- throw new ForbiddenException("Insufficient privileges");
- }
- }
}
import org.sonar.api.ServerComponent;
import org.sonar.api.component.Component;
-import org.sonar.api.web.UserRole;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.properties.PropertyDto;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.user.UserSession;
-import javax.annotation.Nullable;
-
public class QProfileProjectOperations implements ServerComponent {
private final DbClient db;
}
public void addProject(int profileId, long projectId, UserSession userSession) {
+ checkPermission(userSession);
DbSession session = db.openSession(false);
try {
addProject(profileId, projectId, userSession, session);
}
void addProject(int profileId, long projectId, UserSession userSession, DbSession session) {
+ checkPermission(userSession);
ComponentDto project = (ComponentDto) findProjectNotNull(projectId, session);
- checkPermission(userSession, project.key());
QualityProfileDto qualityProfile = findNotNull(profileId, session);
db.propertiesDao().setProperty(new PropertyDto().setKey(
}
public void removeProject(int profileId, long projectId, UserSession userSession) {
+ checkPermission(userSession);
DbSession session = db.openSession(false);
try {
ComponentDto project = (ComponentDto) findProjectNotNull(projectId, session);
- checkPermission(userSession, project.key());
QualityProfileDto qualityProfile = findNotNull(profileId, session);
db.propertiesDao().deleteProjectProperty(QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + qualityProfile.getLanguage(), project.getId(), session);
}
public void removeProject(String language, long projectId, UserSession userSession) {
+ checkPermission(userSession);
DbSession session = db.openSession(false);
try {
ComponentDto project = (ComponentDto) findProjectNotNull(projectId, session);
- checkPermission(userSession, project.key());
db.propertiesDao().deleteProjectProperty(QProfileProjectLookup.PROFILE_PROPERTY_PREFIX + language, project.getId(), session);
session.commit();
}
public void removeAllProjects(int profileId, UserSession userSession) {
- checkPermission(userSession, null);
+ checkPermission(userSession);
DbSession session = db.openSession(false);
try {
QualityProfileDto qualityProfile = findNotNull(profileId, session);
return component;
}
- private void checkPermission(UserSession userSession, @Nullable String projectKey) {
- if (!userSession.hasGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN) && projectKey != null && !userSession.hasProjectPermission(UserRole.ADMIN, projectKey)) {
- throw new ForbiddenException("Insufficient privileges");
- }
+ private void checkPermission(UserSession userSession) {
+ userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
}
}
setupData("shared");
assertThat(dao.getById(4L, session)).isNotNull();
- }
-
- @Test(expected = NotFoundException.class)
- public void fail_to_get_by_id_when_project_not_found() {
- setupData("shared");
-
- dao.getById(111L, session);
- }
-
- @Test
- public void get_nullable_by_id() {
- setupData("shared");
-
- assertThat(dao.getNullableById(4L, session)).isNotNull();
- assertThat(dao.getNullableById(111L, session)).isNull();
+ assertThat(dao.getById(111L, session)).isNull();
}
@Test
when(componentDao.getNullableByKey(session, key1)).thenReturn(file1);
when(componentDao.getNullableByKey(session, key2)).thenReturn(file2);
- when(componentDao.getNullableById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
- when(componentDao.getNullableById(5L, session)).thenReturn(new ComponentDto().setId(5L).setKey("org.codehaus.sonar:sonar-ws-client").setLongName("SonarQube :: Web Service Client"));
+ when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
+ when(componentDao.getById(5L, session)).thenReturn(new ComponentDto().setId(5L).setKey("org.codehaus.sonar:sonar-ws-client").setLongName("SonarQube :: Web Service Client"));
List<DuplicationsParser.Block> blocks = newArrayList();
blocks.add(new DuplicationsParser.Block(newArrayList(
verify(componentDao, times(2)).getNullableByKey(eq(session), anyString());
// Verify call to dao is cached when searching for project / sub project
- verify(componentDao, times(1)).getNullableById(eq(1L), eq(session));
- verify(componentDao, times(1)).getNullableById(eq(5L), eq(session));
+ verify(componentDao, times(1)).getById(eq(1L), eq(session));
+ verify(componentDao, times(1)).getById(eq(5L), eq(session));
}
@Test
when(componentDao.getNullableByKey(session, key1)).thenReturn(file1);
when(componentDao.getNullableByKey(session, key2)).thenReturn(file2);
- when(componentDao.getNullableById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
+ when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
List<DuplicationsParser.Block> blocks = newArrayList();
blocks.add(new DuplicationsParser.Block(newArrayList(
ComponentDto file1 = new ComponentDto().setId(10L).setQualifier("FIL").setKey(key1).setLongName("PropertyDeleteQuery").setProjectId(1L);
when(componentDao.getNullableByKey(session, key1)).thenReturn(file1);
- when(componentDao.getNullableById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
+ when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
List<DuplicationsParser.Block> blocks = newArrayList();
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metric.ValueType;
import org.sonar.api.measures.MetricFinder;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.component.ComponentDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class QualityGatesTest {
QualityGates qGates;
- static final String PROJECT_KEY = "SonarQube";
-
- UserSession authorizedProfileAdminUserSession = MockUserSession.create().setLogin("gaudol").setName("Olivier").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- UserSession authorizedProjectAdminUserSession = MockUserSession.create().setLogin("gaudol").setName("Olivier").addProjectPermissions(UserRole.ADMIN, PROJECT_KEY);
+ UserSession authorizedUserSession = MockUserSession.create().setLogin("gaudol").setName("Olivier").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
UserSession unauthenticatedUserSession = MockUserSession.create();
UserSession unauthorizedUserSession = MockUserSession.create().setLogin("polop").setName("Polop");
@Before
public void initialize() {
- when(componentDao.getById(anyLong(), eq(session))).thenReturn(new ComponentDto().setId(1L).setKey(PROJECT_KEY));
-
when(myBatis.openSession(false)).thenReturn(session);
qGates = new QualityGates(dao, conditionDao, metricFinder, propertiesDao, componentDao, myBatis);
- UserSessionTestUtils.setUserSession(authorizedProfileAdminUserSession);
+ UserSessionTestUtils.setUserSession(authorizedUserSession);
}
@Test
public void should_select_default_qgate() throws Exception {
long defaultId = 42L;
String defaultName = "Default Name";
- when(dao.selectById(defaultId)).thenReturn(new QualityGateDto().setId(defaultId).setName(defaultName));
+ when(dao.selectById(defaultId)).thenReturn(new QualityGateDto().setId(defaultId).setName(defaultName ));
qGates.setDefault(defaultId);
verify(dao).selectById(defaultId);
ArgumentCaptor<PropertyDto> propertyCaptor = ArgumentCaptor.forClass(PropertyDto.class);
QualityGateConditionDto cond1 = new QualityGateConditionDto().setMetricId(metric1Id);
QualityGateConditionDto cond2 = new QualityGateConditionDto().setMetricId(metric2Id);
Collection<QualityGateConditionDto> conditions = ImmutableList.of(cond1, cond2);
- when(conditionDao.selectForQualityGate(qGateId)).thenReturn(conditions);
+ when(conditionDao.selectForQualityGate(qGateId)).thenReturn(conditions );
Metric metric1 = mock(Metric.class);
when(metric1.getKey()).thenReturn(metric1Key);
when(metricFinder.findById((int) metric1Id)).thenReturn(metric1);
QualityGateConditionDto cond1 = new QualityGateConditionDto().setMetricId(metric1Id);
QualityGateConditionDto cond2 = new QualityGateConditionDto().setMetricId(metric2Id);
Collection<QualityGateConditionDto> conditions = ImmutableList.of(cond1, cond2);
- when(conditionDao.selectForQualityGate(qGateId)).thenReturn(conditions);
+ when(conditionDao.selectForQualityGate(qGateId)).thenReturn(conditions );
Metric metric1 = mock(Metric.class);
when(metric1.getKey()).thenReturn(metric1Key);
when(metricFinder.findById((int) metric1Id)).thenReturn(metric1);
Long qGateId = 42L;
Long projectId = 24L;
when(dao.selectById(qGateId)).thenReturn(new QualityGateDto().setId(qGateId));
+ when(componentDao.existsById(projectId, session)).thenReturn(true);
qGates.associateProject(qGateId, projectId);
verify(dao).selectById(qGateId);
ArgumentCaptor<PropertyDto> propertyCaptor = ArgumentCaptor.forClass(PropertyDto.class);
assertThat(property.getValue()).isEqualTo("42");
}
- @Test
- public void associate_project_with_project_admin_permission() {
- UserSessionTestUtils.setUserSession(authorizedProjectAdminUserSession);
-
+ @Test(expected = NotFoundException.class)
+ public void should_fail_associate_project_on_not_existing_project() {
Long qGateId = 42L;
Long projectId = 24L;
when(dao.selectById(qGateId)).thenReturn(new QualityGateDto().setId(qGateId));
- qGates.associateProject(qGateId, projectId);
- verify(dao).selectById(qGateId);
- ArgumentCaptor<PropertyDto> propertyCaptor = ArgumentCaptor.forClass(PropertyDto.class);
- verify(propertiesDao).setProperty(propertyCaptor.capture());
- PropertyDto property = propertyCaptor.getValue();
- assertThat(property.getKey()).isEqualTo("sonar.qualitygate");
- assertThat(property.getResourceId()).isEqualTo(projectId);
- assertThat(property.getValue()).isEqualTo("42");
+ qGates.associateProject(qGateId , projectId);
}
@Test
Long qGateId = 42L;
Long projectId = 24L;
when(dao.selectById(qGateId)).thenReturn(new QualityGateDto().setId(qGateId));
- qGates.dissociateProject(qGateId, projectId);
- verify(dao).selectById(qGateId);
- verify(propertiesDao).deleteProjectProperty("sonar.qualitygate", projectId);
- }
-
- @Test
- public void dissociate_project_with_project_admin_permission() {
- UserSessionTestUtils.setUserSession(authorizedProjectAdminUserSession);
-
- Long qGateId = 42L;
- Long projectId = 24L;
- when(dao.selectById(qGateId)).thenReturn(new QualityGateDto().setId(qGateId));
+ when(componentDao.existsById(projectId, session)).thenReturn(true);
qGates.dissociateProject(qGateId, projectId);
verify(dao).selectById(qGateId);
verify(propertiesDao).deleteProjectProperty("sonar.qualitygate", projectId);
import org.apache.ibatis.annotations.Param;
import org.sonar.core.component.ComponentDto;
-import javax.annotation.CheckForNull;
-
import java.util.List;
/**
*/
public interface ComponentMapper {
- @CheckForNull
ComponentDto selectByKey(String key);
- @CheckForNull
ComponentDto selectById(long id);
- @CheckForNull
ComponentDto selectRootProjectByKey(String key);
- @CheckForNull
ComponentDto selectParentModuleByKey(String key);
/**