You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2019/05/21 15:31:24 UTC

[sling-org-apache-sling-app-cms] branch master updated: Adding a test for the full flow through the Transformation Servlet

This is an automated email from the ASF dual-hosted git repository.

dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git


The following commit(s) were added to refs/heads/master by this push:
     new 55628ae  Adding a test for the full flow through the Transformation Servlet
55628ae is described below

commit 55628aedac786dd85779cb57beb87a46ca6402e4
Author: Dan Klco <dk...@apache.org>
AuthorDate: Tue May 21 11:31:14 2019 -0400

    Adding a test for the full flow through the Transformation Servlet
---
 .../internal/servlets/TransformServletTest.java    |  93 +++++++++++++++++++++
 core/src/test/resources/thumbnail.png              | Bin 0 -> 20437 bytes
 2 files changed, 93 insertions(+)

diff --git a/core/src/test/java/org/apache/sling/cms/core/internal/servlets/TransformServletTest.java b/core/src/test/java/org/apache/sling/cms/core/internal/servlets/TransformServletTest.java
new file mode 100644
index 0000000..b995b1e
--- /dev/null
+++ b/core/src/test/java/org/apache/sling/cms/core/internal/servlets/TransformServletTest.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.sling.cms.core.internal.servlets;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.reflect.FieldUtils;
+import org.apache.sling.cms.core.helpers.SlingCMSContextHelper;
+import org.apache.sling.cms.core.internal.transformation.CropHandler;
+import org.apache.sling.cms.core.internal.transformation.FileThumbnailTransformerImpl;
+import org.apache.sling.cms.core.internal.transformation.ImageThumbnailProvider;
+import org.apache.sling.cms.core.internal.transformation.PdfThumbnailProvider;
+import org.apache.sling.cms.core.internal.transformation.SizeHandler;
+import org.apache.sling.cms.transformation.ThumbnailProvider;
+import org.apache.sling.cms.transformation.TransformationHandler;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Lists;
+
+public class TransformServletTest {
+
+    private static final Logger log = LoggerFactory.getLogger(TransformServletTest.class);
+    
+    private TransformServlet ts = new TransformServlet();;
+
+    @Rule
+    public final SlingContext context = new SlingContext();
+
+    @Before
+    public void init() throws IllegalAccessException {
+        SlingCMSContextHelper.initContext(context);
+
+        FileThumbnailTransformerImpl transformer = new FileThumbnailTransformerImpl();
+        transformer.setHandlers(Lists.asList(new CropHandler(), new TransformationHandler[] { new SizeHandler() }));
+        transformer.setThumbnailProviders(
+                Lists.asList(new ImageThumbnailProvider(), new ThumbnailProvider[] { new PdfThumbnailProvider() }));
+        
+        FieldUtils.writeDeclaredField(ts,"transformer", transformer, true);
+    }
+
+    @Test
+    public void testValid() throws IOException, ServletException {
+        log.info("testContentTypes");
+
+        context.currentResource("/content/apache/sling-apache-org/index/apache.png");
+        context.requestPathInfo().setSuffix("/size-200-200/crop-center.png");
+        context.requestPathInfo().setExtension("transform");
+
+        ts.doGet(context.request(), context.response());
+
+        byte[] thumb = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("thumbnail.png"));
+        assertArrayEquals(thumb, context.response().getOutput());
+    }
+
+    @Test
+    public void testInvalid() throws IOException, ServletException {
+        log.info("testContentTypes");
+
+        context.currentResource("/content/apache/sling-apache-org/index/apache.png");
+        context.requestPathInfo().setSuffix("/size-200-200/crop-left.png");
+        context.requestPathInfo().setExtension("transform");
+
+        ts.doGet(context.request(), context.response());
+
+        assertEquals(400, context.response().getStatus());
+    }
+
+}
diff --git a/core/src/test/resources/thumbnail.png b/core/src/test/resources/thumbnail.png
new file mode 100644
index 0000000..f38c3a4
Binary files /dev/null and b/core/src/test/resources/thumbnail.png differ