/*
 * Copyright (C) 2003-2012 eXo Platform SAS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.exoplatform.wcm.connector.collaboration;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.jcr.Node;

import org.apache.commons.lang3.StringUtils;

import org.exoplatform.BaseConnectorTestCase;
import org.exoplatform.services.jcr.impl.core.NodeImpl;
import org.exoplatform.services.jcr.util.Text;
import org.exoplatform.services.rest.impl.ContainerResponse;
import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
import org.exoplatform.services.rest.wadl.research.HTTPMethods;
import org.exoplatform.services.security.ConversationState;
import org.exoplatform.services.security.Identity;
import org.exoplatform.services.wcm.core.NodetypeConstant;


/**
 * Created by The eXo Platform SAS
 * Author : Nguyen The Vinh From ECM Of eXoPlatform
 *          vinh_nguyen@exoplatform.com
 * 23 Aug 2012  
 */
public class TestThumbnailRESTService extends BaseConnectorTestCase{
  
  public void setUp() throws Exception {
    super.setUp();
    ThumbnailRESTService restService = (ThumbnailRESTService) this.container.getComponentInstanceOfType(ThumbnailRESTService.class);
    this.binder.addResource(restService, null);
  }

  public void testGetOriginImageSpecialCharacter() throws Exception{
    String restPath = "/thumbnailImage/origin/repository/collaboration/" + URLEncoder.encode(Text.escapeIllegalJcrChars("~!@%23$%^()`=}{-%22,.___ -.png"), "UTF-8");
    ConversationState.setCurrent(new ConversationState(new Identity("john")));
    applyUserSession("john", "gtn", "collaboration");
    /* Prepare the favourite nodes */
    ContainerResponse response = service(HTTPMethods.GET.toString(), restPath, StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
  }

  public void testGetOriginImage() throws Exception{
    String restPath = "/thumbnailImage/origin/repository/collaboration/offices.jpg";
    ConversationState.setCurrent(new ConversationState(new Identity("john")));
    /* Prepare the favourite nodes */
    ContainerResponse response = service(HTTPMethods.GET.toString(), restPath, StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
  }
  
  public void testGetLargeImage() throws Exception{
    String restPath = "/thumbnailImage/large/repository/collaboration/offices.jpg";
    ConversationState.setCurrent(new ConversationState(new Identity("john")));
    /* Prepare the favourite nodes */
    ContainerResponse response = service(HTTPMethods.GET.toString(), restPath, StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
  }
  
  public void testGetBigImage() throws Exception{
    String restPath = "/thumbnailImage/big/repository/collaboration/offices.jpg";
    ConversationState.setCurrent(new ConversationState(new Identity("john")));
    /* Prepare the favourite nodes */
    ContainerResponse response = service(HTTPMethods.GET.toString(), restPath, StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
  }
  
  public void testGetMediumImage() throws Exception{
    String restPath = "/thumbnailImage/medium/repository/collaboration/offices.jpg";
    ConversationState.setCurrent(new ConversationState(new Identity("john")));
    /* Prepare the favourite nodes */
    ContainerResponse response = service(HTTPMethods.GET.toString(), restPath, StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
  }
  
  public void testGetSmallImage() throws Exception{
    applyUserSession("john", "gtn", "collaboration");
    String restPath = "/thumbnailImage/small/repository/collaboration/metro.pdf";
    ConversationState.setCurrent(new ConversationState(new Identity("john")));
    MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
    
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, -10);
    SimpleDateFormat dateFormat = new SimpleDateFormat(ThumbnailRESTService.IF_MODIFIED_SINCE_DATE_FORMAT);
    headers.putSingle("If-Modified-Since", dateFormat.format(cal.getTime()));
    
    /* Prepare the favourite nodes */
    ContainerResponse response = service(HTTPMethods.GET.toString(), restPath, StringUtils.EMPTY, headers, null);
    assertNotNull(response);
    if(response.getStatus() == 200) {
      assertEquals("image", response.getContentType().getType());
    }  
  }

  public void testGetCustomImage() throws Exception{
    String workspaceName = "collaboration";
    String userName = "john";
    Node rootNode = session.getRootNode();
    Node node = rootNode.addNode("test");
    node.setProperty(NodetypeConstant.JCR_MIME_TYPE, "image/png");
    session.save();
    String identifier = ((NodeImpl) node).getIdentifier();
    applyUserSession(userName, "gtn", workspaceName);
    ConversationState.setCurrent(new ConversationState(new Identity(userName)));
    String restPath = "/thumbnailImage/custom/250x250/collaboration/"+identifier;
    MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, -10);
    SimpleDateFormat dateFormat = new SimpleDateFormat(ThumbnailRESTService.IF_MODIFIED_SINCE_DATE_FORMAT);
    headers.putSingle("If-Modified-Since", dateFormat.format(cal.getTime()));
    ContainerResponse response = service(HTTPMethods.GET.toString(), restPath, StringUtils.EMPTY, headers, null);
    assertNotNull(response);
    assertEquals(200,response.getStatus());
  }
  
  public void tearDown() throws Exception {
    super.tearDown();
  }
}
