]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 Fix issue on move up/down
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 20 Mar 2014 16:27:12 +0000 (17:27 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 20 Mar 2014 16:27:20 +0000 (17:27 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/server/debt/DebtModel.java
sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtCharacteristic.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelLookup.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelOperations.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelLookupTest.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelOperationsTest.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java

index e2bd93cba6ce4cbe7fc8629e0e3ee1a6ad441cbe..cfc8f323cbab03405ce9c9782acdb4c0ca2b3028 100644 (file)
@@ -29,10 +29,16 @@ import java.util.List;
  */
 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);
 
 }
index 9c531cdaa24ed0bf2c14c8ce18dd170f0cca08d3..0ecf1a5bf3e6a82ad8b9d74163c57e83eaabf23c 100644 (file)
@@ -20,6 +20,8 @@
 
 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;
@@ -109,4 +111,9 @@ public class DefaultDebtCharacteristic implements DebtCharacteristic {
     return this;
   }
 
+  @Override
+  public String toString() {
+    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+  }
+
 }
index aa79358d0ad2658ef5198043ce10eb2dee3f2750..da97cadd0d3905af8249bff418719429c51a68c0 100644 (file)
@@ -47,7 +47,7 @@ public class DebtModelLookup implements ServerComponent {
     return toCharacteristics(dao.selectEnabledRootCharacteristics());
   }
 
-  public List<DebtCharacteristic> characteristics() {
+  public List<DebtCharacteristic> allCharacteristics() {
     return toCharacteristics(dao.selectEnabledCharacteristics());
   }
 
index 871f777a894e986da560fc30caf883e7d2d24d46..4b7c60047462dece9ee906b733895d64b40e532d 100644 (file)
@@ -21,6 +21,8 @@
 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;
@@ -126,14 +128,25 @@ public class DebtModelOperations implements ServerComponent {
 
     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()));
index c21e1d16b8a5f9a54f8b841f1c9ef44d4777851a..483f4fa5c28b10ae00fad50402d77f5e45a4e731 100644 (file)
@@ -44,12 +44,12 @@ public class DebtModelService implements DebtModel {
     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
index 6c72ca00e313322c3827050d66ea62f8f5d80a7b..eedc79f38affdef8e3a59042e10a6646834aff52 100644 (file)
@@ -60,9 +60,9 @@ public class DebtModelLookupTest {
   }
 
   @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
index cc8400499a1c9ea90e600b9bd5f72a6944eb3151..f3ab22fe6b3bc7709995a0abfcbecdc963f8d155 100644 (file)
@@ -108,7 +108,7 @@ public class DebtModelOperationsTest {
     }).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
@@ -234,8 +234,11 @@ public class DebtModelOperationsTest {
 
   @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);
 
@@ -251,9 +254,11 @@ public class DebtModelOperationsTest {
 
   @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);
 
@@ -262,8 +267,11 @@ public class DebtModelOperationsTest {
 
   @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);
 
@@ -279,9 +287,11 @@ public class DebtModelOperationsTest {
 
   @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);
 
index e02bb0133a3aa6a8a2ce8809619f4a198966c33f..9c044e6508af258d5bfed03d0165326b0f9d82c6 100644 (file)
@@ -48,14 +48,14 @@ public class DebtModelServiceTest {
 
   @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