package org.datacite.mds.validation.constraints;

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

import java.util.HashSet;
import java.util.Set;

import org.datacite.mds.domain.Datacentre;
import org.datacite.mds.domain.Dataset;
import org.datacite.mds.domain.Prefix;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/META-INF/spring/applicationContext.xml")
public class MatchDoiPrefixTest extends AbstractContraintsTest {

    Dataset dataset;
    
    @Value("${handle.testPrefix}")
    String testPrefix;

    @Before
    public void init() {
        // create simple datacentre with two allowed prefixes
        Datacentre datacentre = new Datacentre();
        Set<Prefix> prefixes = new HashSet<Prefix>();
        Prefix prefix = new Prefix();
        prefix.setPrefix("10.4711");
        prefixes.add(prefix);
        datacentre.setPrefixes(prefixes);

        dataset = new Dataset();
        dataset.setDatacentre(datacentre);
    }

    @Test
    public void test() {
        assertTrue(isValid(null));
        assertTrue(isValid("10.4711/test"));
        assertTrue(isValid(testPrefix + "/test"));
        assertFalse(isValid("10.1234/test"));
        
        dataset.setDatacentre(null);
        assertTrue(isValid("foobar"));
    }

    boolean isValid(String doi) {
        dataset.setDoi(doi);
        return super.isValidAnnotation(dataset, MatchDoiPrefix.class);
    }
}

@@ -302,16 +302,56 @@
 Prefix;%0A
+import org.datacite.mds.test.TestUtils;%0A
 import o
@@ -914,70 +914,8 @@
 ) %7B%0A
-        // create simple datacentre with two allowed prefixes%0A
     
@@ -972,201 +972,68 @@
     
-Set%3CPrefix%3E prefixes = new HashS
+datacentre.s
 et
-%3C
 Prefix
-%3E();%0A        Prefix prefix = new Prefix();%0A        prefix.setPrefix(%2210.4711%22);%0A        prefixes.add(prefix);%0A        datacentre.setPrefixes(prefixes);%0A
+es(TestUtils.createPrefixes(%2210.4711%22));
 %0A   

simplify test