Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging

File ImagingServletTest.java

 

Code metrics

0
58
13
2
270
168
13
0.22
4.46
6.5
1
7.8% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ImagingServletTest 68 51 3.2% 10 61
0.00%
ImagingServletTest.TestImageGenerator 237 7 28.6% 3 10
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2009-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging;
35   
36    import static org.junit.Assert.*;
37    import static org.mockito.Mockito.*;
38   
39    import info.magnolia.cms.beans.config.MIMEMapping;
40    import info.magnolia.cms.security.User;
41    import info.magnolia.context.MgnlContext;
42    import info.magnolia.context.SystemContext;
43    import info.magnolia.imaging.caching.CachingStrategy;
44    import info.magnolia.init.MagnoliaConfigurationProperties;
45    import info.magnolia.repository.RepositoryConstants;
46    import info.magnolia.test.ComponentsTestUtil;
47    import info.magnolia.test.mock.MockUtil;
48    import info.magnolia.test.mock.MockWebContext;
49    import info.magnolia.test.mock.jcr.SessionTestUtil;
50   
51    import java.awt.image.BufferedImage;
52    import java.io.ByteArrayOutputStream;
53    import java.io.IOException;
54    import java.util.Collections;
55   
56    import javax.inject.Provider;
57    import javax.jcr.Session;
58    import javax.servlet.ServletOutputStream;
59    import javax.servlet.http.HttpServletRequest;
60    import javax.servlet.http.HttpServletResponse;
61   
62    import org.junit.After;
63    import org.junit.Before;
64    import org.junit.Ignore;
65    import org.junit.Test;
66   
67    @Ignore
 
68    public class ImagingServletTest {
69   
70    private HttpServletRequest req;
71    private HttpServletResponse res;
72    private ImagingServlet imagingServlet;
73    private ByteArrayOutputStream fakedOut;
74    private OutputFormat outputFormat;
75    private ImagingModule imagingModule;
76    private TestImageGenerator imageGenerator;
77   
 
78  0 toggle @Before
79    public void setUp() throws Exception {
80  0 req = mock(HttpServletRequest.class);
81  0 res = mock(HttpServletResponse.class);
82  0 fakedOut = new ByteArrayOutputStream();
83  0 outputFormat = new OutputFormat();
84  0 imagingModule = new ImagingModule();
85   
86   
87    // GIVEN
88  0 final ServletOutputStream servletOut = new ServletOutputStream() {
 
89  0 toggle @Override
90    public void write(int b) throws IOException {
91  0 fakedOut.write(b);
92    }
93    };
94  0 Session configMimeMapping = SessionTestUtil.createSession(RepositoryConstants.CONFIG,
95    "/server/MIMEMapping/jpg",
96    "/server/MIMEMapping/jpg.extension=jpg",
97    "/server/MIMEMapping/jpg.mime-type=image/jpeg",
98    "/server/MIMEMapping/gif",
99    "/server/MIMEMapping/gif.extension=gif",
100    "/server/MIMEMapping/gif.mime-type=image/gif",
101    "/server/MIMEMapping/png",
102    "/server/MIMEMapping/png.extension=png",
103    "/server/MIMEMapping/png.mime-type=image/png"
104    );
105  0 Session imagingSession = SessionTestUtil.createSession(ImagingModule.IMAGING,
106    "/myGenerator/path-to/dummyUri/generated.@type=mgnl:resource",
107    "/myGenerator/path-to/dummyUri/generated.jcr\\:data=" + new String(new byte[] { 1, 2, 3 }, "UTF-8"),
108    "/myGenerator/path-to/dummyUri/generated.jcr\\:mimeType=image/gif",
109    "/myGenerator/path-to/dummyUri/generated.jcr\\:lastModified=123"
110    );
111   
112   
113  0 final MockWebContext ctx = new MockWebContext();
114  0 when(req.getRequestURI()).thenReturn("dummyUri");
115  0 when(res.getOutputStream()).thenReturn(servletOut);
116   
117  0 final ParameterProviderFactory<HttpServletRequest, String> ppFactory = new ParameterProviderFactory<HttpServletRequest, String>() {
 
118  0 toggle @Override
119    public ParameterProvider<String> newParameterProviderFor(HttpServletRequest context) {
120  0 return new StringParameterProvider(context.getRequestURI());
121    }
122   
 
123    toggle @Override
124    public CachingStrategy<String> getCachingStrategy() {
125    return new StringBasedCachingStrategy();
126    }
127    };
128   
129  0 imageGenerator = new TestImageGenerator(ppFactory, outputFormat);
130  0 imagingModule.setGenerators(Collections.<String, ImageGenerator>singletonMap(imageGenerator.getName(), imageGenerator));
131   
132  0 Provider<SystemContext> contextProvider = mock(Provider.class);
133  0 when(contextProvider.get()).thenReturn(ctx);
134  0 imagingServlet = new ImagingServlet(new Imaging(imagingModule, contextProvider), new Provider<ImagingModule>() {
 
135  0 toggle @Override
136    public ImagingModule get() {
137  0 return imagingModule;
138    }
139    }, mock(MagnoliaConfigurationProperties.class));
140   
141  0 ctx.setUser(mock(User.class));
142   
143  0 MgnlContext.setInstance(ctx);
144   
145  0 ComponentsTestUtil.setInstance(SystemContext.class, ctx);
146  0 MockUtil.setSystemContextSessionAndHierarchyManager(imagingSession);
147  0 MockUtil.setSystemContextSessionAndHierarchyManager(configMimeMapping);
148   
149  0 MIMEMapping.init();
150    }
151   
 
152  0 toggle @After
153    public void tearDown() throws Exception {
154  0 MgnlContext.setInstance(null);
155  0 ComponentsTestUtil.clear();
156    }
157   
 
158  0 toggle @Test
159    public void requestToFactoryToGeneratorToImage() throws Exception {
160    // GIVEN
161  0 when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.png");
162  0 outputFormat.setFormatName("png");
163   
164    // WHEN
165  0 imagingServlet.doGet(req, res);
166   
167    // THEN
168  0 verify(res, times(1)).flushBuffer();
169   
170  0 assertTrue("ImageGenerator.generate() wasn't called !", imageGenerator.imageGeneratorWasCalled);
171   
172    // TODO - disabled for now
173    // yes, we could instead feed the test with a FileOutputStream instead of doing the shenanigans of reading/saving, but this piece below shouldn't stay here
174    // it just generates a small black jpeg...
175    // final BufferedImage img = ImageIO.read(new ByteArrayInputStream(fakedOut.toByteArray()));
176    // final String filename = getClass().getSimpleName() + ".jpg";
177    // ImageIO.write(img, "jpg", new File(filename));
178    // Runtime.getRuntime().exec("open " + filename);
179    }
180   
 
181  0 toggle @Test
182    public void mimeTypeIsSetWhenCachingGeneratedImages() throws Exception {
183    // GIVEN
184  0 when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.JPG");
185  0 outputFormat.setFormatName("JPG");
186   
187  0 imagingModule.setStoreGeneratedImages(true);
188   
189    // WHEN
190  0 imagingServlet.doGet(req, res);
191   
192    // THEN
193  0 verify(res).setContentType("image/jpeg");
194    }
195   
 
196  0 toggle @Test
197    public void mimeTypeIsSetWhenNotCachingGeneratedImages() throws Exception {
198    // GIVEN
199  0 when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/node.JPG");
200  0 outputFormat.setFormatName("JPG");
201   
202  0 imagingModule.setStoreGeneratedImages(false);
203   
204    // WHEN
205  0 imagingServlet.doGet(req, res);
206   
207    // THEN
208  0 verify(res).setContentType("image/jpeg");
209    }
210   
 
211  0 toggle @Test
212    public void notFoundWhenRequestingNonExistingImage() throws Exception {
213    // GIVEN
214  0 when(req.getPathInfo()).thenReturn("/myGenerator/someWorkspace/some/path/to/a/nodeDoesNotExist.jpg");
215  0 imagingModule.setStoreGeneratedImages(false);
216   
217    // WHEN
218  0 imagingServlet.doGet(req, res);
219   
220    // THEN
221  0 verify(res).sendError(404);
222    }
223   
 
224  0 toggle @Test
225    public void notFoundWhenRequestingNonExistingPath() throws Exception {
226    // GIVEN
227  0 when(req.getPathInfo()).thenReturn("/nonsense.abc");
228  0 imagingModule.setStoreGeneratedImages(false);
229   
230    // WHEN
231  0 imagingServlet.doGet(req, res);
232   
233    // THEN
234  0 verify(res).sendError(404);
235    }
236   
 
237    private static class TestImageGenerator<P extends ParameterProvider<?>> implements ImageGenerator<P> {
238    private final ParameterProviderFactory ppFactory;
239    private final OutputFormat outputFormat;
240    boolean imageGeneratorWasCalled;
241   
 
242  0 toggle public TestImageGenerator(ParameterProviderFactory ppFactory, OutputFormat outputFormat) {
243  0 this.ppFactory = ppFactory;
244  0 this.outputFormat = outputFormat;
245  0 imageGeneratorWasCalled = false;
246    }
247   
 
248  0 toggle @Override
249    public BufferedImage generate(P params) {
250  0 assertEquals("dummyUri", params.getParameter());
251  0 imageGeneratorWasCalled = true;
252  0 return new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
253    }
254   
 
255    toggle @Override
256    public ParameterProviderFactory getParameterProviderFactory() {
257    return ppFactory;
258    }
259   
 
260  0 toggle @Override
261    public OutputFormat getOutputFormat(P params) {
262  0 return outputFormat;
263    }
264   
 
265    toggle @Override
266    public String getName() {
267    return "myGenerator";
268    }
269    }
270    }