/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.slc.sli.ingestion.smooks.mappings;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;

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;
import org.xml.sax.SAXException;

import org.slc.sli.ingestion.NeutralRecord;
import org.slc.sli.ingestion.util.EntityTestUtils;

/**
 * Smooks test for AssessmentItem
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext-test.xml" })
public class AssessmentItemTest {

    @Value("${sli.ingestion.recordLevelDeltaEntities}")
    private String recordLevelDeltaEnabledEntityNames;

    private String validXmlTestData = "<InterchangeAssessmentMetadata xmlns=\"http://ed-fi.org/0100\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"Interchange-AssessmentMetadata.xsd\">"
            + "<AssessmentItem id='test-id'>"
            + "  <IdentificationCode>test-code</IdentificationCode>"
            + "  <ItemCategory>List Question</ItemCategory>"
            + "  <MaxRawScore>100</MaxRawScore>"
            + "  <CorrectResponse>Hello World!</CorrectResponse>"
            + "  <LearningStandardReference>"
            + "    <LearningStandardIdentity>"
            + "      <LearningStandardId ContentStandardName='Common Core'>"
            + "        <IdentificationCode>id-code-1</IdentificationCode>"
            + "      </LearningStandardId>"
            + "    </LearningStandardIdentity>"
            + "  </LearningStandardReference>"
            + "  <LearningStandardReference>"
            + "    <LearningStandardIdentity>"
            + "      <LearningStandardId ContentStandardName='Unusual Periphery'>"
            + "        <IdentificationCode>id-code-2</IdentificationCode>"
            + "      </LearningStandardId>"
            + "    </LearningStandardIdentity>"
            + "  </LearningStandardReference>"
            + "  <Nomenclature>nomen</Nomenclature>"
            + "</AssessmentItem>" + "</InterchangeAssessmentMetadata>";


    @SuppressWarnings("unchecked")
    @Test
    public void testLearningObjectiveXML() throws IOException, SAXException {
        String smooksConfig = "smooks_conf/smooks-all-xml.xml";
        String targetSelector = "InterchangeAssessmentMetadata/AssessmentItem";

        NeutralRecord nr = EntityTestUtils.smooksGetSingleNeutralRecord(smooksConfig, targetSelector, validXmlTestData, recordLevelDeltaEnabledEntityNames);
        Map<String, Object> m = nr.getAttributes();
        Assert.assertEquals("test-id", nr.getLocalId());
        Assert.assertEquals("test-code", m.get("identificationCode"));
        Assert.assertEquals("List Question", m.get("itemCategory"));
        Assert.assertEquals(100, m.get("maxRawScore"));
        Assert.assertEquals("Hello World!", m.get("correctResponse"));

        List<Map<String, Object>> refs = (List<Map<String, Object>>) nr.getAttributes().get("learningStandards");
        Assert.assertNotNull(refs);
        Assert.assertEquals(2, refs.size());
        Assert.assertEquals("id-code-1", refs.get(0).get("identificationCode"));
        Assert.assertEquals("Common Core", refs.get(0).get("contentStandardName"));
        Assert.assertEquals("id-code-2", refs.get(1).get("identificationCode"));
        Assert.assertEquals("Unusual Periphery", refs.get(1).get("contentStandardName"));

        Assert.assertEquals("nomen", m.get("nomenclature"));
    }
}

@@ -819,16 +819,111 @@
 unWith;%0A
+import org.slc.sli.ingestion.NeutralRecord;%0Aimport org.slc.sli.ingestion.util.EntityTestUtils;%0A
 import o
@@ -1145,104 +1145,8 @@
 n;%0A%0A
-import org.slc.sli.ingestion.NeutralRecord;%0Aimport org.slc.sli.ingestion.util.EntityTestUtils;%0A%0A
 /**%0A
@@ -1955,24 +1955,441 @@
 tResponse%3E%22%0A
+            + %22   %3CAssessmentReference%3E%22%0A            + %22    %3CAssessmentIdentity%3E%22%0A            + %22      %3CAssessmentTitle%3Ec-aKzuT08%3C/AssessmentTitle%3E%22%0A            + %22      %3CAcademicSubject%3EEnglish%3C/AcademicSubject%3E%22%0A            + %22      %3CGradeLevelAssessed%3EPostsecondary%3C/GradeLevelAssessed%3E%22%0A            + %22      %3CVersion%3E1%3C/Version%3E%22%0A            + %22    %3C/AssessmentIdentity%3E%22%0A            + %22  %3C/AssessmentReference%3E%22%0A
             
@@ -4605,24 +4605,680 @@
 dardName%22));
+%0A        %0A        Map%3CString, Object%3E assessmentRef = (Map%3CString, Object%3E) nr.getAttributes().get(%22assessmentReference%22);%0A        Assert.assertNotNull(assessmentRef);%0A        Map%3CString, Object%3E assessmentIdentity = (Map%3CString, Object%3E) assessmentRef.get(%22AssessmentIdentity%22);%0A        Assert.assertNotNull(assessmentIdentity);%0A        Assert.assertEquals(%22c-aKzuT08%22, assessmentIdentity.get(%22AssessmentTitle%22));%0A        Assert.assertEquals(%22English%22, assessmentIdentity.get(%22AcademicSubject%22));%0A        Assert.assertEquals(%22Postsecondary%22, assessmentIdentity.get(%22GradeLevelAssessed%22));%0A        Assert.assertEquals(1, assessmentIdentity.get(%22Version%22));
 %0A%0A        As

update assessmentitem smooks mapping unit test