/*
 * 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.scaffold;

import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.net.URI;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;

/**
 * @author jstokes
 */
public class ResourceDocumentationTest {
    private ResourceDocumentation rDoc; // class under test

    private DocumentManipulator documentManipulator = new DocumentManipulator();
    private final String TEST_WADL_LOC = "/test_wadl.wadl";
    private final String TEST_RESOURCE_LOC = "/test_resources.json";
    private Document testWadl;

    @Before
    public void setup() throws Exception {
        URI wadlUri = this.getClass().getResource(TEST_WADL_LOC).toURI();
        testWadl = documentManipulator.parseDocument(new File(wadlUri));
        rDoc = new ResourceDocumentation(testWadl, TEST_RESOURCE_LOC);
        rDoc.addDocumentation();
    }

    @Test
    public void testAddDocumentation() throws Exception {
        NodeList section = documentManipulator.getNodeList(testWadl,
                "//resource[@path='v1/sections']");
        assertEquals(1, section.getLength());

        Node sectionNodeDoc = documentManipulator.getNodeList(testWadl,
                "//resource[@path='v1/sections']/doc").item(0);
        assertNotNull(sectionNodeDoc);
        assertEquals("test /sections doc", sectionNodeDoc.getFirstChild().getNodeValue());

        Node sectionNodeDV = documentManipulator.getNodeList(testWadl,
                "//resource[@path='v1/sections']/deprecatedVersion").item(0);
        assertNotNull(sectionNodeDV);
        assertEquals("v3", sectionNodeDV.getFirstChild().getNodeValue());

        Node sectionNodeDR = documentManipulator.getNodeList(testWadl,
                "//resource[@path='v1/sections']/deprecatedReason").item(0);
        assertNull(sectionNodeDR);

        Node gradebookEntryDoc = documentManipulator.getNodeList(testWadl,
                "//resource[@path='v1/sections']/resource[@path='{id}/gradebookEntries']/doc").item(0);
        assertNotNull(gradebookEntryDoc);
        assertEquals("test section/{id}/gradebookEntries doc", gradebookEntryDoc.getFirstChild().getNodeValue());

        Node gradebookEntryDV = documentManipulator.getNodeList(testWadl,
                "//resource[@path='v1/sections']/resource[@path='{id}/gradebookEntries']/deprecatedVersion").item(0);
        assertNotNull(gradebookEntryDV);
        assertEquals("v1", gradebookEntryDV.getFirstChild().getNodeValue());

        Node gradebookEntryDR = documentManipulator.getNodeList(testWadl,
                "//resource[@path='v1/sections']/resource[@path='{id}/gradebookEntries']/deprecatedReason").item(0);
        assertNotNull(gradebookEntryDR);
        assertEquals("no good!", gradebookEntryDR.getFirstChild().getNodeValue());
    }
}

@@ -1691,920 +1691,42 @@
 dDoc
-umentation() throws Exception %7B%0A        NodeList section = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D%22);%0A        assertEquals(1, section.getLength());%0A%0A        Node sectionNodeDoc = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D/doc%22).item(0);%0A        assertNotNull(sectionNodeDoc);%0A        assertEquals(%22test /sections doc%22, sectionNodeDoc.getFirstChild().getNodeValue());%0A%0A        Node sectionNodeDV = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D/deprecatedVersion%22).item(0);%0A        assertNotNull(sectionNodeDV);%0A        assertEquals(%22v3%22, sectionNodeDV.getFirstChild().getNodeValue());%0A%0A        Node sectionNodeDR = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D/deprecatedReason%22).item(0);%0A        assertNull(sectionNodeDR);%0A
+ToSubResource() throws Exception %7B
 %0A   
@@ -2049,32 +2049,116 @@
 etNodeValue());%0A
+    %7D%0A%0A    @Test%0A    public void testAddDeprecatedToSubResource() throws Exception %7B
 %0A        Node gr
@@ -2773,11 +2773,1037 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testAddDeprecated() throws Exception %7B%0A        Node sectionNodeDV = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D/deprecatedVersion%22).item(0);%0A        assertNotNull(sectionNodeDV);%0A        assertEquals(%22v3%22, sectionNodeDV.getFirstChild().getNodeValue());%0A%0A        Node sectionNodeDR = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D/deprecatedReason%22).item(0);%0A        assertNull(sectionNodeDR);%0A    %7D%0A%0A    @Test%0A    public void testAddDocumentation() throws Exception %7B%0A        NodeList section = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D%22);%0A        assertEquals(1, section.getLength());%0A%0A        Node sectionNodeDoc = documentManipulator.getNodeList(testWadl,%0A                %22//resource%5B@path='v1/sections'%5D/doc%22).item(0);%0A        assertNotNull(sectionNodeDoc);%0A        assertEquals(%22test /sections doc%22, sectionNodeDoc.getFirstChild().getNodeValue());%0A    %7D
 %0A%7D%0A

Refactor resourcedocumentation test