/*
Copyright 2006 - 2010 Under Dusken
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 no.dusken.aranea.control;
import no.dusken.aranea.model.Image;
import no.dusken.aranea.service.ImageService;
import no.dusken.aranea.util.ImageUtils;
import no.dusken.common.exception.PageNotFoundException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestImageController{
private ImageController imgController;
private File resourceDir;
private ImageService imageService;
private ImageUtils imageUtils;
@Before
public void setUp() throws IOException {
//This class will be tested, so creating testing object.
imgController = new ImageController();
resourceDir = new File("src/test/resources/images/");
imageService = mock(ImageService.class);
imageUtils = mock(ImageUtils.class);
imgController.setImageService(imageService);
imgController.setImageUtils(imageUtils);
}
@Test
public void testGetImage() throws Exception {
//creating image object.
Image image = new Image();
image.setID(123L);
//mocking imageSernice.getEntity method to return image object I just created.
when(imageService.getEntity(123L)).thenReturn(image);
//mocking imageFile.
File imageFile = mock(File.class);
//mocking imageFile.exists method to return true.
when(imageFile.exists()).thenReturn(true);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.addParameter("ID", "123");
//mocking imageUtils.getImageFile(Image)
when(imageUtils.getImageFile(image)).thenReturn(imageFile);
ModelAndView mav = imgController.handleRequestInternal(request, response);
assertEquals("Did not return correct file.",mav.getModel().get("file"),imageFile);
}
@Test
public void testResizeImage_Width() throws Exception {
//creating image object.
Image image = new Image();
image.setID(123L);
//mocking imageSernice.getEntity method to return image object I just created.
when(imageService.getEntity(123L)).thenReturn(image);
//mocking imageFile.
File imageFile = mock(File.class);
//mocking imageFile.exists method to return true.
when(imageFile.exists()).thenReturn(true);
//mocking resizedImageFile
File resizedImageFile = mock(File.class);
when(resizedImageFile.toString()).thenReturn("resized");
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.addParameter("ID", "123");
request.addParameter("maxwidth", "30");
//mocking imageUtils.getImageFile(Image)
when(imageUtils.getImageFile(image)).thenReturn(imageFile);
//mocking imageUtils.resizeImage(imageFile, maxWidth, maxHeight)
when(imageUtils.resizeImage(imageFile, 30, 0)).thenReturn(resizedImageFile);
ModelAndView mav = imgController.handleRequestInternal(request, response);
assertEquals("Resize image method is wrong.",mav.getModel().get("file"),resizedImageFile);
assertEquals("Resized image is wrong", "resized", resizedImageFile.toString());
}
@Test
public void testResizeImage_Height() throws Exception {
//creating image object.
Image image = new Image();
image.setID(123L);
//mocking imageSernice.getEntity method to return image object I just created.
when(imageService.getEntity(123L)).thenReturn(image);
//mocking imageFile.
File imageFile = mock(File.class);
//mocking imageFile.exists method to return true.
when(imageFile.exists()).thenReturn(true);
//mocking resizedImageFile
File resizedImageFile = mock(File.class);
when(resizedImageFile.toString()).thenReturn("resized");
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.addParameter("ID", "123");
request.addParameter("maxheight", "30");
//mocking imageUtils.getImageFile(Image)
when(imageUtils.getImageFile(image)).thenReturn(imageFile);
//mocking imageUtils.resizeImage(imageFile, maxWidth, maxHeight)
when(imageUtils.resizeImage(imageFile, 0, 30)).thenReturn(resizedImageFile);
ModelAndView mav = imgController.handleRequestInternal(request, response);
assertEquals("Resize image method is wrong.",mav.getModel().get("file"),resizedImageFile);
assertEquals("Resized image is wrong", "resized", resizedImageFile.toString());
}
@Test(expected = PageNotFoundException.class)
public void testGetImage_Null() throws Exception {
//mocking imageSernice.getEntity method to return null.
when(imageService.getEntity(0L)).thenReturn(null);
//mocking imageFile.
File imageFile = mock(File.class);
//mocking imageFile.exists method to return true.
when(imageFile.exists()).thenReturn(true);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.addParameter("ID", "123");
//mocking imageUtils.getImageFile(null)
//this method should not be called, since imageService.getEntity() returned null
when(imageUtils.getImageFile(null)).thenReturn(imageFile);
ModelAndView mav = imgController.handleRequestInternal(request, response);
assertEquals("Was supposed to return null.",null,imageFile);
}
}