package org.navalplanner.business.test.resources.services;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

import java.util.UUID;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.resources.entities.CriterionType;
import org.navalplanner.business.resources.services.CriterionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;

/**
 * Test cases for {@link CriterionTypeService} <br />
 * @author Diego Pino García <dpino@igalia.com>
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
        BUSINESS_SPRING_CONFIG_TEST_FILE })
@Transactional
public class CriterionTypeServiceTest {

    @Autowired
    private CriterionTypeService criterionTypeService;

    public CriterionType createValidCriterionType() {
        String unique = UUID.randomUUID().toString();
        return createValidCriterionType(unique);
    }

    public CriterionType createValidCriterionType(String name) {
        return new CriterionType(name);
    }

    @Test
    public void testSaveCriterionType() throws ValidationException {
        CriterionType criterionType = createValidCriterionType();
        criterionTypeService.save(criterionType);
        assertTrue(criterionTypeService.exists(criterionType));
    }

    @Test
    public void testSaveCriterionTypeTwice() throws ValidationException {
        CriterionType criterionType = createValidCriterionType();
        criterionTypeService.save(criterionType);
        criterionTypeService.save(criterionType);
        assertTrue(criterionTypeService.exists(criterionType));
    }

    @Test(expected=ConstraintViolationException.class)
    public void testCannotSaveTwoDifferentCriterionTypesWithTheSameName() throws ValidationException {
        String unique = UUID.randomUUID().toString();
        CriterionType criterionType = createValidCriterionType(unique);
        criterionTypeService.save(criterionType);
        criterionType = createValidCriterionType(unique);
        criterionTypeService.save(criterionType);
    }

    @Test
    public void testGetAll() throws ValidationException {
        int previous = criterionTypeService.getAll().size();
        CriterionType criterionType = createValidCriterionType();
        criterionTypeService.save(criterionType);
        int now = criterionTypeService.getAll().size();
        assertEquals(now, previous + 1);
    }
}

@@ -558,16 +558,80 @@
 owired;%0A
+import org.springframework.dao.DataIntegrityViolationException;%0A
 import o
@@ -680,24 +680,24 @@
 figuration;%0A
-
 import org.s
@@ -1117,16 +1117,64 @@
 ia.com%3E%0A
+ * @author Javier Moran R%C3%BAa %3Cjmoran@igalia.com%3E%0A
  */%0A@Run
@@ -2312,63 +2312,8 @@
  %7D%0A%0A
-    @Test(expected=ConstraintViolationException.class)%0A
     
@@ -2381,16 +2381,28 @@
 meName()
+%0A           
  throws 
@@ -2415,32 +2415,50 @@
 tionException %7B%0A
+        try %7B%0A    
         String u
@@ -2487,32 +2487,36 @@
 D().toString();%0A
+    
         Criterio
@@ -2571,32 +2571,36 @@
 nique);%0A        
+ 
+   
 criterionTypeSer
@@ -2617,32 +2617,36 @@
 criterionType);%0A
+    
         criterio
@@ -2687,32 +2687,36 @@
 nique);%0A        
+   
+ 
 criterionTypeSer
@@ -2737,24 +2737,241 @@
 erionType);%0A
+        %7D catch (ConstraintViolationException c) %7B%0A            // This exception is raised in postgresql%0A        %7D catch (DataIntegrityViolationException d) %7B%0A            // This exception is raised in HSQL%0A        %7D%0A
     %7D%0A%0A    @

Fix test CannotSaveTwoDifferentCriterionTypesWithTheSameName