diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-02-07 17:58:11 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-02-10 09:48:36 +0100 |
commit | caf7aba77fe1148e0514ab0fa24a5507d585ad37 (patch) | |
tree | be72735ad8ddce5e0964e996a29dfcc48d5dde05 /sonar-db/src | |
parent | a60f3c39d100eb0eae125c01c6932ddfb37417c9 (diff) | |
download | sonarqube-caf7aba77fe1148e0514ab0fa24a5507d585ad37.tar.gz sonarqube-caf7aba77fe1148e0514ab0fa24a5507d585ad37.zip |
SONAR-8754 support guarded in OrganizationDto and OrganizationDao
Diffstat (limited to 'sonar-db/src')
7 files changed, 73 insertions, 25 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDto.java b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDto.java index 095f40e7ef2..2644132fc40 100644 --- a/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDto.java +++ b/sonar-db/src/main/java/org/sonar/db/organization/OrganizationDto.java @@ -34,6 +34,10 @@ public class OrganizationDto { private String url; /** avatar url can be null */ private String avatarUrl; + /** + * Flag indicated whether being root is required to be able to delete this organization. + */ + private boolean guarded = false; private long createdAt; private long updatedAt; @@ -64,24 +68,6 @@ public class OrganizationDto { return this; } - public long getCreatedAt() { - return createdAt; - } - - public OrganizationDto setCreatedAt(long createdAt) { - this.createdAt = createdAt; - return this; - } - - public long getUpdatedAt() { - return updatedAt; - } - - public OrganizationDto setUpdatedAt(long updatedAt) { - this.updatedAt = updatedAt; - return this; - } - public String getDescription() { return description; } @@ -109,6 +95,33 @@ public class OrganizationDto { return this; } + public boolean isGuarded() { + return guarded; + } + + public OrganizationDto setGuarded(boolean guarded) { + this.guarded = guarded; + return this; + } + + public long getCreatedAt() { + return createdAt; + } + + public OrganizationDto setCreatedAt(long createdAt) { + this.createdAt = createdAt; + return this; + } + + public long getUpdatedAt() { + return updatedAt; + } + + public OrganizationDto setUpdatedAt(long updatedAt) { + this.updatedAt = updatedAt; + return this; + } + @Override public String toString() { return "OrganizationDto{" + @@ -118,6 +131,7 @@ public class OrganizationDto { ", description='" + description + '\'' + ", url='" + url + '\'' + ", avatarUrl='" + avatarUrl + '\'' + + ", guarded=" + guarded + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + '}'; diff --git a/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml b/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml index 5b5a44ce526..fa7e3d41862 100644 --- a/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml @@ -9,6 +9,7 @@ org.description as "description", org.url as "url", org.avatar_url as "avatarUrl", + org.guarded as "guarded", org.created_at as "createdAt", org.updated_at as "updatedAt" </sql> @@ -130,6 +131,7 @@ description, url, avatar_url, + guarded, created_at, updated_at ) @@ -141,6 +143,7 @@ #{organization.description, jdbcType=VARCHAR}, #{organization.url, jdbcType=VARCHAR}, #{organization.avatarUrl, jdbcType=VARCHAR}, + #{organization.guarded, jdbcType=BOOLEAN}, #{organization.createdAt, jdbcType=BIGINT}, #{organization.updatedAt, jdbcType=BIGINT} ) diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 00d131c627c..9ebdb68b1c9 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -530,7 +530,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1512'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '1418215735482', '1418215735482'); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; -INSERT INTO ORGANIZATIONS (UUID, KEE, NAME, CREATED_AT, UPDATED_AT) VALUES ('AVdqnciQUUs7Zd3KPvFD', 'default-organization', 'Default Organization', '1474962596482', '1474962596482'); +INSERT INTO ORGANIZATIONS (UUID, KEE, NAME, GUARDED, CREATED_AT, UPDATED_AT) VALUES ('AVdqnciQUUs7Zd3KPvFD', 'default-organization', 'Default Organization', false, '1474962596482', '1474962596482'); INSERT INTO INTERNAL_PROPERTIES (KEE, IS_EMPTY, TEXT_VALUE, CREATED_AT) VALUES ('organization.default', false, 'AVdqnciQUUs7Zd3KPvFD', '1474962596482'); INSERT INTO GROUPS(ID, ORGANIZATION_UUID, NAME, DESCRIPTION, CREATED_AT, UPDATED_AT) VALUES (1, 'AVdqnciQUUs7Zd3KPvFD', 'sonar-administrators', 'System administrators', '2011-09-26 22:27:51.0', '2011-09-26 22:27:51.0'); diff --git a/sonar-db/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java b/sonar-db/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java index 23bcce30f66..5a2df5b94c4 100644 --- a/sonar-db/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/organization/OrganizationDaoTest.java @@ -39,6 +39,8 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.dialect.Dialect; +import org.sonar.db.dialect.Oracle; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; @@ -62,16 +64,18 @@ public class OrganizationDaoTest { .setName("the name 1") .setDescription("the description 1") .setUrl("the url 1") - .setAvatarUrl("the avatar url 1"); + .setAvatarUrl("the avatar url 1") + .setGuarded(false); private static final OrganizationDto ORGANIZATION_DTO_2 = new OrganizationDto() .setUuid("uuid 2") .setKey("the_key 2") .setName("the name 2") .setDescription("the description 2") .setUrl("the url 2") - .setAvatarUrl("the avatar url 2"); - public static final String PERMISSION_1 = "foo"; - public static final String PERMISSION_2 = "bar"; + .setAvatarUrl("the avatar url 2") + .setGuarded(true); + private static final String PERMISSION_1 = "foo"; + private static final String PERMISSION_2 = "bar"; private System2 system2 = mock(System2.class); @@ -117,12 +121,21 @@ public class OrganizationDaoTest { assertThat(row.get("avatarUrl")).isEqualTo(ORGANIZATION_DTO_1.getAvatarUrl()); assertThat(row.get("createdAt")).isEqualTo(ORGANIZATION_DTO_1.getCreatedAt()); assertThat(row.get("updatedAt")).isEqualTo(ORGANIZATION_DTO_1.getUpdatedAt()); + assertThat(row.get("guarded")).isEqualTo(toBool(ORGANIZATION_DTO_1.isGuarded())); assertThat(row.get("defaultTemplate")).isNull(); assertThat(row.get("projectDefaultTemplate")).isNull(); assertThat(row.get("viewDefaultTemplate")).isNull(); } @Test + public void insert_persists_boolean_property_guarded_of_OrganizationDto() { + insertOrganization(ORGANIZATION_DTO_2); + + Map<String, Object> row = selectSingleRow(); + assertThat(row.get("guarded")).isEqualTo(toBool(ORGANIZATION_DTO_2.isGuarded())); + } + + @Test public void description_url_and_avatarUrl_are_optional() { when(system2.now()).thenReturn(SOME_DATE); insertOrganization(copyOf(ORGANIZATION_DTO_1).setDescription(null).setUrl(null).setAvatarUrl(null)); @@ -134,6 +147,7 @@ public class OrganizationDaoTest { assertThat(row.get("description")).isNull(); assertThat(row.get("url")).isNull(); assertThat(row.get("avatarUrl")).isNull(); + assertThat(row.get("guarded")).isEqualTo(toBool(ORGANIZATION_DTO_1.isGuarded())); assertThat(row.get("createdAt")).isEqualTo(SOME_DATE); assertThat(row.get("updatedAt")).isEqualTo(SOME_DATE); assertThat(row.get("defaultTemplate")).isNull(); @@ -141,6 +155,14 @@ public class OrganizationDaoTest { assertThat(row.get("viewDefaultTemplate")).isNull(); } + private Object toBool(boolean guarded) { + Dialect dialect = dbTester.database().getDialect(); + if (dialect.getId().equals(Oracle.ID)) { + return guarded ? 1L : 0L; + } + return guarded; + } + @Test public void insert_fails_if_row_with_uuid_already_exists() { insertOrganization(ORGANIZATION_DTO_1); @@ -836,6 +858,7 @@ public class OrganizationDaoTest { " name," + " default_perm_template_project," + " default_perm_template_view," + + " guarded," + " created_at," + " updated_at" + " )" + @@ -847,6 +870,7 @@ public class OrganizationDaoTest { " ?," + " ?," + " ?," + + " ?," + " ?" + " )")) { preparedStatement.setString(1, organizationUuid); @@ -854,8 +878,9 @@ public class OrganizationDaoTest { preparedStatement.setString(3, organizationUuid); preparedStatement.setString(4, project); preparedStatement.setString(5, view); - preparedStatement.setLong(6, 1000L); - preparedStatement.setLong(7, 2000L); + preparedStatement.setBoolean(6, false); + preparedStatement.setLong(7, 1000L); + preparedStatement.setLong(8, 2000L); preparedStatement.execute(); } catch (SQLException e) { throw new RuntimeException("dirty insert failed", e); @@ -878,6 +903,7 @@ public class OrganizationDaoTest { assertThat(dto.getName()).isEqualTo(ORGANIZATION_DTO_1.getName()); assertThat(dto.getDescription()).isEqualTo(ORGANIZATION_DTO_1.getDescription()); assertThat(dto.getUrl()).isEqualTo(ORGANIZATION_DTO_1.getUrl()); + assertThat(dto.isGuarded()).isEqualTo(ORGANIZATION_DTO_1.isGuarded()); assertThat(dto.getAvatarUrl()).isEqualTo(ORGANIZATION_DTO_1.getAvatarUrl()); assertThat(dto.getCreatedAt()).isEqualTo(ORGANIZATION_DTO_1.getCreatedAt()); assertThat(dto.getUpdatedAt()).isEqualTo(ORGANIZATION_DTO_1.getUpdatedAt()); @@ -889,6 +915,7 @@ public class OrganizationDaoTest { assertThat(dto.getName()).isEqualTo(expected.getName()); assertThat(dto.getDescription()).isEqualTo(expected.getDescription()); assertThat(dto.getUrl()).isEqualTo(expected.getUrl()); + assertThat(dto.isGuarded()).isEqualTo(expected.isGuarded()); assertThat(dto.getAvatarUrl()).isEqualTo(expected.getAvatarUrl()); assertThat(dto.getCreatedAt()).isEqualTo(expected.getCreatedAt()); assertThat(dto.getUpdatedAt()).isEqualTo(expected.getUpdatedAt()); @@ -897,6 +924,7 @@ public class OrganizationDaoTest { private Map<String, Object> selectSingleRow() { List<Map<String, Object>> rows = dbTester.select("select" + " uuid as \"uuid\", kee as \"key\", name as \"name\", description as \"description\", url as \"url\", avatar_url as \"avatarUrl\"," + + " guarded as \"guarded\"," + " created_at as \"createdAt\", updated_at as \"updatedAt\"," + " default_perm_template_project as \"projectDefaultPermTemplate\"," + " default_perm_template_view as \"viewDefaultPermTemplate\"" + diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml index 7c4f24eda93..596dd4eec2f 100644 --- a/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml +++ b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/selectForIndexing.xml @@ -3,6 +3,7 @@ <organizations uuid="org1" kee="org1_key" name="org1_name" + guarded="[false]" created_at="1000" updated_at="1000"/> diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/shared.xml b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/shared.xml index f3656c5ba02..9456f3b138b 100644 --- a/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/shared.xml +++ b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoTest/shared.xml @@ -3,6 +3,7 @@ <organizations uuid="org1" kee="org1_key" name="org1_name" + guarded="[false]" created_at="1000" updated_at="1000"/> diff --git a/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoWithDuplicatedKeysTest/schema.sql b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoWithDuplicatedKeysTest/schema.sql index 678afc9442f..dc2e084f3b9 100644 --- a/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoWithDuplicatedKeysTest/schema.sql +++ b/sonar-db/src/test/resources/org/sonar/db/component/ComponentDaoWithDuplicatedKeysTest/schema.sql @@ -5,6 +5,7 @@ CREATE TABLE "ORGANIZATIONS" ( "DESCRIPTION" VARCHAR(256), "URL" VARCHAR(256), "AVATAR_URL" VARCHAR(256), + "GUARDED" BOOLEAN NOT NULL, "CREATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL ); |