/*
* Copyright 2002-2007 the original author or authors.
*
* 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.internna.iwebmvc.spring.mvc.controllers;
import java.io.File;
import java.io.FileInputStream;
import org.internna.iwebmvc.core.IWebMvcException;
import org.internna.iwebmvc.core.crypto.impl.DES3CiphererDecipherer;
import org.internna.iwebmvc.model.DocumentHolder;
import org.internna.iwebmvc.repository.FileRepository;
import org.internna.iwebmvc.utils.IO;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.junit.Assert.*;
/**
* @author Jose Noheda
* @since 1.0
*/
public class RemoteRepositoryAccessControllerTest {
private RemoteRepositoryAccessController controller;
private static File root = new File("root");
private String uri;
private DES3CiphererDecipherer crypto;
@Before
public void setup() throws Exception {
controller = new RemoteRepositoryAccessController();
crypto = new DES3CiphererDecipherer();
crypto.init();
controller.decipherer = crypto;
FileRepository repository = new FileRepository();
repository.setDocumentRoot(root);
controller.documentRepository = repository;
DocumentHolder doc = new DocumentHolder();
doc.setIdentifier("web/images/i18n/globe.gif");
doc.setStream(new FileInputStream(new File(doc.getIdentifier())));
doc.setMimeType("image/jpg");
repository.store(doc);
uri = crypto.encrypt(doc.getUri());
}
@Test
public void serve() throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setOutputStreamAccessAllowed(true);
controller.serve(crypto.encrypt("no"), null, null, response);
assertEquals("File could not be found", MockHttpServletResponse.SC_NOT_FOUND, response.getStatus());
controller.serve(uri, null, null, response);
assertTrue("Content streamed" , response.getContentAsByteArray().length > 1000);
}
@AfterClass
public static void tearDown() {
IO.delete(root);
if (root.exists()) throw new IWebMvcException("Please manually delete " + root.getAbsolutePath());
}
}