@@ -1,191 +0,0 @@ | |||
/* | |||
* 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); | |||
} | |||
} |
@@ -19,9 +19,6 @@ | |||
*/ | |||
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.Nullable; | |||
import org.sonar.api.component.Component; | |||
@@ -30,29 +27,24 @@ import org.sonar.api.resources.Qualifiers; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
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.organization.DefaultOrganizationProvider; | |||
import org.sonar.server.permission.PermissionTemplateService; | |||
import org.sonar.server.util.RubyUtils; | |||
import static org.sonar.server.component.NewComponent.newComponentBuilder; | |||
public class DefaultRubyComponentService implements RubyComponentService { | |||
private final DbClient dbClient; | |||
private final ResourceDao resourceDao; | |||
private final ComponentService componentService; | |||
private final PermissionTemplateService permissionTemplateService; | |||
private final FavoriteUpdater favoriteUpdater; | |||
private final DefaultOrganizationProvider defaultOrganizationProvider; | |||
public DefaultRubyComponentService(DbClient dbClient, ResourceDao resourceDao, ComponentService componentService, | |||
public DefaultRubyComponentService(DbClient dbClient, ComponentService componentService, | |||
PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater, | |||
DefaultOrganizationProvider defaultOrganizationProvider) { | |||
this.dbClient = dbClient; | |||
this.resourceDao = resourceDao; | |||
this.componentService = componentService; | |||
this.permissionTemplateService = permissionTemplateService; | |||
this.favoriteUpdater = favoriteUpdater; | |||
@@ -102,25 +94,4 @@ public class DefaultRubyComponentService implements RubyComponentService { | |||
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(); | |||
} | |||
} |
@@ -1,86 +0,0 @@ | |||
/* | |||
* 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); | |||
} | |||
} |
@@ -19,8 +19,6 @@ | |||
*/ | |||
package org.sonar.server.component; | |||
import java.util.List; | |||
import java.util.Map; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
@@ -30,10 +28,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.component.ComponentDbTester; | |||
import org.sonar.db.component.ComponentDto; | |||
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.ComponentIndexer; | |||
import org.sonar.server.es.EsTester; | |||
@@ -47,8 +43,6 @@ import org.sonar.server.organization.TestDefaultOrganizationProvider; | |||
import org.sonar.server.permission.PermissionTemplateService; | |||
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.mockito.Matchers.any; | |||
import static org.mockito.Matchers.eq; | |||
@@ -74,16 +68,13 @@ public class DefaultRubyComponentServiceTest { | |||
private DbClient dbClient = db.getDbClient(); | |||
private DbSession dbSession = db.getSession(); | |||
private ResourceDao resourceDao = dbClient.resourceDao(); | |||
private ComponentService componentService = new ComponentService(dbClient, i18n, userSession, system2, new ComponentFinder(dbClient), | |||
new ProjectMeasuresIndexer(system2, dbClient, es.client()), new ComponentIndexer(dbClient, es.client())); | |||
private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class); | |||
private FavoriteUpdater favoriteUpdater = mock(FavoriteUpdater.class); | |||
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); | |||
private String defaultOrganizationUuid; | |||
@@ -118,53 +109,4 @@ public class DefaultRubyComponentServiceTest { | |||
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(); | |||
} | |||
} |
@@ -19,8 +19,6 @@ | |||
*/ | |||
package org.sonar.db.component; | |||
import java.util.Collection; | |||
import java.util.Collections; | |||
import java.util.List; | |||
import javax.annotation.CheckForNull; | |||
import org.apache.ibatis.session.SqlSession; | |||
@@ -112,21 +110,6 @@ public class ResourceDao extends AbstractDao { | |||
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 | |||
*/ |
@@ -19,7 +19,6 @@ | |||
*/ | |||
package org.sonar.db.component; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import org.apache.ibatis.annotations.Param; | |||
@@ -28,8 +27,6 @@ public interface ResourceMapper { | |||
List<ResourceDto> selectResources(ResourceQuery query); | |||
List<ResourceDto> selectProvisionedProjects(@Param("qualifiers") Collection<String> qualifier); | |||
ResourceDto selectProvisionedProject(@Param("key") String key); | |||
void updateAuthorizationDate(@Param("projectId") Long projectId, @Param("authorizationDate") Long authorizationDate); |
@@ -3,32 +3,6 @@ | |||
<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"> | |||
<id property="id" column="id"/> | |||
<result property="key" column="kee"/> | |||
@@ -93,21 +67,6 @@ | |||
where p.uuid=#{uuid} | |||
</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 p.* from projects p | |||
left join snapshots s on s.component_uuid=p.uuid |
@@ -19,28 +19,23 @@ | |||
*/ | |||
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.Test; | |||
import org.sonar.api.component.Component; | |||
import org.sonar.api.utils.System2; | |||
import org.sonar.db.DbTester; | |||
import static com.google.common.collect.Lists.newArrayList; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Mockito.mock; | |||
import static org.mockito.Mockito.when; | |||
public class ResourceDaoTest { | |||
static System2 system = mock(System2.class); | |||
private static System2 system = mock(System2.class); | |||
@Rule | |||
public DbTester dbTester = DbTester.create(system); | |||
ResourceDao underTest = dbTester.getDbClient().resourceDao(); | |||
private ResourceDao underTest = dbTester.getDbClient().resourceDao(); | |||
@Test | |||
public void get_resource_by_uuid() { | |||
@@ -73,18 +68,6 @@ public class ResourceDaoTest { | |||
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 | |||
public void update_authorization_date() { | |||
dbTester.prepareDbUnit(getClass(), "update_authorization_date.xml"); | |||
@@ -95,8 +78,4 @@ public class ResourceDaoTest { | |||
dbTester.assertDbUnit(getClass(), "update_authorization_date-result.xml", "projects"); | |||
} | |||
private List<String> getKeys(final List<Component> components) { | |||
return newArrayList(Iterables.transform(components, Component::key)); | |||
} | |||
} |