@Override
public boolean apply(@Nullable GroupDto input) {
- return input != null && input.getKey().equals(key);
+ return input != null && input.getName().equals(key);
}
}
}
--- /dev/null
+#
+# SonarQube, open source software quality management tool.
+# Copyright (C) 2008-2014 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# SonarQube 6.2
+#
+class AddOrganizationUuidToGroups < ActiveRecord::Migration
+
+ def self.up
+ execute_java_migration('org.sonar.db.version.v62.AddOrganizationUuidToGroups')
+ end
+end
this.system = system;
}
- public GroupDto selectOrFailByName(DbSession session, String key) {
- GroupDto group = selectByName(session, key);
+ /**
+ * @deprecated organization should be added as a parameter
+ */
+ @Deprecated
+ public GroupDto selectOrFailByName(DbSession session, String name) {
+ GroupDto group = selectByName(session, name);
if (group == null) {
- throw new RowNotFoundException(String.format("Could not find a group with name '%s'", key));
+ throw new RowNotFoundException(String.format("Could not find a group with name '%s'", name));
}
return group;
}
+ /**
+ * @deprecated organization should be added as a parameter
+ */
+ @Deprecated
@CheckForNull
public GroupDto selectByName(DbSession session, String key) {
return mapper(session).selectByKey(key);
}
+ /**
+ * @deprecated organization should be added as a parameter
+ */
+ @Deprecated
public List<GroupDto> selectByNames(DbSession session, Collection<String> names) {
return executeLargeInputs(names, mapper(session)::selectByNames);
}
*/
package org.sonar.db.user;
+import java.util.Date;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import org.sonar.db.Dto;
-public class GroupDto extends Dto<String> {
+public class GroupDto {
private Long id;
private String name;
private String description;
+ private String organizationUuid;
+ private Date createdAt;
+ private Date updatedAt;
+
+ public final Date getCreatedAt() {
+ return this.createdAt;
+ }
+
+ public final Date getUpdatedAt() {
+ return this.updatedAt;
+ }
public Long getId() {
return id;
return this;
}
- @Override
- public String getKey() {
- return name;
+ public String getOrganizationUuid() {
+ return organizationUuid;
+ }
+
+ public GroupDto setOrganizationUuid(String s) {
+ this.organizationUuid = s;
+ return this;
}
+ public GroupDto setCreatedAt(Date d) {
+ this.createdAt = d;
+ return this;
+ }
+
+ public GroupDto setUpdatedAt(Date d) {
+ this.updatedAt = d;
+ return this;
+ }
+
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder("GroupDto{");
+ sb.append("id=").append(id);
+ sb.append(", name='").append(name).append('\'');
+ sb.append(", description='").append(description).append('\'');
+ sb.append(", organizationUuid='").append(organizationUuid).append('\'');
+ sb.append(", createdAt=").append(createdAt);
+ sb.append(", updatedAt=").append(updatedAt);
+ sb.append('}');
+ return sb.toString();
+ }
}
* versions must be previously upgraded to LTS version.
* Note that the value can't be less than current LTS version.
*/
- public static final int MIN_UPGRADE_VERSION = 1152;
+ public static final int MIN_UPGRADE_VERSION = 1_152;
/**
* These tables are still involved in DB migrations, so potentially
import org.sonar.db.version.v61.PopulateTableProperties2;
import org.sonar.db.version.v61.RemoveViewsDefinitionFromProperties;
import org.sonar.db.version.v61.ShrinkModuleUuidPathOfProjects;
+<<<<<<< HEAD
import org.sonar.db.version.v62.AddIsRootColumnOnTableUsers;
+=======
+import org.sonar.db.version.v62.AddOrganizationUuidToGroups;
+>>>>>>> SONAR-8134 add db column groups.organization_uuid
import org.sonar.db.version.v62.CreateDefaultOrganization;
import org.sonar.db.version.v62.CreateTableOrganizations;
import org.sonar.db.version.v62.DeletePermissionShareDashboard;
CreateTableOrganizations.class,
CreateDefaultOrganization.class,
DeletePermissionShareDashboard.class,
+<<<<<<< HEAD
AddIsRootColumnOnTableUsers.class,
PopulateIsRootColumnOnTableUsers.class,
MakeRootColumnNotNullOnTableUsers.class);
+=======
+ AddOrganizationUuidToGroups.class
+ );
+>>>>>>> SONAR-8134 add db column groups.organization_uuid
}
}
--- /dev/null
+/*
+ * 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.db.version.v62;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.version.AddColumnsBuilder;
+import org.sonar.db.version.DdlChange;
+import org.sonar.db.version.VarcharColumnDef;
+
+import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class AddOrganizationUuidToGroups extends DdlChange {
+
+ public AddOrganizationUuidToGroups(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ VarcharColumnDef column = newVarcharColumnDefBuilder()
+ .setColumnName("organization_uuid")
+ .setIsNullable(true)
+ .setLimit(40)
+ .build();
+ context.execute(new AddColumnsBuilder(getDialect(), "groups").addColumn(column).build());
+ }
+}
g.id as id,
g.name as name,
g.description as description,
+ g.organization_uuid as organizationUuid,
g.created_at as "createdAt",
g.updated_at as "updatedAt"
</sql>
</delete>
<select id="selectByUserLogin" parameterType="string" resultType="Group">
- SELECT
+ select
<include refid="groupColumns"/>
- FROM groups g
- INNER JOIN groups_users gu on gu.group_id=g.id
- INNER JOIN users u on u.id=gu.user_id
- <where>
- u.login=#{login}
- </where>
+ from groups g
+ inner join groups_users gu on gu.group_id = g.id
+ inner join users u on u.id = gu.user_id
+ where u.login=#{login}
</select>
<select id="selectByNames" parameterType="map" resultType="Group">
</select>
<insert id="insert" parameterType="Group" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
- INSERT INTO groups (name, description, created_at, updated_at)
- VALUES (#{name}, #{description}, #{createdAt}, #{updatedAt})
+ insert into groups (organization_uuid, name, description, created_at, updated_at)
+ values (
+ #{organizationUuid,jdbcType=VARCHAR},
+ #{name,jdbcType=VARCHAR},
+ #{description,jdbcType=VARCHAR},
+ #{createdAt},
+ #{updatedAt}
+ )
</insert>
<update id="update" parameterType="Group">
CREATE TABLE "GROUPS" (
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "ORGANIZATION_UUID" VARCHAR(40),
"NAME" VARCHAR(500),
"DESCRIPTION" VARCHAR(200),
"CREATED_AT" TIMESTAMP,
package org.sonar.db.user;
import java.util.Collections;
+import java.util.Date;
import java.util.List;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.RowNotFoundException;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
public class GroupDaoTest {
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
+ private static final long NOW = 1_500_000L;
- final DbSession dbSession = db.getSession();
- System2 system2 = mock(System2.class);
+ private System2 system2 = mock(System2.class);
- GroupDao underTest = new GroupDao(system2);
+ @Rule
+ public DbTester db = DbTester.create(system2);
+ private final DbSession dbSession = db.getSession();
+ private GroupDao underTest = new GroupDao(system2);
+
+ // not static as group id is changed in each test
+ private final GroupDto aGroup = new GroupDto()
+ .setName("the-name")
+ .setDescription("the description")
+ .setOrganizationUuid("org1");
+
+ @Before
+ public void setUp() {
+ when(system2.now()).thenReturn(NOW);
+ }
@Test
- public void select_by_key() {
- db.prepareDbUnit(getClass(), "select_by_key.xml");
+ public void test_insert_and_selectOrFailByName() {
+ db.getDbClient().groupDao().insert(dbSession, aGroup);
+
+ GroupDto group = underTest.selectOrFailByName(dbSession, aGroup.getName());
- GroupDto group = underTest.selectOrFailByName(dbSession, "sonar-users");
+ assertThat(group.getId()).isNotNull();
+ assertThat(group.getOrganizationUuid()).isEqualTo(aGroup.getOrganizationUuid());
+ assertThat(group.getName()).isEqualTo(aGroup.getName());
+ assertThat(group.getDescription()).isEqualTo(aGroup.getDescription());
+ assertThat(group.getCreatedAt()).isEqualTo(new Date(NOW));
+ assertThat(group.getUpdatedAt()).isEqualTo(new Date(NOW));
+ }
- assertThat(group).isNotNull();
- assertThat(group.getId()).isEqualTo(1L);
- assertThat(group.getName()).isEqualTo("sonar-users");
- assertThat(group.getDescription()).isEqualTo("Sonar Users");
- assertThat(group.getCreatedAt()).isEqualTo(DateUtils.parseDate("2014-09-07"));
- assertThat(group.getUpdatedAt()).isEqualTo(DateUtils.parseDate("2014-09-08"));
+ @Test(expected = RowNotFoundException.class)
+ public void selectOrFailByName_throws_NFE_if_not_found() {
+ underTest.selectOrFailByName(dbSession, "missing");
}
@Test
- public void select_by_id() {
- db.prepareDbUnit(getClass(), "select_by_key.xml");
+ public void selectOrFailById() {
+ db.getDbClient().groupDao().insert(dbSession, aGroup);
- GroupDto group = underTest.selectOrFailById(dbSession, 1L);
+ GroupDto group = underTest.selectOrFailById(dbSession, aGroup.getId());
- assertThat(group).isNotNull();
- assertThat(group.getId()).isEqualTo(1L);
- assertThat(group.getName()).isEqualTo("sonar-users");
- assertThat(group.getDescription()).isEqualTo("Sonar Users");
- assertThat(group.getCreatedAt()).isEqualTo(DateUtils.parseDate("2014-09-07"));
- assertThat(group.getUpdatedAt()).isEqualTo(DateUtils.parseDate("2014-09-08"));
+ assertThat(group.getName()).isEqualTo(aGroup.getName());
}
@Test
- public void find_by_user_login() {
+ public void selectByUserLogin() {
db.prepareDbUnit(getClass(), "find_by_user_login.xml");
assertThat(underTest.selectByUserLogin(dbSession, "john")).hasSize(2);
}
@Test
- public void select_by_names() {
- underTest.insert(dbSession, new GroupDto().setName("group1"));
- underTest.insert(dbSession, new GroupDto().setName("group2"));
- underTest.insert(dbSession, new GroupDto().setName("group3"));
+ public void selectByNames() {
+ underTest.insert(dbSession, newGroupDto().setName("group1"));
+ underTest.insert(dbSession, newGroupDto().setName("group2"));
+ underTest.insert(dbSession, newGroupDto().setName("group3"));
dbSession.commit();
assertThat(underTest.selectByNames(dbSession, asList("group1", "group2", "group3"))).hasSize(3);
assertThat(underTest.selectByNames(dbSession, singletonList("group1"))).hasSize(1);
assertThat(underTest.selectByNames(dbSession, asList("group1", "unknown"))).hasSize(1);
- assertThat(underTest.selectByNames(dbSession, Collections.<String>emptyList())).isEmpty();
- }
-
- @Test
- public void insert() {
- when(system2.now()).thenReturn(DateUtils.parseDate("2014-09-08").getTime());
- db.prepareDbUnit(getClass(), "empty.xml");
- GroupDto dto = new GroupDto()
- .setId(1L)
- .setName("sonar-users")
- .setDescription("Sonar Users");
-
- underTest.insert(dbSession, dto);
- dbSession.commit();
-
- db.assertDbUnit(getClass(), "insert-result.xml", "groups");
+ assertThat(underTest.selectByNames(dbSession, Collections.emptyList())).isEmpty();
}
@Test
public void update() {
- when(system2.now()).thenReturn(DateUtils.parseDate("2013-07-25").getTime());
- db.prepareDbUnit(getClass(), "update.xml");
+ db.getDbClient().groupDao().insert(dbSession, aGroup);
GroupDto dto = new GroupDto()
- .setId(1L)
+ .setId(aGroup.getId())
.setName("new-name")
- .setDescription("New Description");
+ .setDescription("New description")
+ .setOrganizationUuid("another-org")
+ .setCreatedAt(new Date(NOW + 1_000L));
underTest.update(dbSession, dto);
- dbSession.commit();
- db.assertDbUnit(getClass(), "update-result.xml", "groups");
+ GroupDto reloaded = underTest.selectById(dbSession, aGroup.getId());
+
+ // verify mutable fields
+ assertThat(reloaded.getName()).isEqualTo("new-name");
+ assertThat(reloaded.getDescription()).isEqualTo("New description");
+
+ // immutable fields --> to be ignored
+ assertThat(reloaded.getOrganizationUuid()).isEqualTo(aGroup.getOrganizationUuid());
+ assertThat(reloaded.getCreatedAt()).isEqualTo(aGroup.getCreatedAt());
}
@Test
- public void select_by_query() {
+ public void selectByQuery() {
db.prepareDbUnit(getClass(), "select_by_query.xml");
/*
}
@Test
- public void count_by_query() {
+ public void countByQuery() {
db.prepareDbUnit(getClass(), "select_by_query.xml");
// Null query
}
@Test
- public void delete_by_id() {
- db.prepareDbUnit(getClass(), "select_by_key.xml");
+ public void deleteById() {
+ db.getDbClient().groupDao().insert(dbSession, aGroup);
- GroupDao groupDao = underTest;
- groupDao.deleteById(dbSession, 1L);
- dbSession.commit();
+ underTest.deleteById(dbSession, aGroup.getId());
- assertThat(groupDao.countByQuery(dbSession, null)).isZero();
+ assertThat(db.countRowsOfTable(dbSession, "groups")).isEqualTo(0);
}
}
+++ /dev/null
-/*
- * 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.db.user;
-
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class GroupDtoTest {
-
- @Test
- public void getter_and_setter() {
- GroupDto dto = new GroupDto()
- .setId(1L)
- .setName("sonar-users")
- .setDescription("Sonar users");
-
- assertThat(dto.getKey()).isEqualTo("sonar-users");
- assertThat(dto.getName()).isEqualTo("sonar-users");
- assertThat(dto.getId()).isEqualTo(1L);
- assertThat(dto.getDescription()).isEqualTo("Sonar users");
- }
-
-}
public class GroupTesting {
+ private GroupTesting() {
+ // only statics
+ }
+
public static GroupDto newGroupDto() {
GroupDto group = new GroupDto()
+ .setOrganizationUuid(randomAlphanumeric(40))
.setName(randomAlphanumeric(255))
- .setDescription(randomAlphanumeric(200));
- group.setCreatedAt(new Date(nextLong()));
- group.setUpdatedAt(new Date(nextLong()));
+ .setDescription(randomAlphanumeric(200))
+ .setCreatedAt(new Date(nextLong()))
+ .setUpdatedAt(new Date(nextLong()));
return group;
}
}
--- /dev/null
+/*
+ * 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.db.version.v62;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+
+public class AddOrganizationUuidToGroupsTest {
+
+ @Rule
+ public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, AddOrganizationUuidToGroupsTest.class, "previous-groups.sql");
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private AddOrganizationUuidToGroups underTest = new AddOrganizationUuidToGroups(dbTester.database());
+
+ @Test
+ public void creates_table_on_empty_db() throws SQLException {
+ underTest.execute();
+
+ dbTester.assertColumnDefinition("groups", "organization_uuid", Types.VARCHAR, 40, true);
+ }
+
+ @Test
+ public void migration_is_not_reentrant() throws SQLException {
+ underTest.execute();
+
+ expectedException.expect(IllegalStateException.class);
+
+ underTest.execute();
+ }
+}
+++ /dev/null
-<dataset>
-
-
-</dataset>
name="sonar-users"
description="Sonar Users"
created_at="2014-09-07"
- updated_at="2014-09-08"/>
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
<groups id="2"
name="sonar-admins"
description="Sonar Admins"
created_at="2014-09-07"
- updated_at="2014-09-08"/>
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
<groups id="3"
name="sonar-reviewers"
description="Sonar Reviewers"
created_at="2014-09-07"
- updated_at="2014-09-08"/>
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
<groups_users user_id="100"
group_id="1"/>
email="jo@hn.com"
created_at="1418215735482"
updated_at="1418215735482"
- active="[true]"
- is_root="[false]"/>
+ active="[true]"/>
</dataset>
+++ /dev/null
-<dataset>
-
- <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-08" updated_at="2014-09-08"/>
-
-</dataset>
+++ /dev/null
-<dataset>
-
- <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-07" updated_at="2014-09-08"/>
-
-</dataset>
<dataset>
- <groups id="1" name="sonar-users" description="Sonar Users" created_at="2014-09-07" updated_at="2014-09-08"/>
- <groups id="2" name="SONAR-ADMINS" description="Sonar Admins" created_at="2014-09-07" updated_at="2014-09-08"/>
- <groups id="3" name="customers-group1" description="Group 1" created_at="2014-09-07" updated_at="2014-09-08"/>
- <groups id="4" name="customers-group2" description="Group 2" created_at="2014-09-07" updated_at="2014-09-08"/>
- <groups id="5" name="customers-group3" description="Group 3" created_at="2014-09-07" updated_at="2014-09-08"/>
+ <groups id="1"
+ name="sonar-users"
+ description="Sonar Users"
+ created_at="2014-09-07"
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
+ <groups id="2"
+ name="SONAR-ADMINS"
+ description="Sonar Admins"
+ created_at="2014-09-07"
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
+ <groups id="3"
+ name="customers-group1"
+ description="Group 1"
+ created_at="2014-09-07"
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
+ <groups id="4"
+ name="customers-group2"
+ description="Group 2"
+ created_at="2014-09-07"
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
+ <groups id="5"
+ name="customers-group3"
+ description="Group 3"
+ created_at="2014-09-07"
+ updated_at="2014-09-08"
+ organization_uuid="org1"/>
</dataset>
+++ /dev/null
-<dataset>
-
- <groups id="1" name="new-name" description="New Description" created_at="2011-04-25" updated_at="2013-07-25"/>
-
- <groups id="2" name="untouched" description="Untouched" created_at="2003-03-23" updated_at="2006-09-09"/>
-
-</dataset>
+++ /dev/null
-<dataset>
-
- <groups id="1" name="old-name" description="Old Description" created_at="2011-04-25" updated_at="2011-04-25"/>
-
- <groups id="2" name="untouched" description="Untouched" created_at="2003-03-23" updated_at="2006-09-09"/>
-
-</dataset>
--- /dev/null
+CREATE TABLE "GROUPS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "NAME" VARCHAR(500),
+ "DESCRIPTION" VARCHAR(200),
+ "CREATED_AT" TIMESTAMP,
+ "UPDATED_AT" TIMESTAMP
+);