+++ /dev/null
-/*
- * 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.core.db;
-
-import org.sonar.api.ServerComponent;
-import org.sonar.core.persistence.DbSession;
-
-import javax.annotation.CheckForNull;
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.List;
-
-public interface Dao<E extends Dto<K>, K extends Serializable> extends ServerComponent {
-
- @CheckForNull
- E getByKey(K key, DbSession session);
-
- E update(E item, DbSession session);
-
- List<E> update(List<E> items, DbSession session);
-
- E insert(E item, DbSession session);
-
- List<E> insert(List<E> items, DbSession session);
-
- void delete(E item, DbSession session);
-
- void delete(Collection<E> items, DbSession session);
-
- void deleteByKey(K key, DbSession session);
-
- Iterable<K> keysOfRowsUpdatedAfter(long timestamp, DbSession session);
-
-}
+++ /dev/null
-/*
- * 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.core.db;
-
-import java.io.Serializable;
-import java.util.Date;
-
-public abstract class Dto<K extends Serializable> {
-
- private Date createdAt;
- private Date updatedAt;
-
- public abstract K getKey();
-
- public void setCreatedAt(Date datetime){
- this.createdAt = datetime;
- }
-
- public void setUpdatedAt(Date datetime){
- this.updatedAt = datetime;
- }
-
- public Date getCreatedAt(){
- return this.createdAt;
- }
-
- public Date getUpdatedAt(){
- return this.updatedAt;
- }
-}
+++ /dev/null
-/*
- * 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.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.core.db;
-
-import javax.annotation.ParametersAreNonnullByDefault;
--- /dev/null
+/*
+ * 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.core.persistence;
+
+import java.io.Serializable;
+import java.util.Date;
+
+public abstract class Dto<K extends Serializable> {
+
+ private Date createdAt;
+ private Date updatedAt;
+
+ public abstract K getKey();
+
+ public void setCreatedAt(Date datetime){
+ this.createdAt = datetime;
+ }
+
+ public void setUpdatedAt(Date datetime){
+ this.updatedAt = datetime;
+ }
+
+ public Date getCreatedAt(){
+ return this.createdAt;
+ }
+
+ public Date getUpdatedAt(){
+ return this.updatedAt;
+ }
+}
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.SeverityUtil;
package org.sonar.core.qualityprofile.db;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.rule.RuleKey;
import org.sonar.check.Cardinality;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
import org.sonar.api.utils.System2;
-import org.sonar.core.db.Dao;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import org.sonar.core.persistence.DbSession;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.search.IndexDefinition;
import org.sonar.server.search.action.DtoIndexAction;
import org.sonar.server.search.action.EmbeddedIndexAction;
return doGetByKey(key, session);
}
+ public E getNonNullByKey(K key, DbSession session) {
+ E value = doGetByKey(key, session);
+ if (value == null) {
+ throw new NotFoundException(String.format("Key '%s' not found", key));
+ }
+ return value;
+ }
+
@Override
public E update(E item, DbSession session) {
item.setUpdatedAt(new Date(system2.now()));
@Override
public void delete(E item, DbSession session) {
- Preconditions.checkNotNull(item.getKey(),"Dto does not have a valid Key");
+ Preconditions.checkNotNull(item.getKey(), "Dto does not have a valid Key");
deleteByKey(item.getKey(), session);
}
protected void enqueueUpdate(Object nestedItem, K key, DbSession session) {
session.enqueue(new EmbeddedIndexAction<K>(
- this.getIndexType(), IndexAction.Method.UPDATE, nestedItem, key));
+ this.getIndexType(), IndexAction.Method.UPDATE, nestedItem, key));
}
public void enqueueDelete(Object nestedItem, K key, DbSession session) {
session.enqueue(new EmbeddedIndexAction<K>(
- this.getIndexType(), IndexAction.Method.DELETE, nestedItem, key));
+ this.getIndexType(), IndexAction.Method.DELETE, nestedItem, key));
}
public void enqueueInsert(Object nestedItem, K key, DbSession session) {
session.enqueue(new EmbeddedIndexAction<K>(
- this.getIndexType(), IndexAction.Method.INSERT, nestedItem, key));
+ this.getIndexType(), IndexAction.Method.INSERT, nestedItem, key));
}
}
--- /dev/null
+/*
+ * 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.db;
+
+import org.sonar.api.ServerComponent;
+import org.sonar.core.persistence.Dto;
+import org.sonar.core.persistence.DbSession;
+
+import javax.annotation.CheckForNull;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+
+public interface Dao<E extends Dto<K>, K extends Serializable> extends ServerComponent {
+
+ @CheckForNull
+ E getByKey(K key, DbSession session);
+
+ E getNonNullByKey(K key, DbSession session);
+
+ E update(E item, DbSession session);
+
+ List<E> update(List<E> items, DbSession session);
+
+ E insert(E item, DbSession session);
+
+ List<E> insert(List<E> items, DbSession session);
+
+ void delete(E item, DbSession session);
+
+ void delete(Collection<E> items, DbSession session);
+
+ void deleteByKey(K key, DbSession session);
+
+ Iterable<K> keysOfRowsUpdatedAfter(long timestamp, DbSession session);
+
+}
*/
package org.sonar.server.rule2;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.rule2.index.RuleIndex;
import org.sonar.server.rule2.index.RuleNormalizer;
import org.sonar.server.rule2.index.RuleQuery;
import org.sonar.server.user.UserSession;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+import java.util.Date;
import java.util.Set;
/**
DbSession dbSession = db.openSession(false);
try {
- RuleDto rule = db.ruleDao().getByKey(ruleKey, dbSession);
- if (rule == null) {
- throw new NotFoundException(String.format("Rule %s not found", ruleKey));
- }
+ RuleDto rule = db.ruleDao().getNonNullByKey(ruleKey, dbSession);
boolean changed = RuleTagHelper.applyTags(rule, tags);
if (changed) {
db.ruleDao().update(rule, dbSession);
}
}
+ /**
+ * Extend rule description by adding a note.
+ * @param ruleKey the required key
+ * @param markdownNote markdown text. <code>null</code> to remove current note.
+ */
+ public void setNote(RuleKey ruleKey, @Nullable String markdownNote) {
+ UserSession userSession = UserSession.get();
+ checkAdminPermission(userSession);
+ DbSession dbSession = db.openSession(false);
+ try {
+ RuleDto rule = db.ruleDao().getNonNullByKey(ruleKey, dbSession);
+ if (StringUtils.isBlank(markdownNote)) {
+ rule.setNoteData(null);
+ rule.setNoteCreatedAt(null);
+ rule.setNoteUpdatedAt(null);
+ rule.setNoteUserLogin(null);
+ } else {
+ rule.setNoteData(markdownNote);
+ rule.setNoteCreatedAt(rule.getNoteCreatedAt() != null ? rule.getNoteCreatedAt() : new Date());
+ rule.setNoteUpdatedAt(new Date());
+ rule.setNoteUserLogin(userSession.login());
+ }
+ db.ruleDao().update(rule, dbSession);
+ dbSession.commit();
+ } finally {
+ dbSession.close();
+ }
+ }
+
private void checkAdminPermission(UserSession userSession) {
userSession.checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.core.cluster.WorkQueue;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import org.sonar.server.es.ESNode;
import java.io.IOException;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import org.sonar.server.db.DbClient;
import java.io.IOException;
import org.elasticsearch.index.query.QueryBuilder;
import org.picocontainer.Startable;
import org.sonar.api.ServerComponent;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import javax.annotation.CheckForNull;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.core.cluster.WorkQueue;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
import java.io.Serializable;
*/
package org.sonar.server.search.action;
-import org.sonar.core.db.Dto;
+import org.sonar.core.persistence.Dto;
public class DtoIndexAction<E extends Dto> extends IndexAction {
--- /dev/null
+/*
+ * 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.db;
+
+import org.junit.Test;
+import org.sonar.core.persistence.Dto;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.server.exceptions.NotFoundException;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class BaseDaoTest {
+
+ DbSession dbSession = mock(DbSession.class);
+
+ @Test
+ public void getNonNullByKey() throws Exception {
+ BaseDao dao = mock(BaseDao.class);
+ FakeDto dto = new FakeDto("ki");
+ when(dao.doGetByKey("ki", dbSession)).thenReturn(dto);
+ when(dao.getNonNullByKey(anyString(), any(DbSession.class))).thenCallRealMethod();
+
+ assertThat(dao.getNonNullByKey("ki", dbSession)).isSameAs(dto);
+
+ try {
+ dao.getNonNullByKey("unknown", dbSession);
+ fail();
+ } catch (NotFoundException e) {
+ assertThat(e).hasMessage("Key 'unknown' not found");
+ }
+ }
+
+
+ static class FakeDto extends Dto<String> {
+ private final String key;
+
+ public FakeDto(String key) {
+ this.key = key;
+ }
+
+ @Override
+ public String getKey() {
+ return key;
+ }
+ }
+}
--- /dev/null
+/*
+ * 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 com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rule.Severity;
+import org.sonar.api.server.rule.RuleParamType;
+import org.sonar.check.Cardinality;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.core.rule.RuleParamDto;
+import org.sonar.server.rule2.index.RuleIndex;
+import org.sonar.server.rule2.persistence.RuleDao;
+import org.sonar.server.tester.ServerTester;
+
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+/**
+ * Test persistence in db and indexation in es (--> integration of daos and indexes)
+ */
+public class RuleDataMediumTest {
+
+ @ClassRule
+ public static ServerTester tester = new ServerTester();
+
+ RuleDao dao = tester.get(RuleDao.class);
+ RuleIndex index = tester.get(RuleIndex.class);
+ DbSession dbSession;
+
+ @Before
+ public void before() {
+ tester.clearDataStores();
+ dbSession = tester.get(MyBatis.class).openSession(false);
+ }
+
+ @After
+ public void after() {
+ dbSession.close();
+ }
+
+ @Test
+ public void insert_in_db_and_index_in_es() throws InterruptedException {
+ // insert db
+ RuleKey ruleKey = RuleKey.of("javascript", "S001");
+ dao.insert(newRuleDto(ruleKey), dbSession);
+ dbSession.commit();
+
+ // verify that rule is persisted in db
+ RuleDto persistedDto = dao.getByKey(ruleKey, dbSession);
+ assertThat(persistedDto).isNotNull();
+ assertThat(persistedDto.getId()).isGreaterThanOrEqualTo(0);
+ assertThat(persistedDto.getRuleKey()).isEqualTo(ruleKey.rule());
+ assertThat(persistedDto.getLanguage()).isEqualTo("js");
+ assertThat(persistedDto.getTags()).containsOnly("tag1", "tag2");
+ assertThat(persistedDto.getSystemTags()).containsOnly("systag1", "systag2");
+ assertThat(persistedDto.getCreatedAt()).isNotNull();
+ assertThat(persistedDto.getUpdatedAt()).isNotNull();
+
+ // verify that rule is indexed in es
+ index.refresh();
+ Rule hit = index.getByKey(ruleKey);
+ assertThat(hit).isNotNull();
+ assertThat(hit.key().repository()).isEqualTo(ruleKey.repository());
+ assertThat(hit.key().rule()).isEqualTo(ruleKey.rule());
+ assertThat(hit.language()).isEqualTo("js");
+ assertThat(hit.name()).isEqualTo("Rule S001");
+ assertThat(hit.htmlDescription()).isEqualTo("Description S001");
+ assertThat(hit.status()).isEqualTo(RuleStatus.READY);
+ assertThat(hit.createdAt()).isNotNull();
+ assertThat(hit.updatedAt()).isNotNull();
+ assertThat(hit.internalKey()).isEqualTo("InternalKeyS001");
+ assertThat(hit.severity()).isEqualTo("INFO");
+ assertThat(hit.template()).isFalse();
+ assertThat(hit.tags()).containsOnly("tag1", "tag2");
+ assertThat(hit.systemTags()).containsOnly("systag1", "systag2");
+ }
+
+ @Test
+ public void insert_and_index_rule_parameters() {
+ // insert db
+ RuleKey ruleKey = RuleKey.of("javascript", "S001");
+ RuleDto ruleDto = newRuleDto(ruleKey);
+ dao.insert(ruleDto, dbSession);
+ dbSession.commit();
+
+ RuleParamDto minParamDto = new RuleParamDto()
+ .setName("min")
+ .setType(RuleParamType.INTEGER.type())
+ .setDefaultValue("2")
+ .setDescription("Minimum");
+ dao.addRuleParam(ruleDto, minParamDto, dbSession);
+ RuleParamDto maxParamDto = new RuleParamDto()
+ .setName("max")
+ .setType(RuleParamType.INTEGER.type())
+ .setDefaultValue("10")
+ .setDescription("Maximum");
+ dao.addRuleParam(ruleDto, maxParamDto, dbSession);
+ dbSession.commit();
+
+ //Verify that RuleDto has date from insertion
+ RuleDto theRule = dao.getByKey(ruleKey, dbSession);
+ assertThat(theRule.getCreatedAt()).isNotNull();
+ assertThat(theRule.getUpdatedAt()).isNotNull();
+
+ // verify that parameters are persisted in db
+ List<RuleParamDto> persistedDtos = dao.findRuleParamsByRuleKey(theRule.getKey(), dbSession);
+ assertThat(persistedDtos).hasSize(2);
+
+ // verify that parameters are indexed in es
+ index.refresh();
+ Rule hit = index.getByKey(ruleKey);
+ assertThat(hit).isNotNull();
+ assertThat(hit.key()).isNotNull();
+
+ RuleService service = tester.get(RuleService.class);
+ Rule rule = service.getByKey(ruleKey);
+
+ assertThat(rule.params()).hasSize(2);
+ assertThat(Iterables.getLast(rule.params(), null).key()).isEqualTo("max");
+ }
+
+ @Test
+ public void insert_and_update_rule() {
+ // insert db
+ RuleKey ruleKey = RuleKey.of("javascript", "S001");
+ RuleDto ruleDto = newRuleDto(ruleKey)
+ .setTags(ImmutableSet.of("hello"))
+ .setName("first name");
+ dao.insert(ruleDto, dbSession);
+ dbSession.commit();
+
+ // verify that parameters are indexed in es
+ index.refresh();
+ Rule hit = index.getByKey(ruleKey);
+ assertThat(hit.tags()).containsExactly("hello");
+ assertThat(hit.name()).isEqualTo("first name");
+
+ //Update in DB
+ ruleDto.setTags(ImmutableSet.of("world"))
+ .setName("second name");
+ dao.update(ruleDto, dbSession);
+ dbSession.commit();
+
+ // verify that parameters are updated in es
+ index.refresh();
+ hit = index.getByKey(ruleKey);
+ assertThat(hit.tags()).containsExactly("world");
+ assertThat(hit.name()).isEqualTo("second name");
+ }
+
+ @Test
+ public void insert_and_update_rule_param() {
+
+ // insert db
+ RuleKey ruleKey = RuleKey.of("javascript", "S001");
+ RuleDto ruleDto = newRuleDto(ruleKey)
+ .setTags(ImmutableSet.of("hello"))
+ .setName("first name");
+ dao.insert(ruleDto, dbSession);
+ dbSession.commit();
+
+ RuleParamDto minParamDto = new RuleParamDto()
+ .setName("min")
+ .setType(RuleParamType.INTEGER.type())
+ .setDefaultValue("2")
+ .setDescription("Minimum");
+ dao.addRuleParam(ruleDto, minParamDto, dbSession);
+
+ RuleParamDto maxParamDto = new RuleParamDto()
+ .setName("max")
+ .setType(RuleParamType.INTEGER.type())
+ .setDefaultValue("10")
+ .setDescription("Maximum");
+ dao.addRuleParam(ruleDto, maxParamDto, dbSession);
+ dbSession.commit();
+
+ // verify that parameters are indexed in es
+ index.refresh();
+ Rule hit = index.getByKey(ruleKey);
+ assertThat(hit.params()).hasSize(2);
+
+ RuleParam param = hit.params().get(0);
+ assertThat(param.key()).isEqualTo("min");
+ assertThat(param.defaultValue()).isEqualTo("2");
+ assertThat(param.description()).isEqualTo("Minimum");
+
+
+ //Update in DB
+ minParamDto
+ .setDefaultValue("0.5")
+ .setDescription("new description");
+ dao.updateRuleParam(ruleDto, minParamDto, dbSession);
+ dbSession.commit();
+
+ // verify that parameters are updated in es
+ index.refresh();
+ hit = index.getByKey(ruleKey);
+ assertThat(hit.params()).hasSize(2);
+
+ param = hit.params().get(0);
+ assertThat(param.key()).isEqualTo("min");
+ assertThat(param.defaultValue()).isEqualTo("0.5");
+ assertThat(param.description()).isEqualTo("new description");
+ }
+
+ private RuleDto newRuleDto(RuleKey ruleKey) {
+ return new RuleDto()
+ .setRuleKey(ruleKey.rule())
+ .setRepositoryKey(ruleKey.repository())
+ .setName("Rule " + ruleKey.rule())
+ .setDescription("Description " + ruleKey.rule())
+ .setStatus(RuleStatus.READY.toString())
+ .setConfigKey("InternalKey" + ruleKey.rule())
+ .setSeverity(Severity.INFO)
+ .setCardinality(Cardinality.SINGLE)
+ .setLanguage("js")
+ .setTags(ImmutableSet.of("tag1", "tag2"))
+ .setSystemTags(ImmutableSet.of("systag1", "systag2"))
+ .setRemediationFunction("linear")
+ .setDefaultRemediationFunction("linear_offset")
+ .setRemediationCoefficient("1h")
+ .setDefaultRemediationCoefficient("5d")
+ .setRemediationOffset("5min")
+ .setDefaultRemediationOffset("10h")
+ .setEffortToFixDescription(ruleKey.repository() + "." + ruleKey.rule() + ".effortToFix");
+ }
+}
package org.sonar.server.rule2;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import org.junit.After;
import org.junit.Before;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
-import org.sonar.api.server.rule.RuleParamType;
import org.sonar.check.Cardinality;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.rule.RuleDto;
-import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.rule2.index.RuleIndex;
import org.sonar.server.user.MockUserSession;
import java.util.Collections;
-import java.util.List;
import java.util.Set;
import static org.fest.assertions.Assertions.assertThat;
@ClassRule
public static ServerTester tester = new ServerTester();
- MyBatis myBatis = tester.get(MyBatis.class);
RuleDao dao = tester.get(RuleDao.class);
RuleIndex index = tester.get(RuleIndex.class);
RuleService service = tester.get(RuleService.class);
@Before
public void before() {
tester.clearDataStores();
- dbSession = myBatis.openSession(false);
+ dbSession = tester.get(MyBatis.class).openSession(false);
}
@After
dbSession.close();
}
- @Test
- public void insert_in_db_and_index_in_es() throws InterruptedException {
- // insert db
- RuleKey ruleKey = RuleKey.of("javascript", "S001");
- dao.insert(newRuleDto(ruleKey), dbSession);
- dbSession.commit();
-
- // verify that rule is persisted in db
- RuleDto persistedDto = dao.getByKey(ruleKey, dbSession);
- assertThat(persistedDto).isNotNull();
- assertThat(persistedDto.getId()).isGreaterThanOrEqualTo(0);
- assertThat(persistedDto.getRuleKey()).isEqualTo(ruleKey.rule());
- assertThat(persistedDto.getLanguage()).isEqualTo("js");
- assertThat(persistedDto.getTags()).containsOnly("tag1", "tag2");
- assertThat(persistedDto.getSystemTags()).containsOnly("systag1", "systag2");
- assertThat(persistedDto.getCreatedAt()).isNotNull();
- assertThat(persistedDto.getUpdatedAt()).isNotNull();
-
- // verify that rule is indexed in es
- index.refresh();
- Rule hit = index.getByKey(ruleKey);
- assertThat(hit).isNotNull();
- assertThat(hit.key().repository()).isEqualTo(ruleKey.repository());
- assertThat(hit.key().rule()).isEqualTo(ruleKey.rule());
- assertThat(hit.language()).isEqualTo("js");
- assertThat(hit.name()).isEqualTo("Rule S001");
- assertThat(hit.htmlDescription()).isEqualTo("Description S001");
- assertThat(hit.status()).isEqualTo(RuleStatus.READY);
- assertThat(hit.createdAt()).isNotNull();
- assertThat(hit.updatedAt()).isNotNull();
- assertThat(hit.internalKey()).isEqualTo("InternalKeyS001");
- assertThat(hit.severity()).isEqualTo("INFO");
- assertThat(hit.template()).isFalse();
- assertThat(hit.tags()).containsOnly("tag1", "tag2");
- assertThat(hit.systemTags()).containsOnly("systag1", "systag2");
- }
-
- @Test
- public void insert_and_index_rule_parameters() {
- // insert db
- RuleKey ruleKey = RuleKey.of("javascript", "S001");
- RuleDto ruleDto = newRuleDto(ruleKey);
- dao.insert(ruleDto, dbSession);
- dbSession.commit();
-
- RuleParamDto minParamDto = new RuleParamDto()
- .setName("min")
- .setType(RuleParamType.INTEGER.type())
- .setDefaultValue("2")
- .setDescription("Minimum");
- dao.addRuleParam(ruleDto, minParamDto, dbSession);
- RuleParamDto maxParamDto = new RuleParamDto()
- .setName("max")
- .setType(RuleParamType.INTEGER.type())
- .setDefaultValue("10")
- .setDescription("Maximum");
- dao.addRuleParam(ruleDto, maxParamDto, dbSession);
- dbSession.commit();
-
- //Verify that RuleDto has date from insertion
- RuleDto theRule = dao.getByKey(ruleKey, dbSession);
- assertThat(theRule.getCreatedAt()).isNotNull();
- assertThat(theRule.getUpdatedAt()).isNotNull();
-
- // verify that parameters are persisted in db
- List<RuleParamDto> persistedDtos = dao.findRuleParamsByRuleKey(theRule.getKey(), dbSession);
- assertThat(persistedDtos).hasSize(2);
-
- // verify that parameters are indexed in es
- index.refresh();
- Rule hit = index.getByKey(ruleKey);
- assertThat(hit).isNotNull();
- assertThat(hit.key()).isNotNull();
-
- RuleService service = tester.get(RuleService.class);
- Rule rule = service.getByKey(ruleKey);
-
- assertThat(rule.params()).hasSize(2);
- assertThat(Iterables.getLast(rule.params(), null).key()).isEqualTo("max");
- }
-
- @Test
- public void insert_and_update_rule() {
-
- // insert db
- RuleKey ruleKey = RuleKey.of("javascript", "S001");
- RuleDto ruleDto = newRuleDto(ruleKey)
- .setTags(ImmutableSet.of("hello"))
- .setName("first name");
- dao.insert(ruleDto, dbSession);
- dbSession.commit();
-
- // verify that parameters are indexed in es
- index.refresh();
- Rule hit = index.getByKey(ruleKey);
- assertThat(hit.tags()).containsExactly("hello");
- assertThat(hit.name()).isEqualTo("first name");
-
- //Update in DB
- ruleDto.setTags(ImmutableSet.of("world"))
- .setName("second name");
- dao.update(ruleDto, dbSession);
- dbSession.commit();
-
- // verify that parameters are updated in es
- index.refresh();
- hit = index.getByKey(ruleKey);
- assertThat(hit.tags()).containsExactly("world");
- assertThat(hit.name()).isEqualTo("second name");
- }
-
- @Test
- public void insert_and_update_rule_param() {
-
- // insert db
- RuleKey ruleKey = RuleKey.of("javascript", "S001");
- RuleDto ruleDto = newRuleDto(ruleKey)
- .setTags(ImmutableSet.of("hello"))
- .setName("first name");
- dao.insert(ruleDto, dbSession);
- dbSession.commit();
-
- RuleParamDto minParamDto = new RuleParamDto()
- .setName("min")
- .setType(RuleParamType.INTEGER.type())
- .setDefaultValue("2")
- .setDescription("Minimum");
- dao.addRuleParam(ruleDto, minParamDto, dbSession);
-
- RuleParamDto maxParamDto = new RuleParamDto()
- .setName("max")
- .setType(RuleParamType.INTEGER.type())
- .setDefaultValue("10")
- .setDescription("Maximum");
- dao.addRuleParam(ruleDto, maxParamDto, dbSession);
- dbSession.commit();
-
- // verify that parameters are indexed in es
- index.refresh();
- Rule hit = index.getByKey(ruleKey);
- assertThat(hit.params()).hasSize(2);
-
- RuleParam param = hit.params().get(0);
- assertThat(param.key()).isEqualTo("min");
- assertThat(param.defaultValue()).isEqualTo("2");
- assertThat(param.description()).isEqualTo("Minimum");
-
-
- //Update in DB
- minParamDto
- .setDefaultValue("0.5")
- .setDescription("new description");
- dao.updateRuleParam(ruleDto, minParamDto, dbSession);
- dbSession.commit();
-
- // verify that parameters are updated in es
- index.refresh();
- hit = index.getByKey(ruleKey);
- assertThat(hit.params()).hasSize(2);
-
- param = hit.params().get(0);
- assertThat(param.key()).isEqualTo("min");
- assertThat(param.defaultValue()).isEqualTo("0.5");
- assertThat(param.description()).isEqualTo("new description");
- }
-
@Test
public void setTags() throws InterruptedException {
MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
dao.insert(newRuleDto(rule1)
.setTags(Sets.newHashSet("security"))
.setSystemTags(Collections.<String>emptySet()),
- dbSession
- );
+ dbSession);
RuleKey rule2 = RuleKey.of("java", "S001");
dao.insert(newRuleDto(rule2)
service.setTags(RuleKey.of("java", "S001"), Sets.newHashSet("bug", "security"));
fail();
} catch (NotFoundException e) {
- assertThat(e).hasMessage("Rule java:S001 not found");
+ assertThat(e).hasMessage("Key 'java:S001' not found");
}
}
}
}
+ @Test
+ public void setNote() throws Exception {
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("marius");
+ RuleKey ruleKey = RuleKey.of("javascript", "S001");
+ dao.insert(newRuleDto(ruleKey), dbSession);
+ dbSession.commit();
+
+ // 1. CREATE NOTE
+ service.setNote(ruleKey, "my *note*");
+
+ // verify db
+ RuleDto dto = dao.getNonNullByKey(ruleKey, dbSession);
+ assertThat(dto.getNoteData()).isEqualTo("my *note*");
+ assertThat(dto.getNoteCreatedAt()).isNotNull();
+ assertThat(dto.getNoteUpdatedAt()).isNotNull();
+ assertThat(dto.getNoteUserLogin()).isEqualTo("marius");
+
+ // verify es
+ index.refresh();
+ Rule rule = index.getByKey(ruleKey);
+ // TODO
+// assertThat(rule.getNote()).isEqualTo("my *note*");
+// assertThat(rule.getNoteCreatedAt()).isNotNull();
+// assertThat(rule.getNoteUpdatedAt()).isNotNull();
+// assertThat(rule.getNoteUserLogin()).isEqualTo("marius");
+
+ // 2. DELETE NOTE
+ service.setNote(ruleKey, null);
+ dto = dao.getNonNullByKey(ruleKey, dbSession);
+ assertThat(dto.getNoteData()).isNull();
+ assertThat(dto.getNoteCreatedAt()).isNull();
+ assertThat(dto.getNoteUpdatedAt()).isNull();
+ assertThat(dto.getNoteUserLogin()).isNull();
+ index.refresh();
+ rule = index.getByKey(ruleKey);
+ // TODO
+ // assertThat(rule.getNote()).isNull();
+// assertThat(rule.getNoteCreatedAt()).isNull();
+// assertThat(rule.getNoteUpdatedAt()).isNull();
+// assertThat(rule.getNoteUserLogin()).isNull();
+
+ }
+
private RuleDto newRuleDto(RuleKey ruleKey) {
return new RuleDto()
.setRuleKey(ruleKey.rule())