*/
package org.sonar.server.platform.db.migration.version.v62;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbTester;
-import org.sonar.db.organization.OrganizationDto;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
private CreateDefaultOrganization underTest = new CreateDefaultOrganization(dbTester.database(), system2, uuidFactory);
@Test
- public void execute_insert_data_in_organizations_and_internal_properties_when_it_does_not_exist() throws SQLException {
+ public void execute_insert_data_in_organizations_and_internal_properties_when_it_does_not_exist() throws Exception {
long now = 1_222_999L;
String uuid = "a uuid";
when(system2.now()).thenReturn(now);
underTest.execute();
- OrganizationDto organizationDto = dbTester.getDbClient().organizationDao().selectByKey(dbTester.getSession(), DEFAULT_ORGANIZATION_KEY).get();
- assertThat(organizationDto.getUuid()).isEqualTo(uuid);
- assertThat(organizationDto.getKey()).isEqualTo(DEFAULT_ORGANIZATION_KEY);
- assertThat(organizationDto.getName()).isEqualTo(DEFAULT_ORGANIZATION_NAME);
- assertThat(organizationDto.getDescription()).isNull();
- assertThat(organizationDto.getUrl()).isNull();
- assertThat(organizationDto.getAvatarUrl()).isNull();
- assertThat(organizationDto.getCreatedAt()).isEqualTo(now);
- assertThat(organizationDto.getUpdatedAt()).isEqualTo(now);
+ try (Connection connection = dbTester.openConnection();
+ PreparedStatement preparedStatement = createSelectStatementByKey(connection, DEFAULT_ORGANIZATION_KEY);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ if (resultSet.next()) {
+ assertThat(resultSet.getString(1)).isEqualTo(uuid);
+ assertThat(resultSet.getString(2)).isEqualTo(DEFAULT_ORGANIZATION_KEY);
+ assertThat(resultSet.getString(3)).isEqualTo(DEFAULT_ORGANIZATION_NAME);
+ assertThat(resultSet.getString(4)).isNull();
+ assertThat(resultSet.getString(5)).isNull();
+ assertThat(resultSet.getString(6)).isNull();
+ assertThat(resultSet.getLong(7)).isEqualTo(now);
+ assertThat(resultSet.getLong(8)).isEqualTo(now);
+
+ assertThat(resultSet.next()).isFalse();
+ } else {
+ fail("Can't retrieve organization " + uuid);
+ }
+ }
verifyInternalProperty(uuid);
}
@Test
- public void execute_inserts_internal_property_if_default_organization_already_exists() throws SQLException {
+ public void execute_inserts_internal_property_if_default_organization_already_exists() throws Exception {
long past = 2_999_033L;
String uuid = "uuidAAAA";
insertExistingOrganization(uuid, past);
}
@Test
- public void execute_has_no_effect_if_organization_and_internal_property_already_exist() throws SQLException {
+ public void execute_has_no_effect_if_organization_and_internal_property_already_exist() throws Exception {
long past = 2_999_033L;
String uuid = "uuidAAAA";
insertExistingOrganization(uuid, past);
dbTester.commit();
}
- private void insertExistingOrganization(String uuid, long past) {
- when(system2.now()).thenReturn(past);
- dbTester.getDbClient().organizationDao().insert(dbTester.getSession(),
- new OrganizationDto()
- .setUuid(uuid)
- .setKey(DEFAULT_ORGANIZATION_KEY)
- .setName("whatever"));
- dbTester.commit();
+ private void insertExistingOrganization(String uuid, long past) throws Exception {
+ try (Connection connection = dbTester.openConnection();
+ PreparedStatement preparedStatement = connection.prepareStatement("insert into organizations (uuid,kee,name,created_at,updated_at) values (?,?,?,?,?)")) {
+ preparedStatement.setString(1, uuid);
+ preparedStatement.setString(2, DEFAULT_ORGANIZATION_KEY);
+ preparedStatement.setString(3, "whatever");
+ preparedStatement.setLong(4, past);
+ preparedStatement.setLong(5, past);
+ preparedStatement.execute();
+ if (!connection.getAutoCommit()) {
+ connection.commit();
+ }
+ }
+ }
+
+ private void verifyExistingOrganization(String uuid, long past) throws Exception {
+ try (Connection connection = dbTester.openConnection();
+ PreparedStatement preparedStatement = createSelectStatementByUuid(connection, uuid);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ if (resultSet.next()) {
+ assertThat(resultSet.getString(1)).isEqualTo(uuid);
+ assertThat(resultSet.getString(2)).isEqualTo(DEFAULT_ORGANIZATION_KEY);
+ assertThat(resultSet.getString(3)).isEqualTo("whatever");
+ assertThat(resultSet.getString(4)).isNull();
+ assertThat(resultSet.getString(5)).isNull();
+ assertThat(resultSet.getString(6)).isNull();
+ assertThat(resultSet.getLong(7)).isEqualTo(past);
+ assertThat(resultSet.getLong(8)).isEqualTo(past);
+
+ assertThat(resultSet.next()).isFalse();
+ } else {
+ fail("Can't retrieve organization " + uuid);
+ }
+ }
+ }
+
+ private PreparedStatement createSelectStatementByUuid(Connection connection, String uuid) throws SQLException {
+ PreparedStatement preparedStatement = connection.prepareStatement("select uuid,kee,name,description,url,avatar_url,created_at,updated_at from organizations where uuid=?");
+ preparedStatement.setString(1, uuid);
+ return preparedStatement;
}
- private void verifyExistingOrganization(String uuid, long past) {
- OrganizationDto organizationDto = dbTester.getDbClient().organizationDao().selectByKey(dbTester.getSession(), DEFAULT_ORGANIZATION_KEY).get();
- assertThat(organizationDto.getUuid()).isEqualTo(uuid);
- assertThat(organizationDto.getKey()).isEqualTo(DEFAULT_ORGANIZATION_KEY);
- assertThat(organizationDto.getName()).isEqualTo("whatever");
- assertThat(organizationDto.getDescription()).isNull();
- assertThat(organizationDto.getUrl()).isNull();
- assertThat(organizationDto.getAvatarUrl()).isNull();
- assertThat(organizationDto.getCreatedAt()).isEqualTo(past);
- assertThat(organizationDto.getUpdatedAt()).isEqualTo(past);
+ private PreparedStatement createSelectStatementByKey(Connection connection, String kee) throws SQLException {
+ PreparedStatement preparedStatement = connection.prepareStatement("select uuid,kee,name,description,url,avatar_url,created_at,updated_at from organizations where kee=?");
+ preparedStatement.setString(1, kee);
+ return preparedStatement;
}
}
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.platform.db.migration.version.v63;
import java.sql.SQLException;
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="2000"/>
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;
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;
}
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{" +
", description='" + description + '\'' +
", url='" + url + '\'' +
", avatarUrl='" + avatarUrl + '\'' +
+ ", guarded=" + guarded +
", createdAt=" + createdAt +
", updatedAt=" + updatedAt +
'}';
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>
description,
url,
avatar_url,
+ guarded,
created_at,
updated_at
)
#{organization.description, jdbcType=VARCHAR},
#{organization.url, jdbcType=VARCHAR},
#{organization.avatarUrl, jdbcType=VARCHAR},
+ #{organization.guarded, jdbcType=BOOLEAN},
#{organization.createdAt, jdbcType=BIGINT},
#{organization.updatedAt, jdbcType=BIGINT}
)
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');
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;
.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);
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);
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();
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);
" name," +
" default_perm_template_project," +
" default_perm_template_view," +
+ " guarded," +
" created_at," +
" updated_at" +
" )" +
" ?," +
" ?," +
" ?," +
+ " ?," +
" ?" +
" )")) {
preparedStatement.setString(1, organizationUuid);
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);
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());
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());
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\"" +
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="1000"/>
<organizations uuid="org1"
kee="org1_key"
name="org1_name"
+ guarded="[false]"
created_at="1000"
updated_at="1000"/>
"DESCRIPTION" VARCHAR(256),
"URL" VARCHAR(256),
"AVATAR_URL" VARCHAR(256),
+ "GUARDED" BOOLEAN NOT NULL,
"CREATED_AT" BIGINT NOT NULL,
"UPDATED_AT" BIGINT NOT NULL
);