Permalink
Browse files

Convert SimpleImageUploadController to Scala

  • Loading branch information...
ostewart committed Mar 19, 2011
1 parent 3401215 commit 0b73b6a44a378cf19c4e16fd543c57588eb46c72
View
@@ -9,6 +9,12 @@
<webroots>
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" />
</webroots>
+ <sourceRoots>
+ <root url="file://$MODULE_DIR$/src/main/java" />
+ <root url="file://$MODULE_DIR$/src/main/resources" />
+ <root url="file://$MODULE_DIR$/src/test/java" />
+ <root url="file://$MODULE_DIR$/src/test/resources" />
+ </sourceRoots>
</configuration>
</facet>
<facet type="Spring" name="Spring">
@@ -42,6 +48,8 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
+ <sourceFolder url="file://$MODULE_DIR$/src/main/scala" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/src/test/scala" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
@@ -1,57 +0,0 @@
-package com.trailmagic.image.ui.upload;
-
-import com.trailmagic.image.ImageGroup;
-import com.trailmagic.image.ImageService;
-import com.trailmagic.image.Photo;
-import com.trailmagic.util.SecurityUtil;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.servlet.ModelAndView;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.util.HashMap;
-
-@Controller
-@RequestMapping("/upload")
-public class SimpleImageUploadController {
- private ImageService imageService;
- private SecurityUtil securityUtil;
-
- @Autowired
- public SimpleImageUploadController(ImageService imageService, SecurityUtil securityUtil) {
- this.imageService = imageService;
- this.securityUtil = securityUtil;
- }
-
- @RequestMapping
- public ModelAndView showForm() {
- final HashMap<String, Object> model = new HashMap<String, Object>();
- final ImageGroup imageGroup = imageService.findOrCreateDefaultRollForUser(securityUtil.getCurrentUser());
- model.put("imageGroup", imageGroup);
- model.put("nextFramePosition", imageGroup.nextFramePosition());
- return new ModelAndView("groupUpload", model);
- }
-
- @RequestMapping(method = RequestMethod.POST)
- public void handleUpload(@RequestParam(value = "pos", required = false) Integer position,
- @RequestParam(value="fileName") String fileName,
- HttpServletRequest req, HttpServletResponse res) throws IOException {
- final Photo image;
- if (position != null) {
- image = imageService.createImageAtPosition(fileName, req.getInputStream(), position);
- imageService.createManifestations(image, req.getInputStream());
- } else {
- image = imageService.createDefaultImage(fileName);
- imageService.createManifestations(image, req.getInputStream());
- }
-
-
- res.setStatus(303);
- res.addHeader("Location", res.encodeRedirectURL(req.getContextPath() + "/rolls/" + securityUtil.getCurrentUser().getScreenName() + "/uploads/" + image.getId()));
- }
-}
@@ -0,0 +1,43 @@
+package com.trailmagic.image.ui.upload
+
+import com.trailmagic.image.ImageGroup
+import com.trailmagic.image.ImageService
+import com.trailmagic.image.Photo
+import com.trailmagic.util.SecurityUtil
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.stereotype.Controller
+import org.springframework.web.bind.annotation.RequestMapping
+import org.springframework.web.bind.annotation.RequestMethod
+import org.springframework.web.bind.annotation.RequestParam
+import org.springframework.web.servlet.ModelAndView
+import javax.servlet.http.HttpServletRequest
+import javax.servlet.http.HttpServletResponse
+import scala.collection.JavaConverters._
+import scala.collection.mutable.Map
+import org.springframework.http.HttpStatus
+
+@Controller
+@RequestMapping(Array("/upload"))
+class SimpleImageUploadController @Autowired()(imageService: ImageService, securityUtil: SecurityUtil) {
+ @RequestMapping
+ def showForm: ModelAndView = {
+ val imageGroup: ImageGroup = imageService.findOrCreateDefaultRollForUser(securityUtil.getCurrentUser)
+
+ val model = Map("imageGroup" -> imageGroup, "nextFramePosition" -> imageGroup.nextFramePosition)
+ new ModelAndView("groupUpload", model.asJava)
+ }
+
+ @RequestMapping(method = Array(RequestMethod.POST))
+ def handleUpload(@RequestParam(value = "pos", required = false) position: java.lang.Integer,
+ @RequestParam(value = "fileName") fileName: String,
+ req: HttpServletRequest, res: HttpServletResponse) {
+ val image: Photo = position match {
+ case null => imageService.createDefaultImage(fileName)
+ case _ => imageService.createImageAtPosition(fileName, req.getInputStream, position.intValue)
+ }
+
+ imageService.createManifestations(image, req.getInputStream)
+ res.setStatus(HttpStatus.SEE_OTHER.value)
+ res.addHeader("Location", res.encodeRedirectURL(req.getContextPath + "/rolls/" + securityUtil.getCurrentUser.getScreenName + "/uploads/" + image.getId))
+ }
+}
@@ -1,13 +0,0 @@
-package com.trailmagic.image.ui.upload;
-
-import org.junit.Test;
-
-/**
- * Created by: oliver on Date: Nov 7, 2010 Time: 1:29:29 AM
- */
-public class SimpleImageUploadControllerTest {
- @Test
- public void testSetsFileNameFromParameter() {
-
- }
-}
@@ -0,0 +1,45 @@
+package com.trailmagic.image.ui.upload
+
+import org.junit.{Before, Test}
+import com.trailmagic.util.SecurityUtil
+import org.mockito.{MockitoAnnotations, Mock}
+import org.mockito.Mockito.{times, verify, when}
+import org.springframework.mock.web.{MockHttpServletResponse, MockHttpServletRequest}
+import com.trailmagic.image.{Photo, ImageService}
+import com.trailmagic.user.User
+
+class SimpleImageUploadControllerTest {
+ var controller: SimpleImageUploadController = _
+ @Mock var imageService: ImageService = _
+ @Mock var securityUtil: SecurityUtil = _
+
+ @Before
+ def setUp() {
+ MockitoAnnotations.initMocks(this)
+ controller = new SimpleImageUploadController(imageService, securityUtil)
+ }
+
+ @Test
+ def testCreatesDefaultImageWhenNoPositionParameterIsGiven {
+ val fileName = "niceImage.jpg"
+ when(imageService.createDefaultImage(fileName)).thenReturn(new Photo())
+ when(securityUtil.getCurrentUser).thenReturn(new User("tester"))
+
+ controller.handleUpload(null, fileName, new MockHttpServletRequest, new MockHttpServletResponse)
+
+ verify(imageService, times(1)).createDefaultImage(fileName)
+ }
+
+ @Test
+ def testCreatesImageAtPositionWhenPositionParameterSupplied {
+ val fileName = "niceImage.jpg"
+ val pos = 6
+ val req = new MockHttpServletRequest
+ when(imageService.createImageAtPosition(fileName, req.getInputStream, pos)).thenReturn(new Photo())
+ when(securityUtil.getCurrentUser).thenReturn(new User("tester"))
+
+ controller.handleUpload(pos, fileName, req, new MockHttpServletResponse)
+
+ verify(imageService, times(1)).createImageAtPosition(fileName, req.getInputStream, pos)
+ }
+}

0 comments on commit 0b73b6a

Please sign in to comment.