/*
 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     dmetzler
 */
package org.nuxeo.ecm.restapi.jaxrs.io;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nuxeo.ecm.automation.io.services.JsonFactoryManager;
import org.nuxeo.ecm.automation.jaxrs.io.documents.JSONDocumentModelReader;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.test.annotations.Granularity;
import org.nuxeo.ecm.core.test.annotations.RepositoryConfig;
import org.nuxeo.ecm.restapi.jaxrs.io.documents.JSONDocumentModelListReader;
import org.nuxeo.ecm.restapi.test.JSONDocumentHelper;
import org.nuxeo.ecm.restapi.test.RestServerFeature;
import org.nuxeo.ecm.restapi.test.RestServerInit;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;

import com.google.inject.Inject;

/**
 * @since 5.7.3
 */
@RunWith(FeaturesRunner.class)
@Features({ RestServerFeature.class })
@RepositoryConfig(cleanup = Granularity.METHOD, init = RestServerInit.class)
public class DocumentModelListReaderTest {

    private HttpServletRequest request = mock(HttpServletRequest.class);

    @Inject
    CoreSession session;

    @Inject
    JsonFactoryManager factoryManager;

    @Before
    public void doBefore() {
        when(request.getUserPrincipal()).thenReturn(session.getPrincipal());
    }

    @Test
    public void iCanReadADocument() throws Exception {
        DocumentModel note1 = RestServerInit.getNote(1, session);

        String json = JSONDocumentHelper.getDocAsJson(note1);


        JsonParser jp = getParserFor(json);
        DocumentModel doc = JSONDocumentModelReader.readJson(jp, null, request);

        assertNotNull(doc);
        assertEquals(note1.getId(), doc.getId());

    }

    @Test
    public void iCanReadADocumentModelList() throws Exception {
        DocumentModel note1 = RestServerInit.getNote(1, session);
        DocumentModel note2 = RestServerInit.getNote(2, session);

        String docsAsJson = JSONDocumentHelper.getDocsListAsJson(note1, note2);


        JsonParser jp = getParserFor(docsAsJson);

        DocumentModelList docs = JSONDocumentModelListReader.readRequest(
               jp, null, request);
        assertEquals(2, docs.size());

        assertEquals(RestServerInit.getNote(1, session).getId(),
                docs.get(0).getId());

    }

    private JsonParser getParserFor(String docsAsJson) throws IOException,
            JsonParseException {
        return factoryManager.getJsonFactory().createJsonParser(docsAsJson);
    }

}

@@ -1007,24 +1007,48 @@
 JsonParser;%0A
+import org.junit.After;%0A
 import org.j
@@ -1056,24 +1056,24 @@
 nit.Before;%0A
-
 import org.j
@@ -1744,16 +1744,208 @@
 erInit;%0A
+import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;%0Aimport org.nuxeo.ecm.webengine.jaxrs.session.SessionRef;%0Aimport org.nuxeo.ecm.webengine.jaxrs.session.impl.PerRequestCoreProvider;%0A
 import o
@@ -2298,16 +2298,22 @@
 private 
+final 
 HttpServ
@@ -2362,24 +2362,113 @@
 st.class);%0A%0A
+    protected final PerRequestCoreProvider coreProvider = new PerRequestCoreProvider();%0A%0A
     @Inject%0A
@@ -2663,16 +2663,268 @@
 pal());%0A
+        when(request.getAttribute(SessionFactory.SESSION_FACTORY_KEY)).thenReturn(coreProvider);%0A    %7D%0A%0A    @After%0A    public void closeWebEngineSession() %7B%0A        for (SessionRef ref:coreProvider.getSessions()) %7B%0A            ref.destroy();%0A        %7D%0A
     %7D%0A%0A 
@@ -3113,25 +3113,24 @@
 on(note1);%0A%0A
-%0A
         Json
@@ -3617,17 +3617,16 @@
 ote2);%0A%0A
-%0A
         
@@ -3741,16 +3741,19 @@
 Request(
+jp,
 %0A       
@@ -3760,19 +3760,16 @@
         
-jp,
  null, r

fix session core session leak in webengine test