From 6381abfb0276af35d36f711f8398bda53d3789de Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Tue, 1 Oct 2013 10:17:21 +0200 Subject: [PATCH] Enhance coverage --- .../DefaultRubyComponentServiceTest.java | 50 +++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java b/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java index e563f88fc76..825913351ef 100644 --- a/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java @@ -20,8 +20,9 @@ package org.sonar.server.component; -import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.exceptions.BadRequestException; import org.sonar.core.resource.ResourceDto; import org.mockito.ArgumentCaptor; import org.sonar.api.resources.Qualifiers; @@ -96,14 +97,44 @@ public class DefaultRubyComponentServiceTest { String componentName = "New Project"; String scope = Scopes.PROJECT; String qualifier = Qualifiers.PROJECT; - long componentId = Long.MAX_VALUE; - ComponentDto component = mock(ComponentDto.class); - when(component.getId()).thenReturn(componentId); when(resourceDao.findByKey(componentKey)).thenReturn(null); componentService.createComponent(componentKey, componentName, scope, qualifier); } + @Test(expected = BadRequestException.class) + public void should_throw_if_component_already_exists() { + String componentKey = "new-project"; + String componentName = "New Project"; + String scope = Scopes.PROJECT; + String qualifier = Qualifiers.PROJECT; + when(resourceDao.findByKey(componentKey)).thenReturn(mock(ComponentDto.class)); + + componentService.createComponent(componentKey, componentName, scope, qualifier); + } + + @Test(expected = NotFoundException.class) + public void should_throw_if_updating_unknown_component() { + final long componentId = 1234l; + when(resourceDao.getResource(componentId)).thenReturn(null); + componentService.updateComponent(componentId, "key", "name"); + } + + @Test + public void should_update_component() { + final long componentId = 1234l; + final String newKey = "newKey"; + final String newName = "newName"; + ResourceDto resource = mock(ResourceDto.class); + when(resourceDao.getResource(componentId)).thenReturn(resource); + when(resource.setKey(newKey)).thenReturn(resource); + when(resource.setName(newName)).thenReturn(resource); + componentService.updateComponent(componentId, newKey, newName); + verify(resource).setKey(newKey); + verify(resource).setName(newName); + verify(resourceDao).insertOrUpdate(resource); + } + @Test public void should_find() { List qualifiers = newArrayList("TRK"); @@ -158,6 +189,17 @@ public class DefaultRubyComponentServiceTest { verify(finder).find(any(ComponentQuery.class), anyListOf(Component.class)); } + @Test + public void should_find_provisioned_projects() { + List qualifiers = newArrayList("TRK"); + + Map map = newHashMap(); + map.put("qualifiers", qualifiers); + + componentService.findProvisionedProjects(map); + verify(resourceDao).selectProvisionedProjects(anyListOf(String.class)); + } + @Test public void should_create_query_from_parameters() { Map map = newHashMap(); -- 2.39.5