From cc113fb821ec49d6380bba42b01d2c0dfdc16b4b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 15 May 2014 16:41:00 +0200 Subject: [PATCH] SONAR-5305 Return component favourite information --- .../sonar/core/properties/PropertiesDao.java | 11 +++ .../core/properties/PropertiesMapper.java | 2 + .../sonar/core/properties/PropertyQuery.java | 76 +++++++++++++++++++ .../core/properties/PropertiesMapper.xml | 16 ++++ .../core/properties/PropertiesDaoTest.java | 13 ++++ .../PropertiesDaoTest/select_by_query.xml | 19 +++++ .../component/ws/ComponentAppAction.java | 29 ++++++- .../component/ws/ComponentAppActionTest.java | 11 ++- .../server/component/ws/ComponentsWsTest.java | 3 +- .../ws/ComponentAppActionTest/app.json | 1 + 10 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index 521077c4bf1..5ac44bf1c4e 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -29,6 +29,7 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import javax.annotation.Nullable; + import java.util.List; import java.util.Map; @@ -110,6 +111,16 @@ public class PropertiesDao implements BatchComponent, ServerComponent { } } + public List selectByQuery(PropertyQuery query) { + SqlSession session = mybatis.openSession(false); + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + try { + return mapper.selectByQuery(query); + } finally { + MyBatis.closeQuietly(session); + } + } + public void setProperty(PropertyDto property, SqlSession session) { PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); PropertyDto persistedProperty = mapper.selectByKey(property); diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java index 8dff3f887de..6cdf28160af 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java @@ -39,6 +39,8 @@ public interface PropertiesMapper { PropertyDto selectByKey(PropertyDto key); + List selectByQuery(@Param("query") PropertyQuery query); + void update(PropertyDto property); void insert(PropertyDto property); diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java new file mode 100644 index 00000000000..da489e95bf4 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertyQuery.java @@ -0,0 +1,76 @@ +/* + * 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. + */ + +package org.sonar.core.properties; + +public class PropertyQuery { + + private final String key; + private final Long componentId; + private final Integer userId; + + private PropertyQuery(Builder builder) { + this.key = builder.key; + this.componentId = builder.componentId; + this.userId = builder.userId; + } + + public String key() { + return key; + } + + public Long componentId() { + return componentId; + } + + public Integer userId() { + return userId; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String key; + private Long componentId; + private Integer userId; + + public Builder setKey(String key) { + this.key = key; + return this; + } + + public Builder setComponentId(Long componentId) { + this.componentId = componentId; + return this; + } + + public Builder setUserId(Integer userId) { + this.userId = userId; + return this; + } + + public PropertyQuery build() { + return new PropertyQuery(this); + } + } + +} diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml index eff90628032..30f14b06672 100644 --- a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml @@ -64,6 +64,22 @@ + + update properties set text_value = #{value} where id = #{id} diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java index 974bf4da7d8..9e902a97357 100644 --- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java @@ -142,6 +142,19 @@ public class PropertiesDaoTest extends AbstractDaoTestCase { assertThat(property.getValue(), is("two")); } + @Test + public void select_by_query() { + setupData("select_by_query"); + + List results = dao.selectByQuery(PropertyQuery.builder().setKey("user.two").setComponentId(10L).setUserId(100).build()); + assertThat(results).hasSize(1); + assertThat(results.get(0).getValue()).isEqualTo("two"); + + results = dao.selectByQuery(PropertyQuery.builder().setKey("user.one").setUserId(100).build()); + assertThat(results).hasSize(1); + assertThat(results.get(0).getValue()).isEqualTo("one"); + } + @Test public void setProperty_update() { setupData("update"); diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml new file mode 100644 index 00000000000..2873d69a5bc --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/select_by_query.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java b/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java index e7832443a90..538fd564b60 100644 --- a/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java +++ b/sonar-server/src/main/java/org/sonar/server/component/ws/ComponentAppAction.java @@ -29,6 +29,9 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; import org.sonar.api.web.UserRole; import org.sonar.core.component.ComponentDto; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; +import org.sonar.core.properties.PropertyQuery; import org.sonar.core.resource.ResourceDao; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; @@ -36,14 +39,18 @@ import org.sonar.server.user.UserSession; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import java.util.List; + public class ComponentAppAction implements RequestHandler { private static final String KEY = "key"; private final ResourceDao resourceDao; + private final PropertiesDao propertiesDao; - public ComponentAppAction(ResourceDao resourceDao) { + public ComponentAppAction(ResourceDao resourceDao, PropertiesDao propertiesDao) { this.resourceDao = resourceDao; + this.propertiesDao = propertiesDao; } void define(WebService.NewController controller) { @@ -64,7 +71,8 @@ public class ComponentAppAction implements RequestHandler { @Override public void handle(Request request, Response response) { String fileKey = request.mandatoryParam(KEY); - UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey); + UserSession userSession = UserSession.get(); + userSession.checkComponentPermission(UserRole.CODEVIEWER, fileKey); JsonWriter json = response.newJsonWriter(); json.beginObject(); @@ -74,6 +82,13 @@ public class ComponentAppAction implements RequestHandler { throw new NotFoundException(String.format("Component '%s' does not exists.", fileKey)); } + List propertyDtos = propertiesDao.selectByQuery(PropertyQuery.builder() + .setKey("favourite") + .setComponentId(component.getId()) + .setUserId(userSession.userId()) + .build()); + boolean isFavourite = propertyDtos.size() == 1; + json.prop("key", component.key()); json.prop("path", component.path()); json.prop("name", component.name()); @@ -85,6 +100,8 @@ public class ComponentAppAction implements RequestHandler { Component project = componentById(component.projectId()); json.prop("projectName", project != null ? project.longName() : null); + json.prop("fav", isFavourite); + json.endObject(); json.close(); } @@ -97,4 +114,12 @@ public class ComponentAppAction implements RequestHandler { return null; } +// private Map findDataFromComponent(String fileKey, String metricKey) { +// MeasureDataDto data = measuresDao.findByComponentKeyAndMetricKey(fileKey, metricKey); +// if (data != null) { +// return KeyValueFormat.parseIntInt(data.getData()); +// } +// return Maps.newHashMap(); +// } + } diff --git a/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java b/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java index d2a3b02b460..a333bf7f621 100644 --- a/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentAppActionTest.java @@ -27,10 +27,15 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.web.UserRole; import org.sonar.core.component.ComponentDto; +import org.sonar.core.properties.PropertiesDao; +import org.sonar.core.properties.PropertyDto; +import org.sonar.core.properties.PropertyQuery; import org.sonar.core.resource.ResourceDao; import org.sonar.server.user.MockUserSession; import org.sonar.server.ws.WsTester; +import static com.google.common.collect.Lists.newArrayList; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -39,11 +44,14 @@ public class ComponentAppActionTest { @Mock ResourceDao resourceDao; + @Mock + PropertiesDao propertiesDao; + WsTester tester; @Before public void setUp() throws Exception { - tester = new WsTester(new ComponentsWs(new ComponentAppAction(resourceDao))); + tester = new WsTester(new ComponentsWs(new ComponentAppAction(resourceDao, propertiesDao))); } @Test @@ -57,6 +65,7 @@ public class ComponentAppActionTest { when(resourceDao.selectComponentByKey(componentKey)).thenReturn(file); when(resourceDao.findById(5L)).thenReturn(new ComponentDto().setId(5L).setLongName("SonarQube :: Plugin API")); when(resourceDao.findById(1L)).thenReturn(new ComponentDto().setId(1L).setLongName("SonarQube")); + when(propertiesDao.selectByQuery(any(PropertyQuery.class))).thenReturn(newArrayList(new PropertyDto())); WsTester.TestRequest request = tester.newGetRequest("api/components", "app").setParam("key", componentKey); request.execute().assertJson(getClass(), "app.json"); diff --git a/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentsWsTest.java b/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentsWsTest.java index 25c817d3ca7..2b2c548ae2a 100644 --- a/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentsWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/component/ws/ComponentsWsTest.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Test; import org.sonar.api.server.ws.RailsHandler; import org.sonar.api.server.ws.WebService; +import org.sonar.core.properties.PropertiesDao; import org.sonar.core.resource.ResourceDao; import org.sonar.server.ws.WsTester; @@ -36,7 +37,7 @@ public class ComponentsWsTest { @Before public void setUp() throws Exception { - WsTester tester = new WsTester(new ComponentsWs(new ComponentAppAction(mock(ResourceDao.class)))); + WsTester tester = new WsTester(new ComponentsWs(new ComponentAppAction(mock(ResourceDao.class), mock(PropertiesDao.class)))); controller = tester.controller("api/components"); } diff --git a/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app.json b/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app.json index 28f3d67474e..ff887f9a770 100644 --- a/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app.json +++ b/sonar-server/src/test/resources/org/sonar/server/component/ws/ComponentAppActionTest/app.json @@ -5,4 +5,5 @@ "q": "FIL", "subProjectName": "SonarQube :: Plugin API", "projectName": "SonarQube", + "fav": true } -- 2.39.5