package org.datacite.mds.domain;

import static org.junit.Assert.*;

import org.datacite.mds.test.TestUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/META-INF/spring/applicationContext.xml")
@Transactional 
public class DatacentreTest {
            
    @Test
    @Rollback
    public void testIncQuotaUsed() {
        Datacentre datacentre;
        Integer expectedQuota;

        Allocator allocator = TestUtils.createAllocator("AL");
        allocator.persist();
        datacentre = TestUtils.createDatacentre("AL.DC", allocator);
        datacentre.setDoiQuotaUsed(42);
        expectedQuota = 43;
        datacentre.persist();
        
        datacentre.incQuotaUsed(Datacentre.ForceRefresh.YES);
        
        assertEquals(expectedQuota, datacentre.getDoiQuotaUsed());

        Datacentre datacentre2 = Datacentre.findDatacentre(datacentre.getId());
        assertEquals(expectedQuota, datacentre2.getDoiQuotaUsed());
    }

    @Test
    @Rollback
    public void testIsQuotaExceeded() {
        Datacentre datacentre;

        Allocator allocator = TestUtils.createAllocator("AL");
        allocator.persist();
        datacentre = TestUtils.createDatacentre("AL.DC", allocator);
        datacentre.setDoiQuotaUsed(42);
        datacentre.setDoiQuotaAllowed(43);
        datacentre.persist();
        
        assertFalse(datacentre.isQuotaExceeded());
        
        datacentre.incQuotaUsed(Datacentre.ForceRefresh.YES);

        assertTrue(datacentre.isQuotaExceeded());
        
        datacentre.setDoiQuotaAllowed(-1);
        datacentre.persist();

        assertFalse(datacentre.isQuotaExceeded());
    }

}

@@ -669,202 +669,84 @@
 ntre
-;%0A        Integer expectedQuota;%0A%0A        Allocator allocator = TestUtils.createAllocator(%22AL%22);%0A        allocator.persist();%0A        datacentre = TestUtils.createDatacentre(%22AL.DC%22, allocator);
+ = TestUtils.createDefaultDatacentre();%0A        Integer expectedQuota = 43;%0A
 %0A   
@@ -786,36 +786,8 @@
 2);%0A
-        expectedQuota = 43;%0A
     
@@ -1212,169 +1212,45 @@
 ntre
-;%0A%0A        Allocator allocator = TestUtils.createAllocator(%22AL%22);%0A        allocator.persist();%0A        datacentre = TestUtils.createDatacentre(%22AL.DC%22, allocator
+ = TestUtils.createDefaultDatacentre(
 );%0A 

simplify test