package org.apereo.cas.authentication.principal;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Collections;

import static org.junit.Assert.*;

/**
 * Handles tests for {@link DefaultPrincipalAttributesRepository}.
 * @author Misagh Moayyed
 * @since 4.1
 */
public class DefaultPrincipalAttributesRepositoryTests {

    private static final File JSON_FILE = new File(FileUtils.getTempDirectoryPath(), "defaultPrincipalAttributesRepository.json");
    private static final ObjectMapper MAPPER = new ObjectMapper();

    private final PrincipalFactory factory = new DefaultPrincipalFactory();

    @Test
    public void checkNoAttributes() {
        final PrincipalAttributesRepository rep = new DefaultPrincipalAttributesRepository();
        assertEquals(rep.getAttributes(this.factory.createPrincipal("uid")).size(), 0);
    }

    @Test
    public void checkInitialAttributes() {
        final Principal p = this.factory.createPrincipal("uid",
                Collections.singletonMap("mail", "final@example.com"));
        final PrincipalAttributesRepository rep = new DefaultPrincipalAttributesRepository();
        assertEquals(rep.getAttributes(p).size(), 1);
        assertTrue(rep.getAttributes(p).containsKey("mail"));
    }

    @Test
    public void verifySerializeADefaultPrincipalAttributesRepositoryToJson() throws IOException {
        final PrincipalAttributesRepository repositoryWritten = new DefaultPrincipalAttributesRepository();

        MAPPER.writeValue(JSON_FILE, repositoryWritten);

        final PrincipalAttributesRepository repositoryRead = MAPPER.readValue(JSON_FILE, DefaultPrincipalAttributesRepository.class);

        assertEquals(repositoryWritten, repositoryRead);
    }
}

@@ -157,16 +157,110 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;%0Aimport org.springframework.test.context.junit4.SpringRunner;
 %0A%0Aimport
@@ -483,16 +483,45 @@
 4.1%0A */%0A
+@RunWith(SpringRunner.class)%0A
 public c
@@ -1203,24 +1203,8 @@
 id%22,
-%0A               
  Col
@@ -1685,25 +1685,24 @@
 pository();%0A
-%0A
         MAPP
@@ -1746,17 +1746,16 @@
 itten);%0A
-%0A
         
@@ -1880,17 +1880,16 @@
 class);%0A
-%0A
         

fix tests