private static final int BAD_REQUEST = 400;
- private List<Message> errors = newArrayList();
+ private final List<Message> errors;
public BadRequestException(String message) {
super(BAD_REQUEST, message);
+ this.errors = newArrayList();
}
- public BadRequestException(@Nullable String message, List<Message> errors) {
+ public BadRequestException(String message, List<Message> errors) {
super(BAD_REQUEST, message);
this.errors = errors;
}
public BadRequestException(@Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams) {
super(BAD_REQUEST, message, l10nKey, l10nParams);
+ this.errors = newArrayList();
}
public BadRequestException(@Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams, List<Message> errors) {
private void addCharacteristics(IssueQueryResult result, DefaultIssue issue, JsonWriter json) {
Characteristic requirement = technicalDebtManager.findRequirementByRule(result.rule(issue));
// Requirement can be null if it has been disabled
- if (requirement != null && requirement.rootId() != null && requirement.parentId() != null) {
- Characteristic characteristic = technicalDebtManager.findCharacteristicById(requirement.rootId());
+ if (requirement != null) {
+ Characteristic characteristic = findCharacteristicById(requirement.rootId());
json.prop("characteristic", characteristic != null ? characteristic.name() : null);
- Characteristic subCharacteristic = technicalDebtManager.findCharacteristicById(requirement.parentId());
+ Characteristic subCharacteristic = findCharacteristicById(requirement.parentId());
json.prop("subCharacteristic", subCharacteristic != null ? subCharacteristic.name() : null);
}
}
+ @CheckForNull
+ private Characteristic findCharacteristicById(@Nullable Integer id){
+ if (id != null) {
+ return technicalDebtManager.findCharacteristicById(id);
+ }
+ return null;
+ }
+
private void writeTransitions(Issue issue, JsonWriter json) {
json.name("transitions").beginArray();
if (UserSession.get().isLoggedIn()) {
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.util.Date;
import java.util.List;
activeRuleDao.insert(activeRuleParam, session);
session.commit();
newParams.add(activeRuleParam);
- actions.add(profilesManager.ruleParamChanged(activeRule.getProfileId(), activeRule.getId(), parentParam.getKey(), null, parentParam.getValue(), getLoggedName(userSession)));
+ actions.add(profilesManager.ruleParamChanged(activeRule.getProfileId(), activeRule.getId(), parentParam.getKey(), null, parentParam.getValue(),
+ getLoggedName(userSession)));
}
}
return newParams;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.util.List;
import java.util.Map;
private final ProfilesManager profilesManager;
public QProfileOperations(MyBatis myBatis, QualityProfileDao dao, ActiveRuleDao activeRuleDao, PropertiesDao propertiesDao,
- QProfileRepositoryExporter exporter, PreviewCache dryRunCache, ESActiveRule esActiveRule, QProfileLookup profileLookup, ProfilesManager profilesManager) {
+ QProfileRepositoryExporter exporter, PreviewCache dryRunCache, ESActiveRule esActiveRule, QProfileLookup profileLookup,
+ ProfilesManager profilesManager) {
this.myBatis = myBatis;
this.dao = dao;
this.activeRuleDao = activeRuleDao;
return language;
}
+ @CheckForNull
public String name() {
return name;
}
return this;
}
- public Builder setName(String name) {
+ public Builder setName(@Nullable String name) {
this.name = name;
return this;
}
import javax.annotation.Nullable;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
public class HtmlSourceDecorator implements ServerComponent {
this.snapshotDataDao = snapshotDataDao;
}
- @CheckForNull
public List<String> getDecoratedSourceAsHtml(String componentKey, @Nullable Integer from, @Nullable Integer to) {
SqlSession session = mybatis.openSession();
try {
String snapshotSource = snapshotSourceDao.selectSnapshotSourceByComponentKey(componentKey, session);
return decorate(snapshotSource, snapshotDataEntries, from, to);
}
- return null;
+ return Collections.emptyList();
} finally {
MyBatis.closeQuietly(session);
}
UserSession.get().checkProjectPermission(UserRole.CODEVIEWER, project.getKey());
List<String> decoratedSource = sourceDecorator.getDecoratedSourceAsHtml(componentKey, from, to);
- if (decoratedSource != null) {
+ if (!decoratedSource.isEmpty()) {
return decoratedSource;
} else {
return deprecatedSourceDecorator.getSourceAsHtml(componentKey, from, to);
}
@Test
- public void text_error_with_message() throws Exception {
+ public void text_error_with_list_of_errors() throws Exception {
BadRequestException exception = BadRequestException.of("error", newArrayList(BadRequestException.Message.of("new error")));
assertThat(exception.errors()).hasSize(1);
assertThat(exception.errors().get(0).text()).isEqualTo("new error");
}
+ @Test
+ public void text_error_with_list_of_l18n_errors() throws Exception {
+ BadRequestException exception = BadRequestException.of("error", newArrayList(BadRequestException.Message.ofL10n("issue.error.123", "10")));
+
+ assertThat(exception.errors()).hasSize(1);
+ assertThat(exception.errors().get(0).l10nKey()).isEqualTo("issue.error.123");
+ assertThat(exception.errors().get(0).l10nParams()).containsOnly("10");
+ }
+
+ @Test
+ public void list_of_errors() throws Exception {
+ BadRequestException exception = BadRequestException.of(newArrayList(BadRequestException.Message.of("new error")));
+
+ assertThat(exception.errors()).hasSize(1);
+ assertThat(exception.errors().get(0).text()).isEqualTo("new error");
+ }
+
@Test
public void l10n_errors() throws Exception {
BadRequestException exception = BadRequestException.ofL10n("issue.error.123", "10");
assertThat(exception.l10nParams()).containsOnly("10");
}
+ @Test
+ public void l10n_errors_with_list_of_errors() throws Exception {
+ BadRequestException exception = BadRequestException.ofL10n(newArrayList(BadRequestException.Message.of("new error")), "issue.error.123", "10");
+ assertThat(exception.getMessage()).isNull();
+ assertThat(exception.l10nKey()).isEqualTo("issue.error.123");
+ assertThat(exception.l10nParams()).containsOnly("10");
+ assertThat(exception.errors()).hasSize(1);
+ assertThat(exception.errors().get(0).text()).isEqualTo("new error");
+ }
+
@Test
public void test_equals_and_hashcode() throws Exception {
+ BadRequestException.Message msg = BadRequestException.Message.of("error1");
+ BadRequestException.Message sameMsg = BadRequestException.Message.of("error1");
+ BadRequestException.Message msg2 = BadRequestException.Message.of("error2");
+
+ assertThat(msg.toString()).contains("error1");
+ assertThat(msg).isEqualTo(sameMsg);
+ assertThat(msg).isEqualTo(sameMsg);
+ assertThat(msg.hashCode()).isEqualTo(msg.hashCode());
+ assertThat(msg.hashCode()).isEqualTo(sameMsg.hashCode());
+
+ assertThat(msg).isNotEqualTo(msg2);
+ }
+
+ @Test
+ public void test_equals_and_hashcode_on_l10n() throws Exception {
BadRequestException.Message msg = BadRequestException.Message.ofL10n("error.123", "10");
BadRequestException.Message sameMsg = BadRequestException.Message.ofL10n("error.123", "10");
BadRequestException.Message msg2 = BadRequestException.Message.ofL10n("error.123", "200");
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.user.MockUserSession;
+import java.util.Collections;
+
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.*;
String componentKey = "org.sonar.sample:Sample";
MockUserSession.set().addProjectPermissions(UserRole.CODEVIEWER, projectKey);
when(resourceDao.getRootProjectByComponentKey(componentKey)).thenReturn(new ResourceDto().setKey(projectKey));
- when(sourceDecorator.getDecoratedSourceAsHtml(eq(componentKey), anyInt(), anyInt())).thenReturn(null);
+ when(sourceDecorator.getDecoratedSourceAsHtml(eq(componentKey), anyInt(), anyInt())).thenReturn(Collections.<String>emptyList());
service.getSourcesByComponent(componentKey, 1, 2);