package org.datacite.mds.validation.constraints;

// TODO test for parameter hasToExist

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

import org.apache.commons.lang.ArrayUtils;
import org.junit.Test;

public class SymbolTest extends AbstractContraintsTest {

    String[] allowAllocator = { "AB", "ABCDEFGH", "AB-CD" };
    String[] allowDatacentre = { "ABC.DE", "ABCDEF.AAA", "AB.CD-EF", "AB-CD.EF-GH" };
    String[] disallow = { "A", "ABCDEFGHI", "AB.C", "AB.CDEFHIJKL", "abcde", "abc.def", "AB-", "-AB", "A--B", "AB.-C",
            "AB.C-", "AB.C--D" };

    @Symbol(Symbol.Type.ALLOCATOR)
    String symbolAllocator;
    
    @Test
    public void testAllocator() {
        symbolAllocator = null;
        assertTrue(getValidationHelper().isValid(this, "symbolAllocator"));
        for (String symbol : allowAllocator) {
            symbolAllocator = symbol;
            assertTrue(getValidationHelper().isValid(this, "symbolAllocator"));
        }
        for (String symbol : (String[]) ArrayUtils.addAll(allowDatacentre, disallow)) {
            symbolAllocator = symbol;
            assertFalse(getValidationHelper().isValid(this, "symbolAllocator"));
        }
    }

    @Symbol(Symbol.Type.DATACENTRE)
    String symbolDatacentre;

    @Test
    public void testDatacentre() {
        symbolDatacentre = null;
        assertTrue(getValidationHelper().isValid(this, "symbolDatacentre"));
        for (String symbol : allowDatacentre) {
            symbolDatacentre = symbol;
            assertTrue(getValidationHelper().isValid(this, "symbolDatacentre"));
        }
        for (String symbol : (String[]) ArrayUtils.addAll(allowAllocator, disallow)) {
            symbolDatacentre = symbol;
            assertFalse(getValidationHelper().isValid(this, "symbolDatacentre"));
        }
    }

    @Symbol( { Symbol.Type.ALLOCATOR, Symbol.Type.DATACENTRE })
    String symbolBoth;

    @Test
    public void testBoth() {
        symbolBoth = null;
        assertTrue(getValidationHelper().isValid(this, "symbolBoth"));
        for (String symbol : (String[]) ArrayUtils.addAll(allowDatacentre, allowAllocator)) {
            symbolBoth = symbol;
            assertTrue(getValidationHelper().isValid(this, "symbolBoth"));
        }
        for (String symbol : disallow) {
            symbolBoth = symbol;
            assertFalse(getValidationHelper().isValid(this, "symbolBoth"));
        }
    }
}

@@ -47,47 +47,8 @@
 s;%0A%0A
-// TODO test for parameter hasToExist%0A%0A
 impo
@@ -189,20 +189,579 @@
 org.
-junit.Test;%0A
+datacite.mds.domain.Allocator;%0Aimport org.datacite.mds.domain.AllocatorOrDatacentre;%0Aimport org.datacite.mds.util.DomainUtils;%0Aimport org.easymock.EasyMock;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.powermock.api.easymock.PowerMock;%0Aimport org.powermock.core.classloader.annotations.PowerMockIgnore;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunner;%0A%0A@RunWith(PowerMockRunner.class)%0A@PowerMockIgnore( %7B %22javax.*%22, %22org.apache.log4j.*%22 %7D)%0A@PrepareForTest( %7B DomainUtils.class %7D)
 %0Apub
@@ -1179,20 +1179,16 @@
 ocator;%0A
-    
 %0A    @Te
@@ -2957,11 +2957,881 @@
  %7D%0A    %7D
+%0A%0A    @Symbol(value = Symbol.Type.ALLOCATOR, hasToExist = true)%0A    String existingSymbol = %22DUMMY%22;%0A%0A    @Test%0A    public void testExistingSymbol() %7B%0A        mockFindAllocatorOrDatacentre(new Allocator());%0A        assertTrue(isValidExistingSymbol());%0A        PowerMock.verifyAll();%0A    %7D%0A%0A    @Test%0A    public void testNonExistingSymbol() %7B%0A        mockFindAllocatorOrDatacentre(null);%0A        assertFalse(isValidExistingSymbol());%0A        PowerMock.verifyAll();%0A    %7D%0A%0A    boolean isValidExistingSymbol() %7B%0A        return getValidationHelper().isValid(this, %22existingSymbol%22);%0A    %7D%0A%0A    void mockFindAllocatorOrDatacentre(AllocatorOrDatacentre mockReturn) %7B%0A        PowerMock.mockStatic(DomainUtils.class);%0A        EasyMock.expect(DomainUtils.findAllocatorOrDatacentreBySymbol(existingSymbol)).andReturn(mockReturn);%0A        PowerMock.replay(DomainUtils.class);%0A    %7D
 %0A%7D%0A

add test for has-to-exist symbol