*/
public interface DebtModel extends ServerComponent {
+ /**
+ * @return all characteristics
+ */
+ List<DebtCharacteristic> allCharacteristics();
+
+ /**
+ * @return only characteristics of highest level
+ */
List<DebtCharacteristic> characteristics();
- List<DebtCharacteristic> rootCharacteristics();
-
DebtCharacteristic characteristicById(int id);
}
package org.sonar.api.server.debt.internal;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.server.debt.DebtCharacteristic;
import javax.annotation.CheckForNull;
return this;
}
+ @Override
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+ }
+
}
return toCharacteristics(dao.selectEnabledRootCharacteristics());
}
- public List<DebtCharacteristic> characteristics() {
+ public List<DebtCharacteristic> allCharacteristics() {
return toCharacteristics(dao.selectEnabledCharacteristics());
}
package org.sonar.server.debt;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.ServerComponent;
import org.sonar.api.server.debt.DebtCharacteristic;
SqlSession session = mybatis.openSession();
try {
- CharacteristicDto dto = findCharacteristic(characteristicId, session);
+ final CharacteristicDto dto = findCharacteristic(characteristicId, session);
int currentOrder = dto.getOrder();
- CharacteristicDto dtoToSwitchOrderWith = moveUpOrDown ? dao.selectPrevious(currentOrder, session) : dao.selectNext(currentOrder, session);
- // Do nothing when characteristic is already to the new location
- if (dtoToSwitchOrderWith == null) {
+ // characteristics should be order by 'order'
+ List<CharacteristicDto> rootCharacteristics = dao.selectEnabledRootCharacteristics(session);
+ int currentPosition = Iterables.indexOf(rootCharacteristics, new Predicate<CharacteristicDto>() {
+ @Override
+ public boolean apply(CharacteristicDto input) {
+ return input.getKey().equals(dto.getKey());
+ }
+ });
+ Integer nextMove = moveUpOrDown ? (currentPosition > 0 ? currentPosition - 1 : null) : (currentPosition < rootCharacteristics.size()-1 ? currentPosition + 1 : null);
+
+ // Do nothing when characteristic is already to the good location
+ if (nextMove == null) {
return toCharacteristic(dto);
}
+
+ CharacteristicDto dtoToSwitchOrderWith = Iterables.get(rootCharacteristics, nextMove);
int nextOrder = dtoToSwitchOrderWith.getOrder();
dtoToSwitchOrderWith.setOrder(currentOrder);
dtoToSwitchOrderWith.setUpdatedAt(new Date(system2.now()));
this.debtModelRestore = debtModelRestore;
}
- public List<DebtCharacteristic> rootCharacteristics() {
+ public List<DebtCharacteristic> characteristics() {
return debtModelLookup.rootCharacteristics();
}
- public List<DebtCharacteristic> characteristics() {
- return debtModelLookup.characteristics();
+ public List<DebtCharacteristic> allCharacteristics() {
+ return debtModelLookup.allCharacteristics();
}
@CheckForNull
}
@Test
- public void find_characteristics() {
+ public void find_all_characteristics() {
when(dao.selectEnabledCharacteristics()).thenReturn(newArrayList(characteristicDto));
- assertThat(service.characteristics()).hasSize(1);
+ assertThat(service.allCharacteristics()).hasSize(1);
}
@Test
}).when(dao).insert(any(CharacteristicDto.class), any(SqlSession.class));
when(mybatis.openSession()).thenReturn(session);
- service = new DebtModelOperations(mybatis, dao, ruleDao,system2);
+ service = new DebtModelOperations(mybatis, dao, ruleDao, system2);
}
@Test
@Test
public void move_up() {
- when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setOrder(2));
- when(dao.selectPrevious(2, session)).thenReturn(new CharacteristicDto().setId(2).setOrder(1));
+ when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2));
+ when(dao.selectEnabledRootCharacteristics(session)).thenReturn(newArrayList(
+ new CharacteristicDto().setId(2).setKey("PORTABILITY").setOrder(1),
+ new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2)
+ ));
DebtCharacteristic result = service.moveUp(10);
@Test
public void do_nothing_when_move_up_and_already_on_top() {
- CharacteristicDto dto = new CharacteristicDto().setId(10).setOrder(1);
- when(dao.selectById(10, session)).thenReturn(dto);
- when(dao.selectPrevious(1, session)).thenReturn(null);
+ when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(1));
+ when(dao.selectEnabledRootCharacteristics(session)).thenReturn(newArrayList(
+ new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(1),
+ new CharacteristicDto().setId(2).setKey("PORTABILITY").setOrder(2)
+ ));
service.moveUp(10);
@Test
public void move_down() {
- when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setOrder(2));
- when(dao.selectNext(2, session)).thenReturn(new CharacteristicDto().setId(2).setOrder(3));
+ when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2));
+ when(dao.selectEnabledRootCharacteristics(session)).thenReturn(newArrayList(
+ new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2),
+ new CharacteristicDto().setId(2).setKey("PORTABILITY").setOrder(3)
+ ));
DebtCharacteristic result = service.moveDown(10);
@Test
public void do_nothing_when_move_down_and_already_on_bottom() {
- CharacteristicDto dto = new CharacteristicDto().setId(10).setOrder(5);
- when(dao.selectById(10, session)).thenReturn(dto);
- when(dao.selectNext(5, session)).thenReturn(null);
+ when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2));
+ when(dao.selectEnabledRootCharacteristics(session)).thenReturn(newArrayList(
+ new CharacteristicDto().setId(2).setKey("PORTABILITY").setOrder(1),
+ new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2)
+ ));
service.moveDown(10);
@Test
public void find_root_characteristics() {
- service.rootCharacteristics();
+ service.characteristics();
verify(debtModelLookup).rootCharacteristics();
}
@Test
- public void find_characteristics() {
- service.characteristics();
- verify(debtModelLookup).characteristics();
+ public void find_all_characteristics() {
+ service.allCharacteristics();
+ verify(debtModelLookup).allCharacteristics();
}
@Test