/* | |||||
* 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.component; | |||||
import com.google.common.base.Preconditions; | |||||
import com.google.common.collect.ImmutableSet; | |||||
import org.apache.commons.lang.builder.ReflectionToStringBuilder; | |||||
import javax.annotation.CheckForNull; | |||||
import javax.annotation.Nullable; | |||||
import java.util.Collection; | |||||
import java.util.Collections; | |||||
import java.util.Set; | |||||
/** | |||||
* @since 3.7 | |||||
*/ | |||||
public class ComponentQuery { | |||||
public static final int DEFAULT_PAGE_INDEX = 1; | |||||
public static final int DEFAULT_PAGE_SIZE = 100; | |||||
public static final int NO_PAGINATION = -1; | |||||
public static final String SORT_BY_NAME = "NAME"; | |||||
public static final Set<String> SORTS = ImmutableSet.of(SORT_BY_NAME); | |||||
private final Collection<String> keys; | |||||
private final Collection<String> names; | |||||
private final Collection<String> qualifiers; | |||||
private final String sort; | |||||
private final Boolean asc; | |||||
// max results per page | |||||
private final int pageSize; | |||||
// index of selected page. Start with 1. | |||||
private final int pageIndex; | |||||
private ComponentQuery(Builder builder) { | |||||
this.keys = defaultCollection(builder.keys); | |||||
this.names = defaultCollection(builder.names); | |||||
this.qualifiers = defaultCollection(builder.qualifiers); | |||||
this.sort = builder.sort; | |||||
this.asc = builder.asc; | |||||
this.pageSize = builder.pageSize; | |||||
this.pageIndex = builder.pageIndex; | |||||
} | |||||
/** | |||||
* Pattern of component keys to search. Can contain a sub part of keys. | |||||
* Example : 'org.codehaus' will return 'org.codehaus.sonar', 'org.codehaus.tike', etc. | |||||
*/ | |||||
public Collection<String> keys() { | |||||
return keys; | |||||
} | |||||
/** | |||||
* Pattern of component name to search. Can contain a sub part of names. | |||||
* Example : 'Sona' will return 'Sonar', 'SonarJ', etc. | |||||
*/ | |||||
public Collection<String> names() { | |||||
return names; | |||||
} | |||||
/** | |||||
* Qualifiers of components to search. | |||||
*/ | |||||
public Collection<String> qualifiers() { | |||||
return qualifiers; | |||||
} | |||||
@CheckForNull | |||||
public String sort() { | |||||
return sort; | |||||
} | |||||
@CheckForNull | |||||
public Boolean asc() { | |||||
return asc; | |||||
} | |||||
public int pageSize() { | |||||
return pageSize; | |||||
} | |||||
public int pageIndex() { | |||||
return pageIndex; | |||||
} | |||||
@Override | |||||
public String toString() { | |||||
return ReflectionToStringBuilder.toString(this); | |||||
} | |||||
public static Builder builder() { | |||||
return new Builder(); | |||||
} | |||||
public static class Builder { | |||||
private Collection<String> keys; | |||||
private Collection<String> names; | |||||
private Collection<String> qualifiers; | |||||
private String sort = SORT_BY_NAME; | |||||
private Boolean asc = true; | |||||
private Integer pageSize; | |||||
private Integer pageIndex; | |||||
private Builder() { | |||||
} | |||||
public Builder keys(@Nullable Collection<String> l) { | |||||
this.keys = l; | |||||
return this; | |||||
} | |||||
public Builder names(@Nullable Collection<String> l) { | |||||
this.names = l; | |||||
return this; | |||||
} | |||||
public Builder qualifiers(@Nullable Collection<String> l) { | |||||
this.qualifiers = l; | |||||
return this; | |||||
} | |||||
public Builder sort(@Nullable String s) { | |||||
if (s != null && !SORTS.contains(s)) { | |||||
throw new IllegalArgumentException("Bad sort field: " + s); | |||||
} | |||||
this.sort = s; | |||||
return this; | |||||
} | |||||
public Builder asc(@Nullable Boolean asc) { | |||||
this.asc = asc; | |||||
return this; | |||||
} | |||||
public Builder pageSize(@Nullable Integer i) { | |||||
this.pageSize = i; | |||||
return this; | |||||
} | |||||
public Builder pageIndex(@Nullable Integer i) { | |||||
this.pageIndex = i; | |||||
return this; | |||||
} | |||||
public ComponentQuery build() { | |||||
initPageIndex(); | |||||
initPageSize(); | |||||
return new ComponentQuery(this); | |||||
} | |||||
private void initPageSize() { | |||||
if (pageSize == null) { | |||||
pageSize = DEFAULT_PAGE_SIZE; | |||||
} | |||||
} | |||||
private void initPageIndex() { | |||||
if (pageIndex == null) { | |||||
pageIndex = DEFAULT_PAGE_INDEX; | |||||
} | |||||
Preconditions.checkArgument(pageIndex > 0, "Page index must be greater than 0 (got " + pageIndex + ")"); | |||||
} | |||||
} | |||||
private static <T> Collection<T> defaultCollection(@Nullable Collection<T> c) { | |||||
return c == null ? Collections.<T>emptyList() : Collections.unmodifiableCollection(c); | |||||
} | |||||
} |
*/ | */ | ||||
package org.sonar.server.component; | package org.sonar.server.component; | ||||
import com.google.common.base.Strings; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import javax.annotation.CheckForNull; | import javax.annotation.CheckForNull; | ||||
import javax.annotation.Nullable; | import javax.annotation.Nullable; | ||||
import org.sonar.api.component.Component; | import org.sonar.api.component.Component; | ||||
import org.sonar.db.DbClient; | import org.sonar.db.DbClient; | ||||
import org.sonar.db.DbSession; | import org.sonar.db.DbSession; | ||||
import org.sonar.db.component.ComponentDto; | import org.sonar.db.component.ComponentDto; | ||||
import org.sonar.db.component.ResourceDao; | |||||
import org.sonar.db.component.ResourceDto; | |||||
import org.sonar.server.favorite.FavoriteUpdater; | import org.sonar.server.favorite.FavoriteUpdater; | ||||
import org.sonar.server.organization.DefaultOrganizationProvider; | import org.sonar.server.organization.DefaultOrganizationProvider; | ||||
import org.sonar.server.permission.PermissionTemplateService; | import org.sonar.server.permission.PermissionTemplateService; | ||||
import org.sonar.server.util.RubyUtils; | |||||
import static org.sonar.server.component.NewComponent.newComponentBuilder; | import static org.sonar.server.component.NewComponent.newComponentBuilder; | ||||
public class DefaultRubyComponentService implements RubyComponentService { | public class DefaultRubyComponentService implements RubyComponentService { | ||||
private final DbClient dbClient; | private final DbClient dbClient; | ||||
private final ResourceDao resourceDao; | |||||
private final ComponentService componentService; | private final ComponentService componentService; | ||||
private final PermissionTemplateService permissionTemplateService; | private final PermissionTemplateService permissionTemplateService; | ||||
private final FavoriteUpdater favoriteUpdater; | private final FavoriteUpdater favoriteUpdater; | ||||
private final DefaultOrganizationProvider defaultOrganizationProvider; | private final DefaultOrganizationProvider defaultOrganizationProvider; | ||||
public DefaultRubyComponentService(DbClient dbClient, ResourceDao resourceDao, ComponentService componentService, | |||||
public DefaultRubyComponentService(DbClient dbClient, ComponentService componentService, | |||||
PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater, | PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater, | ||||
DefaultOrganizationProvider defaultOrganizationProvider) { | DefaultOrganizationProvider defaultOrganizationProvider) { | ||||
this.dbClient = dbClient; | this.dbClient = dbClient; | ||||
this.resourceDao = resourceDao; | |||||
this.componentService = componentService; | this.componentService = componentService; | ||||
this.permissionTemplateService = permissionTemplateService; | this.permissionTemplateService = permissionTemplateService; | ||||
this.favoriteUpdater = favoriteUpdater; | this.favoriteUpdater = favoriteUpdater; | ||||
return provisionedComponent.getId(); | return provisionedComponent.getId(); | ||||
} | } | ||||
// Used in GOV | |||||
public List<ResourceDto> findProvisionedProjects(Map<String, Object> params) { | |||||
ComponentQuery query = toQuery(params); | |||||
return resourceDao.selectProvisionedProjects(query.qualifiers()); | |||||
} | |||||
static ComponentQuery toQuery(Map<String, Object> props) { | |||||
ComponentQuery.Builder builder = ComponentQuery.builder() | |||||
.keys(RubyUtils.toStrings(props.get("keys"))) | |||||
.names(RubyUtils.toStrings(props.get("names"))) | |||||
.qualifiers(RubyUtils.toStrings(props.get("qualifiers"))) | |||||
.pageSize(RubyUtils.toInteger(props.get("pageSize"))) | |||||
.pageIndex(RubyUtils.toInteger(props.get("pageIndex"))); | |||||
String sort = (String) props.get("sort"); | |||||
if (!Strings.isNullOrEmpty(sort)) { | |||||
builder.sort(sort); | |||||
builder.asc(RubyUtils.toBoolean(props.get("asc"))); | |||||
} | |||||
return builder.build(); | |||||
} | |||||
} | } |
/* | |||||
* 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.component; | |||||
import org.junit.Test; | |||||
import static com.google.common.collect.Lists.newArrayList; | |||||
import static org.assertj.core.api.Assertions.assertThat; | |||||
public class ComponentQueryTest { | |||||
@Test | |||||
public void should_build_query() { | |||||
ComponentQuery query = ComponentQuery.builder() | |||||
.keys(newArrayList("org.codehaus")) | |||||
.names(newArrayList("Sona")) | |||||
.qualifiers(newArrayList("TRK")) | |||||
.pageSize(10) | |||||
.pageIndex(2) | |||||
.sort(ComponentQuery.SORT_BY_NAME) | |||||
.asc(true) | |||||
.build(); | |||||
assertThat(query.keys()).containsOnly("org.codehaus"); | |||||
assertThat(query.names()).containsOnly("Sona"); | |||||
assertThat(query.qualifiers()).containsOnly("TRK"); | |||||
assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME); | |||||
assertThat(query.asc()).isTrue(); | |||||
assertThat(query.pageSize()).isEqualTo(10); | |||||
assertThat(query.pageIndex()).isEqualTo(2); | |||||
} | |||||
@Test | |||||
public void should_accept_null_sort() { | |||||
ComponentQuery query = ComponentQuery.builder().sort(null).build(); | |||||
assertThat(query.sort()).isNull(); | |||||
} | |||||
@Test | |||||
public void should_sort_by_name_asc_by_default() { | |||||
ComponentQuery query = ComponentQuery.builder().build(); | |||||
assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME); | |||||
assertThat(query.asc()).isTrue(); | |||||
} | |||||
@Test | |||||
public void should_throw_exception_if_sort_is_not_valid() { | |||||
try { | |||||
ComponentQuery.builder() | |||||
.sort("UNKNOWN") | |||||
.build(); | |||||
} catch (Exception e) { | |||||
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Bad sort field: UNKNOWN"); | |||||
} | |||||
} | |||||
@Test | |||||
public void test_default_page_index_and_size() throws Exception { | |||||
ComponentQuery query = ComponentQuery.builder().build(); | |||||
assertThat(query.pageSize()).isEqualTo(ComponentQuery.DEFAULT_PAGE_SIZE); | |||||
assertThat(query.pageIndex()).isEqualTo(ComponentQuery.DEFAULT_PAGE_INDEX); | |||||
} | |||||
@Test | |||||
public void should_build_non_paginated_query() { | |||||
ComponentQuery query = ComponentQuery.builder().pageSize(ComponentQuery.NO_PAGINATION).build(); | |||||
assertThat(query.pageSize()).isEqualTo(ComponentQuery.NO_PAGINATION); | |||||
assertThat(query.pageIndex()).isEqualTo(ComponentQuery.DEFAULT_PAGE_INDEX); | |||||
} | |||||
} |
*/ | */ | ||||
package org.sonar.server.component; | package org.sonar.server.component; | ||||
import java.util.List; | |||||
import java.util.Map; | |||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.sonar.db.DbClient; | import org.sonar.db.DbClient; | ||||
import org.sonar.db.DbSession; | import org.sonar.db.DbSession; | ||||
import org.sonar.db.DbTester; | import org.sonar.db.DbTester; | ||||
import org.sonar.db.component.ComponentDbTester; | |||||
import org.sonar.db.component.ComponentDto; | import org.sonar.db.component.ComponentDto; | ||||
import org.sonar.db.component.ResourceDao; | import org.sonar.db.component.ResourceDao; | ||||
import org.sonar.db.component.ResourceDto; | |||||
import org.sonar.server.component.index.ComponentIndexDefinition; | import org.sonar.server.component.index.ComponentIndexDefinition; | ||||
import org.sonar.server.component.index.ComponentIndexer; | import org.sonar.server.component.index.ComponentIndexer; | ||||
import org.sonar.server.es.EsTester; | import org.sonar.server.es.EsTester; | ||||
import org.sonar.server.permission.PermissionTemplateService; | import org.sonar.server.permission.PermissionTemplateService; | ||||
import org.sonar.server.tester.UserSessionRule; | import org.sonar.server.tester.UserSessionRule; | ||||
import static com.google.common.collect.Lists.newArrayList; | |||||
import static com.google.common.collect.Maps.newHashMap; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.mockito.Matchers.any; | import static org.mockito.Matchers.any; | ||||
import static org.mockito.Matchers.eq; | import static org.mockito.Matchers.eq; | ||||
private DbClient dbClient = db.getDbClient(); | private DbClient dbClient = db.getDbClient(); | ||||
private DbSession dbSession = db.getSession(); | private DbSession dbSession = db.getSession(); | ||||
private ResourceDao resourceDao = dbClient.resourceDao(); | |||||
private ComponentService componentService = new ComponentService(dbClient, i18n, userSession, system2, new ComponentFinder(dbClient), | private ComponentService componentService = new ComponentService(dbClient, i18n, userSession, system2, new ComponentFinder(dbClient), | ||||
new ProjectMeasuresIndexer(system2, dbClient, es.client()), new ComponentIndexer(dbClient, es.client())); | new ProjectMeasuresIndexer(system2, dbClient, es.client()), new ComponentIndexer(dbClient, es.client())); | ||||
private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); | private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); | ||||
private FavoriteUpdater favoriteUpdater = mock(FavoriteUpdater.class); | private FavoriteUpdater favoriteUpdater = mock(FavoriteUpdater.class); | ||||
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); | private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); | ||||
private ComponentDbTester componentDb = new ComponentDbTester(db); | |||||
private DefaultRubyComponentService underTest = new DefaultRubyComponentService(dbClient, resourceDao, componentService, | |||||
private DefaultRubyComponentService underTest = new DefaultRubyComponentService(dbClient, componentService, | |||||
permissionTemplateService, favoriteUpdater, defaultOrganizationProvider); | permissionTemplateService, favoriteUpdater, defaultOrganizationProvider); | ||||
private String defaultOrganizationUuid; | private String defaultOrganizationUuid; | ||||
underTest.createComponent("1234", "New Project", Qualifiers.PROJECT); | underTest.createComponent("1234", "New Project", Qualifiers.PROJECT); | ||||
} | } | ||||
@Test | |||||
public void should_find_provisioned_projects() { | |||||
componentDb.insertProject(); | |||||
List<String> qualifiers = newArrayList("TRK"); | |||||
Map<String, Object> map = newHashMap(); | |||||
map.put("qualifiers", qualifiers); | |||||
List<ResourceDto> resourceDtos = underTest.findProvisionedProjects(map); | |||||
assertThat(resourceDtos).hasSize(1); | |||||
} | |||||
@Test | |||||
public void should_create_query_from_parameters() { | |||||
Map<String, Object> map = newHashMap(); | |||||
map.put("keys", newArrayList("org.codehaus.sonar")); | |||||
map.put("names", newArrayList("Sonar")); | |||||
map.put("qualifiers", newArrayList("TRK")); | |||||
map.put("pageSize", 10l); | |||||
map.put("pageIndex", 50); | |||||
map.put("sort", "NAME"); | |||||
map.put("asc", true); | |||||
ComponentQuery query = DefaultRubyComponentService.toQuery(map); | |||||
assertThat(query.keys()).containsOnly("org.codehaus.sonar"); | |||||
assertThat(query.names()).containsOnly("Sonar"); | |||||
assertThat(query.qualifiers()).containsOnly("TRK"); | |||||
assertThat(query.pageSize()).isEqualTo(10); | |||||
assertThat(query.pageIndex()).isEqualTo(50); | |||||
assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME); | |||||
assertThat(query.asc()).isTrue(); | |||||
} | |||||
@Test | |||||
public void should_create_query_with_default_paging_from_parameters() { | |||||
Map<String, Object> map = newHashMap(); | |||||
map.put("keys", newArrayList("org.codehaus.sonar")); | |||||
map.put("names", newArrayList("Sonar")); | |||||
map.put("qualifiers", newArrayList("TRK")); | |||||
ComponentQuery query = DefaultRubyComponentService.toQuery(map); | |||||
assertThat(query.keys()).containsOnly("org.codehaus.sonar"); | |||||
assertThat(query.names()).containsOnly("Sonar"); | |||||
assertThat(query.qualifiers()).containsOnly("TRK"); | |||||
assertThat(query.pageSize()).isEqualTo(100); | |||||
assertThat(query.pageIndex()).isEqualTo(1); | |||||
assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME); | |||||
assertThat(query.asc()).isTrue(); | |||||
} | |||||
} | } |
*/ | */ | ||||
package org.sonar.db.component; | package org.sonar.db.component; | ||||
import java.util.Collection; | |||||
import java.util.Collections; | |||||
import java.util.List; | import java.util.List; | ||||
import javax.annotation.CheckForNull; | import javax.annotation.CheckForNull; | ||||
import org.apache.ibatis.session.SqlSession; | import org.apache.ibatis.session.SqlSession; | ||||
return null; | return null; | ||||
} | } | ||||
/** | |||||
* Return provisioned projects = enabled projects without snapshot | |||||
*/ | |||||
public List<ResourceDto> selectProvisionedProjects(Collection<String> qualifiers) { | |||||
if (qualifiers.isEmpty()) { | |||||
return Collections.emptyList(); | |||||
} | |||||
SqlSession session = myBatis().openSession(false); | |||||
try { | |||||
return session.getMapper(ResourceMapper.class).selectProvisionedProjects(qualifiers); | |||||
} finally { | |||||
MyBatis.closeQuietly(session); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Return provisioned project with given key | * Return provisioned project with given key | ||||
*/ | */ |
*/ | */ | ||||
package org.sonar.db.component; | package org.sonar.db.component; | ||||
import java.util.Collection; | |||||
import java.util.List; | import java.util.List; | ||||
import org.apache.ibatis.annotations.Param; | import org.apache.ibatis.annotations.Param; | ||||
List<ResourceDto> selectResources(ResourceQuery query); | List<ResourceDto> selectResources(ResourceQuery query); | ||||
List<ResourceDto> selectProvisionedProjects(@Param("qualifiers") Collection<String> qualifier); | |||||
ResourceDto selectProvisionedProject(@Param("key") String key); | ResourceDto selectProvisionedProject(@Param("key") String key); | ||||
void updateAuthorizationDate(@Param("projectId") Long projectId, @Param("authorizationDate") Long authorizationDate); | void updateAuthorizationDate(@Param("projectId") Long projectId, @Param("authorizationDate") Long authorizationDate); |
<mapper namespace="org.sonar.db.component.ResourceMapper"> | <mapper namespace="org.sonar.db.component.ResourceMapper"> | ||||
<resultMap id="snapshotResultMap" type="Snapshot"> | |||||
<id property="id" column="id"/> | |||||
<result property="createdAt" column="created_at"/> | |||||
<result property="buildDate" column="build_date"/> | |||||
<result property="componentUuid" column="component_uuid"/> | |||||
<result property="status" column="status"/> | |||||
<result property="purgeStatus" column="purge_status"/> | |||||
<result property="last" column="islast"/> | |||||
<result property="version" column="version"/> | |||||
<result property="period1Mode" column="period1_mode"/> | |||||
<result property="period2Mode" column="period2_mode"/> | |||||
<result property="period3Mode" column="period3_mode"/> | |||||
<result property="period4Mode" column="period4_mode"/> | |||||
<result property="period5Mode" column="period5_mode"/> | |||||
<result property="period1Param" column="period1_param"/> | |||||
<result property="period2Param" column="period2_param"/> | |||||
<result property="period3Param" column="period3_param"/> | |||||
<result property="period4Param" column="period4_param"/> | |||||
<result property="period5Param" column="period5_param"/> | |||||
<result property="period1Date" column="period1_date"/> | |||||
<result property="period2Date" column="period2_date"/> | |||||
<result property="period3Date" column="period3_date"/> | |||||
<result property="period4Date" column="period4_date"/> | |||||
<result property="period5Date" column="period5_date"/> | |||||
</resultMap> | |||||
<resultMap id="resourceResultMap" type="Resource"> | <resultMap id="resourceResultMap" type="Resource"> | ||||
<id property="id" column="id"/> | <id property="id" column="id"/> | ||||
<result property="key" column="kee"/> | <result property="key" column="kee"/> | ||||
where p.uuid=#{uuid} | where p.uuid=#{uuid} | ||||
</select> | </select> | ||||
<select id="selectProvisionedProjects" parameterType="map" resultMap="resourceResultMap"> | |||||
select p.* from projects p | |||||
left join snapshots s on s.component_uuid=p.uuid | |||||
<where> | |||||
and s.id is null | |||||
<if test="qualifiers != null and qualifiers.size() > 0"> | |||||
and | |||||
<foreach item="qualifier" index="index" collection="qualifiers" open="(" separator=" or " close=")"> | |||||
p.qualifier=#{qualifier} | |||||
</foreach> | |||||
</if> | |||||
and p.copy_component_uuid is null | |||||
</where> | |||||
</select> | |||||
<select id="selectProvisionedProject" parameterType="string" resultMap="resourceResultMap"> | <select id="selectProvisionedProject" parameterType="string" resultMap="resourceResultMap"> | ||||
select p.* from projects p | select p.* from projects p | ||||
left join snapshots s on s.component_uuid=p.uuid | left join snapshots s on s.component_uuid=p.uuid |
*/ | */ | ||||
package org.sonar.db.component; | package org.sonar.db.component; | ||||
import com.google.common.collect.Iterables; | |||||
import java.util.Collections; | |||||
import java.util.List; | |||||
import org.junit.Rule; | import org.junit.Rule; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.sonar.api.component.Component; | |||||
import org.sonar.api.utils.System2; | import org.sonar.api.utils.System2; | ||||
import org.sonar.db.DbTester; | import org.sonar.db.DbTester; | ||||
import static com.google.common.collect.Lists.newArrayList; | |||||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
import static org.mockito.Mockito.when; | import static org.mockito.Mockito.when; | ||||
public class ResourceDaoTest { | public class ResourceDaoTest { | ||||
static System2 system = mock(System2.class); | |||||
private static System2 system = mock(System2.class); | |||||
@Rule | @Rule | ||||
public DbTester dbTester = DbTester.create(system); | public DbTester dbTester = DbTester.create(system); | ||||
ResourceDao underTest = dbTester.getDbClient().resourceDao(); | |||||
private ResourceDao underTest = dbTester.getDbClient().resourceDao(); | |||||
@Test | @Test | ||||
public void get_resource_by_uuid() { | public void get_resource_by_uuid() { | ||||
assertThat(underTest.getRootProjectByComponentKey("unknown")).isNull(); | assertThat(underTest.getRootProjectByComponentKey("unknown")).isNull(); | ||||
} | } | ||||
@Test | |||||
public void should_select_provisioned_projects_by_qualifiers() { | |||||
dbTester.prepareDbUnit(getClass(), "fixture-including-ghost-projects-and-technical-project.xml"); | |||||
List<ResourceDto> components = underTest.selectProvisionedProjects(newArrayList("TRK")); | |||||
assertThat(components).hasSize(1); | |||||
assertThat(components.get(0).getKey()).isEqualTo("org.sample:sample"); | |||||
assertThat(underTest.selectProvisionedProjects(newArrayList("unknown"))).isEmpty(); | |||||
assertThat(underTest.selectProvisionedProjects(Collections.<String>emptyList())).isEmpty(); | |||||
} | |||||
@Test | @Test | ||||
public void update_authorization_date() { | public void update_authorization_date() { | ||||
dbTester.prepareDbUnit(getClass(), "update_authorization_date.xml"); | dbTester.prepareDbUnit(getClass(), "update_authorization_date.xml"); | ||||
dbTester.assertDbUnit(getClass(), "update_authorization_date-result.xml", "projects"); | dbTester.assertDbUnit(getClass(), "update_authorization_date-result.xml", "projects"); | ||||
} | } | ||||
private List<String> getKeys(final List<Component> components) { | |||||
return newArrayList(Iterables.transform(components, Component::key)); | |||||
} | |||||
} | } |