diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-22 12:37:05 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-22 12:37:48 +0200 |
commit | 0d6bfedb3821fc5c30d469240718cdaedd43977f (patch) | |
tree | 32b52378111d5e04e6d44c7b58c6c455321ca0d0 /sonar-batch | |
parent | d74a8af497dc4334907d02d891551d3e539ec9c1 (diff) | |
download | sonarqube-0d6bfedb3821fc5c30d469240718cdaedd43977f.tar.gz sonarqube-0d6bfedb3821fc5c30d469240718cdaedd43977f.zip |
SONAR-5755 Populate uuid column on new resources
Diffstat (limited to 'sonar-batch')
5 files changed, 89 insertions, 18 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 1c37360eb8b..8942a8d3140 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -573,8 +573,8 @@ public class DefaultIndex extends SonarIndex { bucket = new Bucket(resource).setParent(parentBucket); addBucket(resource, bucket); - Resource parentSnapshot = parentBucket != null ? parentBucket.getResource() : null; - Snapshot snapshot = persistence.saveResource(currentProject, resource, parentSnapshot); + Resource parentResource = parentBucket != null ? parentBucket.getResource() : null; + Snapshot snapshot = persistence.saveResource(currentProject, resource, parentResource); if (ResourceUtils.isPersistable(resource) && !Qualifiers.LIBRARY.equals(resource.getQualifier())) { graph.addComponent(resource, snapshot); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index fcc4dd0262c..d9e84a88160 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -19,8 +19,6 @@ */ package org.sonar.batch.index; -import org.sonar.api.resources.Language; - import com.google.common.collect.Maps; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; @@ -29,6 +27,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.File; +import org.sonar.api.resources.Language; import org.sonar.api.resources.Library; import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; @@ -47,6 +46,7 @@ import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.UUID; public final class DefaultResourcePersister implements ResourcePersister { @@ -302,6 +302,7 @@ public final class DefaultResourcePersister implements ResourcePersister { model.setEnabled(Boolean.TRUE); model.setDescription(resource.getDescription()); model.setKey(resource.getEffectiveKey()); + model.setUuid(UUID.randomUUID().toString()); model.setPath(resource.getPath()); Language language = resource.getLanguage(); if (language != null) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java index d90d4e1a24c..c8f440f6718 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java @@ -19,20 +19,26 @@ */ package org.sonar.batch.index; +import org.apache.ibatis.session.SqlSession; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; -import org.sonar.api.database.model.ResourceModel; +import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Directory; import org.sonar.api.resources.File; import org.sonar.api.resources.Library; import org.sonar.api.resources.Project; import org.sonar.api.security.ResourcePermissions; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.component.db.ComponentMapper; +import org.sonar.core.persistence.MyBatis; import org.sonar.jpa.test.AbstractDbUnitTestCase; +import javax.persistence.Query; + import java.text.ParseException; import java.text.SimpleDateFormat; @@ -92,11 +98,19 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(singleProject, null); - checkTables("shouldSaveNewProject", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); - - // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table - ResourceModel model = getSession().getSingleResult(ResourceModel.class, "key", singleProject.getKey()); - assertThat(model.getCreatedAt()).isNotNull(); + checkTables("shouldSaveNewProject", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid"}, "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + SqlSession session = getMyBatis().openSession(false); + try { + ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); + assertThat(newProject.uuid()).isNotNull(); + // SONAR-3636 : created_at must be fed when inserting a new entry in the 'projects' table + assertThat(newProject.getCreatedAt()).isNotNull(); + } finally { + MyBatis.closeQuietly(session); + } } @Test @@ -106,7 +120,16 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(singleCopyProject, null); - checkTables("shouldSaveCopyProject", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); + checkTables("shouldSaveCopyProject", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid"}, "projects", "snapshots"); + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + SqlSession session = getMyBatis().openSession(false); + try { + ComponentDto newProject = session.getMapper(ComponentMapper.class).selectByKey("foo"); + assertThat(newProject.uuid()).isNotNull(); + } finally { + MyBatis.closeQuietly(session); + } } @Test @@ -119,7 +142,26 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveProject(moduleB, multiModuleProject); persister.saveProject(moduleB1, moduleB); - checkTables("shouldSaveNewMultiModulesProject", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); + checkTables("shouldSaveNewMultiModulesProject", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid"}, "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1001); + enableSnapshot(1002); + enableSnapshot(1003); + enableSnapshot(1004); + SqlSession session = getMyBatis().openSession(false); + try { + ComponentDto root = session.getMapper(ComponentMapper.class).selectByKey("root"); + assertThat(root.uuid()).isNotNull(); + ComponentDto a = session.getMapper(ComponentMapper.class).selectByKey("a"); + assertThat(a.uuid()).isNotNull(); + ComponentDto b = session.getMapper(ComponentMapper.class).selectByKey("b"); + assertThat(b.uuid()).isNotNull(); + ComponentDto b1 = session.getMapper(ComponentMapper.class).selectByKey("b1"); + assertThat(b1.uuid()).isNotNull(); + } finally { + MyBatis.closeQuietly(session); + } } @Test @@ -130,9 +172,27 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveProject(singleProject, null); persister.saveResource(singleProject, Directory.create("src/main/java/org/foo", "org.foo").setEffectiveKey("foo:src/main/java/org/foo")); - // check that the directory is attached to the project - checkTables("shouldSaveNewDirectory", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); + checkTables("shouldSaveNewDirectory", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid"}, "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1002); + SqlSession session = getMyBatis().openSession(false); + try { + ComponentDto newDir = session.getMapper(ComponentMapper.class).selectByKey("foo:src/main/java/org/foo"); + assertThat(newDir.uuid()).isNotNull(); + } finally { + MyBatis.closeQuietly(session); + } + } + + private void enableSnapshot(int resourceId) { + String hql = "UPDATE " + Snapshot.class.getSimpleName() + " SET last=true"; + hql += " WHERE project_id=:resourceId"; + Query query = getSession().createQuery(hql); + query.setParameter("resourceId", resourceId); + query.executeUpdate(); + getSession().commit(); } @Test @@ -145,7 +205,17 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));// do nothing, already saved persister.saveResource(singleProject, new Library("junit:junit", "3.2").setEffectiveKey("junit:junit")); - checkTables("shouldSaveNewLibrary", new String[] {"build_date", "created_at", "authorization_updated_at"}, "projects", "snapshots"); + checkTables("shouldSaveNewLibrary", new String[] {"build_date", "created_at", "authorization_updated_at", "uuid"}, "projects", "snapshots"); + + // Need to enable snapshot to make resource visible using ComponentMapper + enableSnapshot(1002); + SqlSession session = getMyBatis().openSession(false); + try { + ComponentDto newLib = session.getMapper(ComponentMapper.class).selectByKey("junit:junit"); + assertThat(newLib.uuid()).isNotNull(); + } finally { + MyBatis.closeQuietly(session); + } } @Test diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml index 9b3e921c819..4ac03aed376 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml @@ -10,11 +10,11 @@ status="P" islast="false" depth="0"/> - <!-- new project --> <projects id="1001" scope="PRJ" qualifier="TRK" kee="foo" root_id="[null]" uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" name="Foo" long_name="Foo" description="some description" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="foo" /> + <!-- new dir --> <projects id="1002" scope="DIR" qualifier="DIR" kee="foo:src/main/java/org/foo" root_id="1001" uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" name="src/main/java/org/foo" long_name="src/main/java/org/foo" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="src/main/java/org/foo" deprecated_kee="[null]" /> diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml index 11da8f2d50a..1f2478a3fc3 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml @@ -11,11 +11,11 @@ <!-- new project --> - <projects id="1001" scope="PRJ" qualifier="TRK" kee="foo" root_id="[null]" uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" + <projects id="1001" scope="PRJ" qualifier="TRK" kee="foo" root_id="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" name="Foo" long_name="Foo" description="some description" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="foo" /> - <projects id="1002" scope="PRJ" qualifier="LIB" kee="junit:junit" root_id="[null]" uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" + <projects id="1002" scope="PRJ" qualifier="LIB" kee="junit:junit" root_id="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]" name="junit:junit" long_name="junit:junit" description="[null]" enabled="true" language="[null]" copy_resource_id="[null]" person_id="[null]" deprecated_kee="[null]" /> |