package org.slc.sli.validation;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.slc.sli.domain.Entity;

/**
 * JUnit for validating section
 * 
 * @author nbrown
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext-test.xml" })
public class SectionValidationTest {

    @Autowired
    private EntityValidator validator;
    
    @Autowired
    private DummyEntityRepository repo;
    
    @Before
    public void init() {
        repo.addEntity("school", "42", ValidationTestUtils.makeDummyEntity("school", "42"));
        repo.addEntity("session", "MySessionId", ValidationTestUtils.makeDummyEntity("session", "MySessionId"));
        repo.addEntity("course", "MyCourseId", ValidationTestUtils.makeDummyEntity("course", "MyCourseId"));
    }

    private Entity goodSection() {
        final Map<String, Object> goodSection = new HashMap<String, Object>();
        goodSection.put("uniqueSectionCode", "Math101");
        goodSection.put("sequenceOfCourse", 1);
        goodSection.put("educationalEnvironment", "Classroom");
        goodSection.put("mediumOfInstruction", "Face-to-face instruction");
        goodSection.put("populationServed", "Regular Students");
        Map<String, Object> credit = new HashMap<String, Object>();
        credit.put("credit", 1.5);
        credit.put("creditType", "Semester hour credit");
        credit.put("creditConversion", 2.5);
        goodSection.put("availableCredit", credit);
        goodSection.put("schoolId", "42");
        goodSection.put("sessionId", "MySessionId");
        goodSection.put("courseId", "MyCourseId");
        return new Entity() {
            
            @Override
            public String getType() {
                return "section";
            }
            
            @Override
            public String getEntityId() {
                return "id";
            }
            
            @Override
            public Map<String, Object> getBody() {
                return goodSection;
            }

            @Override
            public Map<String, Object> getMetaData() {
                return new HashMap<String, Object>();
            }
        };
    }
    
    @Test
    public void testSectionValidation() {
        Entity goodSection = goodSection();
        assertTrue(validator.validate(goodSection));
    }
    
    @Test
    public void testMinimumSection() {
        Entity minSection = goodSection();
        minSection.getBody().remove("educationalEnvironment");
        minSection.getBody().remove("mediumOfInstruction");
        minSection.getBody().remove("populationServed");
        minSection.getBody().remove("availableCredit");
        assertTrue(validator.validate(minSection));
    }
    
    @Test
    public void testMissingRequiredFields() {
        Entity missingSectionCode = goodSection();
        missingSectionCode.getBody().remove("uniqueSectionCode");
        try {
            assertFalse(validator.validate(missingSectionCode));
        } catch (EntityValidationException e) {
            List<ValidationError> errors = e.getValidationErrors();
            ValidationError error = errors.get(0);
            assertEquals(ValidationError.ErrorType.REQUIRED_FIELD_MISSING, error.getType());
        }
        
    }
}

@@ -161,24 +161,53 @@
 ertTrue;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0A
 import java.
@@ -221,16 +221,16 @@
 shMap;%0D%0A
-
 import j
@@ -822,18 +822,22 @@
 Test %7B%0D%0A
+    
 %0D%0A
-
     @Aut
@@ -1310,25 +1310,245 @@
 Id%22));%0D%0A
+        repo.addEntity(%22program%22, %22program1%22, ValidationTestUtils.makeDummyEntity(%22program%22, %22program1%22));%0D%0A        repo.addEntity(%22program%22, %22program2%22, ValidationTestUtils.makeDummyEntity(%22program%22, %22program2%22));%0D%0A
     %7D%0D%0A
+    
 %0D%0A
-
     priv
@@ -2379,24 +2379,218 @@
 ourseId%22);%0D%0A
+        List%3CString%3E programs = new ArrayList%3CString%3E();%0D%0A        programs.add(%22program1%22);%0D%0A        programs.add(%22program2%22);%0D%0A        goodSection.put(%22programReference%22, programs);%0D%0A        %0D%0A
         retu
@@ -3000,16 +3000,28 @@
      %7D%0D%0A
+            
 %0D%0A      

test for lists of direct references