You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2018/11/06 08:24:50 UTC

[GitHub] philipmundt closed pull request #10: SLING-8047 ExportServlet does not write to output stream

philipmundt closed pull request #10: SLING-8047 ExportServlet does not write to output stream
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/10
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pom.xml b/pom.xml
index 6ef84dc..8ec4849 100644
--- a/pom.xml
+++ b/pom.xml
@@ -239,5 +239,11 @@
             <version>16.0.2</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.johnzon</artifactId>
+            <version>1.1.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/models/impl/ExportServlet.java b/src/main/java/org/apache/sling/models/impl/ExportServlet.java
index f557713..5d6f47a 100644
--- a/src/main/java/org/apache/sling/models/impl/ExportServlet.java
+++ b/src/main/java/org/apache/sling/models/impl/ExportServlet.java
@@ -118,6 +118,7 @@ protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse r
                 return;
             }
             response.getWriter().write(exported);
+            response.getWriter().flush();
         } finally {
             scriptHelper.cleanup();
         }
@@ -202,6 +203,6 @@ public String getExportedString(SlingHttpServletRequest request, Map<String, Str
             Object adapter = modelFactory.createModel(request, adapterClass);
             return modelFactory.exportModel(adapter, exporterName, String.class, options);
         }
-    };
+    }
 
 }
diff --git a/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java b/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
index 4e9a047..0ec9d6a 100644
--- a/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
+++ b/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
@@ -59,6 +59,6 @@ public boolean hasInjectAnnotation() {
 
     public ConstructorParameter[] getConstructorParameters() {
         return constructorParametersArray;
-    };
-    
+    }
+
 }
diff --git a/src/test/java/org/apache/sling/models/impl/ExportServletTest.java b/src/test/java/org/apache/sling/models/impl/ExportServletTest.java
new file mode 100644
index 0000000..e4cfcb1
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/impl/ExportServletTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.models.impl;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestPathInfo;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.models.factory.ModelFactory;
+import org.apache.sling.models.testmodels.classes.DefaultStringModel;
+import org.apache.sling.scripting.api.BindingsValuesProvidersByContext;
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.BundleContext;
+
+import javax.json.Json;
+import javax.json.JsonReader;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.util.Collections;
+import java.util.Map;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ExportServletTest {
+
+    private static final String SELECTOR = "model";
+
+    private ExportServlet exportServlet;
+
+    @Mock
+    private BundleContext bundleContext;
+
+    @Mock
+    private ModelFactory modelFactory;
+
+    @Mock
+    private BindingsValuesProvidersByContext bindingsValueProviderByContext;
+
+    @Mock
+    private SlingModelsScriptEngineFactory scriptFactory;
+
+    @Mock
+    private ExportServlet.ExportedObjectAccessor exportedObjectAccessor;
+
+    @Mock
+    private SlingHttpServletRequest slingHttpServletRequest;
+
+    @Mock
+    private SlingHttpServletResponse slingHttpServletResponse;
+
+    @Mock
+    private RequestPathInfo requestPathInfo;
+
+    @Mock
+    private Resource resource;
+
+    @Mock
+    private ResourceResolver resourceResolver;
+
+    private Map<String, String> baseOption = Collections.emptyMap();
+    private Class<?> annotatedClass = DefaultStringModel.class;
+    private String registeredSelector = SELECTOR;
+    private String exporterName = "exporterName";
+
+    @Before
+    public void setUp() {
+        Mockito.when(resource.getResourceResolver()).thenReturn(resourceResolver);
+        Mockito.when(requestPathInfo.getSelectors()).thenReturn(new String[]{SELECTOR});
+        Mockito.when(slingHttpServletRequest.getResponseContentType()).thenReturn("application/json");
+        Mockito.when(slingHttpServletRequest.getParameterMap()).thenReturn(Collections.emptyMap());
+        Mockito.when(slingHttpServletRequest.getRequestPathInfo()).thenReturn(requestPathInfo);
+        Mockito.when(slingHttpServletRequest.getResource()).thenReturn(resource);
+        Mockito.when(slingHttpServletRequest.getProtocol()).thenReturn("HTTP/1.1");
+        Mockito.when(slingHttpServletRequest.getMethod()).thenReturn("GET");
+
+        exportServlet = new ExportServlet(bundleContext, modelFactory, bindingsValueProviderByContext, scriptFactory,
+                annotatedClass, registeredSelector, exporterName, exportedObjectAccessor, baseOption);
+    }
+
+    @Test
+    public void verifyPrintWriterFlushesOutputStream() throws Exception {
+        // GIVEN
+        String expectedJsonString = "{\"firstProperty\":\"firstDefault\",\"secondProperty\":\"firstDefault\"}";
+        Mockito.when(exportedObjectAccessor
+                .getExportedString(Matchers.any(SlingHttpServletRequest.class), Matchers.any(Map.class),
+                        Matchers.any(ModelFactory.class), Matchers.anyString()))
+                .thenReturn(expectedJsonString);
+
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        PrintWriter printWriter = new PrintWriter(byteArrayOutputStream);
+
+        Mockito.when(slingHttpServletResponse.getWriter()).thenReturn(printWriter);
+
+        // WHEN
+        exportServlet.service(slingHttpServletRequest, slingHttpServletResponse);
+
+        // THEN
+        String actualJsonString = byteArrayOutputStream.toString();
+        Assert.assertThat(actualJsonString, CoreMatchers.not(CoreMatchers.is("")));
+        JsonReader expected = Json.createReader(new StringReader(expectedJsonString));
+        JsonReader actual = Json.createReader(new StringReader(actualJsonString));
+        Assert.assertThat(actual.read(), CoreMatchers.is(expected.read()));
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services