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

cziegeler pushed a commit to branch appmod/java-upgrade-20250910060318
in repository https://gitbox.apache.org/repos/asf/sling-samples.git

commit d04f7e8f3f622e9b7609ef27cc5cded90447101e
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Wed Sep 10 09:23:30 2025 +0200

    Restore comprehensive RatingPostServlet test functionality
---
 .../ratings/impl/RatingPostServletTest.java        | 71 ++++++++++++++++++++--
 1 file changed, 67 insertions(+), 4 deletions(-)

diff --git 
a/slingshot/src/test/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServletTest.java
 
b/slingshot/src/test/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServletTest.java
index b01dcfe..784281c 100644
--- 
a/slingshot/src/test/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServletTest.java
+++ 
b/slingshot/src/test/java/org/apache/sling/sample/slingshot/ratings/impl/RatingPostServletTest.java
@@ -18,13 +18,27 @@
  */
 package org.apache.sling.sample.slingshot.ratings.impl;
 
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.sample.slingshot.ratings.RatingsService;
+import org.apache.sling.sample.slingshot.ratings.RatingsUtil;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Rule;
 import org.junit.Test;
-import org.mockito.Mockito;
 
 import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyDouble;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
 
 public class RatingPostServletTest {
 
@@ -32,13 +46,62 @@ public class RatingPostServletTest {
     public final SlingContext context = new SlingContext();
 
     @Test
+    @SuppressWarnings("deprecation") // Using deprecated Sling API for 
backward compatibility testing
     public void successfulSave() throws Exception {
+        
+        // Mock the RatingsService
+        RatingsService ratingsService = mock(RatingsService.class);
+        when(ratingsService.getRating(any(Resource.class))).thenReturn(4.5);
+        
+        // Mock the ResourceResolverFactory
+        ResourceResolverFactory resourceResolverFactory = 
mock(ResourceResolverFactory.class);
+        ResourceResolver resourceResolver = mock(ResourceResolver.class);
+        
when(resourceResolverFactory.getServiceResourceResolver(any())).thenReturn(resourceResolver);
+        
+        // Create test resource
+        Resource testResource = 
context.create().resource("/content/slingshot/users/test/entries/entry1");
+        
when(resourceResolver.getResource(anyString())).thenReturn(testResource);
+        
+        // Register services
+        context.registerService(RatingsService.class, ratingsService);
+        context.registerService(ResourceResolverFactory.class, 
resourceResolverFactory);
 
-        context.registerService(RatingsService.class, 
Mockito.mock(RatingsService.class));
-
+        // Create and register the servlet
         RatingPostServlet servlet = context.registerInjectActivateService(new 
RatingPostServlet());
 
-        // Just verify the servlet can be instantiated and registered
+        // Verify the servlet can be instantiated and registered
         assertNotNull(servlet);
+        
+        // Create mock request and response
+        SlingHttpServletRequest request = mock(SlingHttpServletRequest.class);
+        SlingHttpServletResponse response = 
mock(SlingHttpServletResponse.class);
+        
+        // Set up request parameters
+        
when(request.getParameter(RatingsUtil.PROPERTY_RATING)).thenReturn("5.0");
+        when(request.getRemoteUser()).thenReturn("testuser");
+        when(request.getResource()).thenReturn(testResource);
+        
+        // Set up response writer
+        StringWriter stringWriter = new StringWriter();
+        PrintWriter writer = new PrintWriter(stringWriter);
+        when(response.getWriter()).thenReturn(writer);
+        
+        // Execute the servlet POST method
+        servlet.doPost(request, response);
+        
+        // Verify interactions
+        verify(ratingsService).setRating(any(Resource.class), anyString(), 
anyDouble());
+        verify(ratingsService).getRating(any(Resource.class));
+        verify(response).setContentType("application/json");
+        verify(response).setCharacterEncoding("utf-8");
+        verify(response).setStatus(200);
+        
+        // Verify the JSON response contains the rating
+        writer.flush();
+        String responseContent = stringWriter.toString();
+        assertNotNull(responseContent);
+        // Basic check that response contains rating JSON structure
+        assert(responseContent.contains("rating"));
+        assert(responseContent.contains("4.5"));
     }
 }

Reply via email to