/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.page;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.xwiki.localization.script.LocalizationScriptService;
import org.xwiki.script.service.ScriptService;
import org.xwiki.test.TestComponentManager;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Set up Localization Services for Page Tests.
 *
 * @version $Id$
 * @since 8.3M2
 */
public final class LocalizationSetup
{
    private LocalizationSetup()
    {
        // Utility class and thus no public constructor.
    }

    /**
     * Sets up localization so that all translation return their key as translation values.
     *
     * @param tcm the stubbed Component Manager for the test
     * @throws Exception when a setup error occurs
     */
    public static void setUp(TestComponentManager tcm) throws Exception
    {
        LocalizationScriptService lss = mock(LocalizationScriptService.class);
        tcm.registerComponent(ScriptService.class, "localization", lss);
        when(lss.render(anyString())).thenAnswer(
            new Answer<String>() {
                @Override
                public String answer(InvocationOnMock invocationOnMock) throws Throwable
                {
                    // Return the translation key as the value
                    return invocationOnMock.getArgument(0);
                }
            }
        );
    }
}

@@ -928,48 +928,66 @@
 ort 
-org.mockito.invocation.InvocationOnMock;
+java.util.Collection;%0Aimport java.util.stream.Collectors;%0A
 %0Aimp
@@ -1175,16 +1175,74 @@
 nager;%0A%0A
+import static org.mockito.ArgumentMatchers.anyCollection;%0A
 import s
@@ -1700,16 +1700,379 @@
  values.
+ When the translation has%0A     * parameters, the parameter are appended comma separated and  between square brackets after the key.%0A     * %3Cp%3E%0A     * For instance, calling %7B@code $services.localization.render('my.key')%7D returns %7B@code %22my.key%22%7D, and calling%0A     * %7B@code $services.localization.render('my.key2', 'a', 1)%7D returns %7B@code %22my.key2 %5Ba, 1%5D%22%7D.%0A     * 
 %0A     *%0A
@@ -2417,24 +2417,388 @@
 ion%22, lss);%0A
+        %0A        // The translations are mocked by returning the translation, suffixed with the list of the String.valueOf%0A        // values of the translation parameters if they exist.%0A        // We mock the translations instead of using their actual values because they are subject to change from%0A        // Weblate, possibly making the build fail unexpectedly.%0A
         when
@@ -2851,12 +2851,9 @@
     
-new 
+(
 Answ
@@ -2866,127 +2866,146 @@
 ing%3E
-(
 ) 
-%7B%0A                @Override%0A                public String answer(InvocationOnMock invocationOnMock) throws Throwable
+invocationOnMock -%3E %7B%0A                // Return the translation key as the value%0A                return invocationOnMock.getArgument(0);
 %0A   
@@ -3009,24 +3009,30 @@
         
+    %7D%0A
         
 %7B%0A      
@@ -3019,25 +3019,27 @@
   %7D%0A        
-%7B
+);%0A
 %0A           
@@ -3035,37 +3035,206 @@
         
-            // Return
+when(lss.render(anyString(), anyCollection())).thenAnswer((Answer%3CString%3E) invocationOnMock -%3E %7B%0A            // Displays the comma separated list of parameters between squared brackets after
  the tra
@@ -3241,32 +3241,48 @@
 nslation key as 
+%0A            // 
 the value%0A      
@@ -3266,32 +3266,33 @@
     // the value
+.
 %0A               
@@ -3284,38 +3284,161 @@
             
-        return
+// For instance: my.key %5BparamA, paramB%5D%0A            Object key = invocationOnMock.getArgument(0);%0A            Collection%3C?%3E parameters =
  invocationO
@@ -3447,33 +3447,33 @@
 ock.getArgument(
-0
+1
 );%0A             
@@ -3475,27 +3475,141 @@
     
-    %7D%0A            %7D
+return parameters.stream()%0A                .map(String::valueOf)%0A                .collect(Collectors.joining(%22, %22, key + %22 %5B%22, %22%5D%22));
 %0A   
@@ -3613,16 +3613,17 @@
         
+%7D
 );%0A    %7D

Improve the localization setup to handle localizations with parameters.