diff options
author | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-05 18:59:16 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-05-05 20:21:50 +0200 |
commit | e668a7a2c56f280f15a7e6c94de75ddfe889ec77 (patch) | |
tree | 1458c11555faf60b84dc089cc75fa920fc8d3e88 /sonar-server | |
parent | eba452e84789165901b57d45d602666d8d3c243f (diff) | |
download | sonarqube-e668a7a2c56f280f15a7e6c94de75ddfe889ec77.tar.gz sonarqube-e668a7a2c56f280f15a7e6c94de75ddfe889ec77.zip |
Using BaseDao and NestedIndex for ActiveRules in ActiveRuleIndex
Diffstat (limited to 'sonar-server')
22 files changed, 634 insertions, 428 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java b/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java index 7a13d125ad0..665b798f51b 100644 --- a/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java +++ b/sonar-server/src/main/java/org/sonar/server/cluster/LocalQueueWorker.java @@ -44,7 +44,7 @@ public class LocalQueueWorker extends ThreadPoolExecutor /* Save all instances of Index<?> */ this.indexes = new HashMap<String, Index>(); for (Index index : allIndexes) { - this.indexes.put(index.getIndexName(), index); + this.indexes.put(index.getIndexType(), index); } } @@ -53,8 +53,8 @@ public class LocalQueueWorker extends ThreadPoolExecutor super.beforeExecute(t, r); if (IndexAction.class.isAssignableFrom(r.getClass())) { IndexAction ia = (IndexAction) r; - LOG.debug("Task is an IndexAction for {}", ia.getIndexName()); - ia.setIndex(indexes.get(ia.getIndexName())); + LOG.debug("Task is an IndexAction for {}", ia.getIndexType()); + ia.setIndex(indexes.get(ia.getIndexType())); } } diff --git a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java index 7e0ba1286bf..0c40a2e4b4c 100644 --- a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ b/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java @@ -25,6 +25,7 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.server.search.DtoIndexAction; import org.sonar.server.search.IndexAction; +import org.sonar.server.search.IndexDefinition; import org.sonar.server.search.KeyIndexAction; import java.io.Serializable; @@ -34,13 +35,21 @@ public abstract class BaseDao<T, E extends Dto<K>, K extends Serializable> protected final MyBatis mybatis; private Class<T> mapperClass; + protected final IndexDefinition indexDefinition; - protected BaseDao(Class<T> mapperClass, MyBatis myBatis) { + protected BaseDao(IndexDefinition indexDefinition, Class<T> mapperClass, MyBatis myBatis) { + this.indexDefinition = indexDefinition; this.mapperClass = mapperClass; this.mybatis = myBatis; } - protected abstract String getIndexName(); + public String getIndexName() { + return this.indexDefinition.getIndexName(); + } + + public String getIndexType() { + return this.indexDefinition.getIndexType(); + } protected abstract E doGetByKey(K key, DbSession session); @@ -55,6 +64,7 @@ public abstract class BaseDao<T, E extends Dto<K>, K extends Serializable> protected T getMapper(DbSession session) { return session.getMapper(mapperClass); } + protected MyBatis getMyBatis() { return this.mybatis; } @@ -72,7 +82,7 @@ public abstract class BaseDao<T, E extends Dto<K>, K extends Serializable> @Override public E update(E item, DbSession session) { this.doUpdate(item, session); - session.enqueue(new DtoIndexAction<E>(this.getIndexName(), + session.enqueue(new DtoIndexAction<E>(this.getIndexType(), IndexAction.Method.UPDATE, item)); return item; } @@ -92,7 +102,7 @@ public abstract class BaseDao<T, E extends Dto<K>, K extends Serializable> @Override public E insert(E item, DbSession session) { this.doInsert(item, session); - session.enqueue(new DtoIndexAction<E>(this.getIndexName(), + session.enqueue(new DtoIndexAction<E>(this.getIndexType(), IndexAction.Method.INSERT, item)); return item; } @@ -111,9 +121,9 @@ public abstract class BaseDao<T, E extends Dto<K>, K extends Serializable> @Override public void delete(E item, DbSession session) { - session.enqueue(new DtoIndexAction<E>(this.getIndexName(), - IndexAction.Method.DELETE, item)); this.doDelete(item, session); + session.enqueue(new DtoIndexAction<E>(this.getIndexType(), + IndexAction.Method.DELETE, item)); } @Override @@ -129,9 +139,9 @@ public abstract class BaseDao<T, E extends Dto<K>, K extends Serializable> @Override public void deleteByKey(K key, DbSession session) { - session.enqueue(new KeyIndexAction<K>(this.getIndexName(), - IndexAction.Method.DELETE, key)); this.doDeleteByKey(key, session); + session.enqueue(new KeyIndexAction<K>(this.getIndexType(), + IndexAction.Method.DELETE, key)); } @Override diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index dfbb4a140dd..8dea431807e 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -184,7 +184,8 @@ class ServerComponents { TempFolderCleaner.class, new TempFolderProvider(), System2.INSTANCE, - org.sonar.server.rule2.RuleDao.class + org.sonar.server.rule2.RuleDao.class, + org.sonar.server.rule2.ActiveRuleDao.class )); components.addAll(CorePropertyDefinitions.all()); components.addAll(DatabaseMigrations.CLASSES); diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleDao.java b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleDao.java index 6087aa66948..1c21797d3dc 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleDao.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleDao.java @@ -21,7 +21,6 @@ package org.sonar.server.rule2; import com.google.common.collect.Lists; -import org.apache.ibatis.session.SqlSession; import org.hibernate.cfg.NotYetImplementedException; import org.sonar.api.ServerComponent; import org.sonar.core.persistence.DbSession; @@ -33,9 +32,10 @@ import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.qualityprofile.db.QualityProfileKey; -import org.sonar.core.rule.RuleConstants; import org.sonar.core.rule.RuleDto; import org.sonar.server.db.BaseDao; +import org.sonar.server.search.EmbeddedIndexAction; +import org.sonar.server.search.IndexAction; import javax.annotation.CheckForNull; import java.util.Collection; @@ -52,17 +52,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti private QualityProfileDao qDao; public ActiveRuleDao(MyBatis mybatis, QualityProfileDao qDao, RuleDao ruleDao) { - super(ActiveRuleMapper.class, mybatis); + super(new ActiveRuleIndexDefinition(), ActiveRuleMapper.class, mybatis); this.ruleDao = ruleDao; this.qDao = qDao; } @Override - protected String getIndexName() { - return RuleConstants.INDEX_NAME; - } - - @Override public Iterable<ActiveRuleKey> keysOfRowsUpdatedAfter(long timestamp) { throw new NotYetImplementedException("Need to implement ActiveRuleDto.doGetByKey() method"); } @@ -77,13 +72,13 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti @Override protected ActiveRuleDto doInsert(ActiveRuleDto item, DbSession session) { getMapper(session).insert(item); - return setActiveRuleKey(item); + return setActiveRuleKey(item, session); } @Override protected ActiveRuleDto doUpdate(ActiveRuleDto item, DbSession session) { getMapper(session).update(item); - return setActiveRuleKey(item); + return setActiveRuleKey(item, session); } @Override @@ -98,29 +93,32 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti /** Helper methods to get the RuleKey and QualityProfileKey -- Temporary */ - private ActiveRuleDto setActiveRuleKey(ActiveRuleDto dto){ - RuleDto ruleDto = ruleDao.selectById(dto.getId()); - QualityProfileDto qDto = qDao.selectById(dto.getId()); + private ActiveRuleDto setActiveRuleKey(ActiveRuleDto dto, DbSession session){ + RuleDto ruleDto = ruleDao.selectById(dto.getRulId(), session); + QualityProfileDto qDto = qDao.selectById(dto.getProfileId(), session); if(qDto != null && ruleDto != null) { dto.setKey(QualityProfileKey.of(qDto.getName(), qDto.getLanguage()), ruleDto.getKey()); + } else { + session.close(); + throw new IllegalStateException("ActiveRule needs to have a valid Key!!!"); } return dto; } - private List<ActiveRuleDto> setActiveRuleKey(List<ActiveRuleDto> dtos){ + private List<ActiveRuleDto> setActiveRuleKey(List<ActiveRuleDto> dtos, DbSession session){ for(ActiveRuleDto dto:dtos) { - setActiveRuleKey(dto); + setActiveRuleKey(dto, session); } return dtos; } - public void delete(int activeRuleId, SqlSession session) { + public void delete(int activeRuleId, DbSession session) { session.getMapper(ActiveRuleMapper.class).delete(activeRuleId); } public void delete(int activeRuleId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { delete(activeRuleId, session); session.commit(); @@ -129,12 +127,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public void deleteFromRule(int ruleId, SqlSession session) { + public void deleteFromRule(int ruleId, DbSession session) { session.getMapper(ActiveRuleMapper.class).deleteFromRule(ruleId); } public void deleteFromRule(int ruleId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { deleteFromRule(ruleId, session); session.commit(); @@ -143,12 +141,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public void deleteFromProfile(int profileId, SqlSession session) { + public void deleteFromProfile(int profileId, DbSession session) { session.getMapper(ActiveRuleMapper.class).deleteFromProfile(profileId); } public void deleteFromProfile(int profileId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { deleteFromProfile(profileId, session); session.commit(); @@ -158,7 +156,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } public List<ActiveRuleDto> selectByIds(List<Integer> ids) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectByIds(ids, session); } finally { @@ -166,7 +164,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public List<ActiveRuleDto> selectByIds(Collection<Integer> ids, SqlSession session) { + public List<ActiveRuleDto> selectByIds(Collection<Integer> ids, DbSession session) { if (ids.isEmpty()) { return Collections.emptyList(); } @@ -176,11 +174,11 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti List<ActiveRuleDto> dtos = session.selectList("org.sonar.core.qualityprofile.db.ActiveRuleMapper.selectByIds", newArrayList(idsPartition)); dtosList.addAll(dtos); } - return setActiveRuleKey(dtosList); + return setActiveRuleKey(dtosList, session); } public List<ActiveRuleDto> selectAll() { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectAll(session); } finally { @@ -188,12 +186,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public List<ActiveRuleDto> selectAll(SqlSession session) { - return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectAll()); + public List<ActiveRuleDto> selectAll(DbSession session) { + return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectAll(), session); } public List<ActiveRuleDto> selectByRuleId(int ruleId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectByRuleId(ruleId, session); } finally { @@ -201,12 +199,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public List<ActiveRuleDto> selectByRuleId(int ruleId, SqlSession session) { - return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectByRuleId(ruleId)); + public List<ActiveRuleDto> selectByRuleId(int ruleId, DbSession session) { + return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectByRuleId(ruleId), session); } public List<ActiveRuleDto> selectByProfileId(int profileId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectByProfileId(profileId, session); } finally { @@ -214,14 +212,14 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public List<ActiveRuleDto> selectByProfileId(int profileId, SqlSession session) { - return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectByProfileId(profileId)); + public List<ActiveRuleDto> selectByProfileId(int profileId, DbSession session) { + return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectByProfileId(profileId), session); } @CheckForNull public ActiveRuleDto selectById(int id) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectById(id, session); } finally { @@ -230,13 +228,13 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } @CheckForNull - public ActiveRuleDto selectById(int id, SqlSession session) { - return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectById(id)); + public ActiveRuleDto selectById(int id, DbSession session) { + return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectById(id), session); } @CheckForNull public ActiveRuleDto selectByProfileAndRule(int profileId, int ruleId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectByProfileAndRule(profileId, ruleId, session); } finally { @@ -245,16 +243,21 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } @CheckForNull - public ActiveRuleDto selectByProfileAndRule(int profileId, int ruleId, SqlSession session) { - return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectByProfileAndRule(profileId, ruleId)); + public ActiveRuleDto selectByProfileAndRule(int profileId, int ruleId, DbSession session) { + return setActiveRuleKey(session.getMapper(ActiveRuleMapper.class).selectByProfileAndRule(profileId, ruleId), session); } - public void insert(ActiveRuleParamDto dto, SqlSession session) { - session.getMapper(ActiveRuleMapper.class).insertParameter(dto); + public void insert(ActiveRuleParamDto param, DbSession session) { + session.getMapper(ActiveRuleMapper.class).insertParameter(param); + ActiveRuleDto dto = this.selectById(param.getActiveRuleId(), session); + if(dto != null) { + session.enqueue(new EmbeddedIndexAction<ActiveRuleKey>(this.getIndexType(), + IndexAction.Method.UPDATE, param, dto.getKey())); + } } public void insert(ActiveRuleParamDto dto) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { insert(dto, session); session.commit(); @@ -263,12 +266,17 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public void update(ActiveRuleParamDto dto, SqlSession session) { - session.getMapper(ActiveRuleMapper.class).updateParameter(dto); + public void update(ActiveRuleParamDto param, DbSession session) { + session.getMapper(ActiveRuleMapper.class).updateParameter(param); + ActiveRuleDto dto = this.selectById(param.getActiveRuleId(), session); + if(dto != null) { + session.enqueue(new EmbeddedIndexAction<ActiveRuleKey>(this.getIndexType(), + IndexAction.Method.UPDATE, param, dto.getKey())); + } } public void update(ActiveRuleParamDto dto) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { update(dto, session); session.commit(); @@ -278,12 +286,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } - public void deleteParameter(int activeRuleParamId, SqlSession session) { + public void deleteParameter(int activeRuleParamId, DbSession session) { session.getMapper(ActiveRuleMapper.class).deleteParameter(activeRuleParamId); } public void deleteParameter(int activeRuleParamId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { deleteParameter(activeRuleParamId, session); session.commit(); @@ -292,12 +300,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public void deleteParameters(int activeRuleId, SqlSession session) { + public void deleteParameters(int activeRuleId, DbSession session) { session.getMapper(ActiveRuleMapper.class).deleteParameters(activeRuleId); } public void deleteParameters(int activeRuleId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { deleteParameters(activeRuleId, session); session.commit(); @@ -306,12 +314,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public void deleteParametersWithParamId(int id, SqlSession session) { + public void deleteParametersWithParamId(int id, DbSession session) { session.getMapper(ActiveRuleMapper.class).deleteParametersWithParamId(id); } public void deleteParametersFromProfile(int profileId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { deleteParametersFromProfile(profileId, session); session.commit(); @@ -320,12 +328,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public void deleteParametersFromProfile(int profileId, SqlSession session) { + public void deleteParametersFromProfile(int profileId, DbSession session) { session.getMapper(ActiveRuleMapper.class).deleteParametersFromProfile(profileId); } public ActiveRuleParamDto selectParamById(Integer activeRuleParamId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return session.getMapper(ActiveRuleMapper.class).selectParamById(activeRuleParamId); } finally { @@ -334,7 +342,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } public ActiveRuleParamDto selectParamByActiveRuleAndKey(int activeRuleId, String key) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectParamByActiveRuleAndKey(activeRuleId, key, session); } finally { @@ -342,12 +350,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public ActiveRuleParamDto selectParamByActiveRuleAndKey(int activeRuleId, String key, SqlSession session) { + public ActiveRuleParamDto selectParamByActiveRuleAndKey(int activeRuleId, String key, DbSession session) { return session.getMapper(ActiveRuleMapper.class).selectParamByActiveRuleAndKey(activeRuleId, key); } public List<ActiveRuleParamDto> selectParamsByActiveRuleId(int activeRuleId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectParamsByActiveRuleId(activeRuleId, session); } finally { @@ -355,12 +363,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public List<ActiveRuleParamDto> selectParamsByActiveRuleId(int activeRuleId, SqlSession session) { + public List<ActiveRuleParamDto> selectParamsByActiveRuleId(int activeRuleId, DbSession session) { return session.getMapper(ActiveRuleMapper.class).selectParamsByActiveRuleId(activeRuleId); } public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(List<Integer> activeRuleIds) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectParamsByActiveRuleIds(activeRuleIds, session); } finally { @@ -368,7 +376,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(Collection<Integer> activeRuleIds, SqlSession session) { + public List<ActiveRuleParamDto> selectParamsByActiveRuleIds(Collection<Integer> activeRuleIds, DbSession session) { if (activeRuleIds.isEmpty()) { return Collections.emptyList(); } @@ -382,7 +390,7 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } public List<ActiveRuleParamDto> selectAllParams() { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return selectAllParams(session); } finally { @@ -390,12 +398,12 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti } } - public List<ActiveRuleParamDto> selectAllParams(SqlSession session) { + public List<ActiveRuleParamDto> selectAllParams(DbSession session) { return session.getMapper(ActiveRuleMapper.class).selectAllParams(); } public List<ActiveRuleParamDto> selectParamsByProfileId(int profileId) { - SqlSession session = mybatis.openSession(false); + DbSession session = mybatis.openSession(false); try { return session.getMapper(ActiveRuleMapper.class).selectParamsByProfileId(profileId); } finally { diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleIndex.java index bb559448e8f..8457ce1b3cb 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleIndex.java @@ -1,163 +1,86 @@ -///* -// * SonarQube, open source software quality management tool. -// * Copyright (C) 2008-2014 SonarSource -// * mailto:contact AT sonarsource DOT com -// * -// * SonarQube is free software; you can redistribute it and/or -// * modify it under the terms of the GNU Lesser General Public -// * License as published by the Free Software Foundation; either -// * version 3 of the License, or (at your option) any later version. -// * -// * SonarQube is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// * Lesser General Public License for more details. -// * -// * You should have received a copy of the GNU Lesser General Public License -// * along with this program; if not, write to the Free Software Foundation, -// * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -// */ -//package org.sonar.server.rule2; -// -//import org.elasticsearch.action.search.SearchRequestBuilder; -//import org.elasticsearch.action.search.SearchResponse; -//import org.elasticsearch.index.query.BoolFilterBuilder; -//import org.elasticsearch.index.query.FilterBuilders; -//import org.elasticsearch.index.query.QueryBuilder; -//import org.elasticsearch.index.query.QueryBuilders; -//import org.elasticsearch.search.sort.FieldSortBuilder; -//import org.elasticsearch.search.sort.SortBuilders; -//import org.elasticsearch.search.sort.SortOrder; -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -//import org.sonar.api.rule.RuleKey; -//import org.sonar.api.rule.RuleStatus; -//import org.sonar.core.cluster.WorkQueue; -//import org.sonar.core.profiling.Profiling; -//import org.sonar.core.rule.RuleConstants; -//import org.sonar.server.es.ESNode; -//import org.sonar.server.rule2.RuleNormalizer.RuleField; -//import org.sonar.server.search.QueryOptions; -//import org.sonar.server.search.Results; -// -//import java.util.ArrayList; -//import java.util.Collection; -// -//public class ActiveRuleIndex extends RuleIndex { -// -// private static final Logger LOG = LoggerFactory.getLogger(ActiveRuleIndex.class); -// -// public ActiveRuleIndex(ActiveRuleNormalizer normalizer, WorkQueue workQueue, Profiling profiling, ESNode node) { -// super(normalizer, workQueue, profiling, node); -// } -// -// @Override -// public String getIndexName() { -// return RuleConstants.INDEX_NAME; -// } -// -// @Override -// protected String getType() { -// return RuleConstants.ES_TYPE; -// } -// -// protected String getKeyValue(RuleKey key) { -// return key.toString(); -// } -// -// public Results search(RuleQuery query, QueryOptions options) { -// -// SearchRequestBuilder esSearch = getClient() -// .prepareSearch(this.getIndexName()) -// .setIndices(this.getIndexName()); -// -// /* Build main query (search based) */ -// QueryBuilder qb; -// if (query.getQueryText() != null && !query.getQueryText().isEmpty()) { -// qb = QueryBuilders.multiMatchQuery(query.getQueryText(), -// "_id", -// RuleField.NAME.key(), -// RuleField.NAME.key()+".search", -// RuleField.DESCRIPTION.key(), -// RuleField.KEY.key(), -// RuleField.LANGUAGE.key(), -// RuleField.TAGS.key()); -// } else { -// qb = QueryBuilders.matchAllQuery(); -// } -// -// /* Build main filter (match based) */ -// BoolFilterBuilder fb = FilterBuilders.boolFilter(); -// this.addTermFilter(RuleField.LANGUAGE.key(), query.getLanguages(), fb); -// this.addTermFilter(RuleField.REPOSITORY.key(), query.getRepositories(), fb); -// this.addTermFilter(RuleField.SEVERITY.key(), query.getSeverities(), fb); -// this.addTermFilter(RuleField.KEY.key(), query.getKey(), fb); -// if(query.getStatuses() != null && !query.getStatuses().isEmpty()) { -// Collection<String> stringStatus = new ArrayList<String>(); -// for (RuleStatus status : query.getStatuses()) { -// stringStatus.add(status.name()); -// } -// this.addTermFilter(RuleField.STATUS.key(), stringStatus, fb); -// } -// -// /* Integrate Query */ -// QueryBuilder mainQuery; -// if((query.getLanguages() != null && !query.getLanguages().isEmpty()) || -// (query.getRepositories() != null && !query.getRepositories().isEmpty()) || -// (query.getSeverities() != null && !query.getSeverities().isEmpty()) || -// (query.getStatuses() != null && !query.getStatuses().isEmpty()) || -// (query.getKey() != null && !query.getKey().isEmpty())) { -// mainQuery = QueryBuilders.filteredQuery(qb, fb); -// } else { -// mainQuery = qb; -// } -// esSearch.setQuery(mainQuery); -// -// /* Integrate Facets */ -// if(options.isFacet()) { -// this.setFacets(esSearch); -// } -// -// /* integrate Query Sort */ -// if(query.getSortField() != null){ -// FieldSortBuilder sort = SortBuilders.fieldSort(query.getSortField().field().key()); -// if(query.isAscendingSort()){ -// sort.order(SortOrder.ASC); -// } else { -// sort.order(SortOrder.DESC); -// } -// esSearch.addSort(sort); -// } else if(query.getQueryText() != null && !query.getQueryText().isEmpty()){ -// esSearch.addSort(SortBuilders.scoreSort()); -// } else { -// esSearch.addSort(RuleField.UPDATED_AT.key(), SortOrder.DESC); -// } -// -// /* integrate Option's Pagination */ -// esSearch.setFrom(options.getOffset()); -// esSearch.setSize(options.getLimit()); -// -// /* integrate Option's Fields */ -// if (options.getFieldsToReturn() != null && -// !options.getFieldsToReturn().isEmpty()) { -// for(String field:options.getFieldsToReturn()) { -// esSearch.addField(field); -// } -// } else { -// for (RuleField field : RuleField.values()) { -// esSearch.addField(field.key()); -// } -// } -// -// /* Get results */ -// SearchResponse esResult = esSearch.get(); -// -// /* Integrate ES Results */ -// Results results = new Results(esResult) -// .setTotal((int) esResult.getHits().totalHits()) -// .setTime(esResult.getTookInMillis()) -// .setHits(this.toHit(esResult.getHits())); -// -// return results; -// } -//} +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +/* +* SonarQube, open source software quality management tool. +* Copyright (C) 2008-2014 SonarSource +* mailto:contact AT sonarsource DOT com +* +* SonarQube is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 3 of the License, or (at your option) any later version. +* +* SonarQube is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this program; if not, write to the Free Software Foundation, +* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ +package org.sonar.server.rule2; + +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.core.cluster.WorkQueue; +import org.sonar.core.profiling.Profiling; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.ActiveRuleKey; +import org.sonar.server.search.BaseIndex; +import org.sonar.server.search.NestedIndex; + +import java.io.IOException; + +public class ActiveRuleIndex extends NestedIndex<ActiveRuleDto, ActiveRuleKey> { + + private static final Logger LOG = LoggerFactory.getLogger(ActiveRuleIndex.class); + + public ActiveRuleIndex(ActiveRuleNormalizer normalizer, WorkQueue workQueue, Profiling profiling, BaseIndex<?, ?> index) { + super(new ActiveRuleIndexDefinition(), normalizer, workQueue, profiling, index); + } + + @Override + protected String getParentKeyValue(ActiveRuleKey key) { + return key.ruleKey().toString(); + } + + @Override + protected String getIndexField() { + return RuleNormalizer.RuleField.ACTIVE.key(); + } + + @Override + protected XContentBuilder getIndexSettings() throws IOException { + return null; + } + + @Override + protected XContentBuilder getMapping() throws IOException { + return null; + } + + @Override + protected void setFacets(SearchRequestBuilder query) { + + } +}
\ No newline at end of file diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleIndexDefinition.java b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleIndexDefinition.java new file mode 100644 index 00000000000..f2ee9285f64 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleIndexDefinition.java @@ -0,0 +1,38 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.rule2; + +import org.sonar.server.search.IndexDefinition; + +public class ActiveRuleIndexDefinition implements IndexDefinition { + + private static final String INDEX_NAME = "rules2"; + private static final String INDEX_TYPE = "activeRule2"; + + @Override + public String getIndexName() { + return ActiveRuleIndexDefinition.INDEX_NAME; + } + + @Override + public String getIndexType() { + return ActiveRuleIndexDefinition.INDEX_TYPE; + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java new file mode 100644 index 00000000000..90a0d5c7a70 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/ActiveRuleNormalizer.java @@ -0,0 +1,131 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.rule2; + +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; +import org.sonar.core.qualityprofile.db.ActiveRuleKey; +import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; +import org.sonar.server.search.BaseNormalizer; + +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; + +public class ActiveRuleNormalizer extends BaseNormalizer<ActiveRuleDto, ActiveRuleKey> { + + ActiveRuleDao activeRuleDao; + + public static enum ActiveRuleField { + OVERRIDE("override"), + INHERITANCE("inheritance"), + PROFILE_ID("profile"), + SEVERITY("severity"), + PARENT_ID("parent"), + PARAMS("params"); + + private final String key; + + private ActiveRuleField(final String key) { + this.key = key; + } + + public String key() { + return key; + } + + @Override + public String toString() { + return key; + } + } + + public static enum ActiveRuleParamField { + NAME("name"), + VALUE("value"); + + private final String key; + + private ActiveRuleParamField(final String key) { + this.key = key; + } + + public String key() { + return key; + } + + @Override + public String toString() { + return key; + } + } + + public ActiveRuleNormalizer(ActiveRuleDao activeRuleDao) { + this.activeRuleDao = activeRuleDao; + } + + @Override + public UpdateRequest normalize(ActiveRuleKey key) throws Exception { + return normalize(activeRuleDao.getByKey(key)); + } + + public UpdateRequest normalize(ActiveRuleParamDto param, ActiveRuleKey key) throws Exception { + XContentBuilder document = jsonBuilder().startObject(); + document.startObject(RuleNormalizer.RuleField.ACTIVE.key()); + document.startObject(key.toString()); + document.startObject(ActiveRuleField.PARAMS.key()); + document.startObject(param.getKey()); + indexField(ActiveRuleParamField.VALUE.key(), param.getValue(), document); + document.endObject(); + document.endObject(); + document.endObject(); + document.endObject(); + document.endObject(); + + /* Creating updateRequest */ + UpdateRequest request = new UpdateRequest() + .doc(document); + request.docAsUpsert(true); + return request; + + } + + @Override + public UpdateRequest normalize(ActiveRuleDto rule) throws Exception { + XContentBuilder document = jsonBuilder().startObject(); + + document.startObject(RuleNormalizer.RuleField.ACTIVE.key()); + document.startObject(rule.getKey().toString()); + indexField(ActiveRuleField.OVERRIDE.key(), rule.doesOverride(), document); + indexField(ActiveRuleField.INHERITANCE.key(), rule.getInheritance(), document); + indexField(ActiveRuleField.PROFILE_ID.key(), rule.getProfileId(), document); + indexField(ActiveRuleField.SEVERITY.key(), rule.getSeverityString(), document); + indexField(ActiveRuleField.PARENT_ID.key(), rule.getParentId(), document); + + /* Done normalizing for Rule */ + document.endObject(); + document.endObject(); + + /* Creating updateRequest */ + UpdateRequest request = new UpdateRequest() + .doc(document); + request.docAsUpsert(true); + return request; + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java index 3929c6cdcd8..5199f144403 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java @@ -27,7 +27,6 @@ import org.sonar.api.ServerComponent; import org.sonar.api.rule.RuleKey; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; -import org.sonar.core.rule.RuleConstants; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleMapper; import org.sonar.core.rule.RuleParamDto; @@ -49,15 +48,9 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> implements BatchComponent, ServerComponent { public RuleDao(MyBatis mybatis) { - super(RuleMapper.class, mybatis); + super(new RuleIndexDefinition(), RuleMapper.class, mybatis); } - @Override - protected String getIndexName() { - return RuleConstants.INDEX_NAME; - } - - @Override @CheckForNull protected RuleDto doGetByKey(RuleKey key, DbSession session) { return getMapper(session).selectByKey(key); @@ -176,7 +169,7 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> DbSession session = mybatis.openSession(true); try { for (RuleDto rule : rules) { - session.enqueue(new DtoIndexAction<RuleDto>(this.getIndexName(), + session.enqueue(new DtoIndexAction<RuleDto>(this.getIndexType(), IndexAction.Method.INSERT, rule)); getMapper(session).batchInsert(rule); } @@ -238,7 +231,7 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> getMapper(session).insertParameter(param); RuleDto dto = this.selectById(param.getRuleId(), session); if(dto != null){ - session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexName(), + session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.INSERT, param, dto.getKey())); } @@ -258,7 +251,7 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> getMapper(session).updateParameter(param); RuleDto dto = this.selectById(param.getRuleId(), session); if(dto != null) { - session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexName(), + session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.UPDATE, param, dto.getKey())); } @@ -286,28 +279,28 @@ public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> public void insert(RuleRuleTagDto newTag, DbSession session) { System.out.println("newTag = [" + newTag + "], session = [" + session + "]"); getMapper(session).insertTag(newTag); - session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexName(), + session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.INSERT, newTag, this.selectById(newTag.getRuleId(), session).getKey())); } public void deleteParam(RuleParamDto persistedParam, DbSession session) { getMapper(session).deleteParameter(persistedParam.getId()); - session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexName(), + session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.DELETE, persistedParam, this.selectById(persistedParam.getRuleId(), session).getKey())); } public void deleteTag(RuleRuleTagDto tagToDelete, DbSession session) { getMapper(session).deleteTag(tagToDelete.getId().intValue()); - session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexName(), + session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.DELETE, tagToDelete, this.selectById(tagToDelete.getRuleId(), session).getKey())); } public void update(RuleRuleTagDto existingTag, DbSession session) { getMapper(session).updateTag(existingTag); - session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexName(), + session.enqueue(new EmbeddedIndexAction<RuleKey>(this.getIndexType(), IndexAction.Method.UPDATE, existingTag, this.selectById(existingTag.getRuleId(), session).getKey())); } diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java index 358c587cfe7..049395ab153 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java @@ -38,7 +38,6 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.core.cluster.WorkQueue; import org.sonar.core.profiling.Profiling; -import org.sonar.core.rule.RuleConstants; import org.sonar.core.rule.RuleDto; import org.sonar.server.es.ESNode; import org.sonar.server.rule2.RuleNormalizer.RuleField; @@ -53,7 +52,7 @@ import java.util.Set; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -public class RuleIndex extends BaseIndex<RuleKey, RuleDto> { +public class RuleIndex extends BaseIndex<RuleDto, RuleKey> { private static final Logger LOG = LoggerFactory.getLogger(RuleIndex.class); @@ -76,17 +75,7 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> { public RuleIndex(RuleNormalizer normalizer, WorkQueue workQueue, Profiling profiling, ESNode node) { - super(normalizer, workQueue, profiling, node); - } - - @Override - public String getIndexName() { - return RuleConstants.INDEX_NAME; - } - - @Override - protected String getType() { - return RuleConstants.ES_TYPE; + super(new RuleIndexDefinition(), normalizer, workQueue, profiling, node); } protected String getKeyValue(RuleKey key) { @@ -149,7 +138,7 @@ public class RuleIndex extends BaseIndex<RuleKey, RuleDto> { @Override protected XContentBuilder getMapping() throws IOException { XContentBuilder mapping = jsonBuilder().startObject() - .startObject(this.getType()) + .startObject(this.indexDefinition.getIndexType()) .field("dynamic", true) .startObject("properties"); diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndexDefinition.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndexDefinition.java new file mode 100644 index 00000000000..790f121d6f8 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndexDefinition.java @@ -0,0 +1,39 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.rule2; + +import org.sonar.server.search.IndexDefinition; + +public final class RuleIndexDefinition implements IndexDefinition { + + private static final String INDEX_NAME = "rules2"; + private static final String INDEX_DOMAIN = "rules"; + private static final String INDEX_TYPE = "rule2"; + + @Override + public String getIndexName() { + return RuleIndexDefinition.INDEX_NAME; + } + + @Override + public String getIndexType() { + return RuleIndexDefinition.INDEX_TYPE; + } +}
\ No newline at end of file diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java index d39272870a4..941b8d7ddfc 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleNormalizer.java @@ -23,26 +23,19 @@ import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.common.xcontent.XContentBuilder; import org.sonar.api.rule.RuleKey; import org.sonar.check.Cardinality; -import org.sonar.core.qualityprofile.db.ActiveRuleDao; -import org.sonar.core.qualityprofile.db.ActiveRuleDto; -import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.rule.RuleRuleTagDto; -import org.sonar.core.rule.RuleTagDao; import org.sonar.core.rule.RuleTagType; import org.sonar.server.search.BaseNormalizer; import java.io.IOException; -import java.util.List; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { private final RuleDao ruleDao; - private final ActiveRuleDao activeRuleDao; - private final RuleTagDao ruleTagDao; public static enum RuleField { KEY("key"), @@ -99,54 +92,8 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { } } - public static enum ActiveRuleField { - OVERRIDE("override"), - INHERITANCE("inheritance"), - PROFILE_ID("profile"), - SEVERITY("severity"), - PARENT_ID("parent"), - PARAMS("params"); - - private final String key; - - private ActiveRuleField(final String key) { - this.key = key; - } - - public String key() { - return key; - } - - @Override - public String toString() { - return key; - } - } - - public static enum ActiveRuleParamField { - NAME("name"), - VALUE("value"); - - private final String key; - - private ActiveRuleParamField(final String key) { - this.key = key; - } - - public String key() { - return key; - } - - @Override - public String toString() { - return key; - } - } - - public RuleNormalizer(RuleDao ruleDao, ActiveRuleDao activeRuleDao, RuleTagDao ruleTagDao) { + public RuleNormalizer(RuleDao ruleDao){ this.ruleDao = ruleDao; - this.activeRuleDao = activeRuleDao; - this.ruleTagDao = ruleTagDao; } @Override @@ -172,34 +119,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> { document.startArray(RuleField.TAGS.key()).endArray(); document.startArray(RuleField.SYSTEM_TAGS.key()).endArray(); document.startObject(RuleField.PARAMS.key()).endObject(); - - /* Normalize activeRules */ - List<ActiveRuleDto> activeRules = activeRuleDao.selectByRuleId(rule.getId()); - document.startObject(RuleField.ACTIVE.key()); - if (!activeRules.isEmpty()) { - for (ActiveRuleDto activeRule : activeRules) { - document.startObject(activeRule.getProfileId().toString()); - indexField(ActiveRuleField.OVERRIDE.key(), activeRule.doesOverride(), document); - indexField(ActiveRuleField.INHERITANCE.key(), activeRule.getInheritance(), document); - indexField(ActiveRuleField.PROFILE_ID.key(), activeRule.getProfileId(), document); - indexField(ActiveRuleField.SEVERITY.key(), activeRule.getSeverityString(), document); - indexField(ActiveRuleField.PARENT_ID.key(), activeRule.getParentId(), document); - - /* Get all activeRuleParams */ - List<ActiveRuleParamDto> activeRuleParams = activeRuleDao.selectParamsByActiveRuleId(activeRule.getId()); - if(!activeRuleParams.isEmpty()) { - document.startObject(ActiveRuleField.PARAMS.key()); - for (ActiveRuleParamDto param : activeRuleParams) { - document.startObject(param.getKey()); - indexField(ActiveRuleParamField.VALUE.key(), param.getValue(), document); - document.endObject(); - } - document.endObject(); - } - document.endObject(); - } - } - document.endObject(); + document.startObject(RuleField.ACTIVE.key()).endObject(); /* Done normalizing for Rule */ document.endObject(); diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index 5464fc3e0eb..526b331fb81 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -46,25 +46,41 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; -public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implements Index<E, K> { +public abstract class BaseIndex<E extends Dto<K>, K extends Serializable> implements Index<E, K> { private static final Logger LOG = LoggerFactory.getLogger(BaseIndex.class); private final Profiling profiling; private final ESNode node; protected BaseNormalizer<E, K> normalizer; + protected final IndexDefinition indexDefinition; - public BaseIndex(BaseNormalizer<E, K> normalizer, WorkQueue workQueue, + public BaseIndex(IndexDefinition indexDefinition, BaseNormalizer<E, K> normalizer, WorkQueue workQueue, Profiling profiling, ESNode node) { this.normalizer = normalizer; this.profiling = profiling; this.node = node; + this.indexDefinition = indexDefinition; + } + + @Override + public String getIndexName() { + return this.indexDefinition.getIndexName(); + } + + @Override + public String getIndexType() { + return this.indexDefinition.getIndexType(); } protected Client getClient() { return node.client(); } + protected ESNode getNode() { + return this.node; + } + /* Component Methods */ @Override @@ -81,7 +97,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem /* Cluster And ES Stats/Client methods */ - private void initializeIndex() { + protected void initializeIndex() { String index = this.getIndexName(); @@ -94,7 +110,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem LOG.info("Setup of index {}", this.getIndexName()); getClient().admin().indices().prepareCreate(index) .setSettings(getIndexSettings()) - .addMapping(getType(), getMapping()) + .addMapping(this.indexDefinition.getIndexType(), getMapping()) .execute().actionGet(); } catch (Exception e) { throw new RuntimeException("Invalid configuration for index " + this.getIndexName(), e); @@ -106,8 +122,6 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem protected abstract XContentBuilder getIndexSettings() throws IOException; - protected abstract String getType(); - protected abstract XContentBuilder getMapping() throws IOException; /* Base CRUD methods */ @@ -121,21 +135,21 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem @Override public Hit getByKey(K key) { - GetResponse result = getClient().prepareGet(this.getIndexName(), this.getType(), this.getKeyValue(key)) + GetResponse result = getClient().prepareGet(this.getIndexName(), + this.indexDefinition.getIndexType(), this.getKeyValue(key)) .get(); return Hit.fromMap(0, result.getSourceAsMap()); } - private void insertDocument(UpdateRequest request, String key) throws Exception { - LOG.debug("INSERT _id:{} in index {}", key, this.getIndexName()); + private void insertDocument(UpdateRequest request, K key) throws Exception { + LOG.debug("INSERT _id:{} in index {}", this.getKeyValue(key), this.getIndexName()); updateDocument(request, key); } @Override public void insert(Object obj, K key) throws Exception { if (this.normalizer.canNormalize(obj.getClass(), key.getClass())) { - this.updateDocument(this.normalizer.normalizeOther(obj, key), - this.getKeyValue(key)); + this.updateDocument(this.normalizer.normalizeOther(obj, key),key); } else { throw new IllegalStateException("No normalizer method available for "+ obj.getClass().getSimpleName()+ " in "+ normalizer.getClass().getSimpleName()); @@ -146,16 +160,12 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem public void insertByDto(E item) { try { UpdateRequest doc = normalizer.normalize(item); - String keyValue = getKeyValue(item.getKey()); - if (doc != null && keyValue != null && !keyValue.isEmpty()) { - insertDocument(doc, keyValue); - } else { - LOG.error("Could not normalize document {} for insert in {}", - keyValue, getIndexName()); - } + insertDocument(doc, item.getKey()); } catch (Exception e) { - LOG.error("Could not update document for index {}: {}", - getIndexName(), e.getMessage()); + throw new IllegalStateException(this.getClass().getSimpleName() + + "cannot execute INSERT_BY_DTO for " + item.getClass().getSimpleName() + + " as " + this.getIndexType() + + " on key: "+ item.getKey(), e); } } @@ -163,33 +173,29 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem public void insertByKey(K key) { try { UpdateRequest doc = normalizer.normalize(key); - String keyValue = getKeyValue(key); - if (doc != null && keyValue != null && !keyValue.isEmpty()) { - insertDocument(doc, keyValue); - } else { - LOG.error("Could not normalize document {} for insert in {}", - key, getIndexName()); - } + insertDocument(doc, key); } catch (Exception e) { - LOG.error("Could not update document for index {}: {}", - getIndexName(), e.getMessage()); + throw new IllegalStateException(this.getClass().getSimpleName() + + "cannot execute INSERT_BY_KEY for " + key.getClass().getSimpleName() + + " as " + this.getIndexType() + + " on key: "+ key, e); } } - private void updateDocument(UpdateRequest request, String key) throws Exception { + protected void updateDocument(UpdateRequest request, K key) throws Exception { LOG.debug("UPDATE _id:{} in index {}", key, this.getIndexName()); - getClient().update(request.id(key) - .type(this.getType()) - .index(this.getIndexName())).get(); + getClient().update(request + .index(this.getIndexName()) + .id(this.getKeyValue(key)) + .type(this.getIndexType())).get(); } @Override public void update(Object obj, K key) throws Exception { if (this.normalizer.canNormalize(obj.getClass(), key.getClass())) { - this.updateDocument(this.normalizer.normalizeOther(obj, key), - this.getKeyValue(key)); + this.updateDocument(this.normalizer.normalizeOther(obj, key),key); } else { throw new IllegalStateException("Index " + this.getIndexName() + " cannot execute INSERT for class: " + obj.getClass()); @@ -200,8 +206,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem public void updateByDto(E item) { try { UpdateRequest doc = normalizer.normalize(item); - String keyValue = getKeyValue(item.getKey()); - this.updateDocument(doc, keyValue); + this.updateDocument(doc, item.getKey()); } catch (Exception e) { LOG.error("Could not update document for index {}: {}", this.getIndexName(), e.getMessage()); @@ -212,18 +217,20 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem public void updateByKey(K key) { try { UpdateRequest doc = normalizer.normalize(key); - String keyValue = getKeyValue(key); - this.updateDocument(doc, keyValue); + this.updateDocument(doc, key); } catch (Exception e) { LOG.error("Could not update document for index {}: {}", this.getIndexName(), e.getMessage()); } } - private void deleteDocument(String key) throws ExecutionException, InterruptedException { + private void deleteDocument(K key) throws ExecutionException, InterruptedException { LOG.debug("DELETE _id:{} in index {}", key, this.getIndexName()); getClient() - .prepareDelete(this.getIndexName(), this.getType(), key) + .prepareDelete() + .setIndex(this.getIndexName()) + .setType(this.indexDefinition.getIndexType()) + .setIndex(this.getKeyValue(key)) .get(); } @@ -240,7 +247,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem @Override public void deleteByKey(K key) { try { - this.deleteDocument(this.getKeyValue(key)); + this.deleteDocument(key); } catch (Exception e) { LOG.error("Could not DELETE _id:{} for index {}: {}", this.getKeyValue(key), this.getIndexName(), e.getMessage()); @@ -250,7 +257,7 @@ public abstract class BaseIndex<K extends Serializable, E extends Dto<K>> implem @Override public void deleteByDto(E item) { try { - this.deleteDocument(this.getKeyValue(item.getKey())); + this.deleteDocument(item.getKey()); } catch (Exception e) { LOG.error("Could not DELETE _id:{} for index {}: {}", this.getKeyValue(item.getKey()), this.getIndexName(), e.getMessage()); diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java b/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java index 47f41f8d096..1de481a5680 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseNormalizer.java @@ -38,15 +38,15 @@ public abstract class BaseNormalizer<E extends Dto<K>, K extends Serializable> { } } - public UpdateRequest normalizeOther(Object object, K key) throws Exception { + public UpdateRequest normalizeOther(Object object, Object key) throws Exception { return (UpdateRequest) this.getClass() .getMethod("normalize", object.getClass(), key.getClass()) .invoke(this, object, key); } - public abstract UpdateRequest normalize(K key) throws IOException; + public abstract UpdateRequest normalize(K key) throws IOException, Exception; - public abstract UpdateRequest normalize(E dto) throws IOException; + public abstract UpdateRequest normalize(E dto) throws Exception; private static final Logger LOG = LoggerFactory.getLogger(BaseNormalizer.class); diff --git a/sonar-server/src/main/java/org/sonar/server/search/DtoIndexAction.java b/sonar-server/src/main/java/org/sonar/server/search/DtoIndexAction.java index 6afa2598e0a..d278b29aad5 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/DtoIndexAction.java +++ b/sonar-server/src/main/java/org/sonar/server/search/DtoIndexAction.java @@ -25,8 +25,8 @@ public class DtoIndexAction<E extends Dto> extends IndexAction { private final E item; - public DtoIndexAction(String indexName, Method method, E item) { - super(indexName, method); + public DtoIndexAction(String indexType, Method method, E item) { + super(indexType, method); this.item = item; } @@ -41,11 +41,10 @@ public class DtoIndexAction<E extends Dto> extends IndexAction { index.updateByDto(this.item); } } catch (Exception e) { - e.printStackTrace(); - - throw new IllegalStateException("Index " + this.getIndexName() + " cannot execute " + - this.getMethod() + " for " + item.getKey()); - + throw new IllegalStateException(this.getClass().getSimpleName() + + " cannot execute " + this.getMethod() + " for " + this.item.getClass().getSimpleName() + + " as " + this.getIndexType() + + " on key: "+ this.item.getKey(), e); } } diff --git a/sonar-server/src/main/java/org/sonar/server/search/EmbeddedIndexAction.java b/sonar-server/src/main/java/org/sonar/server/search/EmbeddedIndexAction.java index b5b54470c59..543b865651d 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/EmbeddedIndexAction.java +++ b/sonar-server/src/main/java/org/sonar/server/search/EmbeddedIndexAction.java @@ -26,9 +26,9 @@ public class EmbeddedIndexAction<K extends Serializable> extends IndexAction { private final Object item; private final K key; - public EmbeddedIndexAction(String indexName, Method method, Object item, K key){ - super(indexName, method); - this.indexName = indexName; + public EmbeddedIndexAction(String indexType, Method method, Object item, K key){ + super(indexType, method); + this.indexType = indexType; this.method = method; this.key = key; this.item = item; @@ -45,8 +45,9 @@ public class EmbeddedIndexAction<K extends Serializable> extends IndexAction { index.update(this.item, this.key); } } catch (Exception e) { - throw new IllegalStateException("Index " + this.getIndexName() + " cannot execute " + - this.getMethod() + " for " +this.item.getClass().getSimpleName() + + throw new IllegalStateException(this.getClass().getSimpleName() + + "cannot execute " + this.getMethod() + " for " + this.item.getClass().getSimpleName() + + " as " + this.getIndexType() + " on key: "+ this.key, e); } } diff --git a/sonar-server/src/main/java/org/sonar/server/search/Index.java b/sonar-server/src/main/java/org/sonar/server/search/Index.java index 927b066192a..06fb65e0db3 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/Index.java +++ b/sonar-server/src/main/java/org/sonar/server/search/Index.java @@ -27,11 +27,13 @@ import java.io.Serializable; public interface Index<E extends Dto<K>, K extends Serializable> extends Startable { - String getIndexName(); - @CheckForNull Hit getByKey(K item); + String getIndexType(); + + String getIndexName(); + void refresh(); void insert(Object obj, K key) throws Exception; diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java b/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java index 409e6537e28..3c568e72d61 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexAction.java @@ -31,14 +31,14 @@ public abstract class IndexAction extends QueueAction { INSERT, UPDATE, DELETE } - protected String indexName; + protected String indexType; protected Method method; protected Index index; - public IndexAction(String indexName, Method method){ + public IndexAction(String indexType, Method method){ super(); - this.indexName = indexName; + this.indexType = indexType; this.method = method; } @@ -46,12 +46,12 @@ public abstract class IndexAction extends QueueAction { return this.method; } - public String getIndexName() { - return indexName; + public String getIndexType() { + return indexType; } - public void setIndexName(String indexName) { - this.indexName = indexName; + public void setIndexType(String indexType) { + this.indexType = indexType; } public void setMethod(Method method) { diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexDefinition.java b/sonar-server/src/main/java/org/sonar/server/search/IndexDefinition.java new file mode 100644 index 00000000000..c7932911a7b --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexDefinition.java @@ -0,0 +1,27 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.search; + +public interface IndexDefinition { + + String getIndexName(); + + String getIndexType(); +} diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java b/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java index abaeac31bc7..8dd300a5c08 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexUtils.java @@ -21,6 +21,8 @@ package org.sonar.server.search; import com.google.common.collect.ImmutableList; import org.sonar.server.cluster.LocalQueueWorker; +import org.sonar.server.rule2.ActiveRuleIndex; +import org.sonar.server.rule2.ActiveRuleNormalizer; import org.sonar.server.rule2.RuleIndex; import org.sonar.server.rule2.RuleNormalizer; @@ -33,7 +35,9 @@ public final class IndexUtils { public static List<Class> getIndexClasses() { return ImmutableList.<Class>of( RuleNormalizer.class, + ActiveRuleNormalizer.class, RuleIndex.class, + ActiveRuleIndex.class, LocalQueueWorker.class ); } diff --git a/sonar-server/src/main/java/org/sonar/server/search/KeyIndexAction.java b/sonar-server/src/main/java/org/sonar/server/search/KeyIndexAction.java index f066f79ecbb..7333a27654e 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/KeyIndexAction.java +++ b/sonar-server/src/main/java/org/sonar/server/search/KeyIndexAction.java @@ -25,8 +25,8 @@ public class KeyIndexAction<K extends Serializable> extends IndexAction { private final K key; - public KeyIndexAction(String indexName, Method method, K key) { - super(indexName, method); + public KeyIndexAction(String indexType, Method method, K key) { + super(indexType, method); this.key = key; } @@ -41,8 +41,10 @@ public class KeyIndexAction<K extends Serializable> extends IndexAction { index.updateByKey(this.key); } } catch (Exception e) { - throw new IllegalStateException("Index " + this.getIndexName() + " cannot execute " + - this.getMethod() + " for " + key); + throw new IllegalStateException(this.getClass().getSimpleName() + + "cannot execute " + this.getMethod() + " for " + this.key.getClass().getSimpleName() + + " on type: " + this.getIndexType() + + " on key: "+ this.key, e); } } } diff --git a/sonar-server/src/main/java/org/sonar/server/search/NestedIndex.java b/sonar-server/src/main/java/org/sonar/server/search/NestedIndex.java new file mode 100644 index 00000000000..880d969c3f1 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/search/NestedIndex.java @@ -0,0 +1,84 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.search; + +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.client.Client; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.core.cluster.WorkQueue; +import org.sonar.core.db.Dto; +import org.sonar.core.profiling.Profiling; + +import java.io.Serializable; + +public abstract class NestedIndex<E extends Dto<K>, K extends Serializable> + extends BaseIndex<E, K> { + + private static final Logger LOG = LoggerFactory.getLogger(NestedIndex.class); + + protected BaseIndex<?, ?> parentIndex; + + public NestedIndex(IndexDefinition indexDefinition, BaseNormalizer<E, K> normalizer, WorkQueue workQueue, + Profiling profiling, BaseIndex<?,?> index) { + super(indexDefinition, normalizer, workQueue, profiling, index.getNode()); + this.parentIndex = index; + } + + protected Client getClient() { + return parentIndex.getClient(); + } + + /* Base CRUD methods */ + + protected abstract String getParentKeyValue(K key); + + protected abstract String getIndexField(); + + protected String getKeyValue(K key){ + return this.getParentKeyValue(key); + } + + public String getParentIndexType(){ + return "rule2"; + } + + protected void initializeIndex() { + ; + } + + @Override + public Hit getByKey(K key) { + GetResponse result = getClient().prepareGet(this.getIndexName(), this.indexDefinition.getIndexType(), this.getKeyValue(key)) + .get(); + return Hit.fromMap(0, ((java.util.Map<String, Object>) result.getSourceAsMap().get(getIndexField()))); + } + + @Override + protected void updateDocument(UpdateRequest request, K key) throws Exception { + LOG.debug("UPDATE _id:{} in index {}", key, this.getIndexName()); + getClient().update(request + .index(this.getIndexName()) + .id(this.getKeyValue(key)) + .type(this.getParentIndexType())).get(); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java index 13b094bca0b..085e3dbc7dc 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule2/RuleServiceMediumTest.java @@ -31,9 +31,10 @@ import org.sonar.api.utils.DateUtils; import org.sonar.check.Cardinality; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; -import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; +import org.sonar.core.qualityprofile.db.QualityProfileDao; +import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.rule.RuleRuleTagDto; @@ -52,9 +53,13 @@ import static org.fest.assertions.Assertions.assertThat; public class RuleServiceMediumTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static ServerTester tester = new ServerTester() + .setProperty("sonar.es.http.port","8888"); RuleDao dao = tester.get(RuleDao.class); + + QualityProfileDao qualityProfileDao = tester.get(QualityProfileDao.class); + RuleIndex index = tester.get(RuleIndex.class); @Before @@ -140,39 +145,58 @@ public class RuleServiceMediumTest { } @Test - public void insert_and_index_activeRules() { + public void insert_and_index_activeRules() throws InterruptedException { DbSession dbSession = tester.get(MyBatis.class).openSession(false); ActiveRuleDao activeRuleDao = tester.get(ActiveRuleDao.class); + + QualityProfileDto profileDto = new QualityProfileDto() + .setName("myprofile") + .setLanguage("java"); + qualityProfileDao.insert(profileDto, dbSession); + // insert db RuleKey ruleKey = RuleKey.of("javascript", "S001"); RuleDto ruleDto = newRuleDto(ruleKey); dao.insert(ruleDto, dbSession); + dbSession.commit(); + ActiveRuleDto activeRule = new ActiveRuleDto() .setInheritance("inherited") - .setProfileId(1) + .setProfileId(profileDto.getId()) .setRuleId(ruleDto.getId()) .setSeverity(Severity.BLOCKER); + activeRuleDao.insert(activeRule, dbSession); dbSession.commit(); + dbSession.close(); + // verify that activeRules are persisted in db List<ActiveRuleDto> persistedDtos = activeRuleDao.selectByRuleId(ruleDto.getId()); assertThat(persistedDtos).hasSize(1); // verify that activeRules are indexed in es index.refresh(); + + Hit hit = index.getByKey(ruleKey); assertThat(hit).isNotNull(); assertThat(hit.getField(RuleNormalizer.RuleField.ACTIVE.key())).isNotNull(); - RuleService service = tester.get(RuleService.class); - Rule rule = service.getByKey(ruleKey); + Map<String, Object> activeRules = (Map<String, Object>) hit.getField(RuleNormalizer.RuleField.ACTIVE.key()); + assertThat(activeRules).hasSize(1); } @Test - public void insert_and_index_activeRuleParams() { + public void insert_and_index_activeRuleParams() throws InterruptedException { DbSession dbSession = tester.get(MyBatis.class).openSession(false); ActiveRuleDao activeRuleDao = tester.get(ActiveRuleDao.class); + + QualityProfileDto profileDto = new QualityProfileDto() + .setName("myprofile") + .setLanguage("java"); + qualityProfileDao.insert(profileDto, dbSession); + // insert db RuleKey ruleKey = RuleKey.of("javascript", "S001"); RuleDto ruleDto = newRuleDto(ruleKey); @@ -193,7 +217,7 @@ public class RuleServiceMediumTest { ActiveRuleDto activeRule = new ActiveRuleDto() .setInheritance("inherited") - .setProfileId(1) + .setProfileId(profileDto.getId()) .setRuleId(ruleDto.getId()) .setSeverity(Severity.BLOCKER); activeRuleDao.insert(activeRule, dbSession); @@ -213,6 +237,7 @@ public class RuleServiceMediumTest { activeRuleDao.insert(activeRuleMaxParam, dbSession); dbSession.commit(); + dbSession.close(); // verify that activeRulesParams are persisted in db List<ActiveRuleParamDto> persistedDtos = activeRuleDao.selectParamsByActiveRuleId(activeRule.getId()); @@ -220,6 +245,7 @@ public class RuleServiceMediumTest { // verify that activeRulesParams are indexed in es index.refresh(); + Hit hit = index.getByKey(ruleKey); assertThat(hit).isNotNull(); @@ -236,7 +262,7 @@ public class RuleServiceMediumTest { Map<String, String> _activeRuleParamValue = (Map<String, String>) _activeRuleParams.get(maxParam.getName()); assertThat(_activeRuleParamValue).isNotNull().hasSize(1); - assertThat(_activeRuleParamValue.get(RuleNormalizer.ActiveRuleParamField.VALUE.key())).isEqualTo("maximum"); + assertThat(_activeRuleParamValue.get(ActiveRuleNormalizer.ActiveRuleParamField.VALUE.key())).isEqualTo("maximum"); } @@ -283,6 +309,8 @@ public class RuleServiceMediumTest { dao.insert(rTag2, dbSession); dao.insert(rTag3, dbSession); dbSession.commit(); + dbSession.close(); + // verify that tags are persisted in db List<RuleRuleTagDto> persistedDtos = dao.selectTagsByRuleId(ruleDto.getId()); |