]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1352 update backup with new alert period property
authorJulien Lancelot <julien.lancelot@gmail.com>
Wed, 28 Nov 2012 17:51:20 +0000 (18:51 +0100)
committerJulien Lancelot <julien.lancelot@gmail.com>
Wed, 28 Nov 2012 17:51:39 +0000 (18:51 +0100)
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/CheckAlertThresholdsTest.java
sonar-server/src/main/java/org/sonar/server/configuration/ProfilesBackup.java
sonar-server/src/test/java/org/sonar/server/configuration/BackupTest.java
sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-restore-valid.xml
sonar-server/src/test/resources/org/sonar/server/configuration/BackupTest/backup-valid.xml

index d79934da594a988692cfe04ebad2b1080a03b162..90d1ce756e5cc64f745aa5a59fd1e7bca7de1bf7 100644 (file)
@@ -66,6 +66,7 @@ public class CheckAlertThresholdsTest {
     context = mock(DecoratorContext.class);
     periods = mock(Periods.class);
     i18n = mock(I18n.class);
+    when(i18n.message(Mockito.any(Locale.class), Mockito.eq("variation"), Mockito.isNull(String.class))).thenReturn("variation");
 
     measureClasses = new Measure(CoreMetrics.CLASSES, 20d);
     measureCoverage = new Measure(CoreMetrics.COVERAGE, 35d);
@@ -242,7 +243,6 @@ public class CheckAlertThresholdsTest {
     measureClasses.setVariation1(40d);
 
     when(i18n.message(Mockito.any(Locale.class), Mockito.eq("metric.classes.name"), Mockito.isNull(String.class))).thenReturn("Classes");
-    when(i18n.message(Mockito.any(Locale.class), Mockito.eq("variation"), Mockito.isNull(String.class))).thenReturn("variation");
     when(periods.getLabel(1)).thenReturn("since someday");
 
     when(profile.getAlerts()).thenReturn(Arrays.asList(
index 804f6cc54eba4314e434776a09877a626919d165..c15ec5b0f30d1e7c9d842f383286b83f8983b9d5 100644 (file)
@@ -26,15 +26,25 @@ import com.thoughtworks.xstream.converters.UnmarshallingContext;
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 import org.apache.commons.collections.CollectionUtils;
+import org.codehaus.plexus.util.StringUtils;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.database.DatabaseSession;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.profiles.Alert;
 import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.rules.*;
+import org.sonar.api.rules.ActiveRule;
+import org.sonar.api.rules.ActiveRuleParam;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleParam;
+import org.sonar.api.rules.RulePriority;
 import org.sonar.jpa.dao.RulesDao;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 
 public class ProfilesBackup implements Backupable {
 
@@ -161,13 +171,19 @@ public class ProfilesBackup implements Backupable {
         writeNode(writer, "operator", alert.getOperator());
         writeNode(writer, "value-error", alert.getValueError());
         writeNode(writer, "value-warning", alert.getValueWarning());
+        writeNode(writer, "period", alert.getPeriod() != null ? Integer.toString(alert.getPeriod()) : "");
         writeNode(writer, "metric-key", alert.getMetric().getKey());
       }
 
       public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
         Map<String, String> values = readNode(reader);
-        return new Alert(null, new Metric(values.get("metric-key")), values.get("operator"), values.get("value-error"),
+        Alert alert = new Alert(null, new Metric(values.get("metric-key")), values.get("operator"), values.get("value-error"),
             values.get("value-warning"));
+        String periodText = values.get("period");
+        if (StringUtils.isNotEmpty(periodText)) {
+          alert.setPeriod(Integer.parseInt(periodText));
+        }
+        return alert;
       }
 
       public boolean canConvert(Class type) {
index 13ac73eb6eb39e7499f134f0510c2ade09b28cfe..bbb88cd763336c00a3a06439614c73d14fbdfcc3 100644 (file)
@@ -31,18 +31,30 @@ import org.sonar.api.database.configuration.Property;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.profiles.Alert;
 import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.rules.*;
+import org.sonar.api.rules.ActiveRule;
+import org.sonar.api.rules.ActiveRuleParam;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleParam;
+import org.sonar.api.rules.RulePriority;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
 
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
@@ -133,14 +145,23 @@ public class BackupTest {
     assertNotNull(testActiveRuleParam.getRuleParam());
     assertEquals("test param key", testActiveRuleParam.getRuleParam().getKey());
 
-    assertEquals(1, testProfile.getAlerts().size());
+    assertEquals(2, testProfile.getAlerts().size());
     Alert testAlert = testProfile.getAlerts().get(0);
     assertEquals(Alert.OPERATOR_GREATER, testAlert.getOperator());
     assertEquals("testError", testAlert.getValueError());
     assertEquals("testWarn", testAlert.getValueWarning());
+    assertThat(testAlert.getPeriod(), nullValue());
     assertNotNull(testAlert.getMetric());
     assertEquals("test key", testAlert.getMetric().getKey());
 
+    Alert testAlert2 = testProfile.getAlerts().get(1);
+    assertEquals(Alert.OPERATOR_SMALLER, testAlert2.getOperator());
+    assertEquals("testError2", testAlert2.getValueError());
+    assertEquals("testWarn2", testAlert2.getValueWarning());
+    assertThat(testAlert2.getPeriod(), is(1));
+    assertNotNull(testAlert2.getMetric());
+    assertEquals("test key2", testAlert2.getMetric().getKey());
+
     // Child profile
     testProfile = profilesIter.next();
     assertEquals("test2 name", testProfile.getName());
@@ -150,6 +171,7 @@ public class BackupTest {
 
     Collection<Rule> rules = sonarConfig.getRules();
     assertThat(rules.size(), is(1));
+
     Rule rule = rules.iterator().next();
     assertThat(rule.getParent().getRepositoryKey(), is("test plugin"));
     assertThat(rule.getParent().getKey(), is("test key"));
@@ -160,6 +182,7 @@ public class BackupTest {
     assertThat(rule.getDescription(), is("test description"));
     assertThat(rule.getSeverity(), is(RulePriority.INFO));
     assertThat(rule.getParams().size(), is(1));
+
     RuleParam param = rule.getParams().get(0);
     assertThat(param.getKey(), is("test param key"));
     assertThat(param.getDefaultValue(), is("test param value"));
@@ -282,7 +305,12 @@ public class BackupTest {
     activeRule2.setParameter("test param key", "test value");
     activeRule2.setInheritance(ActiveRule.OVERRIDES);
 
-    profiles.get(0).getAlerts().add(new Alert(null, new Metric("test key"), Alert.OPERATOR_GREATER, "testError", "testWarn"));
+    Alert alert1 = new Alert(null, new Metric("test key"), Alert.OPERATOR_GREATER, "testError", "testWarn");
+    Alert alert2 = new Alert(null, new Metric("test key2"), Alert.OPERATOR_SMALLER, "testError2", "testWarn2", 1);
+
+    List<Alert> alerts = profiles.get(0).getAlerts();
+    alerts.add(alert1);
+    alerts.add(alert2);
 
     return profiles;
   }
index f61e6e67c4b19bcb32addb990cf5c9c94485c65d..ed5f21bb7686666a96bb4ca9644a0c4e3cb87f7b 100644 (file)
           <operator><![CDATA[>]]></operator>
           <value-error><![CDATA[testError]]></value-error>
           <value-warning><![CDATA[testWarn]]></value-warning>
+          <period><![CDATA[]]></period>
           <metric-key><![CDATA[test key]]></metric-key>
         </alert>
+        <alert>
+          <operator><![CDATA[<]]></operator>
+          <value-error><![CDATA[testError2]]></value-error>
+          <value-warning><![CDATA[testWarn2]]></value-warning>
+          <period><![CDATA[1]]></period>
+          <metric-key><![CDATA[test key2]]></metric-key>
+        </alert>
       </alerts>
     </profile>
     <profile>
index 45ce8b1876bc753c035109503ab89d62b73d6d2d..8af076dc208a707868a73d61f17f394c1195928f 100644 (file)
           <operator><![CDATA[>]]></operator>
           <value-error><![CDATA[testError]]></value-error>
           <value-warning><![CDATA[testWarn]]></value-warning>
+          <period><![CDATA[]]></period>
           <metric-key><![CDATA[test key]]></metric-key>
         </alert>
+        <alert>
+          <operator><![CDATA[<]]></operator>
+          <value-error><![CDATA[testError2]]></value-error>
+          <value-warning><![CDATA[testWarn2]]></value-warning>
+          <period><![CDATA[1]]></period>
+          <metric-key><![CDATA[test key2]]></metric-key>
+        </alert>
       </alerts>
     </profile>
     <profile>