Browse Source

Fix query to check that project exists in Gality Gate

tags/4.4-RC1
Julien Lancelot 10 years ago
parent
commit
8070dafc93

+ 1
- 5
sonar-core/src/main/java/org/sonar/core/component/db/ComponentMapper.java View File

@@ -19,11 +19,7 @@
*/
package org.sonar.core.component.db;

import org.apache.ibatis.annotations.Param;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentQuery;

import java.util.Collection;

/**
* @since 4.3
@@ -34,5 +30,5 @@ public interface ComponentMapper {

ComponentDto selectById(Long id);

Collection<ComponentDto> selectComponents(@Param("query") ComponentQuery query);
long countById(Long id);
}

+ 1
- 1
sonar-core/src/main/java/org/sonar/core/measure/db/MeasureMapper.java View File

@@ -30,6 +30,6 @@ public interface MeasureMapper {

List<MeasureDto> selectByComponentAndMetrics(@Param("componentKey") String componentKey, @Param("metricKeys") List<String> metricKeys);

long count(@Param("key") MeasureKey key);
long countByKey(@Param("key") MeasureKey key);

}

+ 6
- 42
sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml View File

@@ -13,31 +13,6 @@
p.path as path
</sql>

<sql id="sortColumn">
<if test="query.sort() != null">,
<choose>
<when test="'SEVERITY'.equals(query.sort())">
p.severity as severity
</when>
<when test="'STATUS'.equals(query.sort())">
p.status as status
</when>
<when test="'ASSIGNEE'.equals(query.sort())">
p.assignee as assignee
</when>
<when test="'CREATION_DATE'.equals(query.sort())">
p.issue_creation_date as issueCreationDate
</when>
<when test="'UPDATE_DATE'.equals(query.sort())">
p.issue_update_date as issueUpdateDate
</when>
<when test="'CLOSE_DATE'.equals(query.sort())">
p.issue_close_date as issueCloseDate
</when>
</choose>
</if>
</sql>

<select id="selectByKey" parameterType="String" resultType="Component">
SELECT <include refid="componentColumns"/>
FROM projects p
@@ -58,25 +33,14 @@
</where>
</select>

<select id="selectComponents" parameterType="map" resultType="Issue">
select <include refid="componentColumns"/>
from projects p
INNER JOIN snapshots s on s.project_id=p.id and s.islast=${_true}
<include refid="selectQueryConditions"/>
</select>

<sql id="selectQueryConditions">
<select id="countById" parameterType="Long" resultType="long">
SELECT count(p.id)
FROM projects p
<where>
<if test="query.ids().size()>0">
and <foreach item="id" index="index" collection="query.ids()" open="(" separator=" or " close=")">p.id=#{id}
</foreach>
</if>
<if test="query.qualifiers().size()>0">
and <foreach item="qualifier" index="index" collection="query.qualifiers()" open="(" separator=" or " close=")">p.qualifier=#{qualifier}
</foreach>
</if>
AND p.enabled=${_true}
AND p.id=#{id}
</where>
</sql>
</select>

</mapper>


+ 1
- 1
sonar-core/src/main/resources/org/sonar/core/measure/db/MeasureMapper.xml View File

@@ -45,7 +45,7 @@
</where>
</select>

<select id="count" parameterType="map" resultType="long">
<select id="countByKey" parameterType="map" resultType="long">
SELECT count(pm.id)
FROM project_measures pm
INNER JOIN snapshots s ON s.id=pm.snapshot_id AND s.islast=${_true}

+ 2
- 5
sonar-server/src/main/java/org/sonar/server/component/persistence/ComponentDao.java View File

@@ -25,13 +25,10 @@ import org.sonar.api.DaoComponent;
import org.sonar.api.ServerComponent;
import org.sonar.api.utils.System2;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentQuery;
import org.sonar.core.component.db.ComponentMapper;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.db.BaseDao;

import java.util.Collection;

/**
* @since 4.3
*/
@@ -50,8 +47,8 @@ public class ComponentDao extends BaseDao<ComponentMapper, ComponentDto, String>
return getMapper(session).selectById(id);
}

public Collection<ComponentDto> findByQuery(ComponentQuery query, SqlSession session) {
return getMapper(session).selectComponents(query);
public boolean existsById(Long id, SqlSession session) {
return getMapper(session).countById(id) > 0;
}

private ComponentMapper getMapper(SqlSession session) {

+ 2
- 2
sonar-server/src/main/java/org/sonar/server/measure/persistence/MeasureDao.java View File

@@ -52,8 +52,8 @@ public class MeasureDao extends BaseDao<MeasureMapper, MeasureDto, MeasureKey> i
return session.getMapper(MeasureMapper.class).selectByKey(key);
}

public boolean exists(MeasureKey key, DbSession session) {
return session.getMapper(MeasureMapper.class).count(key) > 0;
public boolean existsByKey(MeasureKey key, DbSession session) {
return session.getMapper(MeasureMapper.class).countByKey(key) > 0;
}

public List<MeasureDto> findByComponentKeyAndMetricKeys(String componentKey, List<String> metricKeys, DbSession session){

+ 1
- 3
sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java View File

@@ -29,8 +29,6 @@ import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metric.ValueType;
import org.sonar.api.measures.MetricFinder;
import org.sonar.api.resources.Qualifiers;
import org.sonar.core.component.ComponentQuery;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
@@ -351,7 +349,7 @@ public class QualityGates {
}

private void checkNonNullProject(long projectId, DbSession session) {
if (componentDao.findByQuery(ComponentQuery.create().addIds(projectId).addQualifiers(Qualifiers.PROJECT), session).isEmpty()) {
if (!componentDao.existsById(projectId, session)) {
throw new NotFoundException("There is no project with id=" + projectId);
}
}

+ 1
- 1
sonar-server/src/main/java/org/sonar/server/source/SourceService.java View File

@@ -83,7 +83,7 @@ public class SourceService implements ServerComponent {
}

public boolean hasScmData(String fileKey, DbSession session) {
return measureDao.exists(MeasureKey.of(fileKey, CoreMetrics.SCM_AUTHORS_BY_LINE_KEY), session);
return measureDao.existsByKey(MeasureKey.of(fileKey, CoreMetrics.SCM_AUTHORS_BY_LINE_KEY), session);
}

public boolean hasScmData(String fileKey) {

+ 4
- 9
sonar-server/src/test/java/org/sonar/server/component/persistence/ComponentDaoTest.java View File

@@ -22,9 +22,7 @@ package org.sonar.server.component.persistence;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.Qualifiers;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentQuery;
import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.DbSession;

@@ -88,13 +86,10 @@ public class ComponentDaoTest extends AbstractDaoTestCase {
}

@Test
public void find_by_query() {
public void count_by_id() {
setupData("shared");
assertThat(dao.findByQuery(ComponentQuery.create(), session)).hasSize(4);
assertThat(dao.findByQuery(ComponentQuery.create().addIds(1L), session)).hasSize(1);
assertThat(dao.findByQuery(ComponentQuery.create().addQualifiers(Qualifiers.PROJECT), session)).hasSize(1);
assertThat(dao.findByQuery(ComponentQuery.create().addQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE), session)).hasSize(2);
assertThat(dao.findByQuery(ComponentQuery.create().addIds(1L).addQualifiers(Qualifiers.PROJECT), session)).hasSize(1);
assertThat(dao.findByQuery(ComponentQuery.create().addIds(1L).addQualifiers(Qualifiers.MODULE), session)).hasSize(0);

assertThat(dao.existsById(4L, session)).isTrue();
assertThat(dao.existsById(111L, session)).isFalse();
}
}

+ 3
- 3
sonar-server/src/test/java/org/sonar/server/measure/persistence/MeasureDaoTest.java View File

@@ -95,10 +95,10 @@ public class MeasureDaoTest extends AbstractDaoTestCase {
}

@Test
public void exists() throws Exception {
public void exists_by_key() throws Exception {
setupData("shared");

assertThat(dao.exists(MeasureKey.of("org.struts:struts-core:src/org/struts/RequestContext.java", "ncloc"), session)).isTrue();
assertThat(dao.exists(MeasureKey.of("org.struts:struts-core:src/org/struts/RequestContext.java", "unknown"), session)).isFalse();
assertThat(dao.existsByKey(MeasureKey.of("org.struts:struts-core:src/org/struts/RequestContext.java", "ncloc"), session)).isTrue();
assertThat(dao.existsByKey(MeasureKey.of("org.struts:struts-core:src/org/struts/RequestContext.java", "unknown"), session)).isFalse();
}
}

+ 5
- 9
sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java View File

@@ -34,8 +34,6 @@ import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metric.ValueType;
import org.sonar.api.measures.MetricFinder;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentQuery;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
@@ -493,10 +491,9 @@ public class QualityGatesTest {
Long qGateId = 42L;
Long projectId = 24L;
when(dao.selectById(qGateId)).thenReturn(new QualityGateDto().setId(qGateId));
when(componentDao.findByQuery(any(ComponentQuery.class), eq(session))).thenReturn(ImmutableList.of(new ComponentDto().setId(projectId)));
qGates.associateProject(qGateId , projectId);
when(componentDao.existsById(projectId, session)).thenReturn(true);
qGates.associateProject(qGateId, projectId);
verify(dao).selectById(qGateId);
verify(componentDao).findByQuery(any(ComponentQuery.class), eq(session));
ArgumentCaptor<PropertyDto> propertyCaptor = ArgumentCaptor.forClass(PropertyDto.class);
verify(propertiesDao).setProperty(propertyCaptor.capture());
PropertyDto property = propertyCaptor.getValue();
@@ -506,7 +503,7 @@ public class QualityGatesTest {
}

@Test(expected = NotFoundException.class)
public void should_fail_associate_project_on_unexisting_project() {
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));
@@ -518,10 +515,9 @@ public class QualityGatesTest {
Long qGateId = 42L;
Long projectId = 24L;
when(dao.selectById(qGateId)).thenReturn(new QualityGateDto().setId(qGateId));
when(componentDao.findByQuery(any(ComponentQuery.class), eq(session))).thenReturn(ImmutableList.of(new ComponentDto().setId(projectId)));
qGates.dissociateProject(qGateId , projectId);
when(componentDao.existsById(projectId, session)).thenReturn(true);
qGates.dissociateProject(qGateId, projectId);
verify(dao).selectById(qGateId);
verify(componentDao).findByQuery(any(ComponentQuery.class), eq(session));
verify(propertiesDao).deleteProjectProperty("sonar.qualitygate", projectId);
}


+ 1
- 1
sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java View File

@@ -139,7 +139,7 @@ public class SourceServiceTest {
public void has_scm_data() throws Exception {
MockUserSession.set().addComponentPermission(UserRole.CODEVIEWER, PROJECT_KEY, COMPONENT_KEY);

when(measureDao.exists(any(MeasureKey.class), eq(session))).thenReturn(true);
when(measureDao.existsByKey(any(MeasureKey.class), eq(session))).thenReturn(true);
assertThat(service.hasScmData(COMPONENT_KEY)).isTrue();
}
}

Loading…
Cancel
Save