12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202 |
- /*
- * SonarQube
- * Copyright (C) 2009-2020 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.rule;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Optional;
- import java.util.Set;
- import java.util.function.Consumer;
- import org.apache.ibatis.exceptions.PersistenceException;
- import org.apache.ibatis.session.ResultHandler;
- import org.junit.Before;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.ExpectedException;
- import org.sonar.api.rule.RuleKey;
- import org.sonar.api.rule.RuleStatus;
- import org.sonar.api.rule.Severity;
- import org.sonar.api.rules.RuleQuery;
- import org.sonar.api.rules.RuleType;
- import org.sonar.api.server.debt.DebtRemediationFunction;
- import org.sonar.api.utils.DateUtils;
- import org.sonar.api.utils.System2;
- import org.sonar.db.DbTester;
- import org.sonar.db.RowNotFoundException;
- import org.sonar.db.es.RuleExtensionId;
- import org.sonar.db.organization.OrganizationDto;
- import org.sonar.db.organization.OrganizationTesting;
- import org.sonar.db.rule.RuleDto.Scope;
-
- import static com.google.common.collect.Sets.newHashSet;
- import static java.util.Arrays.asList;
- import static java.util.Collections.emptyList;
- import static java.util.Collections.singletonList;
- import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.assertj.core.api.Assertions.tuple;
- import static org.sonar.api.rule.RuleStatus.REMOVED;
- import static org.sonar.db.rule.RuleTesting.newRuleMetadata;
-
- public class RuleDaoTest {
-
- private static final String ORGANIZATION_UUID = "org-1";
- private static final String UNKNOWN_RULE_UUID = "unknown-uuid";
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private RuleDao underTest = db.getDbClient().ruleDao();
- private OrganizationDto organization;
-
- @Before
- public void before() {
- organization = db.organizations().insert(o -> o.setUuid(ORGANIZATION_UUID));
- }
-
- @Test
- public void selectByKey() {
- RuleDefinitionDto ruleDefinition = db.rules().insert();
- OrganizationDto organization = db.organizations().insert();
- RuleMetadataDto metadata = newRuleMetadata(ruleDefinition, organization);
- db.rules().insertRule(ruleDefinition, metadata);
-
- assertThat(underTest.selectByKey(db.getSession(), organization.getUuid(), RuleKey.of("foo", "bar")))
- .isEmpty();
- RuleDto rule = underTest.selectByKey(db.getSession(), organization.getUuid(), ruleDefinition.getKey()).get();
- assertEquals(rule.getDefinition(), ruleDefinition);
- verifyMetadata(rule.getMetadata(), ruleDefinition, metadata);
- }
-
- @Test
- public void selectByKey_return_rule_even_if_organization_does_not_exist() {
- RuleDefinitionDto ruleDefinition = db.rules().insert();
-
- assertThat(underTest.selectByKey(db.getSession(), OrganizationTesting.newOrganizationDto().getUuid(), ruleDefinition.getKey()))
- .isNotEmpty();
- }
-
- @Test
- public void selectByKey_populates_organizationUuid_even_when_organization_has_no_metadata() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto ruleDefinition = db.rules().insert();
-
- RuleDto rule = underTest.selectByKey(db.getSession(), organization.getUuid(), ruleDefinition.getKey()).get();
- verifyNoMetadata(rule.getMetadata(), organization);
- }
-
- @Test
- public void selectByKey_returns_metadata_of_specified_organization() {
- RuleDefinitionDto ruleDefinition = db.rules().insert();
- OrganizationDto organization1 = db.organizations().insert();
- RuleMetadataDto expectedOrg1 = newRuleMetadata(ruleDefinition, organization1);
- db.rules().insertRule(ruleDefinition, expectedOrg1);
- OrganizationDto organization2 = db.organizations().insert();
- RuleMetadataDto expectedOrg2 = newRuleMetadata(ruleDefinition, organization2);
- db.rules().insertRule(ruleDefinition, expectedOrg2);
-
- RuleDto rule = underTest.selectByKey(db.getSession(), organization1.getUuid(), ruleDefinition.getKey()).get();
- verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg1);
- rule = underTest.selectByKey(db.getSession(), organization2.getUuid(), ruleDefinition.getKey()).get();
- verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg2);
- }
-
- @Test
- public void selectDefinitionByKey() {
- RuleDefinitionDto rule = db.rules().insert();
-
- assertThat(underTest.selectDefinitionByKey(db.getSession(), RuleKey.of("NOT", "FOUND")).isPresent()).isFalse();
-
- Optional<RuleDefinitionDto> reloaded = underTest.selectDefinitionByKey(db.getSession(), rule.getKey());
- assertThat(reloaded.isPresent()).isTrue();
- }
-
- @Test
- public void selectByUuid() {
- RuleDefinitionDto ruleDefinition = db.rules().insert();
- OrganizationDto organization = db.organizations().insert();
- RuleMetadataDto metadata = newRuleMetadata(ruleDefinition, organization);
- RuleDto expected = db.rules().insertRule(ruleDefinition, metadata);
-
- assertThat(underTest.selectByUuid(expected.getUuid() + 500, organization.getUuid(), db.getSession()))
- .isEmpty();
- RuleDto rule = underTest.selectByUuid(expected.getUuid(), organization.getUuid(), db.getSession()).get();
- assertEquals(rule.getDefinition(), ruleDefinition);
- verifyMetadata(rule.getMetadata(), ruleDefinition, metadata);
- }
-
- @Test
- public void selectByUuid_return_rule_even_if_organization_does_not_exist() {
- RuleDefinitionDto ruleDefinition = db.rules().insert();
-
- assertThat(underTest.selectByUuid(ruleDefinition.getUuid(), "dfdfdf", db.getSession()))
- .isNotEmpty();
- }
-
- @Test
- public void selectByUuid_populates_organizationUuid_even_when_organization_has_no_metadata() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto ruleDefinition = db.rules().insert();
-
- RuleDto rule = underTest.selectByUuid(ruleDefinition.getUuid(), organization.getUuid(), db.getSession()).get();
- verifyNoMetadata(rule.getMetadata(), organization);
- }
-
- @Test
- public void selectByUuid_returns_metadata_of_specified_organization() {
- RuleDefinitionDto ruleDefinition = db.rules().insert();
- OrganizationDto organization1 = db.organizations().insert();
- RuleMetadataDto expectedOrg1 = newRuleMetadata(ruleDefinition, organization1);
- db.rules().insertRule(ruleDefinition, expectedOrg1);
- OrganizationDto organization2 = db.organizations().insert();
- RuleMetadataDto expectedOrg2 = newRuleMetadata(ruleDefinition, organization2);
- db.rules().insertRule(ruleDefinition, expectedOrg2);
-
- RuleDto rule = underTest.selectByUuid(ruleDefinition.getUuid(), organization1.getUuid(), db.getSession()).get();
- verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg1);
- rule = underTest.selectByUuid(ruleDefinition.getUuid(), organization2.getUuid(), db.getSession()).get();
- verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg2);
- }
-
- @Test
- public void selectDefinitionByUuid() {
- RuleDefinitionDto rule = db.rules().insert();
-
- assertThat(underTest.selectDefinitionByUuid(UNKNOWN_RULE_UUID, db.getSession())).isEmpty();
- Optional<RuleDefinitionDto> ruleDtoOptional = underTest.selectDefinitionByUuid(rule.getUuid(), db.getSession());
- assertThat(ruleDtoOptional).isPresent();
- }
-
- @Test
- public void selectByUuids() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto rule1 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule1, organization);
- RuleDefinitionDto rule2 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule2, organization);
- RuleDefinitionDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
- db.rules().insertOrUpdateMetadata(removedRule, organization);
-
- assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), singletonList(rule1.getUuid()))).hasSize(1);
- assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2);
- assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid(), UNKNOWN_RULE_UUID))).hasSize(2);
- assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid(), removedRule.getUuid()))).hasSize(3);
- assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), singletonList(UNKNOWN_RULE_UUID))).isEmpty();
- }
-
- @Test
- public void selectByUuids_populates_organizationUuid_even_when_organization_has_no_metadata() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto rule1 = db.rules().insert();
- RuleDefinitionDto rule2 = db.rules().insert();
-
- assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid())))
- .extracting(RuleDto::getOrganizationUuid)
- .containsExactly(organization.getUuid(), organization.getUuid());
- }
-
- @Test
- public void selectDefinitionByUuids() {
- RuleDefinitionDto rule1 = db.rules().insert();
- RuleDefinitionDto rule2 = db.rules().insert();
-
- assertThat(underTest.selectDefinitionByUuids(db.getSession(), singletonList(rule1.getUuid()))).hasSize(1);
- assertThat(underTest.selectDefinitionByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2);
- assertThat(underTest.selectDefinitionByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid(), UNKNOWN_RULE_UUID))).hasSize(2);
- assertThat(underTest.selectDefinitionByUuids(db.getSession(), singletonList(UNKNOWN_RULE_UUID))).isEmpty();
- }
-
- @Test
- public void selectOrFailByKey() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto rule1 = db.rules().insert();
- db.rules().insert();
-
- RuleDto rule = underTest.selectOrFailByKey(db.getSession(), organization, rule1.getKey());
- assertThat(rule.getUuid()).isEqualTo(rule1.getUuid());
- }
-
- @Test
- public void selectOrFailByKey_fails_if_rule_not_found() {
- OrganizationDto organization = db.organizations().insert();
-
- thrown.expect(RowNotFoundException.class);
- thrown.expectMessage("Rule with key 'NOT:FOUND' does not exist");
-
- underTest.selectOrFailByKey(db.getSession(), organization, RuleKey.of("NOT", "FOUND"));
- }
-
- @Test
- public void selectOrFailByKey_populates_organizationUuid_even_when_organization_has_no_metadata() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto rule = db.rules().insert();
-
- assertThat(underTest.selectOrFailByKey(db.getSession(), organization, rule.getKey()).getOrganizationUuid())
- .isEqualTo(organization.getUuid());
- }
-
- @Test
- public void selectOrFailDefinitionByKey_fails_if_rule_not_found() {
- thrown.expect(RowNotFoundException.class);
- thrown.expectMessage("Rule with key 'NOT:FOUND' does not exist");
-
- underTest.selectOrFailDefinitionByKey(db.getSession(), RuleKey.of("NOT", "FOUND"));
- }
-
- @Test
- public void selectByKeys() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto rule1 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule1, organization);
- RuleDefinitionDto rule2 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(rule2, organization);
-
- assertThat(underTest.selectByKeys(db.getSession(), organization.getUuid(), Collections.emptyList())).isEmpty();
- assertThat(underTest.selectByKeys(db.getSession(), organization.getUuid(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();
-
- List<RuleDto> rules = underTest.selectByKeys(db.getSession(), organization.getUuid(), asList(rule1.getKey(), RuleKey.of("java", "OTHER")));
- assertThat(rules).hasSize(1);
- assertThat(rules.get(0).getUuid()).isEqualTo(rule1.getUuid());
- }
-
- @Test
- public void selectByKeys_populates_organizationUuid_even_when_organization_has_no_metadata() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto rule = db.rules().insert();
-
- assertThat(underTest.selectByKeys(db.getSession(), organization.getUuid(), singletonList(rule.getKey())))
- .extracting(RuleDto::getOrganizationUuid)
- .containsExactly(organization.getUuid());
- }
-
- @Test
- public void selectDefinitionByKeys() {
- RuleDefinitionDto rule = db.rules().insert();
-
- assertThat(underTest.selectDefinitionByKeys(db.getSession(), Collections.emptyList())).isEmpty();
- assertThat(underTest.selectDefinitionByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();
-
- List<RuleDefinitionDto> rules = underTest.selectDefinitionByKeys(db.getSession(), asList(rule.getKey(), RuleKey.of("java", "OTHER")));
- assertThat(rules).hasSize(1);
- assertThat(rules.get(0).getUuid()).isEqualTo(rule.getUuid());
- }
-
- @Test
- public void selectAll() {
- OrganizationDto organization = db.organizations().insert();
- RuleDto rule1 = db.rules().insertRule(organization);
- RuleDto rule2 = db.rules().insertRule(organization);
- RuleDto rule3 = db.rules().insertRule(organization);
-
- assertThat(underTest.selectAll(db.getSession(), organization.getUuid()))
- .extracting(RuleDto::getUuid)
- .containsOnly(rule1.getUuid(), rule2.getUuid(), rule3.getUuid());
- }
-
- @Test
- public void selectAll_returns_all_rules_even_if_organization_does_not_exist() {
- RuleDefinitionDto rule1 = db.rules().insert();
- RuleDefinitionDto rule2 = db.rules().insert();
- RuleDefinitionDto rule3 = db.rules().insert();
-
- assertThat(underTest.selectAll(db.getSession(), "dfdfdf"))
- .extracting(RuleDto::getUuid)
- .containsOnly(rule1.getUuid(), rule2.getUuid(), rule3.getUuid());
- }
-
- @Test
- public void selectAll_populates_organizationUuid_even_when_organization_has_no_metadata() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto ruleDefinition1 = db.rules().insert();
- RuleDefinitionDto ruleDefinition2 = db.rules().insert();
-
- List<RuleDto> rules = underTest.selectAll(db.getSession(), organization.getUuid());
- assertThat(rules)
- .extracting(RuleDto::getUuid)
- .containsOnly(ruleDefinition1.getUuid(), ruleDefinition2.getUuid());
- assertThat(rules)
- .extracting(RuleDto::getOrganizationUuid)
- .containsExactly(organization.getUuid(), organization.getUuid());
- }
-
- @Test
- public void selectAll_returns_metadata_of_specified_organization() {
- RuleDefinitionDto ruleDefinition = db.rules().insert();
- OrganizationDto organization = db.organizations().insert();
- RuleMetadataDto expected = newRuleMetadata(ruleDefinition, organization);
- db.rules().insertRule(ruleDefinition, expected);
-
- List<RuleDto> rules = underTest.selectAll(db.getSession(), organization.getUuid());
- assertThat(rules).hasSize(1);
-
- verifyMetadata(rules.iterator().next().getMetadata(), ruleDefinition, expected);
- }
-
- private void assertEquals(RuleDefinitionDto actual, RuleDefinitionDto expected) {
- assertThat(actual.getUuid()).isEqualTo(expected.getUuid());
- assertThat(actual.getRepositoryKey()).isEqualTo(expected.getRepositoryKey());
- assertThat(actual.getRuleKey()).isEqualTo(expected.getRuleKey());
- assertThat(actual.getKey()).isEqualTo(expected.getKey());
- assertThat(actual.getDescription()).isEqualTo(expected.getDescription());
- assertThat(actual.getDescriptionFormat()).isEqualTo(expected.getDescriptionFormat());
- assertThat(actual.getStatus()).isEqualTo(expected.getStatus());
- assertThat(actual.getName()).isEqualTo(expected.getName());
- assertThat(actual.getConfigKey()).isEqualTo(expected.getConfigKey());
- assertThat(actual.getSeverity()).isEqualTo(expected.getSeverity());
- assertThat(actual.getSeverityString()).isEqualTo(expected.getSeverityString());
- assertThat(actual.isExternal()).isEqualTo(expected.isExternal());
- assertThat(actual.isTemplate()).isEqualTo(expected.isTemplate());
- assertThat(actual.getLanguage()).isEqualTo(expected.getLanguage());
- assertThat(actual.getTemplateUuid()).isEqualTo(expected.getTemplateUuid());
- assertThat(actual.getDefRemediationFunction()).isEqualTo(expected.getDefRemediationFunction());
- assertThat(actual.getDefRemediationGapMultiplier()).isEqualTo(expected.getDefRemediationGapMultiplier());
- assertThat(actual.getDefRemediationBaseEffort()).isEqualTo(expected.getDefRemediationBaseEffort());
- assertThat(actual.getGapDescription()).isEqualTo(expected.getGapDescription());
- assertThat(actual.getSystemTags()).isEqualTo(expected.getSystemTags());
- assertThat(actual.getSecurityStandards()).isEqualTo(expected.getSecurityStandards());
- assertThat(actual.getType()).isEqualTo(expected.getType());
- }
-
- private static void verifyMetadata(RuleMetadataDto metadata, RuleDefinitionDto ruleDefinition, RuleMetadataDto expected) {
- assertThat(metadata.getOrganizationUuid()).isEqualTo(expected.getOrganizationUuid());
- assertThat(metadata.getRemediationBaseEffort()).isEqualTo(expected.getRemediationBaseEffort());
- assertThat(metadata.getRemediationFunction()).isEqualTo(expected.getRemediationFunction());
- assertThat(metadata.getRemediationGapMultiplier()).isEqualTo(expected.getRemediationGapMultiplier());
- assertThat(metadata.getTags()).isEqualTo(expected.getTags());
- assertThat(metadata.getNoteData()).isEqualTo(expected.getNoteData());
- assertThat(metadata.getNoteCreatedAt()).isEqualTo(expected.getNoteCreatedAt());
- assertThat(metadata.getNoteUpdatedAt()).isEqualTo(expected.getNoteUpdatedAt());
- assertThat(metadata.getAdHocName()).isEqualTo(expected.getAdHocName());
- assertThat(metadata.getAdHocDescription()).isEqualTo(expected.getAdHocDescription());
- assertThat(metadata.getAdHocSeverity()).isEqualTo(expected.getAdHocSeverity());
- assertThat(metadata.getAdHocType()).isEqualTo(expected.getAdHocType());
- }
-
- private static void verifyNoMetadata(RuleMetadataDto metadata, OrganizationDto organization) {
- assertThat(metadata.getOrganizationUuid()).isEqualTo(organization.getUuid());
- assertThat(metadata.getRemediationBaseEffort()).isNull();
- assertThat(metadata.getRemediationFunction()).isNull();
- assertThat(metadata.getRemediationGapMultiplier()).isNull();
- assertThat(metadata.getTags()).isEmpty();
- assertThat(metadata.getNoteData()).isNull();
- assertThat(metadata.getNoteCreatedAt()).isNull();
- assertThat(metadata.getNoteUpdatedAt()).isNull();
- assertThat(metadata.getAdHocName()).isNull();
- assertThat(metadata.getAdHocDescription()).isNull();
- assertThat(metadata.getAdHocSeverity()).isNull();
- assertThat(metadata.getAdHocType()).isNull();
- }
-
- @Test
- public void selectAllDefinitions() {
- RuleDefinitionDto rule1 = db.rules().insert();
- RuleDefinitionDto rule2 = db.rules().insert();
- RuleDefinitionDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
-
- List<RuleDefinitionDto> ruleDtos = underTest.selectAllDefinitions(db.getSession());
-
- assertThat(ruleDtos).extracting(RuleDefinitionDto::getUuid).containsOnly(rule1.getUuid(), rule2.getUuid(), removedRule.getUuid());
- }
-
- @Test
- public void selectEnabled_with_ResultHandler() {
- RuleDefinitionDto rule = db.rules().insert();
- db.rules().insert(r -> r.setStatus(REMOVED));
-
- final List<RuleDefinitionDto> rules = new ArrayList<>();
- ResultHandler<RuleDefinitionDto> resultHandler = resultContext -> rules.add(resultContext.getResultObject());
- underTest.selectEnabled(db.getSession(), resultHandler);
-
- assertThat(rules.size()).isEqualTo(1);
- RuleDefinitionDto ruleDto = rules.get(0);
- assertThat(ruleDto.getUuid()).isEqualTo(rule.getUuid());
- }
-
- @Test
- public void selectByTypeAndLanguages() {
- OrganizationDto organization = db.organizations().insert();
- OrganizationDto organization2 = db.organizations().insert();
-
- RuleDefinitionDto rule1 = db.rules().insert(
- r -> r.setKey(RuleKey.of("java", "S001"))
- .setConfigKey("S1")
- .setType(RuleType.VULNERABILITY)
- .setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1, organization);
-
- RuleDefinitionDto rule2 = db.rules().insert(
- r -> r.setKey(RuleKey.of("js", "S002"))
- .setType(RuleType.SECURITY_HOTSPOT)
- .setLanguage("js"));
- db.rules().insertOrUpdateMetadata(rule2, organization);
-
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
- .extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
- .containsExactly(tuple(organization.getUuid(), rule1.getUuid(), "java", RuleType.VULNERABILITY.getDbConstant()));
-
- // Rule available also on organization2
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization2.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
- .extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
- .containsExactly(tuple(organization2.getUuid(), rule1.getUuid(), "java", RuleType.VULNERABILITY.getDbConstant()));
-
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("js")))
- .extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
- .containsExactly(tuple(organization.getUuid(), rule2.getUuid(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
-
- // Rule available also on organization2
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization2.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("js")))
- .extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
- .containsExactly(tuple(organization2.getUuid(), rule2.getUuid(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
-
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("java")))
- .isEmpty();
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("js")))
- .isEmpty();
- }
-
- @Test
- public void selectByTypeAndLanguages_return_nothing_when_no_rule_on_languages() {
- OrganizationDto organization = db.organizations().insert();
-
- RuleDefinitionDto rule1 = db.rules().insert(
- r -> r.setKey(RuleKey.of("java", "S001"))
- .setConfigKey("S1")
- .setType(RuleType.VULNERABILITY)
- .setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1, organization);
-
- RuleDefinitionDto rule2 = db.rules().insert(
- r -> r.setKey(RuleKey.of("js", "S002"))
- .setType(RuleType.VULNERABILITY)
- .setLanguage("js"));
- db.rules().insertOrUpdateMetadata(rule2, organization);
-
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("cpp")))
- .isEmpty();
- }
-
- @Test
- public void selectByTypeAndLanguages_return_nothing_when_no_rule_with_type() {
- OrganizationDto organization = db.organizations().insert();
-
- RuleDefinitionDto rule1 = db.rules().insert(
- r -> r.setKey(RuleKey.of("java", "S001"))
- .setConfigKey("S1")
- .setType(RuleType.VULNERABILITY)
- .setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1, organization);
-
- RuleDefinitionDto rule2 = db.rules().insert(
- r -> r.setKey(RuleKey.of("java", "S002"))
- .setType(RuleType.SECURITY_HOTSPOT)
- .setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule2, organization);
-
- RuleDefinitionDto rule3 = db.rules().insert(
- r -> r.setKey(RuleKey.of("java", "S003"))
- .setType(RuleType.CODE_SMELL)
- .setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule3, organization);
-
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.BUG.getDbConstant()), singletonList("java")))
- .isEmpty();
- }
-
- @Test
- public void selectByTypeAndLanguages_ignores_external_rules() {
- OrganizationDto organization = db.organizations().insert();
-
- RuleDefinitionDto rule1 = db.rules().insert(
- r -> r.setKey(RuleKey.of("java", "S001"))
- .setConfigKey("S1")
- .setType(RuleType.VULNERABILITY)
- .setIsExternal(true)
- .setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1, organization);
-
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
- .extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
- .isEmpty();
- }
-
- @Test
- public void selectByTypeAndLanguages_ignores_template_rules() {
- OrganizationDto organization = db.organizations().insert();
-
- RuleDefinitionDto rule1 = db.rules().insert(
- r -> r.setKey(RuleKey.of("java", "S001"))
- .setConfigKey("S1")
- .setType(RuleType.VULNERABILITY)
- .setIsTemplate(true)
- .setLanguage("java"));
- db.rules().insertOrUpdateMetadata(rule1, organization);
-
- assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
- .extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
- .isEmpty();
- }
-
- @Test
- public void select_by_query() {
- OrganizationDto organization = db.organizations().insert();
- RuleDefinitionDto rule1 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S001")).setConfigKey("S1"));
- db.rules().insertOrUpdateMetadata(rule1, organization);
- RuleDefinitionDto rule2 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S002")));
- db.rules().insertOrUpdateMetadata(rule2, organization);
- RuleDefinitionDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
-
- assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create())).hasSize(2);
- assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create().withKey("S001"))).hasSize(1);
- assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create().withConfigKey("S1"))).hasSize(1);
- assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create().withRepositoryKey("java"))).hasSize(2);
- assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(),
- RuleQuery.create().withKey("S001").withConfigKey("S1").withRepositoryKey("java"))).hasSize(1);
- }
-
- @Test
- public void select_by_query_populates_organizationUuid_even_when_organization_has_no_metadata() {
- OrganizationDto organization = db.organizations().insert();
- db.rules().insert();
- db.rules().insert();
-
- assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create()))
- .extracting(RuleDto::getOrganizationUuid)
- .containsExactly(organization.getUuid(), organization.getUuid());
- }
-
- @Test
- public void insert() {
- RuleDefinitionDto newRule = new RuleDefinitionDto()
- .setUuid("rule-uuid")
- .setRuleKey("NewRuleKey")
- .setRepositoryKey("plugin")
- .setName("new name")
- .setDescription("new description")
- .setDescriptionFormat(RuleDto.Format.MARKDOWN)
- .setStatus(RuleStatus.DEPRECATED)
- .setConfigKey("NewConfigKey")
- .setSeverity(Severity.INFO)
- .setIsTemplate(true)
- .setIsExternal(true)
- .setIsAdHoc(true)
- .setLanguage("dart")
- .setTemplateUuid("uuid-3")
- .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
- .setDefRemediationGapMultiplier("5d")
- .setDefRemediationBaseEffort("10h")
- .setGapDescription("squid.S115.effortToFix")
- .setSystemTags(newHashSet("systag1", "systag2"))
- .setSecurityStandards(newHashSet("owaspTop10:a1", "cwe:123"))
- .setType(RuleType.BUG)
- .setScope(Scope.ALL)
- .setCreatedAt(1_500_000_000_000L)
- .setUpdatedAt(2_000_000_000_000L);
- underTest.insert(db.getSession(), newRule);
- db.getSession().commit();
-
- RuleDefinitionDto ruleDto = underTest.selectOrFailDefinitionByKey(db.getSession(), RuleKey.of("plugin", "NewRuleKey"));
- assertThat(ruleDto.getUuid()).isNotNull();
- assertThat(ruleDto.getName()).isEqualTo("new name");
- assertThat(ruleDto.getDescription()).isEqualTo("new description");
- assertThat(ruleDto.getDescriptionFormat()).isEqualTo(RuleDto.Format.MARKDOWN);
- assertThat(ruleDto.getStatus()).isEqualTo(RuleStatus.DEPRECATED);
- assertThat(ruleDto.getRuleKey()).isEqualTo("NewRuleKey");
- assertThat(ruleDto.getRepositoryKey()).isEqualTo("plugin");
- assertThat(ruleDto.getConfigKey()).isEqualTo("NewConfigKey");
- assertThat(ruleDto.getSeverity()).isEqualTo(0);
- assertThat(ruleDto.getLanguage()).isEqualTo("dart");
- assertThat(ruleDto.isTemplate()).isTrue();
- assertThat(ruleDto.isExternal()).isTrue();
- assertThat(ruleDto.isAdHoc()).isTrue();
- assertThat(ruleDto.getTemplateUuid()).isEqualTo("uuid-3");
- assertThat(ruleDto.getDefRemediationFunction()).isEqualTo("LINEAR_OFFSET");
- assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo("5d");
- assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo("10h");
- assertThat(ruleDto.getGapDescription()).isEqualTo("squid.S115.effortToFix");
- assertThat(ruleDto.getSystemTags()).containsOnly("systag1", "systag2");
- assertThat(ruleDto.getSecurityStandards()).containsOnly("owaspTop10:a1", "cwe:123");
- assertThat(ruleDto.getScope()).isEqualTo(Scope.ALL);
- assertThat(ruleDto.getType()).isEqualTo(RuleType.BUG.getDbConstant());
- assertThat(ruleDto.getCreatedAt()).isEqualTo(1_500_000_000_000L);
- assertThat(ruleDto.getUpdatedAt()).isEqualTo(2_000_000_000_000L);
- }
-
- @Test
- public void update_RuleDefinitionDto() {
- RuleDefinitionDto rule = db.rules().insert();
- RuleDefinitionDto ruleToUpdate = new RuleDefinitionDto()
- .setUuid(rule.getUuid())
- .setRuleKey("NewRuleKey")
- .setRepositoryKey("plugin")
- .setName("new name")
- .setDescription("new description")
- .setDescriptionFormat(RuleDto.Format.MARKDOWN)
- .setStatus(RuleStatus.DEPRECATED)
- .setConfigKey("NewConfigKey")
- .setSeverity(Severity.INFO)
- .setIsTemplate(true)
- .setIsExternal(true)
- .setIsAdHoc(true)
- .setLanguage("dart")
- .setTemplateUuid("uuid-3")
- .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
- .setDefRemediationGapMultiplier("5d")
- .setDefRemediationBaseEffort("10h")
- .setGapDescription("squid.S115.effortToFix")
- .setSystemTags(newHashSet("systag1", "systag2"))
- .setSecurityStandards(newHashSet("owaspTop10:a1", "cwe:123"))
- .setScope(Scope.ALL)
- .setType(RuleType.BUG)
- .setUpdatedAt(2_000_000_000_000L);
-
- underTest.update(db.getSession(), ruleToUpdate);
- db.getSession().commit();
-
- RuleDefinitionDto ruleDto = underTest.selectOrFailDefinitionByKey(db.getSession(), RuleKey.of("plugin", "NewRuleKey"));
- assertThat(ruleDto.getName()).isEqualTo("new name");
- assertThat(ruleDto.getDescription()).isEqualTo("new description");
- assertThat(ruleDto.getDescriptionFormat()).isEqualTo(RuleDto.Format.MARKDOWN);
- assertThat(ruleDto.getStatus()).isEqualTo(RuleStatus.DEPRECATED);
- assertThat(ruleDto.getRuleKey()).isEqualTo("NewRuleKey");
- assertThat(ruleDto.getRepositoryKey()).isEqualTo("plugin");
- assertThat(ruleDto.getConfigKey()).isEqualTo("NewConfigKey");
- assertThat(ruleDto.getSeverity()).isEqualTo(0);
- assertThat(ruleDto.getLanguage()).isEqualTo("dart");
- assertThat(ruleDto.isTemplate()).isTrue();
- assertThat(ruleDto.isExternal()).isTrue();
- assertThat(ruleDto.isAdHoc()).isTrue();
- assertThat(ruleDto.getTemplateUuid()).isEqualTo("uuid-3");
- assertThat(ruleDto.getDefRemediationFunction()).isEqualTo("LINEAR_OFFSET");
- assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo("5d");
- assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo("10h");
- assertThat(ruleDto.getGapDescription()).isEqualTo("squid.S115.effortToFix");
- assertThat(ruleDto.getSystemTags()).containsOnly("systag1", "systag2");
- assertThat(ruleDto.getSecurityStandards()).containsOnly("owaspTop10:a1", "cwe:123");
- assertThat(ruleDto.getScope()).isEqualTo(Scope.ALL);
- assertThat(ruleDto.getType()).isEqualTo(RuleType.BUG.getDbConstant());
- assertThat(ruleDto.getCreatedAt()).isEqualTo(rule.getCreatedAt());
- assertThat(ruleDto.getUpdatedAt()).isEqualTo(2_000_000_000_000L);
- }
-
- @Test
- public void update_RuleMetadataDto_inserts_row_in_RULE_METADATA_if_not_exists_yet() {
- RuleDefinitionDto rule = db.rules().insert();
- String organizationUuid = "org-1";
-
- RuleMetadataDto metadataToUpdate = new RuleMetadataDto()
- .setRuleUuid(rule.getUuid())
- .setOrganizationUuid(organizationUuid)
- .setNoteData("My note")
- .setNoteUserUuid("admin")
- .setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime())
- .setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime())
- .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
- .setRemediationGapMultiplier("1h")
- .setRemediationBaseEffort("5min")
- .setTags(newHashSet("tag1", "tag2"))
- .setAdHocName("ad hoc name")
- .setAdHocDescription("ad hoc desc")
- .setAdHocSeverity(Severity.BLOCKER)
- .setAdHocType(RuleType.CODE_SMELL)
- .setCreatedAt(3_500_000_000_000L)
- .setUpdatedAt(4_000_000_000_000L);
-
- underTest.insertOrUpdate(db.getSession(), metadataToUpdate);
- db.getSession().commit();
-
- OrganizationDto organization = OrganizationTesting.newOrganizationDto().setUuid(organizationUuid);
- RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), organization, rule.getKey());
- assertThat(ruleDto.getNoteData()).isEqualTo("My note");
- assertThat(ruleDto.getNoteUserUuid()).isEqualTo("admin");
- assertThat(ruleDto.getNoteCreatedAt()).isNotNull();
- assertThat(ruleDto.getNoteUpdatedAt()).isNotNull();
- assertThat(ruleDto.getRemediationFunction()).isEqualTo("LINEAR");
- assertThat(ruleDto.getRemediationGapMultiplier()).isEqualTo("1h");
- assertThat(ruleDto.getRemediationBaseEffort()).isEqualTo("5min");
- assertThat(ruleDto.getTags()).containsOnly("tag1", "tag2");
- assertThat(ruleDto.getAdHocName()).isEqualTo("ad hoc name");
- assertThat(ruleDto.getAdHocDescription()).isEqualTo("ad hoc desc");
- assertThat(ruleDto.getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
- assertThat(ruleDto.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
- assertThat(ruleDto.getSecurityStandards()).isEmpty();
- assertThat(ruleDto.getCreatedAt()).isEqualTo(3_500_000_000_000L);
- assertThat(ruleDto.getUpdatedAt()).isEqualTo(4_000_000_000_000L);
- // Info from rule definition
- assertThat(ruleDto.getDefRemediationFunction()).isEqualTo(rule.getDefRemediationFunction());
- assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo(rule.getDefRemediationGapMultiplier());
- assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo(rule.getDefRemediationBaseEffort());
- assertThat(ruleDto.getGapDescription()).isEqualTo(rule.getGapDescription());
- assertThat(ruleDto.getSystemTags()).containsAll(rule.getSystemTags());
- assertThat(ruleDto.getType()).isEqualTo(rule.getType());
- }
-
- @Test
- public void update_RuleMetadataDto_updates_row_in_RULE_METADATA_if_already_exists() {
- RuleDefinitionDto rule = db.rules().insert();
- String organizationUuid = "org-1";
- OrganizationDto organization = OrganizationTesting.newOrganizationDto().setUuid(organizationUuid);
- RuleMetadataDto metadataV1 = new RuleMetadataDto()
- .setRuleUuid(rule.getUuid())
- .setOrganizationUuid(organizationUuid)
- .setCreatedAt(3_500_000_000_000L)
- .setUpdatedAt(4_000_000_000_000L);
- RuleMetadataDto metadataV2 = new RuleMetadataDto()
- .setRuleUuid(rule.getUuid())
- .setOrganizationUuid(organizationUuid)
- .setNoteData("My note")
- .setNoteUserUuid("admin")
- .setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime())
- .setNoteUpdatedAt(DateUtils.parseDate("2013-12-20").getTime())
- .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString())
- .setRemediationGapMultiplier("1h")
- .setRemediationBaseEffort("5min")
- .setTags(newHashSet("tag1", "tag2"))
- .setAdHocName("ad hoc name")
- .setAdHocDescription("ad hoc desc")
- .setAdHocSeverity(Severity.BLOCKER)
- .setAdHocType(RuleType.CODE_SMELL)
- .setCreatedAt(6_500_000_000_000L)
- .setUpdatedAt(7_000_000_000_000L);
-
- underTest.insertOrUpdate(db.getSession(), metadataV1);
- db.commit();
-
- assertThat(db.countRowsOfTable("RULES_METADATA")).isEqualTo(1);
- RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), organization, rule.getKey());
- assertThat(ruleDto.getNoteData()).isNull();
- assertThat(ruleDto.getNoteUserUuid()).isNull();
- assertThat(ruleDto.getNoteCreatedAt()).isNull();
- assertThat(ruleDto.getNoteUpdatedAt()).isNull();
- assertThat(ruleDto.getRemediationFunction()).isNull();
- assertThat(ruleDto.getRemediationGapMultiplier()).isNull();
- assertThat(ruleDto.getRemediationBaseEffort()).isNull();
- assertThat(ruleDto.getTags()).isEmpty();
- assertThat(ruleDto.getAdHocName()).isNull();
- assertThat(ruleDto.getAdHocDescription()).isNull();
- assertThat(ruleDto.getAdHocSeverity()).isNull();
- assertThat(ruleDto.getAdHocType()).isNull();
- assertThat(ruleDto.getSecurityStandards()).isEmpty();
- assertThat(ruleDto.getCreatedAt()).isEqualTo(3_500_000_000_000L);
- assertThat(ruleDto.getUpdatedAt()).isEqualTo(4_000_000_000_000L);
-
- underTest.insertOrUpdate(db.getSession(), metadataV2);
- db.commit();
-
- ruleDto = underTest.selectOrFailByKey(db.getSession(), organization, rule.getKey());
- assertThat(ruleDto.getNoteData()).isEqualTo("My note");
- assertThat(ruleDto.getNoteUserUuid()).isEqualTo("admin");
- assertThat(ruleDto.getNoteCreatedAt()).isNotNull();
- assertThat(ruleDto.getNoteUpdatedAt()).isNotNull();
- assertThat(ruleDto.getRemediationFunction()).isEqualTo("LINEAR");
- assertThat(ruleDto.getRemediationGapMultiplier()).isEqualTo("1h");
- assertThat(ruleDto.getRemediationBaseEffort()).isEqualTo("5min");
- assertThat(ruleDto.getTags()).containsOnly("tag1", "tag2");
- assertThat(ruleDto.getAdHocName()).isEqualTo("ad hoc name");
- assertThat(ruleDto.getAdHocDescription()).isEqualTo("ad hoc desc");
- assertThat(ruleDto.getAdHocSeverity()).isEqualTo(Severity.BLOCKER);
- assertThat(ruleDto.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
- assertThat(ruleDto.getSecurityStandards()).isEmpty();
- assertThat(ruleDto.getCreatedAt()).isEqualTo(3_500_000_000_000L);
- assertThat(ruleDto.getUpdatedAt()).isEqualTo(7_000_000_000_000L);
- }
-
- @Test
- public void select_parameters_by_rule_key() {
- RuleDefinitionDto rule = db.rules().insert();
- RuleParamDto ruleParam = db.rules().insertRuleParam(rule);
-
- List<RuleParamDto> ruleDtos = underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey());
-
- assertThat(ruleDtos.size()).isEqualTo(1);
- RuleParamDto ruleDto = ruleDtos.get(0);
- assertThat(ruleDto.getUuid()).isEqualTo(ruleParam.getUuid());
- assertThat(ruleDto.getName()).isEqualTo(ruleParam.getName());
- assertThat(ruleDto.getDescription()).isEqualTo(ruleParam.getDescription());
- assertThat(ruleDto.getType()).isEqualTo(ruleParam.getType());
- assertThat(ruleDto.getRuleUuid()).isEqualTo(rule.getUuid());
- }
-
- @Test
- public void select_parameters_by_rule_keys() {
- RuleDefinitionDto rule1 = db.rules().insert();
- db.rules().insertRuleParam(rule1);
- RuleDefinitionDto rule2 = db.rules().insert();
- db.rules().insertRuleParam(rule2);
-
- assertThat(underTest.selectRuleParamsByRuleKeys(db.getSession(),
- Arrays.asList(rule1.getKey(), rule2.getKey()))).hasSize(2);
-
- assertThat(underTest.selectRuleParamsByRuleKeys(db.getSession(),
- singletonList(RuleKey.of("unknown", "Unknown")))).isEmpty();
- }
-
- @Test
- public void insert_parameter() {
- RuleDefinitionDto ruleDefinitionDto = db.rules().insert();
-
- RuleParamDto orig = RuleParamDto.createFor(ruleDefinitionDto)
- .setName("max")
- .setType("INTEGER")
- .setDefaultValue("30")
- .setDescription("My Parameter");
-
- underTest.insertRuleParam(db.getSession(), ruleDefinitionDto, orig);
-
- List<RuleParamDto> ruleParamDtos = underTest.selectRuleParamsByRuleKey(db.getSession(), ruleDefinitionDto.getKey());
- assertThat(ruleParamDtos).hasSize(1);
-
- RuleParamDto loaded = ruleParamDtos.get(0);
- assertThat(loaded.getRuleUuid()).isEqualTo(orig.getRuleUuid());
- assertThat(loaded.getName()).isEqualTo(orig.getName());
- assertThat(loaded.getType()).isEqualTo(orig.getType());
- assertThat(loaded.getDefaultValue()).isEqualTo(orig.getDefaultValue());
- assertThat(loaded.getDescription()).isEqualTo(orig.getDescription());
- }
-
- @Test
- public void should_fail_to_insert_duplicate_parameter() {
- RuleDefinitionDto ruleDefinitionDto = db.rules().insert();
-
- RuleParamDto param = RuleParamDto.createFor(ruleDefinitionDto)
- .setName("max")
- .setType("INTEGER")
- .setDefaultValue("30")
- .setDescription("My Parameter");
-
- underTest.insertRuleParam(db.getSession(), ruleDefinitionDto, param);
-
- thrown.expect(PersistenceException.class);
- underTest.insertRuleParam(db.getSession(), ruleDefinitionDto, param);
- }
-
- @Test
- public void update_parameter() {
- RuleDefinitionDto rule = db.rules().insert();
- RuleParamDto ruleParam = db.rules().insertRuleParam(rule);
-
- List<RuleParamDto> params = underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey());
- assertThat(params).hasSize(1);
- RuleParamDto param = new RuleParamDto()
- .setUuid(ruleParam.getUuid())
- .setRuleUuid(rule.getUuid())
- // Name will not be updated
- .setName("format")
- .setType("STRING")
- .setDefaultValue("^[a-z]+(\\.[a-z][a-z0-9]*)*$")
- .setDescription("Regular expression used to check the package names against.");
-
- underTest.updateRuleParam(db.getSession(), rule, param);
-
- assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey()))
- .extracting(RuleParamDto::getName, RuleParamDto::getType, RuleParamDto::getDefaultValue, RuleParamDto::getDescription)
- .containsExactlyInAnyOrder(tuple(ruleParam.getName(), "STRING", "^[a-z]+(\\.[a-z][a-z0-9]*)*$", "Regular expression used to check the package names against."));
- }
-
- @Test
- public void delete_parameter() {
- RuleDefinitionDto rule = db.rules().insert();
- RuleParamDto ruleParam = db.rules().insertRuleParam(rule);
- assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey())).hasSize(1);
-
- underTest.deleteRuleParam(db.getSession(), ruleParam.getUuid());
-
- assertThat(underTest.selectRuleParamsByRuleKey(db.getSession(), rule.getKey())).isEmpty();
- }
-
- @Test
- public void scrollIndexingRules_on_empty_table() {
- Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
-
- underTest.scrollIndexingRules(db.getSession(), accumulator);
-
- assertThat(accumulator.list).isEmpty();
- }
-
- @Test
- public void scrollIndexingRules() {
- Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
- RuleDefinitionDto r1 = db.rules().insert();
- RuleDefinitionDto r2 = db.rules().insert(r -> r.setIsExternal(true));
-
- underTest.scrollIndexingRules(db.getSession(), accumulator);
-
- assertThat(accumulator.list)
- .extracting(RuleForIndexingDto::getUuid, RuleForIndexingDto::getRuleKey)
- .containsExactlyInAnyOrder(tuple(r1.getUuid(), r1.getKey()), tuple(r2.getUuid(), r2.getKey()));
- Iterator<RuleForIndexingDto> it = accumulator.list.iterator();
- RuleForIndexingDto firstRule = it.next();
-
- assertThat(firstRule.getRepository()).isEqualTo(r1.getRepositoryKey());
- assertThat(firstRule.getPluginRuleKey()).isEqualTo(r1.getRuleKey());
- assertThat(firstRule.getName()).isEqualTo(r1.getName());
- assertThat(firstRule.getDescription()).isEqualTo(r1.getDescription());
- assertThat(firstRule.getDescriptionFormat()).isEqualTo(r1.getDescriptionFormat());
- assertThat(firstRule.getSeverity()).isEqualTo(r1.getSeverity());
- assertThat(firstRule.getStatus()).isEqualTo(r1.getStatus());
- assertThat(firstRule.isExternal()).isFalse();
- assertThat(firstRule.isTemplate()).isEqualTo(r1.isTemplate());
- assertThat(firstRule.getSystemTags()).isEqualTo(r1.getSystemTags());
- assertThat(firstRule.getSecurityStandards()).isEqualTo(r1.getSecurityStandards());
- assertThat(firstRule.getTemplateRuleKey()).isNull();
- assertThat(firstRule.getTemplateRepository()).isNull();
- assertThat(firstRule.getInternalKey()).isEqualTo(r1.getConfigKey());
- assertThat(firstRule.getLanguage()).isEqualTo(r1.getLanguage());
- assertThat(firstRule.getType()).isEqualTo(r1.getType());
- assertThat(firstRule.getCreatedAt()).isEqualTo(r1.getCreatedAt());
- assertThat(firstRule.getUpdatedAt()).isEqualTo(r1.getUpdatedAt());
-
- RuleForIndexingDto secondRule = it.next();
- assertThat(secondRule.isExternal()).isTrue();
- }
-
- @Test
- public void scrollIndexingRules_maps_rule_definition_fields_for_regular_rule_and_template_rule() {
- Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
- RuleDefinitionDto r1 = db.rules().insert();
- RuleDefinitionDto r2 = db.rules().insert(rule -> rule.setTemplateUuid(r1.getUuid()));
-
- underTest.scrollIndexingRules(db.getSession(), accumulator);
-
- assertThat(accumulator.list).hasSize(2);
- RuleForIndexingDto firstRule = accumulator.list.get(0);
- RuleForIndexingDto secondRule = accumulator.list.get(1);
-
- assertRuleDefinitionFieldsAreEquals(r1, firstRule);
- assertThat(firstRule.getTemplateRuleKey()).isNull();
- assertThat(firstRule.getTemplateRepository()).isNull();
- assertRuleDefinitionFieldsAreEquals(r2, secondRule);
- assertThat(secondRule.getTemplateRuleKey()).isEqualTo(r1.getRuleKey());
- assertThat(secondRule.getTemplateRepository()).isEqualTo(r1.getRepositoryKey());
- }
-
- @Test
- public void scrollIndexingRulesByKeys() {
- Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
- RuleDefinitionDto r1 = db.rules().insert();
- db.rules().insert();
-
- underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(r1.getUuid()), accumulator);
-
- assertThat(accumulator.list)
- .extracting(RuleForIndexingDto::getUuid, RuleForIndexingDto::getRuleKey)
- .containsExactlyInAnyOrder(tuple(r1.getUuid(), r1.getKey()));
- }
-
- @Test
- public void scrollIndexingRulesByKeys_maps_rule_definition_fields_for_regular_rule_and_template_rule() {
- Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
- RuleDefinitionDto r1 = db.rules().insert();
- RuleDefinitionDto r2 = db.rules().insert(rule -> rule.setTemplateUuid(r1.getUuid()));
-
- underTest.scrollIndexingRulesByKeys(db.getSession(), Arrays.asList(r1.getUuid(), r2.getUuid()), accumulator);
-
- assertThat(accumulator.list).hasSize(2);
- RuleForIndexingDto firstRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r1.getUuid())).findFirst().get();
- RuleForIndexingDto secondRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r2.getUuid())).findFirst().get();
-
- assertRuleDefinitionFieldsAreEquals(r1, firstRule);
- assertThat(firstRule.getTemplateRuleKey()).isNull();
- assertThat(firstRule.getTemplateRepository()).isNull();
- assertRuleDefinitionFieldsAreEquals(r2, secondRule);
- assertThat(secondRule.getTemplateRuleKey()).isEqualTo(r1.getRuleKey());
- assertThat(secondRule.getTemplateRepository()).isEqualTo(r1.getRepositoryKey());
- }
-
- private void assertRuleDefinitionFieldsAreEquals(RuleDefinitionDto r1, RuleForIndexingDto firstRule) {
- assertThat(firstRule.getUuid()).isEqualTo(r1.getUuid());
- assertThat(firstRule.getRuleKey()).isEqualTo(r1.getKey());
- assertThat(firstRule.getRepository()).isEqualTo(r1.getRepositoryKey());
- assertThat(firstRule.getPluginRuleKey()).isEqualTo(r1.getRuleKey());
- assertThat(firstRule.getName()).isEqualTo(r1.getName());
- assertThat(firstRule.getDescription()).isEqualTo(r1.getDescription());
- assertThat(firstRule.getDescriptionFormat()).isEqualTo(r1.getDescriptionFormat());
- assertThat(firstRule.getSeverity()).isEqualTo(r1.getSeverity());
- assertThat(firstRule.getSeverityAsString()).isEqualTo(SeverityUtil.getSeverityFromOrdinal(r1.getSeverity()));
- assertThat(firstRule.getStatus()).isEqualTo(r1.getStatus());
- assertThat(firstRule.isTemplate()).isEqualTo(r1.isTemplate());
- assertThat(firstRule.getSystemTags()).isEqualTo(r1.getSystemTags());
- assertThat(firstRule.getSecurityStandards()).isEqualTo(r1.getSecurityStandards());
- assertThat(firstRule.getInternalKey()).isEqualTo(r1.getConfigKey());
- assertThat(firstRule.getLanguage()).isEqualTo(r1.getLanguage());
- assertThat(firstRule.getType()).isEqualTo(r1.getType());
- assertThat(firstRule.getTypeAsRuleType()).isEqualTo(RuleType.valueOf(r1.getType()));
- assertThat(firstRule.getCreatedAt()).isEqualTo(r1.getCreatedAt());
- assertThat(firstRule.getUpdatedAt()).isEqualTo(r1.getUpdatedAt());
- }
-
- @Test
- public void scrollIndexingRulesByKeys_scrolls_nothing_if_key_does_not_exist() {
- Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
- db.rules().insert();
- String nonExistingRuleUuid = "non-existing-uuid";
-
- underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(nonExistingRuleUuid), accumulator);
-
- assertThat(accumulator.list).isEmpty();
- }
-
- @Test
- public void scrollIndexingRuleExtensions() {
- Accumulator<RuleExtensionForIndexingDto> accumulator = new Accumulator<>();
- RuleDefinitionDto r1 = db.rules().insert();
- RuleMetadataDto r1Extension = db.rules().insertOrUpdateMetadata(r1, organization, r -> r.setTagsField("t1,t2"));
- RuleDefinitionDto r2 = db.rules().insert();
- RuleMetadataDto r2Extension = db.rules().insertOrUpdateMetadata(r2, organization, r -> r.setTagsField("t1,t3"));
-
- underTest.scrollIndexingRuleExtensions(db.getSession(), accumulator);
-
- assertThat(accumulator.list)
- .extracting(RuleExtensionForIndexingDto::getRuleUuid,
- RuleExtensionForIndexingDto::getRuleKey,
- RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
- .containsExactlyInAnyOrder(
- tuple(r1.getUuid(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()),
- tuple(r2.getUuid(), r2.getKey(), organization.getUuid(), r2Extension.getTagsAsString()));
- }
-
- @Test
- public void scrollIndexingRuleExtensionsByIds() {
- Accumulator<RuleExtensionForIndexingDto> accumulator = new Accumulator<>();
- RuleDefinitionDto r1 = db.rules().insert();
- RuleMetadataDto r1Extension = db.rules().insertOrUpdateMetadata(r1, organization, r -> r.setTagsField("t1,t2"));
- RuleExtensionId r1ExtensionId = new RuleExtensionId(organization.getUuid(), r1.getUuid());
- RuleDefinitionDto r2 = db.rules().insert();
- db.rules().insertOrUpdateMetadata(r2, organization, r -> r.setTagsField("t1,t3"));
-
- underTest.scrollIndexingRuleExtensionsByIds(db.getSession(), singletonList(r1ExtensionId), accumulator);
-
- assertThat(accumulator.list)
- .extracting(RuleExtensionForIndexingDto::getRuleUuid,
- RuleExtensionForIndexingDto::getRuleKey,
- RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
- .containsExactlyInAnyOrder(
- tuple(r1.getUuid(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()));
- }
-
- @Test
- public void selectAllDeprecatedRuleKeys() {
- RuleDefinitionDto r1 = db.rules().insert();
- RuleDefinitionDto r2 = db.rules().insert();
-
- db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r1.getUuid()));
- db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r2.getUuid()));
-
- db.getSession().commit();
-
- Set<DeprecatedRuleKeyDto> deprecatedRuleKeyDtos = underTest.selectAllDeprecatedRuleKeys(db.getSession());
- assertThat(deprecatedRuleKeyDtos).hasSize(2);
- }
-
- @Test
- public void selectAllDeprecatedRuleKeys_return_values_even_if_there_is_no_rule() {
- db.rules().insertDeprecatedKey();
- db.rules().insertDeprecatedKey();
-
- Set<DeprecatedRuleKeyDto> deprecatedRuleKeyDtos = underTest.selectAllDeprecatedRuleKeys(db.getSession());
- assertThat(deprecatedRuleKeyDtos).hasSize(2);
- assertThat(deprecatedRuleKeyDtos)
- .extracting(DeprecatedRuleKeyDto::getNewRepositoryKey, DeprecatedRuleKeyDto::getNewRuleKey)
- .containsExactly(
- tuple(null, null),
- tuple(null, null));
- }
-
- @Test
- public void deleteDeprecatedRuleKeys_with_empty_list_has_no_effect() {
- db.rules().insertDeprecatedKey();
- db.rules().insertDeprecatedKey();
-
- assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
-
- underTest.deleteDeprecatedRuleKeys(db.getSession(), emptyList());
-
- assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
- }
-
- @Test
- public void deleteDeprecatedRuleKeys_with_non_existing_uuid_has_no_effect() {
- db.rules().insertDeprecatedKey(d -> d.setUuid("A1"));
- db.rules().insertDeprecatedKey(d -> d.setUuid("A2"));
-
- assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
-
- underTest.deleteDeprecatedRuleKeys(db.getSession(), asList("B1", "B2"));
-
- assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
- }
-
- @Test
- public void deleteDeprecatedRuleKeys() {
- DeprecatedRuleKeyDto deprecatedRuleKeyDto1 = db.rules().insertDeprecatedKey();
- db.rules().insertDeprecatedKey();
-
- assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(2);
-
- underTest.deleteDeprecatedRuleKeys(db.getSession(), singletonList(deprecatedRuleKeyDto1.getUuid()));
- assertThat(underTest.selectAllDeprecatedRuleKeys(db.getSession())).hasSize(1);
- }
-
- @Test
- public void insertDeprecatedRuleKey() {
- RuleDefinitionDto r1 = db.rules().insert();
- DeprecatedRuleKeyDto deprecatedRuleKeyDto = db.rules().insertDeprecatedKey(d -> d.setRuleUuid(r1.getUuid()));
-
- db.getSession().commit();
-
- Set<DeprecatedRuleKeyDto> deprecatedRuleKeyDtos = underTest.selectAllDeprecatedRuleKeys(db.getSession());
- assertThat(deprecatedRuleKeyDtos).hasSize(1);
-
- DeprecatedRuleKeyDto deprecatedRuleKeyDto1 = deprecatedRuleKeyDtos.iterator().next();
- assertThat(deprecatedRuleKeyDto1.getOldRepositoryKey()).isEqualTo(deprecatedRuleKeyDto.getOldRepositoryKey());
- assertThat(deprecatedRuleKeyDto1.getOldRuleKey()).isEqualTo(deprecatedRuleKeyDto.getOldRuleKey());
- assertThat(deprecatedRuleKeyDto1.getNewRepositoryKey()).isEqualTo(r1.getRepositoryKey());
- assertThat(deprecatedRuleKeyDto1.getNewRuleKey()).isEqualTo(r1.getRuleKey());
- assertThat(deprecatedRuleKeyDto1.getUuid()).isEqualTo(deprecatedRuleKeyDto.getUuid());
- assertThat(deprecatedRuleKeyDto1.getCreatedAt()).isEqualTo(deprecatedRuleKeyDto.getCreatedAt());
- assertThat(deprecatedRuleKeyDto1.getRuleUuid()).isEqualTo(r1.getUuid());
- }
-
- @Test
- public void insertDeprecatedRuleKey_with_same_RuleKey_should_fail() {
- String repositoryKey = randomAlphanumeric(50);
- String ruleKey = randomAlphanumeric(50);
- db.rules().insertDeprecatedKey(d -> d.setOldRepositoryKey(repositoryKey)
- .setOldRuleKey(ruleKey));
-
- thrown.expect(PersistenceException.class);
-
- db.rules().insertDeprecatedKey(d -> d.setOldRepositoryKey(repositoryKey)
- .setOldRuleKey(ruleKey));
- }
-
- private static class Accumulator<T> implements Consumer<T> {
- private final List<T> list = new ArrayList<>();
-
- @Override
- public void accept(T dto) {
- list.add(dto);
- }
- }
- }
|