diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2019-10-08 11:12:12 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-10-09 20:21:06 +0200 |
commit | 140e84fbdfd4ec9ddc15a7574d984ff1c059f6a0 (patch) | |
tree | f90f9b95ee59ece7a2347a12316cb5a1c2d5ad11 /server/sonar-webserver-auth | |
parent | 82c9246f858728ef01603989d129342fb9830785 (diff) | |
download | sonarqube-140e84fbdfd4ec9ddc15a7574d984ff1c059f6a0.tar.gz sonarqube-140e84fbdfd4ec9ddc15a7574d984ff1c059f6a0.zip |
fix some quality flaws on the new code period
Diffstat (limited to 'server/sonar-webserver-auth')
2 files changed, 95 insertions, 1 deletions
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/permission/GroupIdOrAnyone.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/permission/GroupIdOrAnyone.java index 4a6add59eb6..c2a21151cbc 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/permission/GroupIdOrAnyone.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/permission/GroupIdOrAnyone.java @@ -41,7 +41,7 @@ public class GroupIdOrAnyone { private GroupIdOrAnyone(String organizationUuid, @Nullable Integer id) { this.id = id; - this.organizationUuid = requireNonNull(organizationUuid); + this.organizationUuid = requireNonNull(organizationUuid, "organizationUuid can't be null"); } public boolean isAnyone() { diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/permission/GroupIdOrAnyoneTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/permission/GroupIdOrAnyoneTest.java new file mode 100644 index 00000000000..6a8a7b4eda2 --- /dev/null +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/permission/GroupIdOrAnyoneTest.java @@ -0,0 +1,94 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.server.permission; + +import java.util.Random; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.user.GroupDto; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.assertj.core.api.Assertions.assertThat; + +public class GroupIdOrAnyoneTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void from_fails_with_NPE_if_GroupDto_organizationUuid_is_null() { + GroupDto dto = new GroupDto(); + + expectOrganizationUuidNPE(); + + GroupIdOrAnyone.from(dto); + } + + @Test + public void for_returns_isAnyone_if_id_is_null() { + String organizationUuid = randomAlphabetic(10); + GroupDto dto = new GroupDto(); + dto.setOrganizationUuid(organizationUuid); + + GroupIdOrAnyone underTest = GroupIdOrAnyone.from(dto); + + assertThat(underTest.getOrganizationUuid()).isEqualTo(organizationUuid); + assertThat(underTest.isAnyone()).isTrue(); + assertThat(underTest.getId()).isNull(); + } + + @Test + public void for_returns_isAnyone_false_if_id_is_not_null() { + int id = new Random().nextInt(199); + String organizationUuid = randomAlphabetic(10); + GroupDto dto = new GroupDto(); + dto.setOrganizationUuid(organizationUuid); + dto.setId(id); + + GroupIdOrAnyone underTest = GroupIdOrAnyone.from(dto); + + assertThat(underTest.getOrganizationUuid()).isEqualTo(organizationUuid); + assertThat(underTest.isAnyone()).isFalse(); + assertThat(underTest.getId()).isEqualTo(id); + } + + @Test + public void forAnyone_fails_with_NPE_if_arg_is_null() { + expectOrganizationUuidNPE(); + + GroupIdOrAnyone.forAnyone(null); + } + + @Test + public void forAnyone_returns_isAnyone_true() { + String organizationUuid = randomAlphabetic(12); + + GroupIdOrAnyone underTest = GroupIdOrAnyone.forAnyone(organizationUuid); + + assertThat(underTest.isAnyone()).isTrue(); + assertThat(underTest.getOrganizationUuid()).isEqualTo(organizationUuid); + assertThat(underTest.getId()).isNull(); + } + + private void expectOrganizationUuidNPE() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("organizationUuid can't be null"); + } +} |