]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5237 - Updated MediumTest and started on RuleService
authorStephane Gamard <stephane.gamard@searchbox.com>
Tue, 29 Apr 2014 15:34:36 +0000 (17:34 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Tue, 29 Apr 2014 15:45:38 +0000 (17:45 +0200)
sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java
sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java
sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java
sonar-server/src/test/java/org/sonar/server/rule2/RuleMediumTest.java

index 03941f200fffd50796f9d91a55dc6b647d72fc03..c096b145ec283b0785d19f62edd7ff26b9f0afe1 100644 (file)
@@ -28,6 +28,8 @@ import java.util.List;
 
 public class RuleImpl implements Rule {
 
+
+
   @Override
   public RuleKey key() {
     // TODO Auto-generated method stub
index 30372871c56948a2b9c2e8b118a6e2ae1fed4b67..6d6fd7e0036f6f29a53ae75daa0a220b916d861e 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.server.rule2;
 
 import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-
 import org.sonar.core.qualityprofile.db.ActiveRuleDao;
 import org.apache.commons.beanutils.BeanUtils;
 import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -92,30 +91,32 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> {
     }
   }
 
+  private void addMatchField(XContentBuilder mapping, String field, String type) throws IOException {
+    mapping.startObject(field)
+      .field("type", type)
+      .field("index", "not_analyzed")
+      .endObject();
+  }
+
   @Override
   protected XContentBuilder getMapping() {
     try {
-      return jsonBuilder().startObject()
+      XContentBuilder mapping = jsonBuilder().startObject()
         .startObject(this.getType())
-        .field("dynamic",true)
-        .startObject("properties")
-        .startObject("id")
-        .field("type", "string")
-        .field("index", "not_analyzed")
-        .endObject()
-        .startObject("key")
-        .field("type", "string")
-        .field("index", "not_analyzed")
-        .endObject()
-        .startObject("repositoryKey")
-        .field("type", "string")
-        .field("index", "not_analyzed")
-        .endObject()
-        .startObject("severity")
-        .field("type", "string")
-        .field("index", "not_analyzed")
-        .endObject()
-        .endObject()
+        .field("dynamic", true)
+        .startObject("properties");
+
+      addMatchField(mapping, "id", "string");
+      addMatchField(mapping, "key", "string");
+      addMatchField(mapping, "repositoryKey", "string");
+      addMatchField(mapping, "severity", "string");
+
+      mapping.startObject("active")
+        .field("type", "nested")
+        .field("dynamic", true)
+        .endObject();
+
+      return mapping.endObject()
         .endObject().endObject();
 
       // return jsonBuilder().startObject()
@@ -154,41 +155,52 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> {
   @Override
   public XContentBuilder normalize(RuleKey key) {
 
-    RuleDto rule = dao.getByKey(key);
-
     try {
 
+      long start = System.currentTimeMillis();
+      RuleDto rule = dao.getByKey(key);
+      LOG.info("Action dao.getByKey(key) in {} took {}ms",
+        this.getIndexName(), (System.currentTimeMillis() - start));
+
       XContentBuilder document = jsonBuilder().startObject();
 
       Map<String, Object> properties = BeanUtils.describe(rule);
 
       for (Entry<String, Object> property : properties.entrySet()) {
-        LOG.info("NORMALIZING: {} -> {}",property.getKey(), property.getValue());
+        LOG.trace("NORMALIZING: {} -> {}", property.getKey(), property.getValue());
         document.field(property.getKey(), property.getValue());
       }
 
+      start = System.currentTimeMillis();
 
-      for(ActiveRuleDto activeRule:activeRuleDao.selectByRuleId(rule.getId())){
+      document.startArray("active");
+      for (ActiveRuleDto activeRule : activeRuleDao.selectByRuleId(rule.getId())) {
+        document.startObject();
         Map<String, Object> activeRuleProperties = BeanUtils.describe(activeRule);
         for (Entry<String, Object> activeRuleProp : activeRuleProperties.entrySet()) {
-          LOG.info("NORMALIZING: --- {} -> {}",activeRuleProp.getKey(), activeRuleProp.getValue());
+          LOG.trace("NORMALIZING: --- {} -> {}", activeRuleProp.getKey(), activeRuleProp.getValue());
           document.field(activeRuleProp.getKey(), activeRuleProp.getValue());
         }
+        document.endObject();
       }
+      document.endArray();
+
+      LOG.info("Action activeRuleDao.selectByRuleId(rule.getId()) in {} took {}ms",
+        this.getIndexName(), (System.currentTimeMillis() - start));
 
       return document.endObject();
     } catch (IOException e) {
-      LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
-      e.printStackTrace();
+      LOG.error("Could not normalize {} in {} because {}",
+        key, this.getClass().getSimpleName(), e.getMessage());
     } catch (IllegalAccessException e) {
-      LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
-      e.printStackTrace();
+      LOG.error("Could not normalize {} in {} because {}",
+        key, this.getClass().getSimpleName(), e.getMessage());
     } catch (InvocationTargetException e) {
-      LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
-      e.printStackTrace();
+      LOG.error("Could not normalize {} in {} because {}",
+        key, this.getClass().getSimpleName(), e.getMessage());
     } catch (NoSuchMethodException e) {
-      LOG.error("Could not normalize {} in {}", key, this.getClass().getSimpleName());
-      e.printStackTrace();
+      LOG.error("Could not normalize {} in {} because {}",
+        key, this.getClass().getSimpleName(), e.getMessage());
     }
     return null;
   }
index dfd0835e3cf34d4f0c3bc4d00cd8ebeb83680f48..488af9ece1f0e874f710c83adc06b1b324e7f224 100644 (file)
@@ -19,6 +19,9 @@
  */
 package org.sonar.server.rule2;
 
+import org.apache.commons.beanutils.BeanUtils;
+
+import org.sonar.api.ServerComponent;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.core.rule.RuleDao;
 import org.sonar.core.rule.RuleDto;
@@ -32,7 +35,7 @@ import java.util.Collections;
 /**
  * @since 4.4
  */
-public class RuleService {
+public class RuleService implements ServerComponent {
 
   private RuleDao dao;
   private RuleIndex index;
@@ -44,10 +47,16 @@ public class RuleService {
 
   @CheckForNull
   public Rule getByKey(RuleKey key) {
-    return null;
+    Hit hit = index.getByKey(key);
+    if(hit != null){
+      return toRule(hit);
+    } else {
+      return null;
+    }
   }
 
   public Collection<Hit> search(RuleQuery query){
+
     return Collections.emptyList();
   }
 
@@ -56,6 +65,8 @@ public class RuleService {
   }
 
   public static Rule toRule(Hit hit){
+    for()
+    BeanUtils.setProperty(bean, name, value);
     return new RuleImpl();
   }
 }
index 41d3f732d17c209fe7f7524d9acbb7e7aa4a434c..727246c4689f5942a70edd97f3bb7d927ad271b0 100644 (file)
@@ -25,6 +25,8 @@ import org.elasticsearch.action.delete.DeleteResponse;
 import org.elasticsearch.action.get.GetResponse;
 import org.elasticsearch.action.index.IndexRequest;
 import org.elasticsearch.action.index.IndexResponse;
+import org.elasticsearch.action.update.UpdateRequest;
+import org.elasticsearch.action.update.UpdateResponse;
 import org.elasticsearch.client.Client;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.slf4j.Logger;
@@ -104,25 +106,25 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
   public void connect() {
     this.client = this.node.client();
-//    /* Settings to access our local ES node */
-//    Settings settings = ImmutableSettings.settingsBuilder()
-//      .put("client.transport.sniff", true)
-//      .put("cluster.name", ES_CLUSTER_NAME)
-//      .put("node.name", "localclient_")
-//      .build();
-//
-//    this.client = new TransportClient(settings)
-//      .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT));
-//
-//    /*
-//     * Cannot do that yet, need version >= 1.0
-//     * ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
-//     * if (nodes.isEmpty()) {
-//     * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!");
-//     * } else {
-//     * log.info("connected to nodes: " + nodes.toString());
-//     * }
-//     */
+    // /* Settings to access our local ES node */
+    // Settings settings = ImmutableSettings.settingsBuilder()
+    // .put("client.transport.sniff", true)
+    // .put("cluster.name", ES_CLUSTER_NAME)
+    // .put("node.name", "localclient_")
+    // .build();
+    //
+    // this.client = new TransportClient(settings)
+    // .addTransportAddress(new InetSocketTransportAddress(LOCAL_ES_NODE_HOST, LOCAL_ES_NODE_PORT));
+    //
+    // /*
+    // * Cannot do that yet, need version >= 1.0
+    // * ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
+    // * if (nodes.isEmpty()) {
+    // * throw new ElasticSearchUnavailableException("No nodes available. Verify ES is running!");
+    // * } else {
+    // * log.info("connected to nodes: " + nodes.toString());
+    // * }
+    // */
   }
 
   /* Cluster And ES Stats/Client methods */
@@ -136,10 +138,10 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
     if (!indexExistsResponse.isExists()) {
 
-      LOG.info("Setup of index {}",this.getIndexName());
+      LOG.info("Setup of index {}", this.getIndexName());
 
       try {
-        LOG.info("Settings: {}",getIndexSettings().string());
+        LOG.info("Settings: {}", getIndexSettings().string());
         LOG.info("Mapping: {}", getMapping().string());
       } catch (IOException e) {
         // TODO Auto-generated catch block
@@ -166,6 +168,8 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
   @Override
   public void executeAction(IndexAction<K> action) {
+    StopWatch watch = this.createWatch();
+    long start = System.currentTimeMillis();
     if (action.getMethod().equals(Method.DELETE)) {
       this.delete(action.getKey());
     } else if (action.getMethod().equals(Method.INSERT)) {
@@ -173,6 +177,9 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
     } else if (action.getMethod().equals(Method.UPDATE)) {
       this.update(action.getKey());
     }
+    LOG.info("Action {} in {} took {}ms", action.getMethod(),
+      this.getIndexName(), (System.currentTimeMillis() - start));
+    watch.stop("Action {} in {}", action.getMethod(), this.getIndexName());
   }
 
   /* Index management methods */
@@ -196,18 +203,37 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
   @Override
   public void insert(K key) {
-    this.update(key);
+    try {
+      XContentBuilder doc = this.normalize(key);
+      String keyvalue = this.getKeyValue(key);
+      if (doc != null && keyvalue != null && !keyvalue.isEmpty()) {
+        LOG.info("Update document with key {}", key);
+        IndexResponse result = getClient().index(
+          new IndexRequest(this.getIndexName(),
+            this.getType(), keyvalue)
+            .source(this.normalize(key))).get();
+      } else {
+        LOG.error("Could not normalize document {} for insert in ",
+          key, this.getIndexName());
+      }
+    } catch (Exception e) {
+      LOG.error("Could not update documet for index {}: {}",
+        this.getIndexName(), e.getMessage());
+    }
   }
 
   @Override
   public void update(K key) {
-    LOG.info("Update document with key {}", key);
-    IndexResponse result = getClient().index(new IndexRequest()
-      .type(this.getType())
-      .index(this.getIndexName())
-      .id(this.getKeyValue(key))
-      .source(this.normalize(key))).actionGet();
-
+    try {
+      LOG.info("Update document with key {}", key);
+      UpdateResponse result = getClient().update(
+        new UpdateRequest(this.getIndexName(),
+          this.getType(), this.getKeyValue(key))
+          .doc(this.normalize(key))).get();
+    } catch (Exception e) {
+      LOG.error("Could not update documet for index {}: {}",
+        this.getIndexName(), e.getMessage());
+    }
   }
 
   @Override
@@ -236,7 +262,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem
 
   @Override
   public Long getLastSynchronization() {
-    // need to read that in the admin index;
+    // TODO need to read that in the admin index;
     return 0l;
   }
 }
index f81399820cbd003fbaae7a350004cbbbb5f01c11..de48aaa602206f9d46ebfaabf8c88472ba23cfa4 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.rule2;
 
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.rule.Severity;
@@ -38,7 +39,18 @@ import static org.fest.assertions.Assertions.assertThat;
 public class RuleMediumTest {
 
   @Rule
-  public ServerTester tester = new ServerTester();
+  public ServerTester tester = new ServerTester()
+  //.setProperty("sonar.log.profilingLevel", "FULL")
+  .setProperty("sonar.es.http.port", "8888");
+
+  private RuleDto dto;
+  private ActiveRuleDto adto;
+
+  @Before
+  public void setup(){
+    dto = getRuleDto(1);
+    adto = getActiveRuleDto(dto);
+  }
 
   private ActiveRuleDto getActiveRuleDto(RuleDto dto){
     return new ActiveRuleDto()
@@ -51,10 +63,10 @@ public class RuleMediumTest {
       .setSeverity(3);
     }
 
-  private RuleDto getRuleDto() {
+  private RuleDto getRuleDto(int id) {
     return new RuleDto()
-      .setId(1)
-      .setRuleKey("NewRuleKey")
+      .setId(id)
+      .setRuleKey("NewRuleKey="+id)
       .setRepositoryKey("plugin")
       .setName("new name")
       .setDescription("new description")
@@ -79,17 +91,16 @@ public class RuleMediumTest {
 
   @Test
   public void test_dao_queue_es_search_loop() {
+
     RuleDao dao = tester.get(RuleDao.class);
     ActiveRuleDao adao = tester.get(ActiveRuleDao.class);
     RuleIndex index = tester.get(RuleIndex.class);
 
-    RuleDto dto = getRuleDto();
     dao.insert(dto);
-
-    adao.insert(getActiveRuleDto(dto));
+    adao.insert(adto);
 
     try {
-      Thread.sleep(100);
+      Thread.sleep(200);
     } catch (InterruptedException e) {
       ;
     }
@@ -97,4 +108,18 @@ public class RuleMediumTest {
     Hit hit = index.getByKey(dto.getKey());
     assertThat(hit.getFields().get("ruleKey")).isEqualTo(dto.getRuleKey());
   }
+
+  @Test
+  public void test_ruleservice_getByKey() {
+    RuleService service = tester.get(RuleService.class);
+    RuleDao dao = tester.get(RuleDao.class);
+
+    dao.insert(dto);
+
+    //org.sonar.server.rule2.Rule rule = service.getByKey(dto.getKey());
+
+    //assertThat(rule.key()).isEqualTo(dto.getKey());
+
+
+  }
 }