Browse Source

Fix use of RowNotFoundException in tests

tags/5.2-RC1
Teryk Bellahsene 9 years ago
parent
commit
c31cbfe245

+ 9
- 2
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileFactory.java View File

@@ -30,6 +30,7 @@ import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.Verifications;

/**
@@ -147,7 +148,10 @@ public class QProfileFactory {

void setDefault(DbSession dbSession, String profileKey) {
Verifications.check(StringUtils.isNotBlank(profileKey), "Profile key must be set");
QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(dbSession, profileKey);
QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, profileKey);
if (profile == null) {
throw new NotFoundException("Quality profile not found: " + profileKey);
}
setDefault(dbSession, profile);
dbSession.commit();
}
@@ -201,7 +205,10 @@ public class QProfileFactory {
Verifications.check(newName.length() < 100, String.format("Name is too long (>%d characters)", 100));
DbSession dbSession = db.openSession(false);
try {
QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(dbSession, key);
QualityProfileDto profile = db.qualityProfileDao().selectByKey(dbSession, key);
if (profile == null) {
throw new NotFoundException("Quality profile not found: " + key);
}
if (!StringUtils.equals(newName, profile.getName())) {
if (db.qualityProfileDao().selectByNameAndLanguage(newName, profile.getLanguage(), dbSession) != null) {
throw new BadRequestException("Quality profile already exists: " + newName);

+ 5
- 4
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java View File

@@ -38,10 +38,14 @@ import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.component.ComponentDao;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.custom.CustomMeasureDao;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.metric.MetricDao;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.metric.MetricTesting;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.component.ComponentTesting;
import org.sonar.server.db.DbClient;
@@ -50,9 +54,6 @@ import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.ServerException;
import org.sonar.db.measure.custom.CustomMeasureDao;
import org.sonar.db.metric.MetricDao;
import org.sonar.db.metric.MetricTesting;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
@@ -361,7 +362,7 @@ public class CreateActionTest {
dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto(DEFAULT_PROJECT_UUID));
dbSession.commit();

expectedException.expect(IllegalStateException.class);
expectedException.expect(RowNotFoundException.class);
expectedException.expectMessage("Metric id '42' not found");

newRequest()

+ 3
- 2
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java View File

@@ -31,13 +31,14 @@ import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.component.ComponentDao;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.custom.CustomMeasureDao;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.server.component.ComponentTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.db.measure.custom.CustomMeasureDao;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
import org.sonar.test.DbTests;
@@ -101,7 +102,7 @@ public class DeleteActionTest {

@Test
public void fail_when_not_found_in_db() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expect(RowNotFoundException.class);

newRequest().setParam(PARAM_ID, "42").execute();
}

+ 4
- 3
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java View File

@@ -34,19 +34,20 @@ import org.sonar.api.utils.System2;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.component.ComponentDao;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.custom.CustomMeasureDao;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.metric.MetricDao;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.metric.MetricTesting;
import org.sonar.server.component.ComponentTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.ServerException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.db.metric.MetricDao;
import org.sonar.db.metric.MetricTesting;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
@@ -243,7 +244,7 @@ public class UpdateActionTest {

@Test
public void fail_if_not_in_db() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expect(RowNotFoundException.class);
expectedException.expectMessage("Custom measure '42' not found.");

ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION)

+ 8
- 6
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileBackuperMediumTest.java View File

@@ -33,10 +33,12 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
@@ -53,6 +55,8 @@ public class QProfileBackuperMediumTest {
@ClassRule
public static ServerTester tester = new ServerTester();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);

DbClient db;
@@ -113,12 +117,10 @@ public class QProfileBackuperMediumTest {

@Test
public void fail_to_backup_unknown_profile() {
try {
tester.get(QProfileBackuper.class).backup("unknown", new StringWriter());
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Quality profile not found: unknown");
}
thrown.expect(RowNotFoundException.class);
thrown.expectMessage("Quality profile not found: unknown");

tester.get(QProfileBackuper.class).backup("unknown", new StringWriter());
}

@Test

+ 20
- 23
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java View File

@@ -24,21 +24,24 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.db.component.ComponentDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.search.IndexClient;
import org.sonar.server.tester.MockUserSession;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.tester.MockUserSession;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@@ -51,6 +54,8 @@ public class QProfileFactoryMediumTest {
@ClassRule
public static ServerTester tester = new ServerTester();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);

DbClient db;
@@ -112,7 +117,6 @@ public class QProfileFactoryMediumTest {
}
}


@Test
public void fail_to_create_if_already_exists() {
QProfileName name = new QProfileName("xoo", "P1");
@@ -175,12 +179,10 @@ public class QProfileFactoryMediumTest {

@Test
public void fail_renaming_if_profile_not_found() {
try {
factory.rename("unknown", "the new name");
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Quality profile not found: unknown");
}
thrown.expect(NotFoundException.class);
thrown.expectMessage("Quality profile not found: unknown");

factory.rename("unknown", "the new name");
}

@Test
@@ -276,12 +278,10 @@ public class QProfileFactoryMediumTest {

@Test
public void fail_if_unknown_profile_to_be_deleted() {
try {
factory.delete(XOO_P1_KEY);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Quality profile not found: XOO_P1");
}
thrown.expect(RowNotFoundException.class);
thrown.expectMessage("Quality profile not found: XOO_P1");

factory.delete(XOO_P1_KEY);
}

@Test
@@ -300,13 +300,10 @@ public class QProfileFactoryMediumTest {

@Test
public void fail_if_unknown_profile_to_be_set_as_default() {
try {
// does not exist
factory.setDefault(XOO_P1_KEY);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Quality profile not found: " + XOO_P1_KEY);
}
thrown.expect(NotFoundException.class);
thrown.expectMessage("Quality profile not found: " + XOO_P1_KEY);

factory.setDefault(XOO_P1_KEY);
}

@Test

+ 2
- 1
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ChangeParentActionMediumTest.java View File

@@ -29,6 +29,7 @@ import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
@@ -244,7 +245,7 @@ public class ChangeParentActionMediumTest {
.execute();
}

@Test(expected = IllegalArgumentException.class)
@Test(expected = RowNotFoundException.class)
public void fail_if_profile_key_and_name_both_set() throws Exception {
QualityProfileDto child = createProfile("xoo", "Child");
session.commit();

+ 2
- 1
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java View File

@@ -32,6 +32,7 @@ import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
@@ -129,7 +130,7 @@ public class RenameActionTest {
.execute();
}

@Test(expected = IllegalArgumentException.class)
@Test(expected = NotFoundException.class)
public void fail_on_unknown_profile() throws Exception {
userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);


+ 3
- 6
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SetDefaultActionTest.java View File

@@ -60,7 +60,6 @@ public class SetDefaultActionTest {

private DbSession session;


@Before
public void setUp() {
dbTester.truncateTables();
@@ -86,7 +85,6 @@ public class SetDefaultActionTest {
public void set_default_profile_using_key() throws Exception {
userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);


checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");

@@ -120,14 +118,13 @@ public class SetDefaultActionTest {
try {
tester.newPostRequest("api/qualityprofiles", "set_default").setParam("profileKey", "unknown-profile-666").execute();
Fail.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
} catch(IllegalArgumentException nfe) {
} catch (NotFoundException nfe) {
assertThat(nfe).hasMessage("Quality profile not found: unknown-profile-666");
checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");
}
}


@Test
public void fail_to_set_default_profile_using_language_and_name() throws Exception {
userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
@@ -135,7 +132,7 @@ public class SetDefaultActionTest {
try {
tester.newPostRequest("api/qualityprofiles", "set_default").setParam("language", xoo2Key).setParam("profileName", "Unknown").execute();
Fail.failBecauseExceptionWasNotThrown(NotFoundException.class);
} catch(NotFoundException nfe) {
} catch (NotFoundException nfe) {
assertThat(nfe).hasMessage("Unable to find a profile for language 'xoo2' with name 'Unknown'");
checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");
@@ -149,7 +146,7 @@ public class SetDefaultActionTest {
try {
tester.newPostRequest("api/qualityprofiles", "set_default").setParam("profileKey", "sonar-way-xoo2-23456").execute().assertNoContent();
Fail.failBecauseExceptionWasNotThrown(ForbiddenException.class);
} catch(ForbiddenException forbidden) {
} catch (ForbiddenException forbidden) {
checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");
}

Loading…
Cancel
Save