Skip to content

Commit

Permalink
SONAR-6610 selectByComponentId for Custom Measures
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Jun 5, 2015
1 parent c77ed68 commit 3c1bd7b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 15 deletions.
Expand Up @@ -56,6 +56,10 @@ public List<CustomMeasureDto> selectByMetricId(DbSession session, int id) {
return mapper(session).selectByMetricId(id); return mapper(session).selectByMetricId(id);
} }


public List<CustomMeasureDto> selectByComponentId(DbSession session, int id) {
return mapper(session).selectByComponentId(id);
}

private CustomMeasureMapper mapper(DbSession session) { private CustomMeasureMapper mapper(DbSession session) {
return session.getMapper(CustomMeasureMapper.class); return session.getMapper(CustomMeasureMapper.class);
} }
Expand Down
Expand Up @@ -21,6 +21,7 @@
package org.sonar.server.custommeasure.persistence; package org.sonar.server.custommeasure.persistence;


import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.ClassRule; import org.junit.ClassRule;
Expand All @@ -34,6 +35,7 @@


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.offset; import static org.assertj.core.api.Assertions.offset;
import static org.sonar.server.custommeasure.persistence.CustomMeasureTesting.newCustomMeasureDto;


@Category(DbTests.class) @Category(DbTests.class)
public class CustomMeasureDaoTest { public class CustomMeasureDaoTest {
Expand All @@ -58,14 +60,14 @@ public void tearDown() {


@Test @Test
public void insert() { public void insert() {
CustomMeasureDto measure = CustomMeasureTesting.newDto(); CustomMeasureDto measure = newCustomMeasureDto();


sut.insert(session, measure); sut.insert(session, measure);


CustomMeasureDto result = sut.selectNullableById(session, measure.getId()); CustomMeasureDto result = sut.selectNullableById(session, measure.getId());
assertThat(result.getId()).isEqualTo(measure.getId()); assertThat(result.getId()).isEqualTo(measure.getId());
assertThat(result.getMetricId()).isEqualTo(measure.getMetricId()); assertThat(result.getMetricId()).isEqualTo(measure.getMetricId());
assertThat(result.getResourceId()).isEqualTo(measure.getResourceId()); assertThat(result.getComponentId()).isEqualTo(measure.getComponentId());
assertThat(result.getDescription()).isEqualTo(measure.getDescription()); assertThat(result.getDescription()).isEqualTo(measure.getDescription());
assertThat(result.getUserLogin()).isEqualTo(measure.getUserLogin()); assertThat(result.getUserLogin()).isEqualTo(measure.getUserLogin());
assertThat(result.getTextValue()).isEqualTo(measure.getTextValue()); assertThat(result.getTextValue()).isEqualTo(measure.getTextValue());
Expand All @@ -76,12 +78,26 @@ public void insert() {


@Test @Test
public void delete() { public void delete() {
CustomMeasureDto measure = CustomMeasureTesting.newDto(); CustomMeasureDto measure = newCustomMeasureDto();
sut.insert(session, measure); sut.insert(session, measure);
assertThat(sut.selectNullableById(session, measure.getId())).isNotNull(); assertThat(sut.selectNullableById(session, measure.getId())).isNotNull();


sut.deleteByMetricIds(session, Arrays.asList(measure.getMetricId())); sut.deleteByMetricIds(session, Arrays.asList(measure.getMetricId()));


assertThat(sut.selectNullableById(session, measure.getId())).isNull(); assertThat(sut.selectNullableById(session, measure.getId())).isNull();
} }

@Test
public void select_by_component_id() {
sut.insert(session, newCustomMeasureDto().setComponentId(1));
sut.insert(session, newCustomMeasureDto().setComponentId(1));
sut.insert(session, newCustomMeasureDto().setComponentId(2));
session.commit();

List<CustomMeasureDto> result = sut.selectByComponentId(session, 1);

assertThat(result).hasSize(2);
assertThat(result).extracting("componentId").containsOnly(1);

}
} }
Expand Up @@ -30,14 +30,14 @@ private CustomMeasureTesting() {
// static stuff only // static stuff only
} }


public static CustomMeasureDto newDto() { public static CustomMeasureDto newCustomMeasureDto() {
return new CustomMeasureDto() return new CustomMeasureDto()
.setDescription(RandomStringUtils.randomAlphanumeric(255)) .setDescription(RandomStringUtils.randomAlphanumeric(255))
.setTextValue(RandomStringUtils.randomAlphanumeric(255)) .setTextValue(RandomStringUtils.randomAlphanumeric(255))
.setUserLogin(RandomStringUtils.randomAlphanumeric(255)) .setUserLogin(RandomStringUtils.randomAlphanumeric(255))
.setValue(RandomUtils.nextDouble()) .setValue(RandomUtils.nextDouble())
.setMetricId(RandomUtils.nextInt()) .setMetricId(RandomUtils.nextInt())
.setResourceId(RandomUtils.nextInt()) .setComponentId(RandomUtils.nextInt())
.setCreatedAt(System2.INSTANCE.now()) .setCreatedAt(System2.INSTANCE.now())
.setUpdatedAt(System2.INSTANCE.now()) .setUpdatedAt(System2.INSTANCE.now())
; ;
Expand Down
Expand Up @@ -204,7 +204,7 @@ public void fail_when_metric_type_is_changed_and_associated_measures_exist() thr
.setUserManaged(true) .setUserManaged(true)
.setEnabled(false); .setEnabled(false);
dbClient.metricDao().insert(dbSession, metric); dbClient.metricDao().insert(dbSession, metric);
dbClient.customMeasureDao().insert(dbSession, CustomMeasureTesting.newDto().setMetricId(metric.getId())); dbClient.customMeasureDao().insert(dbSession, CustomMeasureTesting.newCustomMeasureDto().setMetricId(metric.getId()));
dbSession.commit(); dbSession.commit();


newRequest() newRequest()
Expand Down
Expand Up @@ -117,8 +117,8 @@ public void do_not_delete_non_custom_metric() throws Exception {
public void delete_associated_measures() throws Exception { public void delete_associated_measures() throws Exception {
MetricDto metric = newCustomEnabledMetric(1); MetricDto metric = newCustomEnabledMetric(1);
metricDao.insert(dbSession, metric); metricDao.insert(dbSession, metric);
CustomMeasureDto customMeasure = CustomMeasureTesting.newDto().setMetricId(metric.getId()); CustomMeasureDto customMeasure = CustomMeasureTesting.newCustomMeasureDto().setMetricId(metric.getId());
CustomMeasureDto undeletedCustomMeasure = CustomMeasureTesting.newDto().setMetricId(metric.getId() + 1); CustomMeasureDto undeletedCustomMeasure = CustomMeasureTesting.newCustomMeasureDto().setMetricId(metric.getId() + 1);
dbClient.customMeasureDao().insert(dbSession, customMeasure); dbClient.customMeasureDao().insert(dbSession, customMeasure);
dbClient.customMeasureDao().insert(dbSession, undeletedCustomMeasure); dbClient.customMeasureDao().insert(dbSession, undeletedCustomMeasure);
dbSession.commit(); dbSession.commit();
Expand Down
Expand Up @@ -23,7 +23,7 @@
public class CustomMeasureDto { public class CustomMeasureDto {
private long id; private long id;
private int metricId; private int metricId;
private int resourceId; private int componentId;
private double value; private double value;
private String textValue; private String textValue;
private String userLogin; private String userLogin;
Expand Down Expand Up @@ -67,12 +67,12 @@ public CustomMeasureDto setValue(double value) {
return this; return this;
} }


public int getResourceId() { public int getComponentId() {
return resourceId; return componentId;
} }


public CustomMeasureDto setResourceId(int resourceId) { public CustomMeasureDto setComponentId(int resourceId) {
this.resourceId = resourceId; this.componentId = resourceId;
return this; return this;
} }


Expand Down
Expand Up @@ -31,4 +31,7 @@ public interface CustomMeasureMapper {
CustomMeasureDto selectById(long id); CustomMeasureDto selectById(long id);


List<CustomMeasureDto> selectByMetricId(int id); List<CustomMeasureDto> selectByMetricId(int id);

List<CustomMeasureDto> selectByComponentId(int id);

} }
Expand Up @@ -5,7 +5,7 @@
<sql id="selectColumns"> <sql id="selectColumns">
m.id, m.id,
m.metric_id as metricId, m.metric_id as metricId,
m.resource_id as resourceId, m.resource_id as componentId,
m.value, m.value,
m.text_value as textValue, m.text_value as textValue,
m.user_login as userLogin, m.user_login as userLogin,
Expand All @@ -28,12 +28,19 @@
where m.metric_id=#{metricId} where m.metric_id=#{metricId}
</select> </select>


<select id="selectByComponentId" resultType="CustomMeasure">
select
<include refid="selectColumns"/>
from manual_measures m
where m.resource_id=#{componentId}
</select>

<insert id="insert" parameterType="CustomMeasure" useGeneratedKeys="true" keyColumn="id" keyProperty="id"> <insert id="insert" parameterType="CustomMeasure" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO manual_measures ( INSERT INTO manual_measures (
metric_id, resource_id, value, text_value, user_login, description, created_at, updated_at metric_id, resource_id, value, text_value, user_login, description, created_at, updated_at
) )
VALUES ( VALUES (
#{metricId, jdbcType=INTEGER}, #{resourceId, jdbcType=INTEGER}, #{value, jdbcType=DOUBLE}, #{metricId, jdbcType=INTEGER}, #{componentId, jdbcType=INTEGER}, #{value, jdbcType=DOUBLE},
#{textValue, jdbcType=VARCHAR}, #{userLogin, jdbcType=VARCHAR},#{description, jdbcType=VARCHAR}, #{textValue, jdbcType=VARCHAR}, #{userLogin, jdbcType=VARCHAR},#{description, jdbcType=VARCHAR},
#{createdAt, jdbcType=BIGINT}, #{updatedAt, jdbcType=BIGINT} #{createdAt, jdbcType=BIGINT}, #{updatedAt, jdbcType=BIGINT}
) )
Expand Down

0 comments on commit 3c1bd7b

Please sign in to comment.